mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-14 06:30:57 +00:00
style: introduce ruff as linter and formatter (#1356)
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
This commit is contained in:
committed by
GitHub
parent
09e70d70e9
commit
209f16af76
@@ -2,16 +2,12 @@ from __future__ import annotations
|
||||
|
||||
import typing
|
||||
import json
|
||||
import traceback
|
||||
import base64
|
||||
|
||||
import anthropic
|
||||
import httpx
|
||||
|
||||
from ....core import app
|
||||
from .. import entities, errors, requester
|
||||
from .. import errors, requester
|
||||
|
||||
from .. import entities, errors
|
||||
from ....core import entities as core_entities
|
||||
from ... import entities as llm_entities
|
||||
from ...tools import entities as tools_entities
|
||||
@@ -29,7 +25,6 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
}
|
||||
|
||||
async def initialize(self):
|
||||
|
||||
httpx_client = anthropic._base_client.AsyncHttpxClientWrapper(
|
||||
base_url=self.requester_cfg['base_url'],
|
||||
# cast to a valid type because mypy doesn't understand our type narrowing
|
||||
@@ -40,7 +35,7 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
)
|
||||
|
||||
self.client = anthropic.AsyncAnthropic(
|
||||
api_key="",
|
||||
api_key='',
|
||||
http_client=httpx_client,
|
||||
)
|
||||
|
||||
@@ -55,7 +50,7 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
self.client.api_key = model.token_mgr.get_token()
|
||||
|
||||
args = extra_args.copy()
|
||||
args["model"] = model.model_entity.name
|
||||
args['model'] = model.model_entity.name
|
||||
|
||||
# 处理消息
|
||||
|
||||
@@ -63,14 +58,15 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
system_role_message = None
|
||||
|
||||
for i, m in enumerate(messages):
|
||||
if m.role == "system":
|
||||
if m.role == 'system':
|
||||
system_role_message = m
|
||||
|
||||
messages.pop(i)
|
||||
break
|
||||
|
||||
if isinstance(system_role_message, llm_entities.Message) \
|
||||
and isinstance(system_role_message.content, str):
|
||||
if isinstance(system_role_message, llm_entities.Message) and isinstance(
|
||||
system_role_message.content, str
|
||||
):
|
||||
args['system'] = system_role_message.content
|
||||
|
||||
req_messages = []
|
||||
@@ -79,67 +75,64 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
if m.role == 'tool':
|
||||
tool_call_id = m.tool_call_id
|
||||
|
||||
req_messages.append({
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tool_call_id,
|
||||
"content": m.content
|
||||
}
|
||||
]
|
||||
})
|
||||
req_messages.append(
|
||||
{
|
||||
'role': 'user',
|
||||
'content': [
|
||||
{
|
||||
'type': 'tool_result',
|
||||
'tool_use_id': tool_call_id,
|
||||
'content': m.content,
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
continue
|
||||
|
||||
msg_dict = m.dict(exclude_none=True)
|
||||
|
||||
if isinstance(m.content, str) and m.content.strip() != "":
|
||||
msg_dict["content"] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": m.content
|
||||
}
|
||||
]
|
||||
if isinstance(m.content, str) and m.content.strip() != '':
|
||||
msg_dict['content'] = [{'type': 'text', 'text': m.content}]
|
||||
elif isinstance(m.content, list):
|
||||
|
||||
for i, ce in enumerate(m.content):
|
||||
|
||||
if ce.type == "image_base64":
|
||||
image_b64, image_format = await image.extract_b64_and_format(ce.image_base64)
|
||||
if ce.type == 'image_base64':
|
||||
image_b64, image_format = await image.extract_b64_and_format(
|
||||
ce.image_base64
|
||||
)
|
||||
|
||||
alter_image_ele = {
|
||||
"type": "image",
|
||||
"source": {
|
||||
"type": "base64",
|
||||
"media_type": f"image/{image_format}",
|
||||
"data": image_b64
|
||||
}
|
||||
'type': 'image',
|
||||
'source': {
|
||||
'type': 'base64',
|
||||
'media_type': f'image/{image_format}',
|
||||
'data': image_b64,
|
||||
},
|
||||
}
|
||||
msg_dict["content"][i] = alter_image_ele
|
||||
msg_dict['content'][i] = alter_image_ele
|
||||
|
||||
if m.tool_calls:
|
||||
|
||||
for tool_call in m.tool_calls:
|
||||
msg_dict["content"].append({
|
||||
"type": "tool_use",
|
||||
"id": tool_call.id,
|
||||
"name": tool_call.function.name,
|
||||
"input": json.loads(tool_call.function.arguments)
|
||||
})
|
||||
msg_dict['content'].append(
|
||||
{
|
||||
'type': 'tool_use',
|
||||
'id': tool_call.id,
|
||||
'name': tool_call.function.name,
|
||||
'input': json.loads(tool_call.function.arguments),
|
||||
}
|
||||
)
|
||||
|
||||
del msg_dict["tool_calls"]
|
||||
del msg_dict['tool_calls']
|
||||
|
||||
req_messages.append(msg_dict)
|
||||
|
||||
|
||||
args["messages"] = req_messages
|
||||
|
||||
args['messages'] = req_messages
|
||||
|
||||
if funcs:
|
||||
tools = await self.ap.tool_mgr.generate_tools_for_anthropic(funcs)
|
||||
|
||||
if tools:
|
||||
args["tools"] = tools
|
||||
args['tools'] = tools
|
||||
|
||||
try:
|
||||
# print(json.dumps(args, indent=4, ensure_ascii=False))
|
||||
@@ -149,23 +142,24 @@ class AnthropicMessages(requester.LLMAPIRequester):
|
||||
'content': '',
|
||||
'role': resp.role,
|
||||
}
|
||||
|
||||
|
||||
assert type(resp) is anthropic.types.message.Message
|
||||
|
||||
for block in resp.content:
|
||||
if block.type == 'thinking':
|
||||
args['content'] = '<think>' + block.thinking + '</think>\n' + args['content']
|
||||
args['content'] = (
|
||||
'<think>' + block.thinking + '</think>\n' + args['content']
|
||||
)
|
||||
elif block.type == 'text':
|
||||
args['content'] += block.text
|
||||
elif block.type == 'tool_use':
|
||||
assert type(block) is anthropic.types.tool_use_block.ToolUseBlock
|
||||
tool_call = llm_entities.ToolCall(
|
||||
id=block.id,
|
||||
type="function",
|
||||
type='function',
|
||||
function=llm_entities.FunctionCall(
|
||||
name=block.name,
|
||||
arguments=json.dumps(block.input)
|
||||
)
|
||||
name=block.name, arguments=json.dumps(block.input)
|
||||
),
|
||||
)
|
||||
if 'tool_calls' not in args:
|
||||
args['tool_calls'] = []
|
||||
|
||||
Reference in New Issue
Block a user