-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqwen.py
81 lines (75 loc) · 2.78 KB
/
qwen.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
try:
from .config import qwen_Config
from .base_chat import aichat
from hoshino import aiorequests
except ImportError:
import sys, os
_current_dir = os.path.dirname(__file__)
if _current_dir not in sys.path:
sys.path.insert(0, _current_dir)
from config import qwen_Config
from base_chat import aichat
import aiorequests
class Qwen(aichat):
config: qwen_Config
enable_search: bool = False
def __init__(self):
self.config = qwen_Config()
self.headers = {
'Authorization': f'Bearer {self.config.api_key}',
'Content-Type': 'application/json',
}
async def asend(self, msg, gid, uid):
url = self.config.url
self.data = {
"model": self.config.model,
"messages": [
{
'role': 'user',
'content': msg
},
],
"top_p": self.config.top_p,
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens,
"stream": False,
}
if self.config.system:
self.data['messages'].insert(0, {'role':'system','content': f'{self.config.system}'})
if self.enable_search:
self.data['model'] = self.config.model_search
self.data['enable_search'] = True
# print(self.data)
resp = await aiorequests.post(f'{url}', headers=self.headers, json=self.data)
resp_j = await resp.json()
print(resp_j)
if "error" in resp_j.keys():
# 发生错误
error_code = resp_j['error']['code']
error_message = resp_j['error']['message']
self.response = f"发生错误:\ncode: {error_code}\n{error_message}"
return resp_j
self.response = resp_j['choices'][0]['message']['content']
self.usage = resp_j['usage']
self.completion_tokens = int(resp_j['usage']['completion_tokens'])
self.prompt_tokens = int(resp_j['usage']['prompt_tokens'])
self.total_tokens = int(resp_j['usage']['total_tokens'])
# model很有辨识度,所以就拿model名字来分别记了
await self.token_cost_record(gid, uid, self.total_tokens, self.data['model'])
return resp_j
if __name__ == '__main__':
import asyncio
async def task1():
print("Task 1 is running")
qwen = Qwen()
qwen.enable_search = False
await qwen.asend('如何结束俄乌冲突', 112233445566, 1)
print(qwen.get_response())
print(qwen.get_usage())
print("Task 1 completed")
async def main():
# tasks = [task1(), task2()]
tasks = [task1()]
await asyncio.gather(*tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())