Skip to content

Commit 821177c

Browse files
authored
feat: add a get_tool_calls function in stdlib (#1484)
Signed-off-by: Louis Mandel <lmandel@us.ibm.com>
1 parent 553c271 commit 821177c

20 files changed

+196
-483
lines changed

examples/demo/8-tools.pdl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ defs:
1010
description: Arithmetic expression to calculate
1111
text:
1212
- role: system
13-
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
13+
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with a JSON of the tool to use. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
1414
contribute: [context]
1515
- role: tools
1616
content:
1717
text: ${ tools }
1818
contribute: [context]
1919
- "Out of 1400 participants, 400 passed the test. What percentage is that?\n"
20-
- def: actions
20+
- def: action
2121
model: ollama_chat/granite4:micro
2222
parser: json
23-
spec: [{ name: string, arguments: { expr: string }}]
23+
spec: { name: string, arguments: { expr: string }}
2424
parameters:
2525
drop_params: true
2626
- "\n"
27-
- if: ${ actions[0].name == "calc" }
27+
- if: ${ action.name == "calc" }
2828
then:
2929
text:
3030
- lang: python
31-
code: result = ${ actions[0].arguments.expr }
31+
code: result = ${ action.arguments.expr }

examples/demo/9-react.pdl

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
description: Demonstrating a React program
22
defs:
3-
Calc:
3+
problem: |
4+
How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.
5+
calc:
46
description: Calculator function
57
function:
6-
expr:
7-
type: string
8-
description: Arithmetic expression to calculate
8+
expr:
9+
type: string
10+
description: Arithmetic expression to calculate
911
return:
1012
lang: python
1113
code: result = ${ expr }
12-
Search:
14+
search:
1315
description: Wikipedia search
1416
function:
1517
topic:
@@ -24,72 +26,104 @@ defs:
2426
result = wikipedia.summary("${ topic }")
2527
except wikipedia.WikipediaException as e:
2628
result = str(e)
29+
finish:
30+
description: Respond with the answer
31+
function:
32+
answer:
33+
type: string
34+
description: The answer
35+
return: ${ answer }
2736
tools:
2837
data:
29-
- ${ Calc.signature }
30-
- ${ Search.signature }
38+
- ${ calc.signature }
39+
- ${ search.signature }
40+
- ${ finish.signature }
3141
text:
32-
- role: system
33-
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
34-
contribute: [context]
3542
- role: tools
3643
content:
3744
text: ${ tools }
3845
contribute: [context]
39-
- text:
40-
|
46+
- role: system
47+
content:
48+
text:
49+
- |
50+
You are a helpful assistant that uses the following tools:
51+
- for:
52+
tool: ${ tools }
53+
index: i
54+
repeat: |
55+
56+
${ i+1 }) ${ tool.function.name } - ${ tool.function.description }
57+
The parameters respect the following JSON Schema: ${tool.function.parameters}
58+
- |
59+
60+
Follow this exact ReAct format:
61+
62+
Thought: <describe what you plan to do next>
63+
Action: { "name": <tool_name>, "arguments": <tool_arguments> }
64+
65+
# After I return tool results, continue:
66+
67+
Observation: <what you observed>
68+
69+
# Repeat Thought/Action/Observation as needed.
70+
# When you are ready to answer the user, finish with the action:
71+
72+
Action: { "name": "finish", "arguments": { "answer": <your best possible answer, concise and clear> } }
73+
74+
STRICT FORMAT RULES:
75+
- One action per step.
76+
- Do NOT invent tools.
77+
- Use a tool when available, for example if arithmetic is needed, prefer a calculator tool.
78+
- Keep chain-of-thought under 'Thought:' lines. End with the `finish` action.
79+
contribute: [context]
80+
- text: |
81+
Here is an example of execution traces:
82+
4183
What profession does Nicholas Ray and Elia Kazan have in common?
4284
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
43-
Action:
44-
[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
85+
Action: {"name": "search", "arguments": {"topic": "Nicholas Ray"}}
4586
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
4687
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
47-
Action:
48-
[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
88+
Action: {"name": "search", "arguments": {"topic": "Elia Kazan"}}
4989
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
5090
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
51-
Action:
52-
[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
53-
54-
55-
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
56-
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
57-
Action:
58-
[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
59-
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) ...
60-
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
61-
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
62-
Action:
63-
[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
91+
Action: {"name": "finish", "arguments": {"answer": "director, screenwriter, actor"}}
6492
contribute: [context]
6593

66-
- "How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.\n"
94+
- ${ problem }
6795
- repeat:
6896
text:
6997
- def: thought
7098
model: ollama_chat/granite4:micro
7199
parameters:
72100
stop: ["Action:"]
73-
- "Action:\n"
101+
- text: "Action: "
74102
- def: action
75103
model: ollama_chat/granite4:micro
76-
parameters:
77-
stop: ["\n"]
78104
parser: json
79-
- "\nObservation: "
80-
- match: ${ action[0].name }
105+
spec:
106+
name: string
107+
arguments: object
108+
- defs:
109+
func_name: ${ action.name }
110+
func_args: ${ action.arguments }
111+
text: |-
112+
113+
Observation:
114+
- match: ${ func_name }
81115
with:
82-
- case: Search
116+
- case: search
83117
then:
84-
call: ${ Search }
118+
call: ${ search }
85119
args:
86-
topic: ${ action[0].arguments.topic }
87-
- case: Calc
120+
topic: ${ func_args.topic }
121+
- case: calc
88122
then:
89-
call: ${ Calc }
123+
call: ${ calc }
90124
args:
91-
expr: ${ action[0].arguments.expr }
92-
- case: Finish
93-
then: ${ action[0].arguments.topic }
125+
expr: ${ func_args.expr }
126+
- case: finish
127+
then: ${ func_args.answer }
94128
- "\n"
95-
until: ${ action[0].name == "Finish" }
129+
until: ${ func_name == "finish" }

examples/demos/react.pdl

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
description: Demonstrating a React program
22
defs:
3-
Calc:
3+
problem: |
4+
How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.
5+
calc:
46
description: Calculator function
57
function:
6-
expr:
7-
type: string
8-
description: Arithmetic expression to calculate
8+
expr:
9+
type: string
10+
description: Arithmetic expression to calculate
911
return:
1012
lang: python
1113
code: result = ${ expr }
12-
Search:
14+
search:
1315
description: Wikipedia search
1416
function:
1517
topic:
@@ -24,72 +26,104 @@ defs:
2426
result = wikipedia.summary("${ topic }")
2527
except wikipedia.WikipediaException as e:
2628
result = str(e)
29+
finish:
30+
description: Respond with the answer
31+
function:
32+
answer:
33+
type: string
34+
description: The answer
35+
return: ${ answer }
2736
tools:
2837
data:
29-
- ${ Calc.signature }
30-
- ${ Search.signature }
38+
- ${ calc.signature }
39+
- ${ search.signature }
40+
- ${ finish.signature }
3141
text:
32-
- role: system
33-
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
34-
contribute: [context]
3542
- role: tools
3643
content:
3744
text: ${ tools }
3845
contribute: [context]
39-
- text:
40-
|
46+
- role: system
47+
content:
48+
text:
49+
- |
50+
You are a helpful assistant that uses the following tools:
51+
- for:
52+
tool: ${ tools }
53+
index: i
54+
repeat: |
55+
56+
${ i+1 }) ${ tool.function.name } - ${ tool.function.description }
57+
The parameters respect the following JSON Schema: ${tool.function.parameters}
58+
- |
59+
60+
Follow this exact ReAct format:
61+
62+
Thought: <describe what you plan to do next>
63+
Action: { "name": <tool_name>, "arguments": <tool_arguments> }
64+
65+
# After I return tool results, continue:
66+
67+
Observation: <what you observed>
68+
69+
# Repeat Thought/Action/Observation as needed.
70+
# When you are ready to answer the user, finish with the action:
71+
72+
Action: { "name": "finish", "arguments": { "answer": <your best possible answer, concise and clear> } }
73+
74+
STRICT FORMAT RULES:
75+
- One action per step.
76+
- Do NOT invent tools.
77+
- Use a tool when available, for example if arithmetic is needed, prefer a calculator tool.
78+
- Keep chain-of-thought under 'Thought:' lines. End with the `finish` action.
79+
contribute: [context]
80+
- text: |
81+
Here is an example of execution traces:
82+
4183
What profession does Nicholas Ray and Elia Kazan have in common?
4284
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
43-
Action:
44-
[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
85+
Action: {"name": "search", "arguments": {"topic": "Nicholas Ray"}}
4586
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
4687
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
47-
Action:
48-
[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
88+
Action: {"name": "search", "arguments": {"topic": "Elia Kazan"}}
4989
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
5090
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
51-
Action:
52-
[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
53-
54-
55-
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
56-
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
57-
Action:
58-
[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
59-
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) ...
60-
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
61-
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
62-
Action:
63-
[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
91+
Action: {"name": "finish", "arguments": {"answer": "director, screenwriter, actor"}}
6492
contribute: [context]
6593

66-
- "How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.\n"
94+
- ${ problem }
6795
- repeat:
6896
text:
6997
- def: thought
7098
model: ollama_chat/granite4:micro
7199
parameters:
72100
stop: ["Action:"]
73-
- "Action:\n"
101+
- text: "Action: "
74102
- def: action
75103
model: ollama_chat/granite4:micro
76-
parameters:
77-
stop: ["\n"]
78104
parser: json
79-
- "\nObservation: "
80-
- match: ${ action[0].name }
105+
spec:
106+
name: string
107+
arguments: object
108+
- defs:
109+
func_name: ${ action.name }
110+
func_args: ${ action.arguments }
111+
text: |-
112+
113+
Observation:
114+
- match: ${ func_name }
81115
with:
82-
- case: Search
116+
- case: search
83117
then:
84-
call: ${ Search }
118+
call: ${ search }
85119
args:
86-
topic: ${ action[0].arguments.topic }
87-
- case: Calc
120+
topic: ${ func_args.topic }
121+
- case: calc
88122
then:
89-
call: ${ Calc }
123+
call: ${ calc }
90124
args:
91-
expr: ${ action[0].arguments.expr }
92-
- case: Finish
93-
then: ${ action[0].arguments.topic }
125+
expr: ${ func_args.expr }
126+
- case: finish
127+
then: ${ func_args.answer }
94128
- "\n"
95-
until: ${ action[0].name == "Finish" }
129+
until: ${ func_name == "finish" }

examples/demos/react_call.pdl

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)