mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-23 15:19:33 +00:00
docs: upgrade langfuse example to python sdk v3 (#31654)
As the Langfuse python sdk v3 includes breaking changes, this PR updates the code examples. https://langfuse.com/docs/integrations/langchain/upgrade-paths#python
This commit is contained in:
parent
a79998800c
commit
0cadf4fc9a
@ -15,19 +15,35 @@ pip install langfuse
|
|||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Initialize Langfuse handler
|
from langfuse import Langfuse, get_client
|
||||||
from langfuse.callback import CallbackHandler
|
from langfuse.langchain import CallbackHandler
|
||||||
langfuse_handler = CallbackHandler(
|
from langchain_openai import ChatOpenAI # Example LLM
|
||||||
secret_key="sk-lf-...",
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
public_key="pk-lf-...",
|
|
||||||
host="https://cloud.langfuse.com", # 🇪🇺 EU region
|
# Initialize Langfuse client with constructor arguments
|
||||||
# host="https://us.cloud.langfuse.com", # 🇺🇸 US region
|
Langfuse(
|
||||||
|
public_key="your-public-key",
|
||||||
|
secret_key="your-secret-key",
|
||||||
|
host="https://cloud.langfuse.com" # Optional: defaults to https://cloud.langfuse.com
|
||||||
)
|
)
|
||||||
|
|
||||||
# Your Langchain code
|
# Get the configured client instance
|
||||||
|
langfuse = get_client()
|
||||||
|
|
||||||
# Add Langfuse handler as callback (classic and LCEL)
|
# Initialize the Langfuse handler
|
||||||
chain.invoke({"input": "<user_input>"}, config={"callbacks": [langfuse_handler]})
|
langfuse_handler = CallbackHandler()
|
||||||
|
|
||||||
|
# Create your LangChain components
|
||||||
|
llm = ChatOpenAI(model_name="gpt-4o")
|
||||||
|
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
|
||||||
|
chain = prompt | llm
|
||||||
|
|
||||||
|
# Run your chain with Langfuse tracing
|
||||||
|
response = chain.invoke({"topic": "cats"}, config={"callbacks": [langfuse_handler]})
|
||||||
|
print(response.content)
|
||||||
|
|
||||||
|
# Flush events to Langfuse in short-lived applications
|
||||||
|
langfuse.flush()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Environment variables
|
### Environment variables
|
||||||
@ -43,7 +59,7 @@ LANGFUSE_HOST="https://cloud.langfuse.com"
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# Initialize Langfuse handler
|
# Initialize Langfuse handler
|
||||||
from langfuse.callback import CallbackHandler
|
from langfuse.langchain import CallbackHandler
|
||||||
langfuse_handler = CallbackHandler()
|
langfuse_handler = CallbackHandler()
|
||||||
|
|
||||||
# Your Langchain code
|
# Your Langchain code
|
||||||
@ -140,7 +156,7 @@ Now, we will add then [Langfuse callback handler for LangChain](https://langfuse
|
|||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from langfuse.callback import CallbackHandler
|
from langfuse.langchain import CallbackHandler
|
||||||
|
|
||||||
# Initialize Langfuse CallbackHandler for Langchain (tracing)
|
# Initialize Langfuse CallbackHandler for Langchain (tracing)
|
||||||
langfuse_handler = CallbackHandler()
|
langfuse_handler = CallbackHandler()
|
||||||
|
Loading…
Reference in New Issue
Block a user