Jacky2305 commited on
Commit
fffa174
·
verified ·
1 Parent(s): 507e1be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -30
app.py CHANGED
@@ -1,34 +1,111 @@
1
- import os
2
- import asyncio
3
- from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
4
- from telegram import Update
5
- from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes
6
-
7
- model = OpenAIServerModel(
8
- model_id="qwen2.5-3b-instruct",
9
- api_base="https://jacky2305-llm-api.hf.space/v1",
10
- api_key="sk-any",
11
- )
12
 
13
- agent = CodeAgent(
14
- tools=[DuckDuckGoSearchTool()],
15
- model=model,
16
- additional_authorized_imports=["requests", "json", "re"],
17
  )
18
 
19
- async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
20
- user_msg = update.message.text
21
- await update.message.reply_text(" 思考中...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- try:
24
- result = agent.run(user_msg)
25
- await update.message.reply_text(str(result))
26
- except Exception as e:
27
- await update.message.reply_text(f"❌ 出错了:{e}")
28
-
29
- if __name__ == "__main__":
30
- token = os.environ["TELEGRAM_TOKEN"]
31
- app = ApplicationBuilder().token(token).build()
32
- app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
33
- print("Bot 启动中...")
34
- app.run_polling()
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import json
 
 
 
 
 
 
 
 
4
 
5
+ client = OpenAI(
6
+ base_url="https://jacky2305-llm-api.hf.space/v1",
7
+ api_key="sk-any"
 
8
  )
9
 
10
+ tools = [
11
+ {
12
+ "type": "function",
13
+ "function": {
14
+ "name": "get_weather",
15
+ "description": "获取某个城市的天气",
16
+ "parameters": {
17
+ "type": "object",
18
+ "properties": {
19
+ "city": {"type": "string", "description": "城市名称"}
20
+ },
21
+ "required": ["city"]
22
+ }
23
+ }
24
+ },
25
+ {
26
+ "type": "function",
27
+ "function": {
28
+ "name": "calculate",
29
+ "description": "执行数学计算",
30
+ "parameters": {
31
+ "type": "object",
32
+ "properties": {
33
+ "expression": {"type": "string", "description": "数学表达式"}
34
+ },
35
+ "required": ["expression"]
36
+ }
37
+ }
38
+ }
39
+ ]
40
+
41
+ def run_tool(name, args):
42
+ if name == "get_weather":
43
+ return f"{args['city']}今天晴天,28°C"
44
+ if name == "calculate":
45
+ try:
46
+ return str(eval(args["expression"]))
47
+ except:
48
+ return "计算出错"
49
+
50
+ def test_tool_calling(user_msg):
51
+ log = []
52
+ messages = [{"role": "user", "content": user_msg}]
53
+
54
+ resp = client.chat.completions.create(
55
+ model="qwen2.5-3b-instruct",
56
+ messages=messages,
57
+ tools=tools,
58
+ tool_choice="auto",
59
+ max_tokens=300
60
+ )
61
+
62
+ msg = resp.choices[0].message
63
+ finish_reason = resp.choices[0].finish_reason
64
+ log.append(f"finish_reason: {finish_reason}")
65
+
66
+ if finish_reason != "tool_calls" or not msg.tool_calls:
67
+ log.append(f"⚠️ 模型没有调用工具,直接回答:\n{msg.content}")
68
+ return "\n".join(log)
69
+
70
+ for tool_call in msg.tool_calls:
71
+ name = tool_call.function.name
72
+ args = json.loads(tool_call.function.arguments)
73
+ log.append(f"✅ 调用工具:{name}({args})")
74
+
75
+ result = run_tool(name, args)
76
+ log.append(f"工具返回:{result}")
77
+
78
+ messages.append(msg)
79
+ messages.append({
80
+ "role": "tool",
81
+ "tool_call_id": tool_call.id,
82
+ "content": result
83
+ })
84
+
85
+ resp2 = client.chat.completions.create(
86
+ model="qwen2.5-3b-instruct",
87
+ messages=messages,
88
+ max_tokens=300
89
+ )
90
+ log.append(f"\n最终回答:\n{resp2.choices[0].message.content}")
91
+ return "\n".join(log)
92
+
93
+ with gr.Blocks() as demo:
94
+ gr.Markdown("## Qwen 3B 工具调用测试")
95
+ with gr.Row():
96
+ inp = gr.Textbox(label="输入问题", placeholder="试试:吉隆坡天气怎么样?")
97
+ btn = gr.Button("测试")
98
+ out = gr.Textbox(label="结果日志", lines=15)
99
+
100
+ gr.Examples(
101
+ examples=[
102
+ ["吉隆坡今天天气怎么样?"],
103
+ ["帮我算一下 1234 * 5678"],
104
+ ["你好"], # 不需要工具
105
+ ],
106
+ inputs=inp
107
+ )
108
 
109
+ btn.click(test_tool_calling, inputs=inp, outputs=out)
110
+
111
+ demo.launch()