LightRAG/lightrag/llm
anouarbm 626b42bc40 feat: add optional Langfuse observability integration
This contribution adds optional Langfuse support for LLM observability and tracing.
Langfuse provides a drop-in replacement for the OpenAI client that automatically
tracks all LLM interactions without requiring code changes.

Features:
- Optional Langfuse integration with graceful fallback
- Automatic LLM request/response tracing
- Token usage tracking
- Latency metrics
- Error tracking
- Zero code changes required for existing functionality

Implementation:
- Modified lightrag/llm/openai.py to conditionally use Langfuse's AsyncOpenAI
- Falls back to standard OpenAI client if Langfuse is not installed
- Logs observability status on import

Configuration:
To enable Langfuse tracing, install the observability extras and set environment variables:

```bash
pip install lightrag-hku[observability]

export LANGFUSE_PUBLIC_KEY="your_public_key"
export LANGFUSE_SECRET_KEY="your_secret_key"
export LANGFUSE_HOST="https://cloud.langfuse.com"  # or your self-hosted instance
```

If Langfuse is not installed or environment variables are not set, LightRAG
will use the standard OpenAI client without any functionality changes.

Changes:
- Modified lightrag/llm/openai.py (added optional Langfuse import)
- Updated pyproject.toml with optional 'observability' dependencies

Dependencies (optional):
- langfuse>=3.8.1
2025-11-01 21:40:22 +01:00
..
__init__.py Separated llms from the main llm.py file and fixed some deprication bugs 2025-01-25 00:11:00 +01:00
anthropic.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
azure_openai.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
bedrock.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
binding_options.py Fix boolean parser problem for for LLM environment variable 2025-09-28 19:23:57 +08:00
hf.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
jina.py feat: improve Jina API error handling to show clean messages instead of HTML 2025-08-05 11:46:02 +08:00
llama_index_impl.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
lmdeploy.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
lollms.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00
nvidia_openai.py refactor: Remove deprecated max_token_size from embedding configuration 2025-07-29 10:49:35 +08:00
ollama.py Modernize type hints and remove Python 3.8 compatibility code 2025-10-02 23:15:42 +08:00
openai.py feat: add optional Langfuse observability integration 2025-11-01 21:40:22 +01:00
siliconcloud.py refactor: Remove deprecated max_token_size from embedding configuration 2025-07-29 10:49:35 +08:00
zhipu.py Add Deepseek Style Chain of Thought (CoT) Support for OpenAI Compatible LLM providers 2025-09-09 22:34:36 +08:00