update: 修改anthropic请求体

This commit is contained in:
taylor
2024-10-14 17:34:01 +08:00
parent c03be10fc1
commit 2fd5394773
2 changed files with 33 additions and 18 deletions

View File

@@ -60,9 +60,10 @@ class Platform:
def get_type(self) -> str:
raise NotImplementedError()
async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque:
"""
获取系统提示上下文
"""
async def get_system_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque:
# 创建系统提示词上下文
system_context = deque()
# 生成当前时间
@@ -91,7 +92,12 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes
raise ValueError(f"sorry, my configuration has too many additional prompts "
f"({platform.max_context_messages}) and i'll never see your message. "
f"Update my config to have fewer messages and i'll be able to answer your questions!")
return system_context
"""
获取聊天信息上下文
"""
async def get_chat_context(system_context: deque, plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque:
# 用户历史聊天上下文
chat_context = deque()
# 计算系统提示词单词数
@@ -121,11 +127,16 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes
break
chat_context.appendleft({"role": role, "content": user + message})
return chat_context
"""
获取总消息上下文
"""
async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque:
system_context = get_system_context(plugin, platform, evt)
chat_context = get_chat_context(system_context, plugin, platform, evt)
return system_context + chat_context
async def generate_context_messages(plugin: Plugin, platform: Platform, evt: MessageEvent) -> Generator[MessageEvent, None, None]:
yield evt
if plugin.config['reply_in_thread']:

View File

@@ -82,13 +82,17 @@ class Anthropic(Platform):
self.max_tokens = self.config['max_tokens']
async def create_chat_completion(self, plugin: AbsExtraConfigPlugin, evt: MessageEvent) -> ChatCompletion:
full_context = []
context = await maubot_llmplus.platforms.get_context(plugin, self, evt)
full_context.extend(list(context))
full_chat_context = []
chat_context = await maubot_llmplus.platforms.get_chat_context(plugin, self, evt)
full_chat_context.extend(list(chat_context))
full_system_context = []
system_context = await maubot_llmplus.platforms.get_system_context(plugin, self, evt)
full_system_context.extend(list(system_context))
endpoint = f"{self.url}/v1/messages"
headers = {"x-api-key": self.api_key, "anthropic-version": "2023-06-01", "content-type": "application/json"}
req_body = {"model": self.model, "max_tokens": self.max_tokens, "messages": full_context}
req_body = {"model": self.model, "max_tokens": self.max_tokens, "system": full_system_context, "messages": full_chat_context}
async with self.http.post(endpoint, headers=headers, data=json.dumps(req_body)) as response:
# plugin.log.debug(f"响应内容:{response.status}, {await response.json()}")