Pychrom对接流云GPT代理 Posted on 2023-08-20 | In python Python对接流云GPT代理注册账号 查看余额登录账号 代码Demo123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import requestsimport jsonclass 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.