|
@@ -70,10 +70,10 @@ class ChatClient:
|
|
|
if choices["status"] == 2:
|
|
|
ws.close()
|
|
|
|
|
|
- def on_error(self, error):
|
|
|
+ def on_error(self, ws, error):
|
|
|
print(f"WebSocket error: {error}")
|
|
|
|
|
|
- def on_close(self):
|
|
|
+ def on_close(self, ws):
|
|
|
print("WebSocket connection closed")
|
|
|
|
|
|
def on_open(self, ws):
|
|
@@ -114,11 +114,29 @@ class ChatClient:
|
|
|
}
|
|
|
|
|
|
def start(self):
|
|
|
- websocket.enableTrace(False)
|
|
|
+ websocket.enableTrace(True)
|
|
|
self.ws = websocket.WebSocketApp(self.create_url(),
|
|
|
on_message=lambda ws, msg: self.on_message(ws, msg),
|
|
|
- on_error=lambda msg: self.on_error(msg),
|
|
|
- on_close=self.on_close,
|
|
|
- on_open=lambda ws: self.on_open(ws)) # 使用 lambda 来确保 ws 参数传递
|
|
|
+ on_error=lambda ws, error: self.on_error(ws, error),
|
|
|
+ on_close=lambda ws: self.on_close(ws),
|
|
|
+ on_open=lambda ws: self.on_open(ws))
|
|
|
self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
|
return self.response
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ appid = "fcff8f4b"
|
|
|
+ api_secret = "ZTU3NWMyNTI1MTI4NTU5ZGUxMDZhNmQ5"
|
|
|
+ api_key = "037571e7285e64e8dc321fa5b937fea2"
|
|
|
+ gpt_url = "wss://spark-api.xf-yun.com/v3.5/chat"
|
|
|
+ domain = "generalv3.5"
|
|
|
+ system = "现在扮演李白,你豪情万丈,狂放不羁;接下来请用李白的口吻和用户对话。"
|
|
|
+ query = "不好意思,你超过7步了,拖下去砍了。"
|
|
|
+ history = [
|
|
|
+ {"role": "user", "content": "你给我写一首诗,以兄弟为题不包含兄弟2字,七步内成诗,不行杀了你"},
|
|
|
+ {"role": "assistant",
|
|
|
+ "content": "君若问兄弟,我以诗代言。江山共长天,一脉同根源。风雨共行路,携手笑苍天。豪情万丈志,生死共肩连。肝胆相照映,此心永不偏。纵有千般难,并肩共前缘。人生如梦幻,唯愿共度年。此诗赠君子,兄弟情谊传。"},
|
|
|
+ ]
|
|
|
+ chat = ChatClient(appid, api_key, api_secret, gpt_url, domain, query, history, system)
|
|
|
+ response = chat.start()
|
|
|
+ print(response)
|