Spaces:
Sleeping
Sleeping
Add tool-calling knowledge retrieval in AI responses
Browse filesEnable function-calling flow so the assistant can query bank knowledge before producing final answers, and align config defaults for model and prompt handling.
Made-with: Cursor
- ai_service.py +89 -42
- config.py +2 -8
ai_service.py
CHANGED
|
@@ -1,70 +1,117 @@
|
|
| 1 |
import re
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
from database import db_manager
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def clean_ai_response(text: str):
|
|
|
|
| 6 |
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 7 |
return cleaned_text.strip()
|
| 8 |
|
| 9 |
-
async def
|
| 10 |
-
|
| 11 |
-
if not pc or not index or not hf_client:
|
| 12 |
-
return "Ai service is not available at the moment. Please try again later."
|
| 13 |
-
|
| 14 |
-
# Save user message if database is available and telegram_id is provided
|
| 15 |
-
conversation_history = ""
|
| 16 |
-
if telegram_id and db_manager:
|
| 17 |
-
db_manager.save_message(telegram_id, user_query, "user")
|
| 18 |
-
conversation_history = db_manager.get_formatted_history(telegram_id, limit=6)
|
| 19 |
-
|
| 20 |
query_embedding = pc.inference.embed(
|
| 21 |
model=EMBED_MODEL,
|
| 22 |
-
inputs=[
|
| 23 |
parameters={"input_type": "query"}
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
# Search Pinecone for Bank Context
|
| 27 |
search_results = index.query(
|
| 28 |
vector=query_embedding[0].values,
|
| 29 |
top_k=3,
|
| 30 |
include_metadata=True
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
"""
|
| 49 |
-
print("User content:", user_content)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
temperature=0.1
|
| 58 |
-
max_tokens=800,
|
| 59 |
-
top_p=0.9,
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if telegram_id and db_manager:
|
| 67 |
db_manager.save_message(telegram_id, cleaned_response, "assistant")
|
| 68 |
|
| 69 |
-
return cleaned_response
|
| 70 |
-
|
|
|
|
| 1 |
import re
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from config import pc, index, EMBED_MODEL, hf_client, PROMPT
|
| 5 |
from database import db_manager
|
| 6 |
|
| 7 |
+
|
| 8 |
+
MODEL_NAME = "dolphin-mistral-24b-venice-edition"
|
| 9 |
+
|
| 10 |
def clean_ai_response(text: str):
|
| 11 |
+
if not text: return ""
|
| 12 |
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 13 |
return cleaned_text.strip()
|
| 14 |
|
| 15 |
+
async def search_bank_knowledge(query: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
query_embedding = pc.inference.embed(
|
| 17 |
model=EMBED_MODEL,
|
| 18 |
+
inputs=[query],
|
| 19 |
parameters={"input_type": "query"}
|
| 20 |
)
|
| 21 |
+
|
|
|
|
| 22 |
search_results = index.query(
|
| 23 |
vector=query_embedding[0].values,
|
| 24 |
top_k=3,
|
| 25 |
include_metadata=True
|
| 26 |
)
|
| 27 |
|
| 28 |
+
return "\n".join([res.metadata['original_text'] for res in search_results.matches])
|
| 29 |
|
| 30 |
+
# تعريف الأداة (Tool) الخاصة بالبحث في وثائق البنك
|
| 31 |
+
TOOLS = [
|
| 32 |
+
{
|
| 33 |
+
"type": "function",
|
| 34 |
+
"function": {
|
| 35 |
+
"name": "search_bank_knowledge",
|
| 36 |
+
"description": "استخدم هذه الأداة للبحث في الملف التعريفي الرسمي لبنك حضرموت للحصول على معلومات دقيقة حول الخدمات، الهيكل التنظيمي، رأس المال، والسياسات.",
|
| 37 |
+
"parameters": {
|
| 38 |
+
"type": "object",
|
| 39 |
+
"properties": {
|
| 40 |
+
"query": {
|
| 41 |
+
"type": "string",
|
| 42 |
+
"description": "جملة البحث باللغة العربية (مثال: 'ما هو رأس مال بنك حضرموت؟' أو 'خدمات الأفراد')."
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"required": ["query"]
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
|
| 51 |
+
async def get_ai_response(user_query: str, telegram_id: int = None):
|
| 52 |
+
if not pc or not index or not hf_client:
|
| 53 |
+
return "عذراً، خدمة الذكاء الاصطناعي غير متوفرة حالياً."
|
| 54 |
|
| 55 |
+
# 1. إدارة تاريخ المحادثة
|
| 56 |
+
conversation_history = []
|
| 57 |
+
if telegram_id and db_manager:
|
| 58 |
+
db_manager.save_message(telegram_id, user_query, "user")
|
| 59 |
+
# جلب آخر 6 رسائل لتوفير السياق للموديل
|
| 60 |
+
raw_history = db_manager.get_history(telegram_id, limit=6)
|
| 61 |
+
for msg in raw_history:
|
| 62 |
+
conversation_history.append({"role": msg['role'], "content": msg['content']})
|
| 63 |
+
else:
|
| 64 |
+
conversation_history.append({"role": "user", "content": user_query})
|
| 65 |
|
| 66 |
+
|
| 67 |
+
messages = [{"role": "system", "content": PROMPT}] + conversation_history
|
|
|
|
| 68 |
|
| 69 |
+
|
| 70 |
+
response = hf_client.chat.completions.create(
|
| 71 |
+
model=MODEL_NAME,
|
| 72 |
+
messages=messages,
|
| 73 |
+
tools=TOOLS,
|
| 74 |
+
tool_choice="auto",
|
| 75 |
+
temperature=0.1
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
response_message = response.choices[0].message
|
| 79 |
+
tool_calls = getattr(response_message, 'tool_calls', None)
|
| 80 |
|
| 81 |
+
|
| 82 |
+
if tool_calls:
|
| 83 |
+
|
| 84 |
+
for tool_call in tool_calls:
|
| 85 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 86 |
+
search_query = function_args.get("query")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
extracted_context = await search_bank_knowledge(search_query)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
messages.append(response_message)
|
| 93 |
+
messages.append({
|
| 94 |
+
"role": "tool",
|
| 95 |
+
"tool_call_id": tool_call.id,
|
| 96 |
+
"name": "search_bank_knowledge",
|
| 97 |
+
"content": extracted_context
|
| 98 |
+
})
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
final_response = hf_client.chat.completions.create(
|
| 102 |
+
model=MODEL_NAME,
|
| 103 |
+
messages=messages,
|
| 104 |
+
temperature=0.3
|
| 105 |
+
)
|
| 106 |
+
ai_final_content = final_response.choices[0].message.content
|
| 107 |
+
else:
|
| 108 |
+
|
| 109 |
+
ai_final_content = response_message.content
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
cleaned_response = clean_ai_response(ai_final_content)
|
| 113 |
+
|
| 114 |
if telegram_id and db_manager:
|
| 115 |
db_manager.save_message(telegram_id, cleaned_response, "assistant")
|
| 116 |
|
| 117 |
+
return cleaned_response
|
|
|
config.py
CHANGED
|
@@ -20,15 +20,9 @@ TELEGRAM_URL = f"https://149.154.167.220/bot{TELEGRAM_TOKEN}/sendMessage" if TEL
|
|
| 20 |
EMBED_MODEL = os.environ.get("EMBED_MODEL", "multilingual-e5-large")
|
| 21 |
HF_MODEL = os.environ.get(
|
| 22 |
"HF_MODEL",
|
| 23 |
-
"dphn/Dolphin-Mistral-24B-Venice-Edition
|
| 24 |
-
)
|
| 25 |
-
PROMPT = os.environ.get(
|
| 26 |
-
"PROMPT",
|
| 27 |
-
"You are a helpful customer service assistant for Hadhramout Bank. "
|
| 28 |
-
"Answer the user's question based on the provided context. If the context "
|
| 29 |
-
"doesn't contain the answer, politely say you don't have enough information "
|
| 30 |
-
"to help with that specific query."
|
| 31 |
)
|
|
|
|
| 32 |
|
| 33 |
# Initialize clients only if API keys are available
|
| 34 |
pc = None
|
|
|
|
| 20 |
EMBED_MODEL = os.environ.get("EMBED_MODEL", "multilingual-e5-large")
|
| 21 |
HF_MODEL = os.environ.get(
|
| 22 |
"HF_MODEL",
|
| 23 |
+
"dphn/Dolphin-Mistral-24B-Venice-Edition",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
+
PROMPT = os.environ.get("PROMPT")
|
| 26 |
|
| 27 |
# Initialize clients only if API keys are available
|
| 28 |
pc = None
|