Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -43,6 +43,33 @@ def extract_text(query: str, retriever):
|
|
43 |
return "لم يتم العثور على معلومات مطابقة في الملفات."
|
44 |
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# --- Create NINU Agent ---
|
47 |
def create_ninu_agent(user_docs=None):
|
48 |
bm25_retriever = build_retriever(user_docs) if user_docs else None
|
@@ -66,7 +93,8 @@ def create_ninu_agent(user_docs=None):
|
|
66 |
description="Searches the web for recent information."
|
67 |
)
|
68 |
|
69 |
-
|
|
|
70 |
|
71 |
llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
|
72 |
llm_with_tools = llm.bind_tools(tools)
|
@@ -93,20 +121,16 @@ def run_ninu(query, user_docs=None):
|
|
93 |
conversation = []
|
94 |
|
95 |
intro_prompt = """
|
96 |
-
You are a general AI assistant with access to
|
97 |
-
|
98 |
1. NINU_Lec_retriever: retrieves content from uploaded PDFs based on a query.
|
99 |
2. WebSearch: performs web searches to answer questions about current events or general knowledge.
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
When answering, report your thoughts and finish your answer with the following template:
|
104 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
105 |
-
|
106 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
107 |
-
|
108 |
If you are asked for a number, don't use commas or units (like $, %, etc.) unless specified.
|
109 |
-
|
110 |
If you are asked for a string, avoid articles, abbreviations, and write digits in plain text unless specified.
|
111 |
"""
|
112 |
conversation.append(HumanMessage(content=intro_prompt))
|
@@ -114,3 +138,7 @@ If you are asked for a string, avoid articles, abbreviations, and write digits i
|
|
114 |
|
115 |
response = agent.invoke({"messages": conversation})
|
116 |
return response["messages"][-1].content
|
|
|
|
|
|
|
|
|
|
43 |
return "لم يتم العثور على معلومات مطابقة في الملفات."
|
44 |
|
45 |
|
46 |
+
# --- Additional Tools ---
|
47 |
+
|
48 |
+
def calculator_tool_func(query: str):
|
49 |
+
try:
|
50 |
+
# أداة حساب بسيطة باستخدام eval (يمكن تطويرها لاحقًا)
|
51 |
+
result = str(eval(query, {"__builtins__": {}}))
|
52 |
+
return result
|
53 |
+
except Exception:
|
54 |
+
return "تعذر حساب التعبير المدخل."
|
55 |
+
|
56 |
+
calculator_tool = Tool(
|
57 |
+
name="Calculator",
|
58 |
+
func=calculator_tool_func,
|
59 |
+
description="Performs simple arithmetic calculations."
|
60 |
+
)
|
61 |
+
|
62 |
+
def weather_tool_func(location: str):
|
63 |
+
# يمكن ربطها ب API حقيقي للطقس لاحقًا
|
64 |
+
return f"حالة الطقس في {location}: مشمس، درجة الحرارة 25 درجة مئوية."
|
65 |
+
|
66 |
+
weather_tool = Tool(
|
67 |
+
name="Weather",
|
68 |
+
func=weather_tool_func,
|
69 |
+
description="Provides weather information for a given location."
|
70 |
+
)
|
71 |
+
|
72 |
+
|
73 |
# --- Create NINU Agent ---
|
74 |
def create_ninu_agent(user_docs=None):
|
75 |
bm25_retriever = build_retriever(user_docs) if user_docs else None
|
|
|
93 |
description="Searches the web for recent information."
|
94 |
)
|
95 |
|
96 |
+
# دمج جميع التولز
|
97 |
+
tools = [NINU_tool, SerpAPI_tool, calculator_tool, weather_tool]
|
98 |
|
99 |
llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
|
100 |
llm_with_tools = llm.bind_tools(tools)
|
|
|
121 |
conversation = []
|
122 |
|
123 |
intro_prompt = """
|
124 |
+
You are a general AI assistant with access to several tools:
|
|
|
125 |
1. NINU_Lec_retriever: retrieves content from uploaded PDFs based on a query.
|
126 |
2. WebSearch: performs web searches to answer questions about current events or general knowledge.
|
127 |
+
3. Calculator: performs arithmetic calculations.
|
128 |
+
4. Weather: provides weather information for given locations.
|
129 |
+
Based on the user's query, decide whether to use one or more of these tools.
|
130 |
When answering, report your thoughts and finish your answer with the following template:
|
131 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
|
132 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
|
|
133 |
If you are asked for a number, don't use commas or units (like $, %, etc.) unless specified.
|
|
|
134 |
If you are asked for a string, avoid articles, abbreviations, and write digits in plain text unless specified.
|
135 |
"""
|
136 |
conversation.append(HumanMessage(content=intro_prompt))
|
|
|
138 |
|
139 |
response = agent.invoke({"messages": conversation})
|
140 |
return response["messages"][-1].content
|
141 |
+
|
142 |
+
|
143 |
+
# تسجيل الوكيل باسم `ninu` ليتم استيراده من ملفات أخرى
|
144 |
+
ninu = create_ninu_agent
|