User Tools

Site Tools


язык_программирования_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
язык_программирования_python [2026/11/20 11:10]
val [tool_calling_demo.py]
язык_программирования_python [2026/11/23 13:41] (current)
val [tool_calling_demo.py]
Line 700: Line 700:
  
 # Начальный запрос пользователя # Начальный запрос пользователя
-user_query = "​Привет! Назови мое счастливое число на сегодня и пожелай удачи ​разнообразно." +user_query = "​Привет! Назови мое счастливое число на сегодня и пожелай удачи"​ 
-# user_query = "​Привет! Пожелай мне удачи ​разнообразно."+# user_query = "​Привет! Пожелай мне удачи"​
  
 messages = [ messages = [
Line 780: Line 780:
 ZABBIX_TOKEN = os.environ["​ZABBIX_TOKEN"​] ZABBIX_TOKEN = os.environ["​ZABBIX_TOKEN"​]
 MCP_URL = os.environ.get("​MCP_SERVER_URL",​ "​http://​127.0.0.1:​8080/​mcp"​) MCP_URL = os.environ.get("​MCP_SERVER_URL",​ "​http://​127.0.0.1:​8080/​mcp"​)
 +
 +
 +def debug_log(title:​ str, data):
 +    if hasattr(data,​ "​model_dump"​):​
 +        data = data.model_dump(exclude_none=True)
 +    text = data if isinstance(data,​ str) else json.dumps(data,​ ensure_ascii=False,​ indent=2)
 +    print(f"​\n--- [DEBUG: {title}] ---\n{text}\n{'​-'​ * (20 + len(title))}\n"​)
  
  
Line 807: Line 814:
         llm = AsyncOpenAI(base_url=BASE_URL,​ api_key=API_KEY,​ http_client=llm_http)         llm = AsyncOpenAI(base_url=BASE_URL,​ api_key=API_KEY,​ http_client=llm_http)
  
 +        step = 1
         while True:         while True:
 +            debug_log(
 +                f"​ШАГ {step} | ЗАПРОС К МОДЕЛИ",​
 +                [m.model_dump(exclude_none=True) if hasattr(m, "​model_dump"​) else m for m in messages],
 +            )
 +
             resp = await llm.chat.completions.create(             resp = await llm.chat.completions.create(
                 model=MODEL_ID,​                 model=MODEL_ID,​
Line 814: Line 827:
                 temperature=0.1,​                 temperature=0.1,​
             )             )
 +
 +            debug_log(f"​ШАГ {step} | ПОЛНЫЙ ОТВЕТ МОДЕЛИ",​ resp)
 +
             msg = resp.choices[0].message             msg = resp.choices[0].message
             messages.append(msg)             messages.append(msg)
Line 822: Line 838:
             for tc in msg.tool_calls:​             for tc in msg.tool_calls:​
                 args = json.loads(tc.function.arguments or "​{}"​)                 args = json.loads(tc.function.arguments or "​{}"​)
 +                debug_log(f"​ШАГ {step} | ВЫЗОВ ИНСТРУМЕНТА {tc.function.name}",​ args)
 +
                 res = await session.call_tool(tc.function.name,​ arguments=args)                 res = await session.call_tool(tc.function.name,​ arguments=args)
                 text = "​\n"​.join(c.text for c in res.content if hasattr(c, "​text"​))                 text = "​\n"​.join(c.text for c in res.content if hasattr(c, "​text"​))
 +
 +                debug_log(f"​ШАГ {step} | ОТВЕТ ИНСТРУМЕНТА {tc.function.name}",​ text or "​Пустой ответ от инструмента"​)
 +
                 messages.append({                 messages.append({
                     "​role":​ "​tool",​                     "​role":​ "​tool",​
Line 830: Line 851:
                     "​content":​ text or "​Пустой ответ от инструмента",​                     "​content":​ text or "​Пустой ответ от инструмента",​
                 })                 })
 +
 +            step += 1
 +
  
 if __name__ == "​__main__":​ if __name__ == "​__main__":​
язык_программирования_python.1795162228.txt.gz · Last modified: 2026/11/20 11:10 by val