parent
e75736b903
commit
a70dae980a
Binary file not shown.
@ -0,0 +1,39 @@
|
||||
import requests
|
||||
from Config.Config import MODEL_API_KEY, MODEL_NAME
|
||||
|
||||
class QWenClient:
|
||||
def __init__(self):
|
||||
self.api_key = MODEL_API_KEY
|
||||
self.model_name = MODEL_NAME
|
||||
self.base_url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"
|
||||
|
||||
def generate(self, prompt):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
payload = {
|
||||
"model": self.model_name,
|
||||
"input": {
|
||||
"messages": [
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
response = requests.post(self.base_url, json=payload, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = QWenClient()
|
||||
while True:
|
||||
prompt = input("请输入问题(输入q退出): ")
|
||||
if prompt.lower() == 'q':
|
||||
break
|
||||
try:
|
||||
result = client.generate(prompt)
|
||||
print("回答:", result['output']['text'])
|
||||
except Exception as e:
|
||||
print(f"发生错误: {str(e)}")
|
Binary file not shown.
Loading…
Reference in new issue