Pychrom对接流云GPT代理

Python对接流云GPT代理

注册账号

upload successful

upload successful

查看余额

登录账号

upload successful

upload successful

代码Demo

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


import requests
import json

class ChatGPT:
DEFAULT_API_HOST = "https://liuyuncopilot.com/gateway/outer/"

def __init__(self, apiKey=None, token=None, apiHost=None, userId=None, timeout=30000):
self.apiKey = apiKey
self.token = token
self.apiHost = apiHost or self.DEFAULT_API_HOST
self.userId = userId
self.timeout = timeout

def chat_completion(self, chat_completion_data):
headers = {
'Content-Type': 'application/json'
}

data = {
'chatCompletionReq': chat_completion_data,
'personInfo': {
'userId': self.userId,
'token': self.token
}
}

response = requests.post(f"{self.apiHost}v1/chat/withoutStream", headers=headers, json=data, timeout=self.timeout)

if response.status_code != 200:
raise Exception(f"API call failed with error: {response.text}")

return response.json()

def chat(self, message):
messages = [{"role": "system", "content": message}]
chat_completion_data = {"messages": messages}
response = self.chat_completion(chat_completion_data)
return response.get("choices", [{}])[0].get("message", {}).get("content", "")


if __name__ == '__main__':
# listener = ConsoleStreamListener()

system_message = {"role": "system", "content": "你是一个大诗人,超级会写诗歌"}
message = {"role": "user", "content": "写一篇文章,不少于7000字,题目是:火锅!"}

chat_completion_data = {
"messages": [system_message, message],
"model": "gpt-3.5-turbo-16k"
}
# 替换成你的token
token ="sdf"
# 替换成你的userId
userId="cacd14c0f8aa11edaa9db83902c0"
chat_gpt = ChatGPT(apiKey="YOUR_API_KEY", token=token, userId=userId)

result = chat_gpt.chat_completion(chat_completion_data)
print(result)

# Here, I'm assuming that ConsoleStreamListener is another class or a module
# that you might define in your Python environment to handle the streaming operations.

upload successful