update: 修改anthropic请求体
This commit is contained in:
@@ -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()
|
||||
# 生成当前时间
|
||||
@@ -72,12 +73,12 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes
|
||||
"content": plugin.config['system_prompt'].format(name=plugin.get_bot_name(), timestamp=timestamp)}
|
||||
if plugin.config['enable_multi_user']:
|
||||
system_prompt["content"] += """
|
||||
User messages are in the context of multiperson chatrooms.
|
||||
Each message indicates its sender by prefixing the message with the sender's name followed by a colon, for example:
|
||||
"username: hello world."
|
||||
In this case, the user called "username" sent the message "hello world.". You should not follow this convention in your responses.
|
||||
your response instead could be "hello username!" without including any colons, because you are the only one sending your responses there is no need to prefix them.
|
||||
"""
|
||||
User messages are in the context of multiperson chatrooms.
|
||||
Each message indicates its sender by prefixing the message with the sender's name followed by a colon, for example:
|
||||
"username: hello world."
|
||||
In this case, the user called "username" sent the message "hello world.". You should not follow this convention in your responses.
|
||||
your response instead could be "hello username!" without including any colons, because you are the only one sending your responses there is no need to prefix them.
|
||||
"""
|
||||
if len(system_prompt["content"]) > 0:
|
||||
system_context.append(system_prompt)
|
||||
|
||||
@@ -89,9 +90,14 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes
|
||||
# 如果 消息长度已经超过了配置的消息条数,那么就抛出错误
|
||||
if len(additional_context) > platform.max_context_messages - 1:
|
||||
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!")
|
||||
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']:
|
||||
|
||||
@@ -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()}")
|
||||
|
||||
Reference in New Issue
Block a user