Use Langfuse’s OpenAI client wrapper with Helicone’s base URL:
Copy
import osfrom dotenv import load_dotenvfrom langfuse.openai import openai# Load environment variablesload_dotenv()# Create an OpenAI client with Helicone's base URLclient = openai.OpenAI( api_key=os.getenv("HELICONE_API_KEY"), base_url="https://ai-gateway.helicone.ai/")
4
Make requests with Langfuse tracing
Your existing Langfuse code continues to work without any changes:
Copy
# Make a chat completion requestresponse = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me a fun fact about space."} ], name="fun-fact-request" # Optional: Name of the generation in Langfuse)# Print the assistant's replyprint(response.choices[0].message.content)
#!/usr/bin/env python3import osfrom dotenv import load_dotenvfrom langfuse.openai import openai# Load environment variablesload_dotenv()# Create an OpenAI client with Helicone's base URLclient = openai.OpenAI( api_key=os.getenv("HELICONE_API_KEY"), base_url="https://ai-gateway.helicone.ai/")# Make a chat completion requestresponse = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me a fun fact about space."} ], name="fun-fact-request" # Optional: Name of the generation in Langfuse)# Print the assistant's replyprint(response.choices[0].message.content)
Langfuse supports streaming responses with full observability:
Copy
# Streaming examplestream = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "user", "content": "Write a short story about a robot learning to code."} ], stream=True, name="streaming-story")print("🤖 Assistant (streaming):")for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="", flush=True)print("\n")
import osfrom dotenv import load_dotenvfrom langfuse import observefrom langfuse.openai import openaiload_dotenv()client = openai.OpenAI( base_url="https://ai-gateway.helicone.ai/", api_key=os.getenv("HELICONE_API_KEY"),)@observe() # This decorator enables tracing of the functiondef analyze_text(text: str): # First LLM call: Summarize the text summary_response = summarize_text(text) summary = summary_response.choices[0].message.content # Second LLM call: Analyze the sentiment of the summary sentiment_response = analyze_sentiment(summary) sentiment = sentiment_response.choices[0].message.content return { "summary": summary, "sentiment": sentiment }@observe() # Nested function to be traceddef summarize_text(text: str): return client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You summarize texts in a concise manner."}, {"role": "user", "content": f"Summarize the following text:\n{text}"} ], name="summarize-text" )@observe() # Nested function to be traceddef analyze_sentiment(summary: str): return client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You analyze the sentiment of texts."}, {"role": "user", "content": f"Analyze the sentiment of the following summary:\n{summary}"} ], name="analyze-sentiment" )# Example usagetext_to_analyze = "OpenAI's GPT-4 model has significantly advanced the field of AI, setting new standards for language generation."analyze_text(text_to_analyze)