Open SnowyLchen opened 1 year ago
目前不支持流式输出,后面如果能搞定我会第一时间修改,谢谢您的关注
目前不支持流式输出,后面如果能搞定我会第一时间修改,谢谢您的关注
我用claude修改了发送信息的方法,应该可以流式响应了,大佬看看行不行
from curl_cffi import Curl, CurlOpt
...
def send_message(self, prompt, conversation_id, attachment=None, timeout=500):
url = "https://claude.ai/api/append_message"
# Upload attachment if provided
attachments = []
if attachment:
attachment_response = self.upload_attachment(attachment)
if attachment_response:
attachments = [attachment_response]
else:
return {"Error: Invalid file format. Please try again."}
# Ensure attachments is an empty list when no attachment is provided
if not attachment:
attachments = []
payload = json.dumps({
"completion": {
"prompt": f"{prompt}",
"timezone": "Asia/Kolkata",
"model": "claude-2"
},
"organization_uuid": f"{self.organization_id}",
"conversation_uuid": f"{conversation_id}",
"text": f"{prompt}",
"attachments": attachments
})
headers = [b'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
b'Accept: text/event-stream, text/event-stream',
b'Accept-Language: en-US,en;q=0.5',
b'Referer: https://claude.ai/chats',
b'Content-Type: application/json',
b'Origin: https://claude.ai',
b'DNT: 1',
b'Connection: keep-alive',
b'Cookie: %b'%self.cookie.encode('utf-8'),
b'Sec-Fetch-Dest: empty',
b'Sec-Fetch-Mode: cors',
b'Sec-Fetch-Site: same-origin',
b'TE: trailers']
c = Curl()
def stream_callback(data):
json_str = data.decode('utf-8')
decoded_data = re.sub('\n+', '\n', json_str).strip()
data_strings = decoded_data.split('\n')
for data_string in data_strings:
json_str = data_string[6:].strip()
_data = json.loads(json_str)
if 'completion' in _data:
print(_data['completion'], end="")
c.setopt(CurlOpt.URL, b'https://claude.ai/api/append_message')
c.setopt(CurlOpt.WRITEFUNCTION, stream_callback)
c.setopt(CurlOpt.HTTPHEADER, headers)
c.setopt(CurlOpt.POSTFIELDS, payload)
c.impersonate("chrome110")
c.perform()
c.close()
@ZZY205A2 感谢,我回头测试一下如果可以下个版本会添加到项目中。
如果支持,应该怎么做?