Spaces:
Sleeping
Sleeping
temporary updates
Browse files- ai_service.py +8 -1
ai_service.py
CHANGED
|
@@ -16,6 +16,7 @@ MODEL_NAME = HF_MODEL
|
|
| 16 |
BASE_PROMPT = PROMPT or "You are a helpful banking customer service assistant."
|
| 17 |
|
| 18 |
def clean_ai_response(text: str):
|
|
|
|
| 19 |
if not text: return ""
|
| 20 |
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 21 |
text = re.sub(r'<br\s*/?>', '\n', text)
|
|
@@ -27,12 +28,14 @@ def clean_ai_response(text: str):
|
|
| 27 |
return text.strip()
|
| 28 |
|
| 29 |
async def search_bank_knowledge(query: str):
|
|
|
|
|
|
|
| 30 |
query_embedding = pc.inference.embed(
|
| 31 |
model=EMBED_MODEL,
|
| 32 |
inputs=[query],
|
| 33 |
parameters={"input_type": "query"}
|
| 34 |
)
|
| 35 |
-
|
| 36 |
search_results = index.query(
|
| 37 |
vector=query_embedding[0].values,
|
| 38 |
top_k=3,
|
|
@@ -149,6 +152,7 @@ TOOLS = [
|
|
| 149 |
|
| 150 |
|
| 151 |
async def run_tool(tool_name: str, args: dict, telegram_id: int):
|
|
|
|
| 152 |
if tool_name == "search_bank_knowledge":
|
| 153 |
return await search_bank_knowledge(args["query"])
|
| 154 |
if tool_name == "check_account_balance":
|
|
@@ -175,6 +179,7 @@ async def run_tool(tool_name: str, args: dict, telegram_id: int):
|
|
| 175 |
async def get_ai_response(user_query: str, telegram_id: int):
|
| 176 |
conversation_history = []
|
| 177 |
if db_manager:
|
|
|
|
| 178 |
raw_history = db_manager.get_conversation_history(telegram_id, limit=6)
|
| 179 |
raw_history.reverse()
|
| 180 |
for msg in raw_history:
|
|
@@ -206,6 +211,7 @@ async def get_ai_response(user_query: str, telegram_id: int):
|
|
| 206 |
|
| 207 |
|
| 208 |
def call_hf(msgs):
|
|
|
|
| 209 |
return hf_client.chat.completions.create(
|
| 210 |
model=MODEL_NAME,
|
| 211 |
messages=msgs,
|
|
@@ -238,6 +244,7 @@ async def get_ai_response(user_query: str, telegram_id: int):
|
|
| 238 |
final_response = clean_ai_response(response_message.content if response_message.content else "")
|
| 239 |
|
| 240 |
if db_manager:
|
|
|
|
| 241 |
db_manager.save_message(telegram_id, user_query, "user")
|
| 242 |
db_manager.save_message(telegram_id, final_response, "assistant")
|
| 243 |
|
|
|
|
| 16 |
BASE_PROMPT = PROMPT or "You are a helpful banking customer service assistant."
|
| 17 |
|
| 18 |
def clean_ai_response(text: str):
|
| 19 |
+
print("arriving to clean function: \n")
|
| 20 |
if not text: return ""
|
| 21 |
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 22 |
text = re.sub(r'<br\s*/?>', '\n', text)
|
|
|
|
| 28 |
return text.strip()
|
| 29 |
|
| 30 |
async def search_bank_knowledge(query: str):
|
| 31 |
+
|
| 32 |
+
print("arriving to start embed function: \n")
|
| 33 |
query_embedding = pc.inference.embed(
|
| 34 |
model=EMBED_MODEL,
|
| 35 |
inputs=[query],
|
| 36 |
parameters={"input_type": "query"}
|
| 37 |
)
|
| 38 |
+
print("arriving to index query function: \n")
|
| 39 |
search_results = index.query(
|
| 40 |
vector=query_embedding[0].values,
|
| 41 |
top_k=3,
|
|
|
|
| 152 |
|
| 153 |
|
| 154 |
async def run_tool(tool_name: str, args: dict, telegram_id: int):
|
| 155 |
+
print("arriving to calling tools: \n")
|
| 156 |
if tool_name == "search_bank_knowledge":
|
| 157 |
return await search_bank_knowledge(args["query"])
|
| 158 |
if tool_name == "check_account_balance":
|
|
|
|
| 179 |
async def get_ai_response(user_query: str, telegram_id: int):
|
| 180 |
conversation_history = []
|
| 181 |
if db_manager:
|
| 182 |
+
print("arriving to get history messages: \n")
|
| 183 |
raw_history = db_manager.get_conversation_history(telegram_id, limit=6)
|
| 184 |
raw_history.reverse()
|
| 185 |
for msg in raw_history:
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
def call_hf(msgs):
|
| 214 |
+
print("arriving to call hugging face: \n")
|
| 215 |
return hf_client.chat.completions.create(
|
| 216 |
model=MODEL_NAME,
|
| 217 |
messages=msgs,
|
|
|
|
| 244 |
final_response = clean_ai_response(response_message.content if response_message.content else "")
|
| 245 |
|
| 246 |
if db_manager:
|
| 247 |
+
print("arriving to save message: \n")
|
| 248 |
db_manager.save_message(telegram_id, user_query, "user")
|
| 249 |
db_manager.save_message(telegram_id, final_response, "assistant")
|
| 250 |
|