提交 0d4bbc6d 编写于 作者: H hjdhnx

1

上级 0790e073
...@@ -4,16 +4,19 @@ ...@@ -4,16 +4,19 @@
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------ # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2023/2/7 # Date : 2023/2/7
import json import json
from time import time import time
# 文档地址: https://platform.openai.com/docs/api-reference/completions/create # 文档地址: https://platform.openai.com/docs/api-reference/completions/create
import requests import requests
API_KEY = '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' API_KEY = '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'
AUTH = f'Bearer {API_KEY}' AUTH = f'Bearer {API_KEY}'
def ask_chatpgt(word): def ask_chatpgt(word):
"""
问chatgpt得到答案
"""
headers = { headers = {
'Content-Type':'application/json', 'Content-Type':'application/json',
'Authorization':AUTH, 'Authorization':AUTH,
...@@ -30,23 +33,24 @@ def ask_chatpgt(word): ...@@ -30,23 +33,24 @@ def ask_chatpgt(word):
"stop": [" AI:"] "stop": [" AI:"]
} }
# print(pdata) # print(pdata)
t1 = time() t1 = time.time()
ret = ''
try: try:
r = requests.post('https://api.openai.com/v1/completions',data=json.dumps(pdata),headers=headers) r = requests.post('https://api.openai.com/v1/completions',data=json.dumps(pdata),headers=headers)
ret = r.json() ret = r.json()
# print(ret)
answer = ret['choices'][0]['text'] answer = ret['choices'][0]['text']
except Exception as e: except Exception as e:
answer = f'发生了错误:{e}' answer = f'发生了错误:{e} {ret}'
# print(ret) # print(ret)
# print(answer) # print(answer)
t2 = time() t2 = time.time()
sec = round((t2 - t1)*1000,2) sec = round((t2 - t1)*1000,2)
# print(f'共计耗时:{sec}毫秒') print(f'共计耗时:{sec}毫秒,{answer}')
return [answer,sec] return [answer,sec]
if __name__ == '__main__': if __name__ == '__main__':
print(ask_chatpgt('1+1等于几')) print(ask_chatpgt('1+1等于几'))
# https://chat.openai.com/auth/login
# http://fastapi.frp.mudery.com/other_request/chatgpt # http://fastapi.frp.mudery.com/other_request/chatgpt
# http://spider.scwinbao.com:8274/other_request/chatgpt # http://spider.scwinbao.com:8274/other_request/chatgpt
# print(ask_chatpgt('假如我处于一个荒岛,现在我来扮演玩家,你来扮演电脑,你给我选项,我们玩一个荒岛求生的游戏')) # print(ask_chatpgt('假如我处于一个荒岛,现在我来扮演玩家,你来扮演电脑,你给我选项,我们玩一个荒岛求生的游戏'))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册