-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathqianfan.py
More file actions
executable file
·42 lines (33 loc) · 1.25 KB
/
Copy pathqianfan.py
File metadata and controls
executable file
·42 lines (33 loc) · 1.25 KB
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
#!/usr/bin/env python3
import requests
import json
import argparse
import sys
urls = {
"ErnieBot": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant",
"ErnieBot-turbo": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant",
}
def chat(api_key, secret_key, messages, m):
url = urls[m] + "?access_token=" + get_access_token(api_key, secret_key)
payload = json.dumps({
"messages": messages
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def get_access_token(api_key, secret_key):
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials",
"client_id": api_key, "client_secret": secret_key}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('api_key')
parser.add_argument('secret_key')
parser.add_argument('messages')
parser.add_argument('model')
args = parser.parse_args()
messages = json.loads(args.messages)
chat(args.api_key, args.secret_key, messages, args.model)