-
Notifications
You must be signed in to change notification settings - Fork 1
/
llmapi.py
96 lines (81 loc) · 3.44 KB
/
llmapi.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
93
94
95
96
import os
import json
import requests
from dashscope import Generation
from http import HTTPStatus
sys_content = "You are a helpful assistant. You must seek answers within the provided context first \
and output answers based on the context. When you are unable to find the answer within the \
context, you must use your own knowledge base to answer the question. You are not allowed \
to refuse to answer. If you are forced to answer without being able to find the answer, \
you need to indicate at the end of your response, in parentheses, that this answer was not \
found in the context. For questions with a definitive answer, provide the key answer directly \
without lengthy explanations. The output should be in Chinese."
# 使用通义千问获取答案
# 这里使用了环境变量来获取Key
# 需设置如下的环境变量
# export QIANFAN_ACCESS_KEY="xxxx"
# export QIANFAN_SECRET_KEY="xxxx"
def generate_answer_qwen(query, context):
messages = [
{
"role": "system",
"content": sys_content
},
{
"role": "user",
"content": f"问题: {query} 上下文: {context}"
}
]
responses = Generation.call(model="qwen-max",
messages=messages,
result_format='message', # 设置输出为'message'格式
stream=True, # 设置输出方式为流式输出
incremental_output=True # 增量式流式输出
)
#print(messages)
answer = ''
for response in responses:
if response.status_code == HTTPStatus.OK:
answer += response.output.choices[0]['message']['content']
else:
answer = f'Request ERROR: Request id: {response.request_id}, Status code: {response.status_code}, \
error code: {response.code}, error message: {response.message}'
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
break
return answer
def generate_answer_gpt(query, context):
# 定义 API 密钥
# 请在自己电脑上配置 OpenAI API KEY 环境变量
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
API_URL = os.environ.get('API_URL') # OpenAI或者你选择的服务商与 HTTP Post 相关的 URL,例如 "https://api2.aigcbest.top/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
data = {
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": sys_content
},
{
"role": "user",
"content": f"问题: {query} 上下文: {context}"
}
]
}
response = requests.post(API_URL, headers=headers, data=json.dumps(data)) # 从官方 API 文档获得更多信息
response_json = response.json()
answer = response_json['choices'][0]['message']['content']
return answer
def generate_answer(query, context, model="gpt"):
if model == "qwen":
return generate_answer_qwen(query, context)
elif model == "gpt":
return generate_answer_gpt(query, context)
else:
return "Unsupported model"