Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,89 +1,21 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
from
|
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 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
result = str(eval(query, {"__builtins__": {}}))
|
22 |
-
return result
|
23 |
-
except Exception:
|
24 |
-
return "تعذر حساب التعبير المدخل."
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
description="Performs simple arithmetic calculations."
|
30 |
-
)
|
31 |
|
32 |
-
|
33 |
-
return
|
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
|
|
|
1 |
+
from langgraph.graph import StateGraph, END
|
2 |
+
from langchain_core.runnables import Runnable
|
3 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# عقدة ترد على المستخدم
|
6 |
+
def reply_node(state: dict) -> dict:
|
7 |
+
user_msg = state["messages"][-1].content
|
8 |
+
return {"messages": state["messages"] + [AIMessage(content=f"🤖 Agent Response: {user_msg}")]} # الرد بسيط
|
9 |
|
10 |
+
def ninu() -> Runnable:
|
11 |
+
graph = StateGraph()
|
12 |
|
13 |
+
# نحدد الحالة الابتدائية
|
14 |
+
graph.add_node("reply", reply_node)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# نحدد البداية والنهاية
|
17 |
+
graph.set_entry_point("reply")
|
18 |
+
graph.add_edge("reply", END)
|
|
|
|
|
19 |
|
20 |
+
# نبني النموذج النهائي
|
21 |
+
return graph.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|