原创打造 Claude网页 接口

先进入App unavailable \ Anthropic  

新建聊天聊起来,在聊之前就要打开f12 监听请求 ​​

相关参数打开f12即可找到,注意: 需要在打开f12后发送几条信息给claude才有相关链接显示。

这两个参数就是我们需要的,cookie随便找一个都一样的 

打开postman进行url实体提取:

转化成代码:

import requests
import json
import re
import time

class Claude:
  def __init__(self,org_uuid,con_uuid,cookie):
    self.org_uuid = org_uuid
    self.con_uuid = con_uuid
    self.cookie = cookie
  def send_message(self, query):
    url = "https://claude.ai/api/append_message"
    payload = json.dumps({
      "completion": {
        "prompt": query,
        "timezone": "Asia/Singapore",
        "model": "claude-2",
        "incremental": True
      },
      "organization_uuid": self.org_uuid,
      "conversation_uuid": self.con_uuid,
      "text": query,
      "attachments": []
    })
    headers = {
      'Accept': 'text/event-stream, text/event-stream',
      'Accept-language': 'zh-CN,zh',
      'Content-type': 'application/json',
      'Cookie': self.cookie,
      'Origin': 'https://claude.ai',
      'DNT': '1',
      'Connection': 'keep-alive',
      'Referer': f'https://claude.ai/chat/{self.con_uuid}',
      'Sec-Fetch-Dest': 'empty',
      'Sec-Fetch-Mode': 'cors',
      'Sec-Fetch-Site': 'same-origin',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
      'TE': 'trailers'
    }

    try:
      response = requests.request("POST", url, headers=headers, data=payload,stream = True)
    except Exception as error:
      print(error)
      return False

    if len(response.text) >=60:
      return True
    return False

  def chat_conversation_history(self):
    url = f"https://claude.ai/api/organizations/{self.org_uuid}/chat_conversations/{self.con_uuid}"

    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        # "Accept-Encoding":"gzip, deflate, br",
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Referer': 'https://claude.ai/chats',
        'Content-Type': 'application/json',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Connection': 'keep-alive',
        'Cookie': f'{self.cookie}'
    }

    response = requests.request("GET", url, headers=headers)
    response.encoding = 'utf-8'
    return response.json()

def get_last_answer(history,cache):

  max_index = 0
  for i in history["chat_messages"]:
    if i["index"] > max_index:
      max_index = i["index"]
      body = i
  if max_index not in cache:
    cache.append(max_index)
    answer = body["text"]
    return answer,cache
  return None,cache

def main(org_uuid,con_uuid,cookie):

    claude = Claude(org_uuid, con_uuid,cookie)
    cache = []
    while True:
      query = input("human:")

      result = claude.send_message(query)
      if result:
        print("send success")
      else:
        continue

      try_time_current = 0

      #最大重试次数
      try_time_max = 15

      while True:

          history = claude.chat_conversation_history()
          # print(history)

          answer,cache = get_last_answer(history, cache)
          print("cache :" ,cache)
          if answer == None or answer == query:
            time.sleep(1)
            try_time_current += 1
            if try_time_current > try_time_max:
                answer = "消息获取失败"
                break
            continue

          else:
              break

      print("robot: ", answer)



if __name__ == '__main__':
    #填写自己的参数
    org_uuid= ###
    con_uuid= ###
    cookie = ###

    main(org_uuid,con_uuid,cookie)

最终结果如下

仅供测试,交流,学习。

把这个与微信结合了起来使得可以再微信上聊天claude对接微信(附源码)_Resphina的博客-CSDN博客

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值