update anthropic

This commit is contained in:
better629 2025-02-27 15:55:38 +08:00
parent 8c84de7468
commit 78a9cd3a4c
4 changed files with 25 additions and 10 deletions

View file

@ -33,6 +33,8 @@ class AnthropicLLM(BaseLLM):
if messages[0]["role"] == "system":
kwargs["messages"] = messages[1:]
kwargs["system"] = messages[0]["content"] # set system prompt here
if self.config.reasoning:
kwargs["thinking"] = {"type": "enabled", "budget_tokens": self.config.reasoning_tokens}
return kwargs
def _update_costs(self, usage: Usage, model: str = None, local_calc_usage: bool = True):
@ -40,7 +42,12 @@ class AnthropicLLM(BaseLLM):
super()._update_costs(usage, model)
def get_choice_text(self, resp: Message) -> str:
return resp.content[0].text
if len(resp.content) > 0:
self.reasoning_content = resp.content[0].thinking
text = resp.content[1].text
else:
text = resp.content[0].text
return text
async def _achat_completion(self, messages: list[dict], timeout: int = USE_CONFIG_TIMEOUT) -> Message:
resp: Message = await self.aclient.messages.create(**self._const_kwargs(messages))
@ -53,6 +60,7 @@ class AnthropicLLM(BaseLLM):
async def _achat_completion_stream(self, messages: list[dict], timeout: int = USE_CONFIG_TIMEOUT) -> str:
stream = await self.aclient.messages.create(**self._const_kwargs(messages, stream=True))
collected_content = []
collected_reasoning_content = []
usage = Usage(input_tokens=0, output_tokens=0)
async for event in stream:
event_type = event.type
@ -60,13 +68,19 @@ class AnthropicLLM(BaseLLM):
usage.input_tokens = event.message.usage.input_tokens
usage.output_tokens = event.message.usage.output_tokens
elif event_type == "content_block_delta":
content = event.delta.text
log_llm_stream(content)
collected_content.append(content)
delta_type = event.delta.type
if delta_type == "thinking_delta":
collected_reasoning_content.append(event.delta.thinking)
elif delta_type == "text_delta":
content = event.delta.text
log_llm_stream(content)
collected_content.append(content)
elif event_type == "message_delta":
usage.output_tokens = event.usage.output_tokens # update final output_tokens
log_llm_stream("\n")
self._update_costs(usage)
full_content = "".join(collected_content)
if collected_reasoning_content:
self.reasoning_content = "".join(collected_reasoning_content)
return full_content

View file

@ -57,12 +57,13 @@ class AnthropicProvider(BaseBedrockProvider):
rsp_dict = json.loads(event["chunk"]["bytes"])
if rsp_dict["type"] == "content_block_delta":
reasoning = False
if rsp_dict["delta"]["type"] == "text_delta":
delta_type = rsp_dict["delta"]["type"]
if delta_type == "text_delta":
completions = rsp_dict["delta"]["text"]
elif rsp_dict["delta"]["type"] == "thinking_delta":
elif delta_type == "thinking_delta":
completions = rsp_dict["delta"]["thinking"]
reasoning = True
elif rsp_dict["delta"]["type"] == "signature_delta":
elif delta_type == "signature_delta":
completions = ""
return reasoning, completions
else:

View file

@ -146,7 +146,7 @@ class BedrockLLM(BaseLLM):
collected_reasoning_content.append(chunk_text)
else:
collected_content.append(chunk_text)
log_llm_stream(chunk_text)
log_llm_stream(chunk_text)
if collected_reasoning_content:
self.reasoning_content = "".join(collected_reasoning_content)
return collected_content

View file

@ -31,7 +31,7 @@ tqdm==4.66.2
#unstructured[local-inference]
# selenium>4
# webdriver_manager<3.9
anthropic==0.18.1
anthropic==0.47.2
typing-inspect==0.8.0
libcst==1.0.1
qdrant-client==1.7.0
@ -84,4 +84,4 @@ spark_ai_python~=0.3.30
agentops
tree_sitter~=0.23.2
tree_sitter_python~=0.23.2
httpx==0.27.2
httpx==0.28.1