Skip to main content

cognee_llm/
lib.rs

1//! LLM abstraction layer for Cognee.
2//!
3//! Provides async trait-based abstractions for Large Language Model interactions,
4//! with support for structured output generation (inspired by Python's Instructor).
5//!
6//! # Features
7//!
8//! - **Async-first**: All operations are async, supporting both API calls and local inference
9//! - **Structured outputs**: Generate type-safe structured data (e.g., knowledge graphs) from text
10//! - **Provider-agnostic**: Trait-based design supports OpenAI, Anthropic, Ollama, local models, etc.
11//! - **Configuration**: Flexible configuration with sensible defaults
12//!
13//! # Retry Logic
14//!
15//! This crate does not include built-in retry logic in the trait. Instead, use the
16//! generic `retry_with_backoff` utility from `cognee-utils`:
17//!
18//! ```ignore
19//! use cognee_llm::{Llm, LlmConfig, LlmProvider, LlmError};
20//! use cognee_utils::retry::{retry_with_backoff, RetryConfig, RetryDecision};
21//! use schemars::JsonSchema;
22//! use serde::{Deserialize, Serialize};
23//!
24//! #[derive(Serialize, Deserialize, JsonSchema)]
25//! struct ExtractedData {
26//!     entities: Vec<String>,
27//!     relationships: Vec<(String, String, String)>,
28//! }
29//!
30//! let config = LlmConfig::new(LlmProvider::OpenAI, "gpt-4")
31//!     .with_api_key("sk-...")
32//!     .with_temperature(0.0);
33//!
34//! let llm: Box<dyn Llm> = create_llm(config)?;
35//!
36//! // With retry logic
37//! let retry_config = RetryConfig::new(3, 100, 5000);
38//! let data: ExtractedData = retry_with_backoff(
39//!     retry_config,
40//!     || llm.create_structured_output(
41//!         "Alice told Bob to bring the documents.",
42//!         "Extract entities and relationships from the text.",
43//!         None,
44//!     ),
45//!     |error| match error {
46//!         LlmError::NetworkError(_) | LlmError::RateLimitExceeded(_) => RetryDecision::Retry,
47//!         LlmError::ContentPolicyViolation(_) | LlmError::AuthenticationError(_) => RetryDecision::Abort,
48//!         _ => RetryDecision::Retry,
49//!     },
50//! ).await?;
51//! ```
52
53pub mod adapters;
54pub mod config;
55pub mod dynamic_model;
56pub mod error;
57pub mod factory;
58pub mod llm_trait;
59#[cfg(feature = "mock")]
60pub mod mock;
61pub mod prompts;
62pub mod responses_client;
63mod retry;
64pub mod schema;
65pub mod transcriber;
66pub mod types;
67
68pub use adapters::{AnthropicAdapter, OpenAIAdapter};
69pub use config::{LlmConfig, LlmProvider};
70pub use dynamic_model::{DynamicGraphModel, GraphModelError, graph_schema_to_graph_model};
71pub use error::{LlmError, LlmResult};
72pub use factory::build_openai_compatible_adapter;
73pub use llm_trait::{Llm, LlmExt};
74pub use responses_client::{OpenAIResponsesClient, ResponsesClient, ResponsesRequest};
75pub use schema::{
76    append_corrective_instruction, build_schema_prompt, generate_json_schema,
77    generate_json_schema_string, graph_model_to_schema, graph_model_to_schema_string,
78    schema_required_validator,
79};
80pub use transcriber::{Transcriber, TranscriptionOutput, validate_audio_format};
81pub use types::{
82    DEFAULT_MAX_COMPLETION_TOKENS, GenerationOptions, GenerationResponse, Message, MessageRole,
83    TokenUsage,
84};