社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

unofficial-claude2-api:非官方的Python API

GitHubStore • 3 月前 • 115 次点击  

项目简介


这个非官方的 Python API 通过简单的聊天消息界面提供对 Anthropic 的 Claude AI 的对话功能的访问。

虽然没有得到 Anthropic 的正式支持,但该库可以启用有趣的对话应用程序。

它允许:

  • 与 Claude 创建聊天会话并获取聊天 ID。

  • 向 Claude 发送最多包含 5 个附件文件(txt、pdf、csv 等)的消息,每个附件文件 10 MB。

  • 检索聊天消息历史记录,访问特定的聊天对话。

  • 当不再需要旧的聊天记录时将其删除。

  • 通过代理发送请求。



您可以通过此 API 使用 Claude 执行一些关键操作

  • 提出有关各种主题的问题。克劳德可以谈论时事、流行文化、体育等等。

  • 获取有关复杂主题的有用解释。请克劳德用简单的术语解释概念和想法。

  • 从长文本或文档生成摘要。只需将文件路径作为附件提供给 Claude,即可获得简洁的摘要。

  • 收到对开放式提示和想法的深思熟虑的回应。克劳德可以集思广益,扩展概念,并进行哲学讨论。


如何安装

pip install unofficial-claude2-api

卸载

pip uninstall unofficial-claude2-api


要求

使用 selenium 自动检索 SessionData 对象需要这些要求

  • Python >= 3.10

  • 安装了 Firefox,并且至少有一个配置文件登录到 Claude。

  • geckodriver 安装在 PATH 环境变量中注册的文件夹内。


(滚动浏览此自述文件,您还会发现手动替代方案)

用法示例

from sys import exit as sys_exitfrom claude2_api.client import (    ClaudeAPIClient,    SendMessageResponse,)from claude2_api.session import SessionData, get_session_datafrom claude2_api.errors import ClaudeAPIError, MessageRateLimitError, OverloadError
# Wildcard import will also work the same as above# from claude2_api import *
# List of attachments filepaths, up to 5, max 10 MB eachFILEPATH_LIST = [ "test1.txt", "test2.txt",]
# This function will automatically retrieve a SessionData instance using selenium# It will auto gather cookie session, user agent and organization ID.# Omitting profile argument will use default Firefox profilesession: SessionData = get_session_data()
# Initialize a client instance using a session # Optionally change the requests timeout parameter to best fit your needs...default to 240 seconds.client = ClaudeAPIClient(session, timeout=240)
# Create a new chat and cache the chat_idchat_id = client.create_chat()if not chat_id: # This will not throw MessageRateLimitError # But it still means that account has no more messages left. print("\nMessage limit hit, cannot create chat...") sys_exit(1)
try: # Used for sending message with or without attachments # Returns a SendMessageResponse instance res: SendMessageResponse = client.send_message( chat_id, "Hello!", attachment_paths=FILEPATH_LIST ) # Inspect answer if res.answer: print(res.answer) else: # Inspect response status code and raw answer bytes print(f"\nError code {res.status_code}, raw_answer: {res.raw_answer}")except ClaudeAPIError as e: # Identify the error if isinstance(e, MessageRateLimitError): # The exception will hold these informations about the rate limit: print(f"\nMessage limit hit, resets at {e.reset_date}") print(f"\n{e.sleep_sec} seconds left until -> {e.reset_timestamp}") elif isinstance(e, OverloadError): print(f"\nOverloaded error: {e}") else: print(f"\nGot unknown Claude error: {e}")finally: # Perform chat deletion for cleanup client.delete_chat(chat_id)
# Get a list of all chats idsall_chat_ids = client.get_all_chat_ids()# Delete all chatsfor chat in all_chat_ids: client.delete_chat(chat)
# Or by using a shortcut utilityclient.delete_all_chats()sys_exit(0)



项目链接

github.com/st1vms/unofficial-claude2-api

 关注「GitHubStore」公众号

扫一扫以下微信

1 加入技术交流群,备注开发语言-城市-昵称



Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/166812
 
115 次点击