139 lines
2.4 KiB
Python
139 lines
2.4 KiB
Python
import asyncio
|
|
import ak
|
|
from appPublic.oauth_client import OAuthClient
|
|
desc = {
|
|
"data":{
|
|
"APIKey":ak.AccessKey,
|
|
"SecretKey":ak.AccessKeySecret
|
|
},
|
|
"mapis":{
|
|
"get_access_token":{
|
|
"url":"https://aip.baidubce.com/oauth/2.0/token",
|
|
"method":"POST",
|
|
"headers":[
|
|
{
|
|
"name":"Content-Type",
|
|
"value":"application/json"
|
|
}
|
|
],
|
|
"params":[
|
|
{
|
|
"name":"grant_type",
|
|
"value":"client_credentials"
|
|
},
|
|
{
|
|
"name":"client_id",
|
|
"value":"${APIKey}"
|
|
},
|
|
{
|
|
"name":"client_secret",
|
|
"value":"${SecretKey}"
|
|
}
|
|
],
|
|
"resposne_type":"2",
|
|
"error_field":"error",
|
|
"error_msg_field":"error_description",
|
|
"resp_set_data":[
|
|
{
|
|
"field":"access_token",
|
|
"name":"AccessToken"
|
|
},
|
|
{
|
|
"field":"expires_in",
|
|
"name":"TokenExpiresIn"
|
|
}
|
|
]
|
|
},
|
|
"chat":{
|
|
"url":"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro",
|
|
# "url":"http://localhost/test/show.dspy",
|
|
"method":"POST",
|
|
"headers":[
|
|
{
|
|
"name":"Content-Type",
|
|
"value":"application/json"
|
|
}
|
|
],
|
|
"params":[
|
|
{
|
|
"name":"access_token",
|
|
"value":"${AccessToken}"
|
|
}
|
|
],
|
|
"data":[
|
|
{
|
|
"name":"messages",
|
|
"value":"${messages}"
|
|
},
|
|
{
|
|
"name":"temperature",
|
|
"value":0.1
|
|
},
|
|
{
|
|
"name":"top_p",
|
|
"value":0.8
|
|
},
|
|
{
|
|
"name":"penalty_score",
|
|
"value":1.0
|
|
},
|
|
{
|
|
"name":"stream",
|
|
"value":False
|
|
},
|
|
{
|
|
"name":"system",
|
|
"value":"开元云助手"
|
|
},
|
|
{
|
|
"name":"stop",
|
|
"value":[]
|
|
},
|
|
{
|
|
"name":"max_output_token",
|
|
"value":2048
|
|
}
|
|
],
|
|
"resposne_type":"2",
|
|
"error_field":"error",
|
|
"error_msg_field":"error_description",
|
|
"resp_set_data":[
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
async def main(desc):
|
|
qianfanapi = OAuthClient(desc)
|
|
r = await qianfanapi('get_access_token', {})
|
|
print(f'get_access_token: response={r}, data={qianfanapi.data}')
|
|
msgs = []
|
|
while True:
|
|
print('prompt:')
|
|
p = input()
|
|
if p == '':
|
|
continue
|
|
if p == 'quit':
|
|
break
|
|
msg = {
|
|
"role":"user",
|
|
"content":p
|
|
}
|
|
msgs.append(msg)
|
|
r = await qianfanapi('chat',{"messages":msgs})
|
|
try:
|
|
print(r['result'])
|
|
msg = {
|
|
"role":"assistant",
|
|
"content":r['result']
|
|
}
|
|
msgs.append(msg)
|
|
except Exception as e:
|
|
print(r, e)
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.get_event_loop().run_until_complete(main(desc))
|
|
|