|
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
9 | 9 | "source": [ |
10 | | - "from portkey_ai import Portkey\n", |
11 | | - "from pydantic import BaseModel, Field\n", |
12 | | - "import random\n", |
13 | | - "import json\n", |
14 | 10 | "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", |
15 | 17 | "\n", |
16 | 18 | "portkey = Portkey(\n", |
17 | 19 | " base_url=\"https://ai-gateway.apps.cloud.rt.nyu.edu/v1/\",\n", |
|
41 | 43 | "tools = [\n", |
42 | 44 | " {\n", |
43 | 45 | " \"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", |
57 | 57 | " }\n", |
58 | 58 | " }\n", |
59 | 59 | "]\n", |
|
91 | 91 | "name": "stdout", |
92 | 92 | "output_type": "stream", |
93 | 93 | "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" |
112 | 95 | ] |
113 | 96 | } |
114 | 97 | ], |
115 | 98 | "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", |
119 | 102 | " tools=tools,\n", |
120 | 103 | ")\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)" |
123 | 107 | ] |
124 | 108 | }, |
125 | 109 | { |
|
139 | 123 | { |
140 | 124 | "data": { |
141 | 125 | "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')]" |
143 | 127 | ] |
144 | 128 | }, |
145 | 129 | "execution_count": 5, |
|
148 | 132 | } |
149 | 133 | ], |
150 | 134 | "source": [ |
151 | | - "completion.choices[0].message.tool_calls[0].function" |
| 135 | + "response.output" |
152 | 136 | ] |
153 | 137 | }, |
154 | 138 | { |
155 | 139 | "cell_type": "code", |
156 | 140 | "execution_count": 6, |
157 | | - "id": "df9dc776-f0c4-42f0-9394-3fb033acfb37", |
| 141 | + "id": "6433ef2c-92ef-4365-8f46-5a29150e2187", |
158 | 142 | "metadata": {}, |
159 | 143 | "outputs": [ |
160 | 144 | { |
161 | 145 | "data": { |
162 | 146 | "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'" |
164 | 148 | ] |
165 | 149 | }, |
166 | 150 | "execution_count": 6, |
|
169 | 153 | } |
170 | 154 | ], |
171 | 155 | "source": [ |
172 | | - "completion.choices[0].message.tool_calls[0].function" |
| 156 | + "response.output[0].type" |
173 | 157 | ] |
174 | 158 | }, |
175 | 159 | { |
|
179 | 163 | "metadata": {}, |
180 | 164 | "outputs": [], |
181 | 165 | "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] " |
183 | 167 | ] |
184 | 168 | }, |
185 | 169 | { |
|
189 | 173 | "metadata": {}, |
190 | 174 | "outputs": [], |
191 | 175 | "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\"]" |
193 | 177 | ] |
194 | 178 | }, |
195 | 179 | { |
|
217 | 201 | "metadata": {}, |
218 | 202 | "outputs": [], |
219 | 203 | "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 | + "})" |
226 | 210 | ] |
227 | 211 | }, |
228 | 212 | { |
|
232 | 216 | "metadata": {}, |
233 | 217 | "outputs": [], |
234 | 218 | "source": [ |
235 | | - "messages.append(result_message)" |
| 219 | + "messages.append(tool_call_result)" |
236 | 220 | ] |
237 | 221 | }, |
238 | 222 | { |
|
246 | 230 | "text/plain": [ |
247 | 231 | "[{'role': 'user',\n", |
248 | 232 | " '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'}]" |
254 | 237 | ] |
255 | 238 | }, |
256 | 239 | "execution_count": 12, |
|
277 | 260 | "metadata": {}, |
278 | 261 | "outputs": [ |
279 | 262 | { |
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" |
298 | 290 | } |
299 | 291 | ], |
300 | 292 | "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", |
304 | 296 | " tools=tools,\n", |
305 | 297 | ")\n", |
306 | | - "print(completion.choices[0].message)" |
| 298 | + "\n", |
| 299 | + "console = Console()\n", |
| 300 | + "console.print(Markdown(response.output_text))" |
307 | 301 | ] |
308 | 302 | }, |
309 | 303 | { |
310 | 304 | "cell_type": "code", |
311 | 305 | "execution_count": null, |
312 | | - "id": "715a60bd-4ca2-494a-91f3-233565eb4326", |
| 306 | + "id": "c114728e-7f77-470c-9d5f-d0d6b57b80fd", |
313 | 307 | "metadata": {}, |
314 | 308 | "outputs": [], |
315 | 309 | "source": [] |
|
331 | 325 | "name": "python", |
332 | 326 | "nbconvert_exporter": "python", |
333 | 327 | "pygments_lexer": "ipython3", |
334 | | - "version": "3.14.2" |
| 328 | + "version": "3.14.3" |
335 | 329 | } |
336 | 330 | }, |
337 | 331 | "nbformat": 4, |
|
0 commit comments