Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,21 +1,89 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
graph = StateGraph()
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from langchain.tools import Tool
|
4 |
+
from langchain.utilities import SerpAPIWrapper
|
5 |
+
from langgraph.graph.message import add_messages
|
6 |
+
from langgraph.graph import START, StateGraph
|
7 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
8 |
+
from langchain_core.messages import AnyMessage, HumanMessage
|
9 |
+
from langchain_groq import ChatGroq
|
10 |
+
from typing import TypedDict, Annotated
|
11 |
|
12 |
+
# Load API keys
|
13 |
+
load_dotenv()
|
14 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
15 |
+
serpapi_api_key = os.getenv("SERPAPI_API_KEY")
|
16 |
|
17 |
+
# --- أدوات ---
|
|
|
18 |
|
19 |
+
def calculator_tool_func(query: str):
|
20 |
+
try:
|
21 |
+
result = str(eval(query, {"__builtins__": {}}))
|
22 |
+
return result
|
23 |
+
except Exception:
|
24 |
+
return "تعذر حساب التعبير المدخل."
|
25 |
|
26 |
+
calculator_tool = Tool(
|
27 |
+
name="Calculator",
|
28 |
+
func=calculator_tool_func,
|
29 |
+
description="Performs simple arithmetic calculations."
|
30 |
+
)
|
31 |
|
32 |
+
def weather_tool_func(location: str):
|
33 |
+
return f"حالة الطقس في {location}: مشمس، درجة الحرارة 25 درجة مئوية."
|
34 |
+
|
35 |
+
weather_tool = Tool(
|
36 |
+
name="Weather",
|
37 |
+
func=weather_tool_func,
|
38 |
+
description="Provides weather information for a given location."
|
39 |
+
)
|
40 |
+
|
41 |
+
serpapi = SerpAPIWrapper(serpapi_api_key=serpapi_api_key)
|
42 |
+
SerpAPI_tool = Tool(
|
43 |
+
name="WebSearch",
|
44 |
+
func=serpapi.run,
|
45 |
+
description="Searches the web for recent information."
|
46 |
+
)
|
47 |
+
|
48 |
+
# --- تعريف موديل الذكاء الاصطناعي والوكيل ---
|
49 |
+
|
50 |
+
tools = [SerpAPI_tool, calculator_tool, weather_tool]
|
51 |
+
llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
|
52 |
+
llm_with_tools = llm.bind_tools(tools)
|
53 |
+
|
54 |
+
class AgentState(TypedDict):
|
55 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
56 |
+
|
57 |
+
def assistant(state: AgentState):
|
58 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
59 |
+
|
60 |
+
builder = StateGraph(AgentState)
|
61 |
+
builder.add_node("assistant", assistant)
|
62 |
+
builder.add_node("tools", ToolNode(tools))
|
63 |
+
builder.add_edge(START, "assistant")
|
64 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
65 |
+
builder.add_edge("tools", "assistant")
|
66 |
+
|
67 |
+
# 👇 وكيل باسم ninu يمكن استيراده من أي ملف آخر
|
68 |
+
ninu = builder.compile()
|
69 |
+
|
70 |
+
# --- دالة تشغيل مستقلة (اختياري) ---
|
71 |
+
|
72 |
+
def run_ninu(query: str):
|
73 |
+
conversation = []
|
74 |
+
|
75 |
+
intro_prompt = """
|
76 |
+
You are a general AI assistant with access to the following tools:
|
77 |
+
1. WebSearch: to search for general or recent information from the internet.
|
78 |
+
2. Calculator: to perform arithmetic calculations.
|
79 |
+
3. Weather: to provide weather information.
|
80 |
+
Think step by step, and decide which tools to use to answer the query.
|
81 |
+
Respond with:
|
82 |
+
FINAL ANSWER: [your final answer]
|
83 |
+
Only include what the user asked for in the final answer.
|
84 |
+
"""
|
85 |
+
conversation.append(HumanMessage(content=intro_prompt))
|
86 |
+
conversation.append(HumanMessage(content=query))
|
87 |
+
|
88 |
+
response = ninu.invoke({"messages": conversation})
|
89 |
+
return response["messages"][-1].content
|