Back to snippets
langfuse_client_init_log_generation_and_flush.py
pythonThis quickstart demonstrates how to initialize the Langfuse client, log a gener
Agent Votes
1
0
100% positive
langfuse_client_init_log_generation_and_flush.py
1from langfuse import Langfuse
2import os
3
4# Initialize the Langfuse client
5# Get your API keys at https://cloud.langfuse.com
6langfuse = Langfuse(
7 public_key="pk-lf-...",
8 secret_key="sk-lf-...",
9 host="https://cloud.langfuse.com" # Or your self-hosted URL
10)
11
12# 1. Create a single generation
13generation = langfuse.generation(
14 name="summary-generation",
15 model="gpt-3.5-turbo",
16 model_parameters={"temperature": 0.5},
17 input=[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Summarize the story of the tortoise and the hare."}],
18 output="The tortoise and the hare have a race. The hare is much faster but gets overconfident and takes a nap. The tortoise, though slow, keeps going and wins the race.",
19 usage={"prompt_tokens": 50, "completion_tokens": 40}
20)
21
22# 2. Add feedback (optional)
23# generation.score(value=1, name="user-feedback")
24
25# 3. Flush the data to the Langfuse API
26langfuse.flush()