wwwzhouhui / Claude2-PyAPI

一个使用python 实现Claude2 创建会话、聊天、发送附件、获取历史会话,清理历史记录等功能的api接口,可以对接第三方系统作为二次开发使用。
MIT License
308 stars 50 forks source link

请问一下支持流式输出吗? #18

Open SnowyLchen opened 1 year ago

SnowyLchen commented 1 year ago

如果支持,应该怎么做?

wwwzhouhui commented 1 year ago

目前不支持流式输出,后面如果能搞定我会第一时间修改,谢谢您的关注

ZZY205A2 commented 1 year ago

目前不支持流式输出,后面如果能搞定我会第一时间修改,谢谢您的关注

我用claude修改了发送信息的方法,应该可以流式响应了,大佬看看行不行

Send Message to 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()
wwwzhouhui commented 1 year ago

@ZZY205A2 感谢,我回头测试一下如果可以下个版本会添加到项目中。