wwwzhouhui / Claude2-PyAPI

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

curl_cffi.curl: TypeError: initializer for ctype 'char *' must be a bytes or list or tuple, not str #27

Closed 00drdelius closed 8 months ago

00drdelius commented 8 months ago
from Claude2_PyAPI.claude_api import Client
COOKIE=None
with root.joinpath("claude_cookie").open('r',encoding='utf8') as ck:
    COOKIE=ck.read().strip()
chat_uuid=[
    'ec4adbca-c7a0-4935-8a29-494c803a5793',
    'c92660e3-7f0f-4d3c-a9eb-6e7bd63ee4f7',
    '7ca6b8f1-aeb9-4ea3-a278-4806f6a2fb35',
    '08aa6cfd-8bb4-47e0-932a-9256512d2517',
    '26d967a0-ff0d-4b63-bd62-a38aa24748eb'
]
claude_api=Client(COOKIE)

prompt="Hello Claude! How are you?"

conversation_id = "<conversation_id>"
conversation_id = chat_uuid[0]
response = claude_api.send_message(prompt, conversation_id)
print(response)

简单想尝试下但报错: Exception has occurred: TypeError (note: full exception trace is shown but execution is paused at: _run_module_as_main) initializer for ctype 'char ' must be a bytes or list or tuple, not str File "C:\Users\admin\AppData\Roaming\Python\Python310\site-packages\curl_cffi\curl.py", line 174, in setopt self._headers = lib.curl_slist_append(self._headers, header) File "D:\models\datasets\claude2(fetch)\Claude2_PyAPI\claude_api.py", line 179, in send_message c.setopt(CurlOpt.HTTPHEADER, str(headers)) File "D:\models\datasets\claude2(fetch)\claude2.py", line 26, in response = claude_api.send_message(prompt, conversation_id) File "C:\Program Files\Python310\Lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Program Files\Python310\Lib\runpy.py", line 196, in _run_module_as_main (Current frame) return _run_code(code, main_globals, None, TypeError: initializer for ctype 'char ' must be a bytes or list or tuple, not str

00drdelius commented 8 months ago

Done.

headers = {
    'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
    'Accept': 'text/event-stream, text/event-stream',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://claude.ai/chats',
    'Content-Type': 'application/json',
    'Origin': 'https://claude.ai',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Cookie': f'{self.cookie}',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'TE': 'trailers'
}
# change format added
headers = [f"{k}: {v}".encode() for k,v in headers.items()]