feat: + tongyi and wenxin

This commit is contained in:
csunny
2023-10-10 20:48:53 +08:00
parent 32ae362eac
commit 14cfc34c72
5 changed files with 196 additions and 2 deletions

49
examples/tongyi.py Normal file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import dashscope
import requests
from http import HTTPStatus
from dashscope import Generation
def call_with_messages():
messages = [{'role': 'system', 'content': '你是生活助手机器人。'},
{'role': 'user', 'content': '如何做西红柿鸡蛋?'}]
gen = Generation()
response = gen.call(
Generation.Models.qwen_turbo,
messages=messages,
stream=True,
top_p=0.8,
result_format='message', # set the result to be "message" format.
)
for response in response:
# The response status_code is HTTPStatus.OK indicate success,
# otherwise indicate request is failed, you can get error code
# and message from code and message.
if response.status_code == HTTPStatus.OK:
print(response.output) # The output text
print(response.usage) # The usage information
else:
print(response.code) # The error code.
print(response.message) # The error message.
def build_access_token(api_key: str, secret_key: str) -> str:
"""
Generate Access token according AK, SK
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": api_key, "client_secret": secret_key}
res = requests.get(url=url, params=params)
if res.status_code == 200:
return res.json().get("access_token")
if __name__ == '__main__':
call_with_messages()