-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_api.py
92 lines (58 loc) · 2.39 KB
/
parser_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import copy
import os
import platform
import time
import readline
import json
from shutil import copyfile
from aiohttp import web
import requests
from propose_prompt_response import get_prompt
from propose_prompt_response import convert_to_json
address = '0.0.0.0'
port = '5001'
def convert_prompt(prompt):
total_data = {}
total_data['history'] = None
total_data['prompt'] = prompt
total_data['query'] = None
total_data = json.dumps(total_data)
return total_data
async def handle(request):
print("this is request:{}".format(request))
data = await request.json()
print("this is data:{}".format(data))
message = data['message']
print("this is message:{}".format(message))
url_list = " http://192.168.192.5:3435/api/v1/plugin/list"
jsonList = requests.get(url_list)
print("this is response_status_code: {}".format(jsonList.status_code) )
print("this is response text:{}".format(jsonList.text))
json_data = json.loads(jsonList.text)
json_list = json_data['data']
print("this is json_list:{}".format(json_list))
prompt = get_prompt(json_list, message)
print("\n\n\n\n\===============================================\n\n\n\n")
print("this is prompt:{}".format(prompt))
print("\n\n\n\n\===============================================\n\n\n\n")
with open(f'prompt_parser.txt','w') as f:
f.writelines(prompt)
prompt_path = './prompt_parser.txt'
# post QWen
T1 = time.time()
#response, history = model.chat(tokenizer, prompt, history=history)
url_QWen = 'http://127.0.0.1:48283'
total_data = convert_prompt(prompt_path)
print("\n\n\n\n\nthis is total_data:{}\n\n\n\n\n".format(total_data))
parsed_response = requests.post(url_QWen, data = total_data)
T2 = time.time()
print("this is parse response:{}".format(parsed_response.text))
parsed_response = parsed_response.content.decode()
print("\n\n\n\n\n{}\n\n\n\n\n".format(parsed_response))
response = convert_to_json(parsed_response, json_list)
print("this is response:{}".format(response))
print('程序运行时间:%s毫秒' % ((T2 - T1) * 1000))
return web.Response(text=response)
app = web.Application()
app.add_routes([web.post('/parser', handle)])
web.run_app(app, host=address, port=port)