Ali-Developments commited on
Commit
80ee31a
·
verified ·
1 Parent(s): 34dbfd3

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +21 -29
agent.py CHANGED
@@ -9,12 +9,12 @@ from langchain_core.messages import AnyMessage, HumanMessage
9
  from langchain_groq import ChatGroq
10
  from typing import TypedDict, Annotated
11
 
12
- # Load environment variables
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:
@@ -45,33 +45,31 @@ SerpAPI_tool = Tool(
45
  description="Searches the web for recent information."
46
  )
47
 
48
- # --- بناء الوكيل ---
49
 
50
- def create_search_only_agent():
51
- tools = [SerpAPI_tool, calculator_tool, weather_tool]
52
- llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
53
- llm_with_tools = llm.bind_tools(tools)
54
 
55
- class AgentState(TypedDict):
56
- messages: Annotated[list[AnyMessage], add_messages]
57
 
58
- def assistant(state: AgentState):
59
- return {"messages": [llm_with_tools.invoke(state["messages"])]}
60
 
61
- builder = StateGraph(AgentState)
62
- builder.add_node("assistant", assistant)
63
- builder.add_node("tools", ToolNode(tools))
64
- builder.add_edge(START, "assistant")
65
- builder.add_conditional_edges("assistant", tools_condition)
66
- builder.add_edge("tools", "assistant")
67
 
68
- return builder.compile()
 
69
 
70
- # --- دالة التفاعل ---
71
-
72
- def run_search_agent(query):
73
- agent = create_search_only_agent()
74
 
 
75
  conversation = []
76
 
77
  intro_prompt = """
@@ -87,11 +85,5 @@ Only include what the user asked for in the final answer.
87
  conversation.append(HumanMessage(content=intro_prompt))
88
  conversation.append(HumanMessage(content=query))
89
 
90
- response = agent.invoke({"messages": conversation})
91
  return response["messages"][-1].content
92
-
93
- # يمكن استخدام الدالة التالية لاختبار الوكيل
94
- if __name__ == "__main__":
95
- question = "ما هي عاصمة اليابان وكم عدد سكانها؟"
96
- answer = run_search_agent(question)
97
- print(answer)
 
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:
 
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 = """
 
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