jossoft commited on
Commit
c6ca430
·
1 Parent(s): b3b3821

add langfuse for observability

Browse files
app.py CHANGED
@@ -1,19 +1,37 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
- import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from tools.visit_webpage import VisitWebpageTool
8
  from tools.web_search import DuckDuckGoSearchTool
9
- import requests
10
  from Gradio_UI import GradioUI
11
  import os
 
 
12
  from dotenv import load_dotenv
13
  load_dotenv()
14
 
15
  SERPAPI_KEY = os.getenv("SERPAPI_KEY")
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  @tool
18
  def youtube_search(query: str, max_results: int = 5) -> str:
19
  """A tool that searches YouTube and returns video titles and URLs.
@@ -76,6 +94,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
76
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
77
 
78
 
 
 
79
  final_answer = FinalAnswerTool()
80
 
81
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
@@ -97,7 +117,7 @@ with open("prompts.yaml", 'r') as stream:
97
 
98
  agent = CodeAgent(
99
  model=model,
100
- tools=[final_answer, get_current_time_in_timezone, youtube_search, DuckDuckGoSearchTool(), VisitWebpageTool()], ## add your tools here (don't remove final answer)
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
 
3
  import pytz
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
6
  from tools.visit_webpage import VisitWebpageTool
7
  from tools.web_search import DuckDuckGoSearchTool
 
8
  from Gradio_UI import GradioUI
9
  import os
10
+ from openinference.instrumentation.smolagents import SmolagentsInstrumentor
11
+ from langfuse import get_client
12
  from dotenv import load_dotenv
13
  load_dotenv()
14
 
15
  SERPAPI_KEY = os.getenv("SERPAPI_KEY")
16
 
17
+ langfuse = get_client()
18
+
19
+ # Verify connection
20
+ if langfuse.auth_check():
21
+ print("Langfuse client is authenticated and ready!")
22
+ else:
23
+ print("Authentication failed. Please check your credentials and host.")
24
+
25
+ # Try to instrument with SmolagentsInstrumentor, but handle compatibility issues gracefully
26
+ try:
27
+ SmolagentsInstrumentor().instrument()
28
+ print("✅ SmolagentsInstrumentor successfully initialized!")
29
+ except Exception as e:
30
+ print(f"⚠️ SmolagentsInstrumentor failed: {e}")
31
+ print("📊 Continuing with Langfuse-only tracking...")
32
+
33
+
34
+
35
  @tool
36
  def youtube_search(query: str, max_results: int = 5) -> str:
37
  """A tool that searches YouTube and returns video titles and URLs.
 
94
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
95
 
96
 
97
+
98
+
99
  final_answer = FinalAnswerTool()
100
 
101
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
117
 
118
  agent = CodeAgent(
119
  model=model,
120
+ tools=[get_current_time_in_timezone, youtube_search, DuckDuckGoSearchTool(), VisitWebpageTool(), final_answer], ## add your tools here (don't remove final answer)
121
  max_steps=6,
122
  verbosity_level=1,
123
  grammar=None,
prompts.yaml CHANGED
@@ -319,3 +319,12 @@
319
  "report": |-
320
  Here is the final answer from your managed agent '{{name}}':
321
  {{final_answer}}
 
 
 
 
 
 
 
 
 
 
319
  "report": |-
320
  Here is the final answer from your managed agent '{{name}}':
321
  {{final_answer}}
322
+
323
+ "final_answer":
324
+ "pre_messages": |-
325
+ You have reached the maximum number of steps. Please provide a final answer to the best of your ability.
326
+ Make sure to give as much context as possible in your answer.
327
+ "post_messages": |-
328
+ Thank you for providing your final answer.
329
+ "prompt": |-
330
+ Provide your final answer to the task.
requirements.txt CHANGED
@@ -1,5 +1,11 @@
1
  markdownify
2
- smolagents==1.13.0
 
3
  requests
4
  duckduckgo_search
5
  pandas
 
 
 
 
 
 
1
  markdownify
2
+ smolagents
3
+ smolagents[gradio]
4
  requests
5
  duckduckgo_search
6
  pandas
7
+ transformers
8
+ opentelemetry-sdk
9
+ opentelemetry-exporter-otlp
10
+ openinference-instrumentation-smolagents
11
+ langfuse
tools/superhero_party_theme.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import Tool
2
+
3
+ class SuperheroPartyThemeTool(Tool):
4
+ name = "superhero_party_theme_generator"
5
+ description = """
6
+ This tool suggests creative superhero-themed party ideas based on a category.
7
+ It returns a unique party theme idea."""
8
+
9
+ inputs = {
10
+ "category": {
11
+ "type": "string",
12
+ "description": "The type of superhero party (e.g., 'classic heroes', 'villain masquerade', 'futuristic Gotham').",
13
+ }
14
+ }
15
+
16
+ output_type = "string"
17
+
18
+ def forward(self, category: str):
19
+ themes = {
20
+ "classic heroes": "Justice League Gala: Guests come dressed as their favorite DC heroes with themed cocktails like 'The Kryptonite Punch'.",
21
+ "villain masquerade": "Gotham Rogues' Ball: A mysterious masquerade where guests dress as classic Batman villains.",
22
+ "futuristic Gotham": "Neo-Gotham Night: A cyberpunk-style party inspired by Batman Beyond, with neon decorations and futuristic gadgets."
23
+ }
24
+
25
+ return themes.get(category.lower(), "Themed party idea not found. Try 'classic heroes', 'villain masquerade', or 'futuristic Gotham'.")
tools/visit_webpage.py CHANGED
@@ -1,8 +1,5 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
- import requests
4
- import markdownify
5
- import smolagents
6
  import re
7
 
8
  class VisitWebpageTool(Tool):
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
 
 
 
3
  import re
4
 
5
  class VisitWebpageTool(Tool):
tools/web_search.py CHANGED
@@ -1,6 +1,5 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
- import duckduckgo_search
4
 
5
  class DuckDuckGoSearchTool(Tool):
6
  name = "web_search"
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
 
3
 
4
  class DuckDuckGoSearchTool(Tool):
5
  name = "web_search"