| 1 | from langfuse import observe |
| 2 | from langfuse.openai import openai |
| 3 | |
| 4 | # Create an OpenAI client with OpenRouter's base URL |
| 5 | client = openai.OpenAI( |
| 6 | base_url="https://openrouter.ai/api/v1", |
| 7 | ) |
| 8 | |
| 9 | @observe() # This decorator enables tracing of the function |
| 10 | def analyze_text(text: str): |
| 11 | # First LLM call: Summarize the text |
| 12 | summary_response = summarize_text(text) |
| 13 | summary = summary_response.choices[0].message.content |
| 14 | |
| 15 | # Second LLM call: Analyze the sentiment of the summary |
| 16 | sentiment_response = analyze_sentiment(summary) |
| 17 | sentiment = sentiment_response.choices[0].message.content |
| 18 | |
| 19 | return { |
| 20 | "summary": summary, |
| 21 | "sentiment": sentiment |
| 22 | } |
| 23 | |
| 24 | @observe() # Nested function to be traced |
| 25 | def summarize_text(text: str): |
| 26 | return client.chat.completions.create( |
| 27 | model="openai/gpt-3.5-turbo", |
| 28 | messages=[ |
| 29 | {"role": "system", "content": "You summarize texts in a concise manner."}, |
| 30 | {"role": "user", "content": f"Summarize the following text:\n{text}"} |
| 31 | ], |
| 32 | name="summarize-text" |
| 33 | ) |
| 34 | |
| 35 | @observe() # Nested function to be traced |
| 36 | def analyze_sentiment(summary: str): |
| 37 | return client.chat.completions.create( |
| 38 | model="openai/gpt-3.5-turbo", |
| 39 | messages=[ |
| 40 | {"role": "system", "content": "You analyze the sentiment of texts."}, |
| 41 | {"role": "user", "content": f"Analyze the sentiment of the following summary:\n{summary}"} |
| 42 | ], |
| 43 | name="analyze-sentiment" |
| 44 | ) |
| 45 | |
| 46 | # Example usage |
| 47 | text_to_analyze = "OpenRouter's unified API has significantly advanced the field of AI development, setting new standards for model accessibility." |
| 48 | result = analyze_text(text_to_analyze) |
| 49 | print(result) |