Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SnowyLchen opened this issue Aug 29, 2023 · 4 comments
Open

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

SnowyLchen opened this issue Aug 29, 2023 · 4 comments

Comments

@SnowyLchen
Copy link

如果支持,应该怎么做?

@wwwzhouhui
Copy link
Owner

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

@ZZY205A2
Copy link

ZZY205A2 commented Sep 2, 2023

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

我用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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@wwwzhouhui @SnowyLchen @ZZY205A2 and others