Spaces:
Sleeping
Sleeping
Fix: pass TOOLS to API and handle tool call agentic loop
Browse files- ai_service.py +25 -5
ai_service.py
CHANGED
|
@@ -62,13 +62,33 @@ async def get_ai_response(user_query: str, telegram_id: int):
|
|
| 62 |
loop = asyncio.get_event_loop()
|
| 63 |
|
| 64 |
|
| 65 |
-
def call_hf():
|
| 66 |
return hf_client.chat.completions.create(
|
| 67 |
model=MODEL_NAME,
|
| 68 |
-
messages=
|
|
|
|
|
|
|
| 69 |
temperature=0.1,
|
| 70 |
max_tokens=800
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
completion = await loop.run_in_executor(None, call_hf)
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
loop = asyncio.get_event_loop()
|
| 63 |
|
| 64 |
|
| 65 |
+
def call_hf(msgs):
|
| 66 |
return hf_client.chat.completions.create(
|
| 67 |
model=MODEL_NAME,
|
| 68 |
+
messages=msgs,
|
| 69 |
+
tools=TOOLS,
|
| 70 |
+
tool_choice="auto",
|
| 71 |
temperature=0.1,
|
| 72 |
max_tokens=800
|
| 73 |
)
|
| 74 |
+
|
| 75 |
+
completion = await loop.run_in_executor(None, lambda: call_hf(messages))
|
| 76 |
+
response_message = completion.choices[0].message
|
| 77 |
+
|
| 78 |
+
# Handle tool call if model requests it
|
| 79 |
+
if response_message.tool_calls:
|
| 80 |
+
tool_call = response_message.tool_calls[0]
|
| 81 |
+
args = json.loads(tool_call.function.arguments)
|
| 82 |
+
tool_result = await search_bank_knowledge(args["query"])
|
| 83 |
+
|
| 84 |
+
messages.append(response_message)
|
| 85 |
+
messages.append({
|
| 86 |
+
"role": "tool",
|
| 87 |
+
"tool_call_id": tool_call.id,
|
| 88 |
+
"content": tool_result
|
| 89 |
+
})
|
| 90 |
+
|
| 91 |
+
completion = await loop.run_in_executor(None, lambda: call_hf(messages))
|
| 92 |
+
response_message = completion.choices[0].message
|
| 93 |
+
|
| 94 |
+
return clean_ai_response(response_message.content)
|