Cognee LLM
LLM abstraction layer for Cognee with support for structured output generation.
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
- JSON Schema generation: Automatic schema generation from Rust types using
schemars - Provider-agnostic: Trait-based design supports OpenAI, Anthropic, Ollama, local models, etc.
- Configuration: Flexible configuration with sensible defaults
Usage
OpenAI Adapter
use ;
use JsonSchema;
use ;
// Create OpenAI adapter
let llm = new?;
// Generate structured output
let graph: KnowledgeGraph = llm.create_structured_output.await?;
Custom Base URL (for OpenAI-compatible APIs)
// Use with Ollama, LocalAI, or other OpenAI-compatible services
let llm = new?;
Note: The adapter automatically detects API capabilities:
- OpenAI/Azure: Uses function calling for structured outputs (more reliable)
- Ollama/LocalAI: Automatically falls back to JSON mode with example-based prompts
- No configuration needed - it just works with both!
Basic Trait Definition
use ;
use JsonSchema;
use ;
// Implement the Llm trait for your provider
let llm: = ...;
// Generate structured output
let graph: KnowledgeGraph = llm.create_structured_output.await?;
JSON Schema Generation
The crate automatically generates JSON schemas from your Rust types to guide the LLM:
use ;
use JsonSchema;
use ;
// Generate schema as JSON value
let schema = ;
// Or build a complete prompt with schema embedded
let prompt = ;
With Retry Logic
use ;
use ;
let retry_config = new;
let graph: KnowledgeGraph = retry_with_backoff.await?;
Architecture
The crate provides:
Llmtrait: Core async trait with structured output generation- OpenAI adapter: Production-ready implementation using OpenAI's function calling API
- JSON Schema generation:
schemars-based schema generation from Rust types - Schema utilities: Helper functions to generate schemas and build prompts
- Configuration types:
LlmConfig,LlmProvider,GenerationOptions - Type-safe responses: Generic over
T: Serialize + DeserializeOwned + JsonSchema - Comprehensive errors:
LlmErrorcovers API, network, serialization, rate limit errors
Implementation Details
OpenAI Adapter
The OpenAIAdapter uses a dual-strategy approach for structured outputs:
Primary (Function Calling):
- Schema Generation: Automatically generates JSON schema from your Rust type using
schemars - Function Definition: Creates an OpenAI function with the schema as parameters
- Forced Execution: Sets
function_call: {name: "extract_structured_data"}to force the model to use the function - Validation: Parses and validates the function call arguments into your type
Fallback (JSON Mode):
- Automatic Detection: If function calling isn't supported, automatically switches to JSON mode
- Example Generation: Creates example JSON from the schema (clearer than full schema for LLMs)
- Response Format: Sets
response_format: {"type": "json_object"}for JSON-only responses - Content Parsing: Parses the JSON from the response content
This dual approach provides:
- Universal compatibility: Works with OpenAI, Azure OpenAI, Ollama, LocalAI, and others
- High reliability: Function calling for best results, JSON mode for broad compatibility
- Type safety: Compile-time guarantees about response structure
- Zero configuration: Automatic detection and fallback
Adding New Adapters
To add support for other providers:
- Create a new module in
src/adapters/ - Implement the
Llmtrait - Use
generate_json_schema::<T>()to get the schema - Adapt the schema to the provider's format (function calling, JSON mode, etc.)
- Parse the response into type
T
See src/adapters/openai.rs as a reference implementation.
Testing
Unit Tests
Run the unit tests:
Integration Tests
The crate includes integration tests that exercise a real OpenAI-compatible endpoint (OpenAI, or a local Ollama instance via its OpenAI-compatible API):
# Set environment variables
# Run integration tests
Tests will automatically skip if environment variables are not set.
Adapters
Implemented:
OpenAIAdapter— OpenAI-compatible APIs (also works with Ollama/vLLM/LocalAI)
On-device LiteRT inference (Android) ships in the closed companion crate
cognee-llm-litert.
Planned:
- Anthropic adapter (Claude with tool use)
- Streaming support: real-time token streaming for all adapters