Skip to main content

Crate cognee_llm

Crate cognee_llm 

Source
Expand description

LLM abstraction layer for Cognee.

Provides async trait-based abstractions for Large Language Model interactions, with support for structured output generation (inspired by Python’s Instructor).

§Features

  • Async-first: All operations are async, supporting both API calls and local inference
  • Structured outputs: Generate type-safe structured data (e.g., knowledge graphs) from text
  • Provider-agnostic: Trait-based design supports OpenAI, Anthropic, Ollama, local models, etc.
  • Configuration: Flexible configuration with sensible defaults

§Retry Logic

This crate does not include built-in retry logic in the trait. Instead, use the generic retry_with_backoff utility from cognee-utils:

ⓘ
use cognee_llm::{Llm, LlmConfig, LlmProvider, LlmError};
use cognee_utils::retry::{retry_with_backoff, RetryConfig, RetryDecision};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, JsonSchema)]
struct ExtractedData {
    entities: Vec<String>,
    relationships: Vec<(String, String, String)>,
}

let config = LlmConfig::new(LlmProvider::OpenAI, "gpt-4")
    .with_api_key("sk-...")
    .with_temperature(0.0);

let llm: Box<dyn Llm> = create_llm(config)?;

// With retry logic
let retry_config = RetryConfig::new(3, 100, 5000);
let data: ExtractedData = retry_with_backoff(
    retry_config,
    || llm.create_structured_output(
        "Alice told Bob to bring the documents.",
        "Extract entities and relationships from the text.",
        None,
    ),
    |error| match error {
        LlmError::NetworkError(_) | LlmError::RateLimitExceeded(_) => RetryDecision::Retry,
        LlmError::ContentPolicyViolation(_) | LlmError::AuthenticationError(_) => RetryDecision::Abort,
        _ => RetryDecision::Retry,
    },
).await?;

Re-exports§

pub use adapters::AnthropicAdapter;
pub use adapters::OpenAIAdapter;
pub use config::LlmConfig;
pub use config::LlmProvider;
pub use dynamic_model::DynamicGraphModel;
pub use dynamic_model::GraphModelError;
pub use dynamic_model::graph_schema_to_graph_model;
pub use error::LlmError;
pub use error::LlmResult;
pub use factory::build_openai_compatible_adapter;
pub use llm_trait::Llm;
pub use llm_trait::LlmExt;
pub use responses_client::OpenAIResponsesClient;
pub use responses_client::ResponsesClient;
pub use responses_client::ResponsesRequest;
pub use schema::append_corrective_instruction;
pub use schema::build_schema_prompt;
pub use schema::generate_json_schema;
pub use schema::generate_json_schema_string;
pub use schema::graph_model_to_schema;
pub use schema::graph_model_to_schema_string;
pub use schema::schema_required_validator;
pub use transcriber::Transcriber;
pub use transcriber::TranscriptionOutput;
pub use transcriber::validate_audio_format;
pub use types::DEFAULT_MAX_COMPLETION_TOKENS;
pub use types::GenerationOptions;
pub use types::GenerationResponse;
pub use types::Message;
pub use types::MessageRole;
pub use types::TokenUsage;

Modules§

adapters
LLM provider adapters.
config
LLM configuration.
dynamic_model
Dynamic graph model for cross-SDK schema sharing.
error
Error types for LLM operations.
factory
Shared construction of OpenAI-compatible LLM adapters.
llm_trait
LLM trait definition for structured output generation.
prompts
Compiled-in LLM prompt templates ported from Python.
responses_client
OpenAI Responses API client abstraction.
schema
JSON Schema generation utilities for structured output.
transcriber
Audio transcription trait and types.
types
Common types for LLM operations.