Skip to content

Commit 2949c66

Browse files
authored
Merge pull request #11 from NYU-RTS/responses-api
responses api migration, part 1
2 parents 1b447c0 + 9ee4bf6 commit 2949c66

7 files changed

Lines changed: 1536 additions & 555 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.memsearch/
2+
.agents/

notebooks/custom_tool.ipynb

Lines changed: 77 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"from portkey_ai import Portkey\n",
11-
"from pydantic import BaseModel, Field\n",
12-
"import random\n",
13-
"import json\n",
1410
"import os\n",
11+
"import json\n",
12+
"import random\n",
13+
"\n",
14+
"from rich.console import Console\n",
15+
"from rich.markdown import Markdown\n",
16+
"from portkey_ai import Portkey\n",
1517
"\n",
1618
"portkey = Portkey(\n",
1719
" base_url=\"https://ai-gateway.apps.cloud.rt.nyu.edu/v1/\",\n",
@@ -41,19 +43,17 @@
4143
"tools = [\n",
4244
" {\n",
4345
" \"type\": \"function\",\n",
44-
" \"function\": {\n",
45-
" \"name\": \"roll_dice\",\n",
46-
" \"description\": \"Roll the special N sided dice and return the result\",\n",
47-
" \"parameters\": {\n",
48-
" \"type\": \"object\",\n",
49-
" \"properties\": {\n",
50-
" \"num_sides\": {\n",
51-
" \"type\": \"integer\",\n",
52-
" \"description\": \"Number of sides for the die to roll\"\n",
53-
" }\n",
54-
" },\n",
55-
" \"required\": [\"num_sides\"]\n",
56-
" }\n",
46+
" \"name\": \"roll_dice\",\n",
47+
" \"description\": \"Roll the special N sided dice and return the result\",\n",
48+
" \"parameters\": {\n",
49+
" \"type\": \"object\",\n",
50+
" \"properties\": {\n",
51+
" \"num_sides\": {\n",
52+
" \"type\": \"integer\",\n",
53+
" \"description\": \"Number of sides for the die to roll\"\n",
54+
" }\n",
55+
" },\n",
56+
" \"required\": [\"num_sides\"]\n",
5757
" }\n",
5858
" }\n",
5959
"]\n",
@@ -91,35 +91,19 @@
9191
"name": "stdout",
9292
"output_type": "stream",
9393
"text": [
94-
"{\n",
95-
" \"content\": null,\n",
96-
" \"role\": \"assistant\",\n",
97-
" \"function_call\": null,\n",
98-
" \"tool_calls\": [\n",
99-
" {\n",
100-
" \"id\": \"call_9RM3WYjsITc2woCT0fyfrpZb\",\n",
101-
" \"function\": {\n",
102-
" \"arguments\": \"{\\\"num_sides\\\":10}\",\n",
103-
" \"name\": \"roll_dice\",\n",
104-
" \"thought_signature\": \"CqkDAY89a1/yQwoj7rd5ndc5Q1bC4+QLvd/GIRIz225kMl+TbcJpZB/JlqAORzer+zQKr7XQCNQrSvilQNj8yIL9dl4SOWT1WoXCNK/TWFmibUS71BD2B9mDNzqpg2V8wbWYBxZ3DP6Lg5+NcP90LE9IQ7McjuV1uZ03QSj9NdpvgSOQGZ7SQq8MVSwO0AS2ZmxwKuoXUhLr/LMGjuGnXcYQdbyrUdPZeDYmFy2EpT5qnQgyZNLXSd3TGTDPbYekmfprrQAz5VN7g+wCJ00ri3+hM78XvVo3jDeZcJ9dUfNwH9U03xugzerEEzhUqnCB+KpbosMR1PWlY3wcJOaWyUDy8rXzGjgx28WuYcBl9H04AynwM/Oj/2vS77n3K8O124EAncNjB87ZJ3rKtUYn/sC9PLVEZ62xM7fmUWJERcVchDEHYwDmu18s1FldtzLB+qfGogad6YFS4DkU8UnIk7XpOZJcraIso2WuOMsl+xUD3EDtxCAB/vWUOmLpEXLtiZz+kVnpQxlp+9RfHbJT0fFcRPne31ldiRGQRhOfgDXQxjFu6Lm9Y1Dty7U=\"\n",
105-
" },\n",
106-
" \"type\": \"function\"\n",
107-
" }\n",
108-
" ],\n",
109-
" \"refusal\": null,\n",
110-
" \"audio\": null\n",
111-
"}\n"
94+
"\n"
11295
]
11396
}
11497
],
11598
"source": [
116-
"completion = portkey.chat.completions.create(\n",
117-
" model=\"@vertexai/gemini-3-flash-preview\",\n",
118-
" messages=messages,\n",
99+
"response = portkey.responses.create(\n",
100+
" model=\"@vertexai/gemini-3.5-flash\",\n",
101+
" input=messages,\n",
119102
" tools=tools,\n",
120103
")\n",
121-
"messages.append(completion.choices[0].message)\n",
122-
"print(completion.choices[0].message)"
104+
"\n",
105+
"messages.extend(response.output)\n",
106+
"print(response.output_text)"
123107
]
124108
},
125109
{
@@ -139,7 +123,7 @@
139123
{
140124
"data": {
141125
"text/plain": [
142-
"FunctionCall(arguments='{\"num_sides\":10}', name='roll_dice', thought_signature='CqkDAY89a1/yQwoj7rd5ndc5Q1bC4+QLvd/GIRIz225kMl+TbcJpZB/JlqAORzer+zQKr7XQCNQrSvilQNj8yIL9dl4SOWT1WoXCNK/TWFmibUS71BD2B9mDNzqpg2V8wbWYBxZ3DP6Lg5+NcP90LE9IQ7McjuV1uZ03QSj9NdpvgSOQGZ7SQq8MVSwO0AS2ZmxwKuoXUhLr/LMGjuGnXcYQdbyrUdPZeDYmFy2EpT5qnQgyZNLXSd3TGTDPbYekmfprrQAz5VN7g+wCJ00ri3+hM78XvVo3jDeZcJ9dUfNwH9U03xugzerEEzhUqnCB+KpbosMR1PWlY3wcJOaWyUDy8rXzGjgx28WuYcBl9H04AynwM/Oj/2vS77n3K8O124EAncNjB87ZJ3rKtUYn/sC9PLVEZ62xM7fmUWJERcVchDEHYwDmu18s1FldtzLB+qfGogad6YFS4DkU8UnIk7XpOZJcraIso2WuOMsl+xUD3EDtxCAB/vWUOmLpEXLtiZz+kVnpQxlp+9RfHbJT0fFcRPne31ldiRGQRhOfgDXQxjFu6Lm9Y1Dty7U=')"
126+
"[ResponseFunctionToolCall(arguments='{\"num_sides\":10}', call_id='call_l38M88Up4bBKgxsU9ZshNVcK', name='roll_dice', type='function_call', id='fc_1fa5cd13-13c2-4b66-9937-7907c60fc118', namespace=None, status='completed')]"
143127
]
144128
},
145129
"execution_count": 5,
@@ -148,19 +132,19 @@
148132
}
149133
],
150134
"source": [
151-
"completion.choices[0].message.tool_calls[0].function"
135+
"response.output"
152136
]
153137
},
154138
{
155139
"cell_type": "code",
156140
"execution_count": 6,
157-
"id": "df9dc776-f0c4-42f0-9394-3fb033acfb37",
141+
"id": "6433ef2c-92ef-4365-8f46-5a29150e2187",
158142
"metadata": {},
159143
"outputs": [
160144
{
161145
"data": {
162146
"text/plain": [
163-
"FunctionCall(arguments='{\"num_sides\":10}', name='roll_dice', thought_signature='CqkDAY89a1/yQwoj7rd5ndc5Q1bC4+QLvd/GIRIz225kMl+TbcJpZB/JlqAORzer+zQKr7XQCNQrSvilQNj8yIL9dl4SOWT1WoXCNK/TWFmibUS71BD2B9mDNzqpg2V8wbWYBxZ3DP6Lg5+NcP90LE9IQ7McjuV1uZ03QSj9NdpvgSOQGZ7SQq8MVSwO0AS2ZmxwKuoXUhLr/LMGjuGnXcYQdbyrUdPZeDYmFy2EpT5qnQgyZNLXSd3TGTDPbYekmfprrQAz5VN7g+wCJ00ri3+hM78XvVo3jDeZcJ9dUfNwH9U03xugzerEEzhUqnCB+KpbosMR1PWlY3wcJOaWyUDy8rXzGjgx28WuYcBl9H04AynwM/Oj/2vS77n3K8O124EAncNjB87ZJ3rKtUYn/sC9PLVEZ62xM7fmUWJERcVchDEHYwDmu18s1FldtzLB+qfGogad6YFS4DkU8UnIk7XpOZJcraIso2WuOMsl+xUD3EDtxCAB/vWUOmLpEXLtiZz+kVnpQxlp+9RfHbJT0fFcRPne31ldiRGQRhOfgDXQxjFu6Lm9Y1Dty7U=')"
147+
"'function_call'"
164148
]
165149
},
166150
"execution_count": 6,
@@ -169,7 +153,7 @@
169153
}
170154
],
171155
"source": [
172-
"completion.choices[0].message.tool_calls[0].function"
156+
"response.output[0].type"
173157
]
174158
},
175159
{
@@ -179,7 +163,7 @@
179163
"metadata": {},
180164
"outputs": [],
181165
"source": [
182-
"function_to_call = available_tools[completion.choices[0].message.tool_calls[0].function.name] "
166+
"function_to_call = available_tools[response.output[0].name] "
183167
]
184168
},
185169
{
@@ -189,7 +173,7 @@
189173
"metadata": {},
190174
"outputs": [],
191175
"source": [
192-
"dice_sides_to_roll = json.loads(completion.choices[0].message.tool_calls[0].function.arguments)[\"num_sides\"]"
176+
"dice_sides_to_roll = json.loads(response.output[0].arguments)[\"num_sides\"]"
193177
]
194178
},
195179
{
@@ -217,12 +201,12 @@
217201
"metadata": {},
218202
"outputs": [],
219203
"source": [
220-
"result_message = {\n",
221-
" \"role\": \"tool\",\n",
222-
" \"tool_call_id\": completion.choices[0].message.tool_calls[0].id,\n",
223-
" \"name\": completion.choices[0].message.tool_calls[0].function.name,\n",
224-
" \"content\": json.dumps(dice_roll_result)\n",
225-
"}\n"
204+
"tool_call_result = ({\n",
205+
" \"type\": \"function_call_output\",\n",
206+
" \"call_id\": response.output[0].call_id,\n",
207+
" \"output\": json.dumps(dice_roll_result)\n",
208+
"\n",
209+
"})"
226210
]
227211
},
228212
{
@@ -232,7 +216,7 @@
232216
"metadata": {},
233217
"outputs": [],
234218
"source": [
235-
"messages.append(result_message)"
219+
"messages.append(tool_call_result)"
236220
]
237221
},
238222
{
@@ -246,11 +230,10 @@
246230
"text/plain": [
247231
"[{'role': 'user',\n",
248232
" 'content': 'Roll a 10 sided die and check if the dice roll was valid. Explain your reasoning.'},\n",
249-
" ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_9RM3WYjsITc2woCT0fyfrpZb', function=FunctionCall(arguments='{\"num_sides\":10}', name='roll_dice', thought_signature='CqkDAY89a1/yQwoj7rd5ndc5Q1bC4+QLvd/GIRIz225kMl+TbcJpZB/JlqAORzer+zQKr7XQCNQrSvilQNj8yIL9dl4SOWT1WoXCNK/TWFmibUS71BD2B9mDNzqpg2V8wbWYBxZ3DP6Lg5+NcP90LE9IQ7McjuV1uZ03QSj9NdpvgSOQGZ7SQq8MVSwO0AS2ZmxwKuoXUhLr/LMGjuGnXcYQdbyrUdPZeDYmFy2EpT5qnQgyZNLXSd3TGTDPbYekmfprrQAz5VN7g+wCJ00ri3+hM78XvVo3jDeZcJ9dUfNwH9U03xugzerEEzhUqnCB+KpbosMR1PWlY3wcJOaWyUDy8rXzGjgx28WuYcBl9H04AynwM/Oj/2vS77n3K8O124EAncNjB87ZJ3rKtUYn/sC9PLVEZ62xM7fmUWJERcVchDEHYwDmu18s1FldtzLB+qfGogad6YFS4DkU8UnIk7XpOZJcraIso2WuOMsl+xUD3EDtxCAB/vWUOmLpEXLtiZz+kVnpQxlp+9RfHbJT0fFcRPne31ldiRGQRhOfgDXQxjFu6Lm9Y1Dty7U='), type='function')], refusal=None, audio=None),\n",
250-
" {'role': 'tool',\n",
251-
" 'tool_call_id': 'call_9RM3WYjsITc2woCT0fyfrpZb',\n",
252-
" 'name': 'roll_dice',\n",
253-
" 'content': '7'}]"
233+
" ResponseFunctionToolCall(arguments='{\"num_sides\":10}', call_id='call_l38M88Up4bBKgxsU9ZshNVcK', name='roll_dice', type='function_call', id='fc_1fa5cd13-13c2-4b66-9937-7907c60fc118', namespace=None, status='completed'),\n",
234+
" {'type': 'function_call_output',\n",
235+
" 'call_id': 'call_l38M88Up4bBKgxsU9ZshNVcK',\n",
236+
" 'output': '4'}]"
254237
]
255238
},
256239
"execution_count": 12,
@@ -277,39 +260,50 @@
277260
"metadata": {},
278261
"outputs": [
279262
{
280-
"name": "stdout",
281-
"output_type": "stream",
282-
"text": [
283-
"{\n",
284-
" \"content\": \"The result of the 10-sided die roll is **7**.\\n\\n### Validity Check and Reasoning:\\nThe dice roll is **valid**. \\n\\n**Reasoning:**\\n1. **Range:** A 10-sided die (d10) produces results in the inclusive range of 1 to 10.\\n2. **Result:** The rolled value is 7.\\n3. **Comparison:** Since 7 is an integer greater than or equal to 1 and less than or equal to 10 ($1 \\\\le 7 \\\\le 10$), it falls within the expected mathematical parameters for this die.\",\n",
285-
" \"role\": \"assistant\",\n",
286-
" \"function_call\": null,\n",
287-
" \"tool_calls\": null,\n",
288-
" \"refusal\": null,\n",
289-
" \"audio\": null,\n",
290-
" \"content_blocks\": [\n",
291-
" {\n",
292-
" \"type\": \"text\",\n",
293-
" \"text\": \"The result of the 10-sided die roll is **7**.\\n\\n### Validity Check and Reasoning:\\nThe dice roll is **valid**. \\n\\n**Reasoning:**\\n1. **Range:** A 10-sided die (d10) produces results in the inclusive range of 1 to 10.\\n2. **Result:** The rolled value is 7.\\n3. **Comparison:** Since 7 is an integer greater than or equal to 1 and less than or equal to 10 ($1 \\\\le 7 \\\\le 10$), it falls within the expected mathematical parameters for this die.\"\n",
294-
" }\n",
295-
" ]\n",
296-
"}\n"
297-
]
263+
"data": {
264+
"text/html": [
265+
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">The result of rolling the 10-sided die is <span style=\"font-weight: bold\">4</span>. \n",
266+
"\n",
267+
"<span style=\"font-weight: bold\">Reasoning for validity:</span> \n",
268+
"\n",
269+
"<span style=\"color: #008080; text-decoration-color: #008080\"> 1 </span><span style=\"font-weight: bold\">Range Check:</span> A standard 10-sided die (d10) has faces numbered from 1 to 10. The result obtained is 4, which \n",
270+
"<span style=\"color: #008080; text-decoration-color: #008080\"> </span>falls within this valid range ($1 \\le 4 \\le 10$). \n",
271+
"<span style=\"color: #008080; text-decoration-color: #008080\"> 2 </span><span style=\"font-weight: bold\">Integer Check:</span> Dice rolls must result in whole numbers (integers). The result is 4, which is an integer. \n",
272+
"\n",
273+
"Therefore, the dice roll is <span style=\"font-weight: bold\">valid</span>. \n",
274+
"</pre>\n"
275+
],
276+
"text/plain": [
277+
"The result of rolling the 10-sided die is \u001b[1m4\u001b[0m. \n",
278+
"\n",
279+
"\u001b[1mReasoning for validity:\u001b[0m \n",
280+
"\n",
281+
"\u001b[36m 1 \u001b[0m\u001b[1mRange Check:\u001b[0m A standard 10-sided die (d10) has faces numbered from 1 to 10. The result obtained is 4, which \n",
282+
"\u001b[36m \u001b[0mfalls within this valid range ($1 \\le 4 \\le 10$). \n",
283+
"\u001b[36m 2 \u001b[0m\u001b[1mInteger Check:\u001b[0m Dice rolls must result in whole numbers (integers). The result is 4, which is an integer. \n",
284+
"\n",
285+
"Therefore, the dice roll is \u001b[1mvalid\u001b[0m. \n"
286+
]
287+
},
288+
"metadata": {},
289+
"output_type": "display_data"
298290
}
299291
],
300292
"source": [
301-
"completion = portkey.chat.completions.create(\n",
302-
" model=\"@vertexai/gemini-3-flash-preview\",\n",
303-
" messages=messages,\n",
293+
"response = portkey.responses.create(\n",
294+
" model=\"@vertexai/gemini-3.5-flash\",\n",
295+
" input=messages,\n",
304296
" tools=tools,\n",
305297
")\n",
306-
"print(completion.choices[0].message)"
298+
"\n",
299+
"console = Console()\n",
300+
"console.print(Markdown(response.output_text))"
307301
]
308302
},
309303
{
310304
"cell_type": "code",
311305
"execution_count": null,
312-
"id": "715a60bd-4ca2-494a-91f3-233565eb4326",
306+
"id": "c114728e-7f77-470c-9d5f-d0d6b57b80fd",
313307
"metadata": {},
314308
"outputs": [],
315309
"source": []
@@ -331,7 +325,7 @@
331325
"name": "python",
332326
"nbconvert_exporter": "python",
333327
"pygments_lexer": "ipython3",
334-
"version": "3.14.2"
328+
"version": "3.14.3"
335329
}
336330
},
337331
"nbformat": 4,

0 commit comments

Comments
 (0)