from langfuse import observe
from langfuse.openai import openai
# Create an OpenAI client with OpenRouter's base URL
client = openai.OpenAI(
base_url="https://openrouter.ai/api/v1",
)
@observe() # This decorator enables tracing of the function
def analyze_text(text: str):
# First LLM call: Summarize the text
summary_response = summarize_text(text)
summary = summary_response.output_text
# Second LLM call: Analyze the sentiment of the summary
sentiment_response = analyze_sentiment(summary)
sentiment = sentiment_response.output_text
return {
"summary": summary,
"sentiment": sentiment
}
@observe() # Nested function to be traced
def summarize_text(text: str):
return client.responses.create(
model="openai/gpt-4o-mini",
instructions="You summarize texts in a concise manner.",
input=f"Summarize the following text:\n{text}",
name="summarize-text"
)
@observe() # Nested function to be traced
def analyze_sentiment(summary: str):
return client.responses.create(
model="openai/gpt-4o-mini",
instructions="You analyze the sentiment of texts.",
input=f"Analyze the sentiment of the following summary:\n{summary}",
name="analyze-sentiment"
)
# Example usage
text_to_analyze = "OpenRouter's unified API has significantly advanced the field of AI development, setting new standards for model accessibility."
result = analyze_text(text_to_analyze)
print(result)