Skip to content

Commit a8dffea

Browse files
fix: fix support for image tool results - PTS-8831 (#66)
## Description <!--- Describe your changes in detail --> ## Related Issue For PTS-8831 <!--- Please link to the Github or Linear issue here --> ## Motivation and Context <!--- Why is this change required? What problem does it solve? --> ## How Has This Been Tested? <!--- Please describe how you tested your changes --> <!--- Add new tests to validate the functionality if possible --> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Small, localized change to tool-result serialization plus golden tests; low risk aside from potential downstream assumptions about tool-result `documents` JSON shape. > > **Overview** > Fixes templating of `Role::Tool` messages by handling `ContentType::Image` in tool results: image items are now serialized into the tool-result `documents` list as `{ "image_content": "<placeholder>" }` rather than failing validation. > > Bumps crate version to `0.5.7` and adds new cmd3/cmd4 golden tests covering mixed tool-result `document` + `image` content. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 0547cd8. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 2e90d15 commit a8dffea

File tree

6 files changed

+321
-1
lines changed

6 files changed

+321
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "melody-parsing"
3-
version = "0.5.6"
3+
version = "0.5.7"
44
edition = "2024"
55

66
[dependencies]

src/templating/util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ pub(crate) fn messages_to_template(
380380
.documents
381381
.push(escape_special_tokens(&rendered_obj, special_token_map));
382382
}
383+
} else if content_item.content_type == ContentType::Image {
384+
if let Some(ref obj) = content_item.image {
385+
let img_obj = json!({"image_content": obj.template_placeholder.clone()});
386+
let rendered_obj = add_spaces_to_json_encoding(&to_string(&img_obj)?);
387+
m.tool_results[tool_result_idx].documents.push(rendered_obj);
388+
}
383389
} else {
384390
return Err(MelodyError::TemplateValidation(format!(
385391
"tool message[{i}].content[{j}] invalid content type"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"safety_mode": "CONTEXTUAL",
3+
"citation_quality": "on",
4+
"messages": [
5+
{
6+
"role": "User",
7+
"content": [
8+
{
9+
"type": "text",
10+
"text": "Can you check if I have any reminders for the week?"
11+
},
12+
{
13+
"type": "image",
14+
"image": {"template_placeholder": "<|IMG_PATCH|>"}
15+
}
16+
]
17+
},
18+
{
19+
"role": "Chatbot",
20+
"content": [
21+
{
22+
"type": "text",
23+
"text": "I will use the GetReminders tool to check if the user has any reminders."
24+
}
25+
],
26+
"tool_calls": [
27+
{
28+
"id": "2",
29+
"name": "GetReminders",
30+
"parameters": "{}"
31+
}
32+
]
33+
},
34+
{
35+
"role": "Tool",
36+
"tool_call_id": "2",
37+
"content": [
38+
{
39+
"type": "document",
40+
"document": {"reminder_id": "59-80bc", "task": "Buy groceries", "due_date": "2023-09-11 23:59:59", "status": "pending"}
41+
},
42+
{
43+
"type": "document",
44+
"document": {"reminder_id": "14-579a", "task": "Call Amy", "due_date": "2023-09-11 23:59:59", "status": "pending"}
45+
},
46+
{
47+
"type": "document",
48+
"document": {"reminder_id": "5b-dd80", "task": "finish your report", "due_date": "2023-09-15 23:59:59", "status": "pending"}
49+
},
50+
{
51+
"type": "document",
52+
"document": {"reminder_id": "20-d9ca", "task": "call your mom", "due_date": "2023-09-17 23:59:59", "status": "pending"}
53+
},
54+
{
55+
"type": "image",
56+
"image": {"template_placeholder": "<|IMG_PATCH|>"}
57+
}
58+
]
59+
}
60+
],
61+
"documents": [
62+
{
63+
"content": "pdf chunk 1"
64+
},
65+
{
66+
"content": "pdf chunk 2"
67+
}
68+
],
69+
"available_tools": [
70+
{
71+
"name": "GetReminders",
72+
"description": "Get a list of reminders.",
73+
"parameters": {
74+
"type": "object",
75+
"properties": {},
76+
"required": []
77+
}
78+
},
79+
{
80+
"name": "DeleteReminder",
81+
"description": "Delete a reminder.",
82+
"parameters": {
83+
"type": "object",
84+
"properties": {
85+
"reminder_id": {
86+
"type": "string"
87+
}
88+
},
89+
"required": [
90+
"reminder_id"
91+
]
92+
}
93+
}
94+
]
95+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># System Preamble
2+
You are in contextual safety mode. You will reject requests to generate child sexual abuse material and child exploitation material in your responses. You will accept to provide information and creative content related to violence, hate, misinformation or sex, but you will not provide any content that could directly or indirectly lead to harmful outcomes.
3+
4+
Your information cutoff date is June 2024.
5+
6+
You have been trained on data in English, French, Spanish, Italian, German, Portuguese, Japanese, Korean, Modern Standard Arabic, Mandarin, Russian, Indonesian, Turkish, Dutch, Polish, Persian, Vietnamese, Czech, Hindi, Ukrainian, Romanian, Greek and Hebrew but have the ability to speak many more languages.
7+
8+
You have been trained to have advanced reasoning and tool-use capabilities and you should make best use of these skills to serve user's requests.
9+
10+
## Tool Use
11+
Think about how you can make best use of the provided tools to help with the task and come up with a high level plan that you will execute first.
12+
13+
0. Start by writing <|START_THINKING|> followed by a detailed step by step plan of how you will solve the problem. For each step explain your thinking fully and give details of required tool calls (if needed). Unless specified otherwise, you write your plan in natural language. When you finish, close it out with <|END_THINKING|>.
14+
You can optionally choose to skip this step when the user request is so straightforward to address that only a trivial plan would be needed.
15+
NOTE: You MUST skip this step when you are directly responding to the user's request without using any tools.
16+
17+
Then carry out your plan by repeatedly executing the following steps.
18+
1. Action: write <|START_ACTION|> followed by a list of JSON-formatted tool calls, with each one containing "tool_name" and "parameters" fields.
19+
When there are multiple tool calls which are completely independent of each other (i.e. they can be executed in parallel), you should list them out all together in one step. When you finish, close it out with <|END_ACTION|>.
20+
2. Observation: you will then receive results of those tool calls in JSON format in the very next turn, wrapped around by <|START_TOOL_RESULT|> and <|END_TOOL_RESULT|>. Carefully observe those results and think about what to do next. Note that these results will be provided to you in a separate turn. NEVER hallucinate results.
21+
Every tool call produces a list of results (when a tool call produces no result or a single result, it'll still get wrapped inside a list). Each result is clearly linked to its originating tool call via its "tool_call_id".
22+
3. Reflection: start the next turn by writing <|START_THINKING|> followed by what you've figured out so far, any changes you need to make to your plan, and what you will do next. When you finish, close it out with <|END_THINKING|>.
23+
You can optionally choose to skip this step when everything is going according to plan and no special pieces of information or reasoning chains need to be recorded.
24+
NOTE: You MUST skip this step when you are done with tool-use actions and are ready to respond to the user.
25+
26+
You can repeat the above 3 steps multiple times (could be 0 times too if no suitable tool calls are available or needed), until you decide it's time to finally respond to the user.
27+
28+
4. Response: then break out of the loop and write <|START_RESPONSE|> followed by a piece of text which serves as a response to the user's last request. Use all previous tool calls and results to help you when formulating your response. When you finish, close it out with <|END_RESPONSE|>.
29+
30+
## Grounding
31+
Importantly, note that "Reflection" and "Response" above can be grounded.
32+
Grounding means you associate pieces of texts (called "spans") with those specific tool results that support them (called "sources"). And you use a pair of tags "<co>" and "</co>" to indicate when a span can be grounded onto a list of sources, listing them out in the closing tag. Sources from the same tool call are grouped together and listed as "{tool_call_id}:[{list of result indices}]", before they are joined together by ",". E.g., "<co>span</co: 0:[1,2],1:[0]>" means that "span" is supported by result 1 and 2 from "tool_call_id=0" as well as result 0 from "tool_call_id=1".
33+
34+
## Available Tools
35+
Here is the list of tools that you have available to you.
36+
You can ONLY use the tools listed here. When a tool is not listed below, it is NOT available and you should NEVER attempt to use it.
37+
Each tool is represented as a JSON object with fields like "name", "description", "parameters" (per JSON Schema), and optionally, "responses" (per JSON Schema).
38+
39+
```json
40+
[
41+
{"name": "direct-injected-document", "description": "This is a special tool to directly inject user-uploaded documents into the chat as additional context. DO NOT use this tool by yourself!", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": {"200": {"description": "Successfully returned a list of chunked text snippets from the directly uploaded documents.", "content": {"application/json": {"schema": {"type": "array", "items": {"type": "object", "required": ["url", "snippet"], "properties": {"url": {"type": "string", "description": "The url of the uploaded document."}, "snippet": {"type": "string", "description": "The text snippet for the returned document chunk."}}}}}}}}},
42+
{"name": "GetReminders", "description": "Get a list of reminders.", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": null},
43+
{"name": "DeleteReminder", "description": "Delete a reminder.", "parameters": {"type": "object", "properties": {"reminder_id": {"type": "string"}}, "required": ["reminder_id"]}, "responses": null}
44+
]
45+
```
46+
47+
# Default Preamble
48+
The following instructions are your defaults unless specified elsewhere in developer preamble or user prompt.
49+
- Your name is Command.
50+
- You are a large language model built by Cohere.
51+
- You reply conversationally with a friendly and informative tone and often include introductory statements and follow-up questions.
52+
- If the input is ambiguous, ask clarifying follow-up questions.
53+
- Use Markdown-specific formatting in your response (for example to highlight phrases in bold or italics, create tables, or format code blocks).
54+
- Use LaTeX to generate mathematical notation for complex equations.
55+
- When responding in English, use American English unless context indicates otherwise.
56+
- When outputting responses of more than seven sentences, split the response into paragraphs.
57+
- Prefer the active voice.
58+
- Adhere to the APA style guidelines for punctuation, spelling, hyphenation, capitalization, numbers, lists, and quotation marks. Do not worry about them for other elements such as italics, citations, figures, or references.
59+
- Use gender-neutral pronouns for unspecified persons.
60+
- Limit lists to no more than 10 items unless the list is a set of finite instructions, in which case complete the list.
61+
- Use the third person when asked to write a summary.
62+
- When asked to extract values from source material, use the exact form, separated by commas.
63+
- When generating code output, please provide an explanation after the code.
64+
- When generating code output without specifying the programming language, please generate Python code.
65+
- If you are asked a question that requires reasoning, first think through your answer, slowly and step by step, then answer.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Can you check if I have any reminders for the week?<|IMG_PATCH|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>I will look through the document to address the users needs.<|END_THINKING|><|START_ACTION|>[
66+
{"tool_call_id": "0", "tool_name": "direct-injected-document", "parameters": {}}
67+
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
68+
{
69+
"tool_call_id": "0",
70+
"results": {
71+
"0": {"content": "pdf chunk 1"},
72+
"1": {"content": "pdf chunk 2"}
73+
},
74+
"is_error": null
75+
}
76+
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>I will use the GetReminders tool to check if the user has any reminders.<|END_THINKING|><|START_ACTION|>[
77+
{"tool_call_id": "1", "tool_name": "GetReminders", "parameters": {}}
78+
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
79+
{
80+
"tool_call_id": "1",
81+
"results": {
82+
"0": {"reminder_id": "59-80bc", "task": "Buy groceries", "due_date": "2023-09-11 23:59:59", "status": "pending"},
83+
"1": {"reminder_id": "14-579a", "task": "Call Amy", "due_date": "2023-09-11 23:59:59", "status": "pending"},
84+
"2": {"reminder_id": "5b-dd80", "task": "finish your report", "due_date": "2023-09-15 23:59:59", "status": "pending"},
85+
"3": {"reminder_id": "20-d9ca", "task": "call your mom", "due_date": "2023-09-17 23:59:59", "status": "pending"},
86+
"4": {"image_content": "<|IMG_PATCH|>"}
87+
},
88+
"is_error": null
89+
}
90+
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"grounding": "enabled",
3+
"messages": [
4+
{
5+
"role": "User",
6+
"content": [
7+
{
8+
"type": "text",
9+
"text": "Can you check if I have any reminders for the week?"
10+
},
11+
{
12+
"type": "image",
13+
"image": {"template_placeholder": "<|IMG_PATCH|>"}
14+
}
15+
]
16+
},
17+
{
18+
"role": "Chatbot",
19+
"content": [
20+
{
21+
"type": "text",
22+
"text": "I will use the GetReminders tool to check if the user has any reminders."
23+
}
24+
],
25+
"tool_calls": [
26+
{
27+
"id": "2",
28+
"name": "GetReminders",
29+
"parameters": "{}"
30+
}
31+
]
32+
},
33+
{
34+
"role": "Tool",
35+
"tool_call_id": "2",
36+
"content": [
37+
{
38+
"type": "document",
39+
"document": {"reminder_id": "59-80bc", "task": "Buy groceries", "due_date": "2023-09-11 23:59:59", "status": "pending"}
40+
},
41+
{
42+
"type": "document",
43+
"document": {"reminder_id": "14-579a", "task": "Call Amy", "due_date": "2023-09-11 23:59:59", "status": "pending"}
44+
},
45+
{
46+
"type": "document",
47+
"document": {"reminder_id": "5b-dd80", "task": "finish your report", "due_date": "2023-09-15 23:59:59", "status": "pending"}
48+
},
49+
{
50+
"type": "document",
51+
"document": {"reminder_id": "20-d9ca", "task": "call your mom", "due_date": "2023-09-17 23:59:59", "status": "pending"}
52+
},
53+
{
54+
"type": "image",
55+
"image": {"template_placeholder": "<|IMG_PATCH|>"}
56+
}
57+
]
58+
}
59+
],
60+
"documents": [
61+
{
62+
"content": "pdf chunk 1"
63+
},
64+
{
65+
"content": "pdf chunk 2"
66+
}
67+
],
68+
"available_tools": [
69+
{
70+
"name": "GetReminders",
71+
"description": "Get a list of reminders.",
72+
"parameters": {
73+
"type": "object",
74+
"properties": {},
75+
"required": []
76+
}
77+
},
78+
{
79+
"name": "DeleteReminder",
80+
"description": "Delete a reminder.",
81+
"parameters": {
82+
"type": "object",
83+
"properties": {
84+
"reminder_id": {
85+
"type": "string"
86+
}
87+
},
88+
"required": [
89+
"reminder_id"
90+
]
91+
}
92+
}
93+
]
94+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>Note that both your responses and reflections can be grounded. Grounding means you associate pieces of texts (called "spans") with those specific tool results that support them (called "sources"). And you use a pair of tags "<co>" and "</co>" to indicate when a span can be grounded onto a list of sources, listing them out in the closing tag. Sources from the same tool call are grouped together and listed as "{tool_call_id}:[{list of result indices}]", before they are joined together by ",". E.g., "<co>span</co: 0:[1,2],1:[0]>" means that "span" is supported by result 1 and 2 from "tool_call_id=0" as well as result 0 from "tool_call_id=1".
2+
3+
# Available Tools
4+
```json
5+
[
6+
{"name": "direct-injected-document", "description": "This is a special tool to directly inject user-uploaded documents into the chat as additional context. DO NOT use this tool by yourself!", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": {"200": {"description": "Successfully returned a list of chunked text snippets from the directly uploaded documents.", "content": {"application/json": {"schema": {"type": "array", "items": {"type": "object", "required": ["url", "snippet"], "properties": {"url": {"type": "string", "description": "The url of the uploaded document."}, "snippet": {"type": "string", "description": "The text snippet for the returned document chunk."}}}}}}}}},
7+
{"name": "GetReminders", "description": "Get a list of reminders.", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": null},
8+
{"name": "DeleteReminder", "description": "Delete a reminder.", "parameters": {"type": "object", "properties": {"reminder_id": {"type": "string"}}, "required": ["reminder_id"]}, "responses": null}
9+
]
10+
```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Can you check if I have any reminders for the week?<|END_TEXT|><|IMG_PATCH|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>I will look through the document to address the users needs.<|END_THINKING|><|START_ACTION|>[
11+
{"tool_call_id": "0", "tool_name": "direct-injected-document", "parameters": {}}
12+
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
13+
{
14+
"tool_call_id": "0",
15+
"results": {
16+
"0": {"content": "pdf chunk 1"},
17+
"1": {"content": "pdf chunk 2"}
18+
},
19+
"is_error": null
20+
}
21+
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>I will use the GetReminders tool to check if the user has any reminders.<|END_THINKING|><|START_ACTION|>[
22+
{"tool_call_id": "1", "tool_name": "GetReminders", "parameters": {}}
23+
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
24+
{
25+
"tool_call_id": "1",
26+
"results": {
27+
"0": {"reminder_id": "59-80bc", "task": "Buy groceries", "due_date": "2023-09-11 23:59:59", "status": "pending"},
28+
"1": {"reminder_id": "14-579a", "task": "Call Amy", "due_date": "2023-09-11 23:59:59", "status": "pending"},
29+
"2": {"reminder_id": "5b-dd80", "task": "finish your report", "due_date": "2023-09-15 23:59:59", "status": "pending"},
30+
"3": {"reminder_id": "20-d9ca", "task": "call your mom", "due_date": "2023-09-17 23:59:59", "status": "pending"},
31+
"4": {"image_content": "<|IMG_PATCH|>"}
32+
},
33+
"is_error": null
34+
}
35+
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>

0 commit comments

Comments
 (0)