forked from hl123-123/yiyan-ppt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yiyan.py
69 lines (59 loc) · 2.03 KB
/
yiyan.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
import requests
import json
import os
from config import Config
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
API_Key = Config.API_Key
Secret_Key = Config.Secret_Key
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={API_Key}&client_secret={Secret_Key}".format(API_Key=API_Key,Secret_Key=Secret_Key)
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def yiyan_api(message,access_token,use4=False):
if use4:
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + access_token
else:
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + access_token
payload = json.dumps({
"messages": [
{
"role": "user",
"content": message
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
try:
result = json.loads(response.text)["result"]
except:
print(response.text)
# print(result)
return result
def yiyan_embedding(input_text):
if input_text=="":
input_text=" "
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/embedding-v1?access_token=" + get_access_token()
payload = json.dumps({
"input": [input_text]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
result = json.loads(response.text)["data"][0]["embedding"]
return result
def main():
embed = yiyan_embedding()
print(embed)
if __name__ == '__main__':
main()