Spaces:
No application file
No application file
Giustino Esposito
commited on
Commit
·
067f113
1
Parent(s):
d5ccf60
removed app file
Browse files
app.py
DELETED
@@ -1,110 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
# Import the load_dotenv function from the dotenv library
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
5 |
-
|
6 |
-
from multimodal_tools import extract_text_tool, analyze_image_tool, analyze_audio_tool
|
7 |
-
|
8 |
-
# Load environment variables from .env file
|
9 |
-
load_dotenv()
|
10 |
-
# Read your API key from the environment variable or set it manually
|
11 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
12 |
-
langfuse_secret_key = os.getenv("LANGFUSE_SECRET_KEY")
|
13 |
-
langfuse_public_key = os.getenv("LANGFUSE_PUBLIC_KEY")
|
14 |
-
|
15 |
-
from typing import TypedDict, Annotated
|
16 |
-
from langgraph.graph.message import add_messages
|
17 |
-
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
18 |
-
from langgraph.prebuilt import ToolNode
|
19 |
-
from langgraph.graph import START, StateGraph
|
20 |
-
from langgraph.prebuilt import tools_condition
|
21 |
-
from langchain_community.tools.tavily_search import TavilySearchResults # Importa Tavily
|
22 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
23 |
-
# from langfuse import Langfuse # Langfuse is initialized by CallbackHandler directly
|
24 |
-
from langfuse.callback import CallbackHandler
|
25 |
-
from youtube_tools import youtube_transcript_tool
|
26 |
-
from math_tools import add_tool, subtract_tool, multiply_tool, divide_tool
|
27 |
-
from serpapi_tools import serpapi_search_tool
|
28 |
-
from IPython.display import Image, display
|
29 |
-
# Generate thfrom langchain_community.tools.tavily_search import TavilySearchResults
|
30 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
31 |
-
|
32 |
-
|
33 |
-
# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)
|
34 |
-
langfuse_handler = CallbackHandler(
|
35 |
-
public_key=langfuse_public_key,
|
36 |
-
secret_key=langfuse_secret_key,
|
37 |
-
host="http://localhost:3000"
|
38 |
-
)
|
39 |
-
|
40 |
-
# Create LLM class
|
41 |
-
chat = ChatGoogleGenerativeAI(
|
42 |
-
model= "gemini-2.5-pro-preview-05-06",
|
43 |
-
temperature=0,
|
44 |
-
max_retries=2,
|
45 |
-
google_api_key=api_key,
|
46 |
-
thinking_budget= 0
|
47 |
-
)
|
48 |
-
|
49 |
-
search_tool = TavilySearchResults(
|
50 |
-
name="tavily_web_search", # Puoi personalizzare il nome se vuoi
|
51 |
-
description="Esegue una ricerca web avanzata utilizzando Tavily per informazioni aggiornate e complete. Utile per domande complesse o che richiedono dati recenti. Può essere utile fare più ricerche modificando la query per ottenere risultati migliori.", # Descrizione per l'LLM
|
52 |
-
max_results=5
|
53 |
-
)
|
54 |
-
|
55 |
-
tools = [
|
56 |
-
extract_text_tool,
|
57 |
-
analyze_image_tool,
|
58 |
-
analyze_audio_tool,
|
59 |
-
youtube_transcript_tool,
|
60 |
-
add_tool,
|
61 |
-
subtract_tool,
|
62 |
-
multiply_tool,
|
63 |
-
divide_tool,
|
64 |
-
search_tool
|
65 |
-
]
|
66 |
-
chat_with_tools = chat.bind_tools(tools)
|
67 |
-
|
68 |
-
|
69 |
-
class AgentState(TypedDict):
|
70 |
-
messages: Annotated[list[AnyMessage], add_messages]
|
71 |
-
|
72 |
-
def assistant(state: AgentState):
|
73 |
-
sys_msg = "You are a helpful assistant with access to tools. Understand user requests accurately. Use your tools when needed to answer effectively. Strictly follow all user instructions and constraints." \
|
74 |
-
"Pay attention: your output needs to contain only the final answer without any reasoning since it will be strictly evaluated against a dataset which contains only the specific response." \
|
75 |
-
"Your final output needs to be just the string or integer containing the answer, not an array or technical stuff."
|
76 |
-
return {
|
77 |
-
"messages": [chat_with_tools.invoke([sys_msg] + state["messages"])]
|
78 |
-
}
|
79 |
-
|
80 |
-
|
81 |
-
## The graph
|
82 |
-
builder = StateGraph(AgentState)
|
83 |
-
|
84 |
-
# Define nodes: these do the work
|
85 |
-
builder.add_node("assistant", assistant)
|
86 |
-
builder.add_node("tools", ToolNode(tools))
|
87 |
-
|
88 |
-
# Define edges: these determine how the control flow moves
|
89 |
-
builder.add_edge(START, "assistant")
|
90 |
-
builder.add_conditional_edges(
|
91 |
-
"assistant",
|
92 |
-
# If the latest message requires a tool, route to tools
|
93 |
-
# Otherwise, provide a direct response
|
94 |
-
tools_condition,
|
95 |
-
)
|
96 |
-
builder.add_edge("tools", "assistant")
|
97 |
-
alfred = builder.compile()
|
98 |
-
|
99 |
-
""" # Salva l'immagine del grafo su un file
|
100 |
-
graph_image_bytes = alfred.get_graph(xray=True).draw_mermaid_png()
|
101 |
-
with open("alfred_graph.png", "wb") as f:
|
102 |
-
f.write(graph_image_bytes)
|
103 |
-
print("L'immagine del grafo è stata salvata come alfred_graph.png")
|
104 |
-
|
105 |
-
messages = [HumanMessage(content="Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.")]
|
106 |
-
response = alfred.invoke(input={"messages": messages}, config={"callbacks": [langfuse_handler]})
|
107 |
-
|
108 |
-
print("🎩 Alfred's Response:")
|
109 |
-
print(response['messages'][-1].content)
|
110 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|