enhance: claude stream tool call (#4469)
This commit is contained in:
@@ -6,6 +6,7 @@ features:
|
|||||||
- agent-thought
|
- agent-thought
|
||||||
- vision
|
- vision
|
||||||
- tool-call
|
- tool-call
|
||||||
|
- stream-tool-call
|
||||||
model_properties:
|
model_properties:
|
||||||
mode: chat
|
mode: chat
|
||||||
context_size: 200000
|
context_size: 200000
|
||||||
|
@@ -6,6 +6,7 @@ features:
|
|||||||
- agent-thought
|
- agent-thought
|
||||||
- vision
|
- vision
|
||||||
- tool-call
|
- tool-call
|
||||||
|
- stream-tool-call
|
||||||
model_properties:
|
model_properties:
|
||||||
mode: chat
|
mode: chat
|
||||||
context_size: 200000
|
context_size: 200000
|
||||||
|
@@ -6,6 +6,7 @@ features:
|
|||||||
- agent-thought
|
- agent-thought
|
||||||
- vision
|
- vision
|
||||||
- tool-call
|
- tool-call
|
||||||
|
- stream-tool-call
|
||||||
model_properties:
|
model_properties:
|
||||||
mode: chat
|
mode: chat
|
||||||
context_size: 200000
|
context_size: 200000
|
||||||
|
@@ -324,10 +324,32 @@ class AnthropicLargeLanguageModel(LargeLanguageModel):
|
|||||||
output_tokens = 0
|
output_tokens = 0
|
||||||
finish_reason = None
|
finish_reason = None
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
|
tool_calls: list[AssistantPromptMessage.ToolCall] = []
|
||||||
|
|
||||||
for chunk in response:
|
for chunk in response:
|
||||||
if isinstance(chunk, MessageStartEvent):
|
if isinstance(chunk, MessageStartEvent):
|
||||||
return_model = chunk.message.model
|
if hasattr(chunk, 'content_block'):
|
||||||
input_tokens = chunk.message.usage.input_tokens
|
content_block = chunk.content_block
|
||||||
|
if isinstance(content_block, dict):
|
||||||
|
if content_block.get('type') == 'tool_use':
|
||||||
|
tool_call = AssistantPromptMessage.ToolCall(
|
||||||
|
id=content_block.get('id'),
|
||||||
|
type='function',
|
||||||
|
function=AssistantPromptMessage.ToolCall.ToolCallFunction(
|
||||||
|
name=content_block.get('name'),
|
||||||
|
arguments=''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
tool_calls.append(tool_call)
|
||||||
|
elif hasattr(chunk, 'delta'):
|
||||||
|
delta = chunk.delta
|
||||||
|
if isinstance(delta, dict) and len(tool_calls) > 0:
|
||||||
|
if delta.get('type') == 'input_json_delta':
|
||||||
|
tool_calls[-1].function.arguments += delta.get('partial_json', '')
|
||||||
|
elif chunk.message:
|
||||||
|
return_model = chunk.message.model
|
||||||
|
input_tokens = chunk.message.usage.input_tokens
|
||||||
elif isinstance(chunk, MessageDeltaEvent):
|
elif isinstance(chunk, MessageDeltaEvent):
|
||||||
output_tokens = chunk.usage.output_tokens
|
output_tokens = chunk.usage.output_tokens
|
||||||
finish_reason = chunk.delta.stop_reason
|
finish_reason = chunk.delta.stop_reason
|
||||||
@@ -335,13 +357,19 @@ class AnthropicLargeLanguageModel(LargeLanguageModel):
|
|||||||
# transform usage
|
# transform usage
|
||||||
usage = self._calc_response_usage(model, credentials, input_tokens, output_tokens)
|
usage = self._calc_response_usage(model, credentials, input_tokens, output_tokens)
|
||||||
|
|
||||||
|
# transform empty tool call arguments to {}
|
||||||
|
for tool_call in tool_calls:
|
||||||
|
if not tool_call.function.arguments:
|
||||||
|
tool_call.function.arguments = '{}'
|
||||||
|
|
||||||
yield LLMResultChunk(
|
yield LLMResultChunk(
|
||||||
model=return_model,
|
model=return_model,
|
||||||
prompt_messages=prompt_messages,
|
prompt_messages=prompt_messages,
|
||||||
delta=LLMResultChunkDelta(
|
delta=LLMResultChunkDelta(
|
||||||
index=index + 1,
|
index=index + 1,
|
||||||
message=AssistantPromptMessage(
|
message=AssistantPromptMessage(
|
||||||
content=''
|
content='',
|
||||||
|
tool_calls=tool_calls
|
||||||
),
|
),
|
||||||
finish_reason=finish_reason,
|
finish_reason=finish_reason,
|
||||||
usage=usage
|
usage=usage
|
||||||
|
Reference in New Issue
Block a user