Skip to content

Commit ae2e084

Browse files
authored
Create ChatGPT-OpenAI.py
1 parent 5da6aee commit ae2e084

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

ChatGPT-OpenAI.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import base64
2+
import openai
3+
import requests
4+
import json
5+
from Crypto.Cipher import AES # pip install pycryptodome
6+
7+
openai_url = 'https://api.openai.com/v1/completions'
8+
sbaliyun_url = 'https://cc-api.sbaliyun.com/v1/completions'
9+
sbaliyun_headers = {
10+
'Content-Type': 'application/json',
11+
'referer': 'https://chatgpt.sbaliyun.com/',
12+
}
13+
14+
def sbaliyun():
15+
while True:
16+
msg = input("请输入要问的话:")
17+
if msg == 'exit':
18+
break
19+
msg = aes_encrypt(msg)
20+
json_res = json.dumps({"prompt": msg})
21+
result = requests.post(url=sbaliyun_url, headers=sbaliyun_headers, data=json_res)
22+
try:
23+
print(json.loads(result.text)["choices"][0]["text"])
24+
print()
25+
except:
26+
print("请重新请求")
27+
28+
29+
def aes_encrypt(text):
30+
key = 'L#$@XowPu!uZ&c%u'.encode('utf-8')
31+
iv = '2auvLZzxz7bo#^84'.encode('utf-8')
32+
padding = lambda data: data + (16 - len(data.encode('utf-8')) % 16) * chr(16 - len(data.encode('utf-8')) % 16)
33+
text = padding(text).encode("utf-8")
34+
encryptor = AES.new(key, AES.MODE_CBC, iv)
35+
res = encryptor.encrypt(text)
36+
return base64.b64encode(res).decode('utf-8')
37+
38+
def set_key():
39+
while True:
40+
open_key = input("请输入您的key:")
41+
print("检测key是否正确...")
42+
# 检测key是否正确
43+
if open_key[:3] != 'sk-':
44+
print("key错误,请重新设置key")
45+
continue
46+
openai.api_key = open_key
47+
json_res = json.dumps({"model": "text-davinci-003", "prompt": "你", "max_tokens": 2048, "temperature": 0.5, "n": 1,
48+
"stream": False, "stop": "§"})
49+
openai_headers = {
50+
'Content-Type': 'application/json',
51+
'Authorization': 'Bearer ' + openai.api_key,
52+
}
53+
result = requests.post(url='https://api.openai.com/v1/completions', headers=openai_headers, data=json_res)
54+
# 判断key是否正确
55+
if result.status_code != 200:
56+
print("key错误,请重新设置key")
57+
continue
58+
print("设置成功,您的key为:", open_key)
59+
break
60+
61+
def req_api():
62+
set_key()
63+
while True:
64+
msg = input("请输入要问的话:")
65+
if msg == 'exit':
66+
break
67+
if msg == 'set_key':
68+
set_key()
69+
continue
70+
json_res = json.dumps({"model": "text-davinci-003","prompt": msg, "max_tokens": 2048, "temperature": 0.5, "n": 1, "stream": False, "stop": "§"})
71+
openai_headers = {
72+
'Content-Type': 'application/json',
73+
'Authorization': 'Bearer ' + openai.api_key,
74+
}
75+
result = requests.post(url=openai_url, headers=openai_headers, data=json_res)
76+
# 判断key是否正确
77+
if result.status_code == 401:
78+
print("key错误,请重新设置key")
79+
set_key()
80+
continue
81+
try:
82+
print(json.loads(result.text)["choices"][0]["text"])
83+
print()
84+
except:
85+
print("请重新请求")
86+
87+
if __name__ == '__main__':
88+
while True:
89+
print("ChatGPT - OpenAI".center(50, '-'))
90+
print("输入 'exit' 返回上一级或退出,输入 'set_key' 重新设置key")
91+
print("输入 '1' 使用自己的Key")
92+
print("输入 '2' 使用sbaliyun")
93+
num = input("请输入您的选择:")
94+
if num == 'exit':
95+
break
96+
if num == '1':
97+
req_api()
98+
elif num == '2':
99+
sbaliyun()
100+
else:
101+
print("输入错误,请重新输入")
102+

0 commit comments

Comments
 (0)