Ali-Developments commited on
Commit
5ffb4a3
·
verified ·
1 Parent(s): 1033417

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +6 -6
agent.py CHANGED
@@ -15,7 +15,6 @@ 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__": {}}))
@@ -45,8 +44,7 @@ SerpAPI_tool = Tool(
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)
@@ -64,11 +62,10 @@ 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
 
@@ -86,4 +83,7 @@ Only include what the user asked for in the final answer.
86
  conversation.append(HumanMessage(content=query))
87
 
88
  response = ninu.invoke({"messages": conversation})
89
- return response["messages"][-1].content
 
 
 
 
15
  serpapi_api_key = os.getenv("SERPAPI_API_KEY")
16
 
17
  # --- أدوات ---
 
18
  def calculator_tool_func(query: str):
19
  try:
20
  result = str(eval(query, {"__builtins__": {}}))
 
44
  description="Searches the web for recent information."
45
  )
46
 
47
+ # --- تعريف الموديل والوكلاء ---
 
48
  tools = [SerpAPI_tool, calculator_tool, weather_tool]
49
  llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
50
  llm_with_tools = llm.bind_tools(tools)
 
62
  builder.add_conditional_edges("assistant", tools_condition)
63
  builder.add_edge("tools", "assistant")
64
 
65
+ # 👇 compiled agent object (must be used with .invoke({...}))
66
  ninu = builder.compile()
67
 
68
  # --- دالة تشغيل مستقلة (اختياري) ---
 
69
  def run_ninu(query: str):
70
  conversation = []
71
 
 
83
  conversation.append(HumanMessage(content=query))
84
 
85
  response = ninu.invoke({"messages": conversation})
86
+ for message in reversed(response["messages"]):
87
+ if isinstance(message.content, str) and "FINAL ANSWER:" in message.content:
88
+ return message.content.split("FINAL ANSWER:")[-1].strip()
89
+ return "لم أتمكن من إيجاد إجابة نهائية."