mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 06:07:13 +08:00
Update claude_ai_bot.py
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
|||||||
import uuid
|
import uuid
|
||||||
from curl_cffi import requests
|
from curl_cffi import requests
|
||||||
from bot.bot import Bot
|
from bot.bot import Bot
|
||||||
from bot.chatgpt.chat_gpt_session import ChatGPTSession
|
from bot.claude.claude_ai_session import ClaudeAiSession
|
||||||
from bot.openai.open_ai_image import OpenAIImage
|
from bot.openai.open_ai_image import OpenAIImage
|
||||||
from bot.session_manager import SessionManager
|
from bot.session_manager import SessionManager
|
||||||
from bridge.context import Context, ContextType
|
from bridge.context import Context, ContextType
|
||||||
@@ -16,9 +16,10 @@ from config import conf
|
|||||||
class ClaudeAIBot(Bot, OpenAIImage):
|
class ClaudeAIBot(Bot, OpenAIImage):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")
|
self.sessions = SessionManager(ClaudeAiSession, model=conf().get("model") or "gpt-3.5-turbo")
|
||||||
self.claude_api_cookie = conf().get("claude_api_cookie")
|
self.claude_api_cookie = conf().get("claude_api_cookie")
|
||||||
self.proxy = conf().get("proxy")
|
self.proxy = conf().get("proxy")
|
||||||
|
self.con_uuid_dic = {}
|
||||||
if self.proxy:
|
if self.proxy:
|
||||||
self.proxies = {
|
self.proxies = {
|
||||||
"http": self.proxy,
|
"http": self.proxy,
|
||||||
@@ -27,8 +28,6 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
else:
|
else:
|
||||||
self.proxies = None
|
self.proxies = None
|
||||||
self.org_uuid = self.get_organization_id()
|
self.org_uuid = self.get_organization_id()
|
||||||
self.con_uuid = None
|
|
||||||
self.get_uuid()
|
|
||||||
|
|
||||||
def generate_uuid(self):
|
def generate_uuid(self):
|
||||||
random_uuid = uuid.uuid4()
|
random_uuid = uuid.uuid4()
|
||||||
@@ -40,8 +39,9 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
if conf().get("claude_uuid") != None:
|
if conf().get("claude_uuid") != None:
|
||||||
self.con_uuid = conf().get("claude_uuid")
|
self.con_uuid = conf().get("claude_uuid")
|
||||||
else:
|
else:
|
||||||
self.con_uuid = self.generate_uuid()
|
con_uuid = self.generate_uuid()
|
||||||
self.create_new_chat()
|
self.create_new_chat(con_uuid)
|
||||||
|
|
||||||
|
|
||||||
def get_organization_id(self):
|
def get_organization_id(self):
|
||||||
url = "https://claude.ai/api/organizations"
|
url = "https://claude.ai/api/organizations"
|
||||||
@@ -99,10 +99,16 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
print(response.text)
|
print(response.text)
|
||||||
|
|
||||||
return uuid
|
return uuid
|
||||||
|
|
||||||
def create_new_chat(self):
|
def conversation_share_check(self,session_id):
|
||||||
|
if session_id not in self.con_uuid_dic:
|
||||||
|
self.con_uuid_dic[session_id] = self.generate_uuid()
|
||||||
|
self.create_new_chat(self.con_uuid_dic[session_id])
|
||||||
|
return self.con_uuid_dic[session_id]
|
||||||
|
|
||||||
|
def create_new_chat(self, con_uuid):
|
||||||
url = f"https://claude.ai/api/organizations/{self.org_uuid}/chat_conversations"
|
url = f"https://claude.ai/api/organizations/{self.org_uuid}/chat_conversations"
|
||||||
payload = json.dumps({"uuid": self.con_uuid, "name": ""})
|
payload = json.dumps({"uuid": con_uuid, "name": ""})
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent':
|
'User-Agent':
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
|
||||||
@@ -118,7 +124,7 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
'Sec-Fetch-Site': 'same-origin',
|
'Sec-Fetch-Site': 'same-origin',
|
||||||
'TE': 'trailers'
|
'TE': 'trailers'
|
||||||
}
|
}
|
||||||
response = requests.post( url, headers=headers, data=payload,impersonate="chrome110", proxies= self.proxies)
|
response = requests.post(url, headers=headers, data=payload,impersonate="chrome110", proxies= self.proxies)
|
||||||
# Returns JSON of the newly created conversation information
|
# Returns JSON of the newly created conversation information
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
@@ -138,6 +144,8 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
try:
|
try:
|
||||||
session_id = context["session_id"]
|
session_id = context["session_id"]
|
||||||
session = self.sessions.session_query(query, session_id)
|
session = self.sessions.session_query(query, session_id)
|
||||||
|
con_uuid = self.conversation_share_check(session_id)
|
||||||
|
|
||||||
model = conf().get("model") or "gpt-3.5-turbo"
|
model = conf().get("model") or "gpt-3.5-turbo"
|
||||||
# remove system message
|
# remove system message
|
||||||
if session.messages[0].get("role") == "system":
|
if session.messages[0].get("role") == "system":
|
||||||
@@ -154,7 +162,7 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
"model": "claude-2"
|
"model": "claude-2"
|
||||||
},
|
},
|
||||||
"organization_uuid": f"{self.org_uuid}",
|
"organization_uuid": f"{self.org_uuid}",
|
||||||
"conversation_uuid": f"{self.con_uuid}",
|
"conversation_uuid": f"{con_uuid}",
|
||||||
"text": f"{query}",
|
"text": f"{query}",
|
||||||
"attachments": []
|
"attachments": []
|
||||||
})
|
})
|
||||||
@@ -190,13 +198,14 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
|
|
||||||
reply_content = ''.join(completions)
|
reply_content = ''.join(completions)
|
||||||
logger.info(f"[CLAUDE] reply={reply_content}, total_tokens=invisible")
|
logger.info(f"[CLAUDE] reply={reply_content}, total_tokens=invisible")
|
||||||
|
|
||||||
self.sessions.session_reply(reply_content, session_id, 100)
|
self.sessions.session_reply(reply_content, session_id, 100)
|
||||||
return Reply(ReplyType.TEXT, reply_content)
|
return Reply(ReplyType.TEXT, reply_content)
|
||||||
else:
|
else:
|
||||||
response = res.json()
|
response = res.json()
|
||||||
error = response.get("error")
|
error = response.get("error")
|
||||||
logger.error(f"[CLAUDE] chat failed, status_code={res.status_code}, "
|
logger.error(f"[CLAUDE] chat failed, status_code={res.status_code}, "
|
||||||
f"msg={error.get('message')}, type={error.get('type')}, detail: {res.text}, uuid: {self.con_uuid}")
|
f"msg={error.get('message')}, type={error.get('type')}, detail: {res.text}, uuid: {con_uuid}")
|
||||||
|
|
||||||
if res.status_code >= 500:
|
if res.status_code >= 500:
|
||||||
# server error, need retry
|
# server error, need retry
|
||||||
@@ -210,4 +219,4 @@ class ClaudeAIBot(Bot, OpenAIImage):
|
|||||||
# retry
|
# retry
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
logger.warn(f"[CLAUDE] do retry, times={retry_count}")
|
logger.warn(f"[CLAUDE] do retry, times={retry_count}")
|
||||||
return self._chat(query, context, retry_count + 1)
|
return self._chat(query, context, retry_count + 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user