Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
import requests
|
4 |
from flask import Flask, request, jsonify
|
5 |
from datetime import datetime
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
@@ -113,7 +114,6 @@ class EventEmitter:
|
|
113 |
'metadata': [{'name': title, 'source': url, 'html': False}],
|
114 |
})
|
115 |
|
116 |
-
|
117 |
def make_telegram_request(method, data=None):
|
118 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
119 |
if PHP_PROXY_URL: # 使用代理
|
@@ -265,6 +265,13 @@ def parseCommand(userMessage):
|
|
265 |
command = command.split('@')[0]
|
266 |
return command[1:]
|
267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
async def processAiMessage(chatId, userMessage, fromUserId):
|
269 |
history = chatHistories.get(chatId, [])
|
270 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
@@ -279,13 +286,7 @@ async def processAiMessage(chatId, userMessage, fromUserId):
|
|
279 |
*history
|
280 |
]
|
281 |
|
282 |
-
eventEmitter = EventEmitter(
|
283 |
-
print('Event:', event)
|
284 |
-
if event.get('type') == 'status':
|
285 |
-
await sendTelegramMessage(chatId, f"状态更新: {event['data']['description']}")
|
286 |
-
elif event.get('type') == 'citation':
|
287 |
-
await sendTelegramMessage(chatId, f"引用信息: {event['data']['metadata'][0]['name']}")
|
288 |
-
})
|
289 |
|
290 |
try:
|
291 |
ai_response = requests.post(AI_API_ENDPOINT, headers=AI_API_HEADERS, json={
|
@@ -319,7 +320,7 @@ async def handleAiResponse(ai_data, chatId, history, eventEmitter):
|
|
319 |
toolCalls = choice['message']['tool_calls']
|
320 |
toolResults = []
|
321 |
for toolCall in toolCalls:
|
322 |
-
toolResult = await executeToolCall(toolCall, eventEmitter)
|
323 |
toolResults.append(toolResult)
|
324 |
|
325 |
newMessages = [
|
@@ -340,7 +341,7 @@ async def handleAiResponse(ai_data, chatId, history, eventEmitter):
|
|
340 |
return 'AI 返回了无法识别的格式'
|
341 |
return 'AI 返回了无法识别的格式'
|
342 |
|
343 |
-
async def executeToolCall(toolCall, eventEmitter):
|
344 |
name = toolCall['function']['name']
|
345 |
args = toolCall['function'].get('arguments', {})
|
346 |
|
@@ -510,8 +511,5 @@ async def handle_webhook():
|
|
510 |
def health_check():
|
511 |
return 'OK'
|
512 |
|
513 |
-
|
514 |
-
import asyncio
|
515 |
if __name__ == '__main__':
|
516 |
-
app.run(host='0.0.0.0', port=int(os.environ.get('PORT',
|
517 |
-
|
|
|
3 |
import requests
|
4 |
from flask import Flask, request, jsonify
|
5 |
from datetime import datetime
|
6 |
+
import asyncio
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
|
|
114 |
'metadata': [{'name': title, 'source': url, 'html': False}],
|
115 |
})
|
116 |
|
|
|
117 |
def make_telegram_request(method, data=None):
|
118 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
119 |
if PHP_PROXY_URL: # 使用代理
|
|
|
265 |
command = command.split('@')[0]
|
266 |
return command[1:]
|
267 |
|
268 |
+
async def event_handler(event):
|
269 |
+
print('Event:', event)
|
270 |
+
if event.get('type') == 'status':
|
271 |
+
await sendTelegramMessage(event['data']['chatId'], f"状态更新: {event['data']['description']}")
|
272 |
+
elif event.get('type') == 'citation':
|
273 |
+
await sendTelegramMessage(event['data']['chatId'], f"引用信息: {event['data']['metadata'][0]['name']}")
|
274 |
+
|
275 |
async def processAiMessage(chatId, userMessage, fromUserId):
|
276 |
history = chatHistories.get(chatId, [])
|
277 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
|
|
286 |
*history
|
287 |
]
|
288 |
|
289 |
+
eventEmitter = EventEmitter(event_handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
|
291 |
try:
|
292 |
ai_response = requests.post(AI_API_ENDPOINT, headers=AI_API_HEADERS, json={
|
|
|
320 |
toolCalls = choice['message']['tool_calls']
|
321 |
toolResults = []
|
322 |
for toolCall in toolCalls:
|
323 |
+
toolResult = await executeToolCall(toolCall, eventEmitter,chatId)
|
324 |
toolResults.append(toolResult)
|
325 |
|
326 |
newMessages = [
|
|
|
341 |
return 'AI 返回了无法识别的格式'
|
342 |
return 'AI 返回了无法识别的格式'
|
343 |
|
344 |
+
async def executeToolCall(toolCall, eventEmitter,chatId):
|
345 |
name = toolCall['function']['name']
|
346 |
args = toolCall['function'].get('arguments', {})
|
347 |
|
|
|
511 |
def health_check():
|
512 |
return 'OK'
|
513 |
|
|
|
|
|
514 |
if __name__ == '__main__':
|
515 |
+
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
|
|