Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix input message attribute issues + toolcalling from dogfooding #948

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
Message,
MessageParam,
TextBlock,
TextBlockParam,
ToolResultBlockParam,
ToolUseBlock,
ToolUseBlockParam,
)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
Expand All @@ -30,11 +28,9 @@ def _to_assistant_message_param(
content = []
for block in message.content:
if isinstance(block, TextBlock):
content.append(TextBlockParam(text=block.text, type=block.type))
content.append(block)
elif isinstance(block, ToolUseBlock):
content.append(
ToolUseBlockParam(id=block.id, input=block.input, name=block.name, type=block.type)
)
content.append(block)
else:
assert_never(block)
return MessageParam(content=content, role="assistant")
Expand Down Expand Up @@ -76,6 +72,7 @@ def _get_tool_use_id(message: Message) -> Optional[str]:
messages=messages,
)
messages.append(_to_assistant_message_param(response))

assert (tool_use_id := _get_tool_use_id(response)) is not None, "tool was not called"
messages.append(
MessageParam(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,44 @@ def _get_input_messages(messages: List[Dict[str, str]]) -> Any:
Extracts the messages from the chat response
"""
for i in range(len(messages)):
tool_index = 0
if content := messages[i].get("content"):
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", content
if isinstance(content, str):
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", content
elif isinstance(content, list):
for block in content:
if isinstance(block, ToolUseBlock):
yield (
f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_NAME}",
block.name,
)
yield (
f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}",
safe_json_dumps(block.input),
)
tool_index += 1
elif isinstance(block, TextBlock):
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", block.text
elif isinstance(block, dict):
block_type = block.get("type")
if block_type == "tool_use":
yield (
f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_NAME}",
block.get("name"),
)
yield (
f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}",
safe_json_dumps(block.get("input")),
)
tool_index += 1
if block_type == "tool_result":
yield (
f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}",
block.get("content"),
)
if block_type == "text":
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", block.get("text")

if role := messages[i].get("role"):
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_ROLE}", role

Expand All @@ -283,20 +319,21 @@ def _get_output_messages(response: Any) -> Any:
"""
Extracts the tool call information from the response
"""
for i in range(len(response.content)):
block = response.content[i]
yield f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_ROLE}", response.role
tool_index = 0
for block in response.content:
yield f"{LLM_OUTPUT_MESSAGES}.{0}.{MESSAGE_ROLE}", response.role
if isinstance(block, ToolUseBlock):
yield (
f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_NAME}",
f"{LLM_OUTPUT_MESSAGES}.{0}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_NAME}",
block.name,
)
yield (
f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}",
f"{LLM_OUTPUT_MESSAGES}.{0}.{MESSAGE_TOOL_CALLS}.{tool_index}.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}",
safe_json_dumps(block.input),
)
tool_index += 1
if isinstance(block, TextBlock):
yield f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", block.text
yield f"{LLM_OUTPUT_MESSAGES}.{0}.{MESSAGE_CONTENT}", block.text


def _validate_invocation_parameter(parameter: Any) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
interactions:
- request:
body: '{"max_tokens": 1024, "messages": [{"role": "user", "content": "What is
the weather like in San Francisco in Fahrenheit?"}, {"content": [{"text": "Certainly!
I can help you get the current weather information for San Francisco in Fahrenheit.
To do this, I''ll use the get_weather function. Let me fetch that information
for you right away.", "type": "text"}, {"id": "toolu_01KBqpqR73qWGsMaW3vBzEjz",
"input": {"location": "San Francisco, CA", "unit": "fahrenheit"}, "name": "get_weather",
"type": "tool_use"}], "role": "assistant"}, {"content": [{"tool_use_id": "toolu_01KBqpqR73qWGsMaW3vBzEjz",
"content": "{\"weather\": \"sunny\", \"temperature\": \"75\"}", "type": "tool_result",
"is_error": false}], "role": "user"}], "model": "claude-3-5-sonnet-20240620",
"tools": [{"name": "get_weather", "description": "Get the current weather in
a given location", "input_schema": {"type": "object", "properties": {"location":
{"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description":
"The unit of temperature, either \"celsius\" or \"fahrenheit\""}}, "required":
["location"]}}]}'
headers: {}
method: POST
uri: https://api.anthropic.com/v1/messages
response:
body:
string: "{\"id\":\"msg_01Wyq8BmnzSNNQ2nEcfHdxwh\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-3-5-sonnet-20240620\",\"content\":[{\"type\":\"text\",\"text\":\"Great!
I've received the current weather information for San Francisco. Here's what
I can tell you:\\n\\nThe weather in San Francisco is currently sunny, and
the temperature is 75\xB0F (Fahrenheit).\\n\\nIt's a pleasant day in San Francisco
with warm temperatures and clear skies. This kind of weather is great for
outdoor activities or sightseeing if you're in the area. Remember that San
Francisco's weather can change throughout the day, so it's always a good idea
to be prepared for potential shifts in temperature or conditions.\\n\\nIs
there anything else you'd like to know about the weather in San Francisco
or any other location?\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":568,\"output_tokens\":137}}"
headers: {}
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interactions:
- request:
body: '{"max_tokens": 1024, "messages": [{"role": "user", "content": "What is
the weather like in San Francisco in Fahrenheit?"}, {"content": [{"text": "Certainly!
I can help you get the current weather information for San Francisco in Fahrenheit.
To do this, I''ll use the get_weather function. Let me fetch that information
for you right away.", "type": "text"}, {"id": "toolu_01KBqpqR73qWGsMaW3vBzEjz",
"input": {"location": "San Francisco, CA", "unit": "fahrenheit"}, "name": "get_weather",
"type": "tool_use"}], "role": "assistant"}, {"content": [{"tool_use_id": "toolu_01KBqpqR73qWGsMaW3vBzEjz",
"content": "{\"weather\": \"sunny\", \"temperature\": \"75\"}", "type": "tool_result",
"is_error": false}], "role": "user"}], "model": "claude-3-5-sonnet-20240620",
"tools": [{"name": "get_weather", "description": "Get the current weather in
a given location", "input_schema": {"type": "object", "properties": {"location":
{"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description":
"The unit of temperature, either \"celsius\" or \"fahrenheit\""}}, "required":
["location"]}}]}'
headers: {}
method: POST
uri: https://api.anthropic.com/v1/messages
response:
body:
string: "{\"id\":\"msg_01D1W4HqvciREYXMXJjKNa28\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-3-5-sonnet-20240620\",\"content\":[{\"type\":\"text\",\"text\":\"Based
on the information I've received, here's the current weather in San Francisco:\\n\\nThe
weather in San Francisco is currently sunny with a temperature of 75\xB0F
(Fahrenheit).\\n\\nThis is quite pleasant weather for San Francisco! It's
a warm, sunny day, which is great for outdoor activities or sightseeing if
you're in the area. Remember that San Francisco's weather can change quickly,
so it's always a good idea to bring layers, even on sunny days.\\n\\nIs there
anything else you'd like to know about the weather in San Francisco or any
other location?\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":568,\"output_tokens\":126}}"
headers: {}
status:
code: 200
message: OK
version: 1
Loading
Loading