|
67 | 67 | },
|
68 | 68 | {
|
69 | 69 | "cell_type": "code",
|
70 |
| - "execution_count": 2, |
| 70 | + "execution_count": 4, |
71 | 71 | "metadata": {
|
72 | 72 | "id": "79Cu3eLlEOCA"
|
73 | 73 | },
|
|
110 | 110 | },
|
111 | 111 | {
|
112 | 112 | "cell_type": "code",
|
113 |
| - "execution_count": 3, |
| 113 | + "execution_count": 5, |
114 | 114 | "metadata": {
|
115 | 115 | "id": "8IZjc893Ef1c"
|
116 | 116 | },
|
|
138 | 138 | },
|
139 | 139 | {
|
140 | 140 | "cell_type": "code",
|
141 |
| - "execution_count": 4, |
| 141 | + "execution_count": 6, |
142 | 142 | "metadata": {
|
143 | 143 | "id": "iKWRHCjVF55-"
|
144 | 144 | },
|
|
197 | 197 | },
|
198 | 198 | {
|
199 | 199 | "cell_type": "code",
|
200 |
| - "execution_count": 5, |
| 200 | + "execution_count": 7, |
201 | 201 | "metadata": {
|
202 | 202 | "colab": {
|
203 | 203 | "base_uri": "https://localhost:8080/"
|
|
210 | 210 | "name": "stdout",
|
211 | 211 | "output_type": "stream",
|
212 | 212 | "text": [
|
213 |
| - "{\"steps\":[{\"explanation\":\"First, we need to isolate the term with the variable, 8x, by eliminating the constant term on the left-hand side. We do this by subtracting 7 from both sides of the equation.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"After subtracting 7 from both sides, the equation simplifies to 8x = -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, to solve for x, we need to divide both sides of the equation by 8, the coefficient of x.\",\"output\":\"8x/8 = -30/8\"},{\"explanation\":\"Dividing each side by 8 gives us x = -30/8, which simplifies to x = -15/4. This is done by dividing both the numerator and the denominator by 2.\",\"output\":\"x = -15/4\"}],\"final_answer\":\"x = -15/4\"}\n" |
| 213 | + "{\"steps\":[{\"explanation\":\"We need to isolate the term with the variable, 8x. So, we start by subtracting 7 from both sides to remove the constant term on the left side.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"The +7 and -7 on the left side cancel each other out, leaving us with 8x. The right side simplifies to -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"To solve for x, divide both sides of the equation by 8, which is the coefficient of x.\",\"output\":\"x = -30 / 8\"},{\"explanation\":\"Simplify the fraction -30/8 by finding the greatest common divisor, which is 2.\",\"output\":\"x = -15 / 4\"}],\"final_answer\":\"x = -15/4\"}\n" |
214 | 214 | ]
|
215 | 215 | }
|
216 | 216 | ],
|
|
225 | 225 | },
|
226 | 226 | {
|
227 | 227 | "cell_type": "code",
|
228 |
| - "execution_count": null, |
| 228 | + "execution_count": 8, |
229 | 229 | "metadata": {
|
230 | 230 | "colab": {
|
231 | 231 | "base_uri": "https://localhost:8080/",
|
|
234 | 234 | "id": "mAwEts-hCA73",
|
235 | 235 | "outputId": "9101c8ff-7b83-4ae6-f6c0-0da6a45ab251"
|
236 | 236 | },
|
237 |
| - "outputs": [], |
| 237 | + "outputs": [ |
| 238 | + { |
| 239 | + "name": "stdout", |
| 240 | + "output_type": "stream", |
| 241 | + "text": [ |
| 242 | + "Step 1: We need to isolate the term with the variable, 8x. So, we start by subtracting 7 from both sides to remove the constant term on the left side.\n", |
| 243 | + "\n", |
| 244 | + "8x + 7 - 7 = -23 - 7\n", |
| 245 | + "\n", |
| 246 | + "\n", |
| 247 | + "Step 2: The +7 and -7 on the left side cancel each other out, leaving us with 8x. The right side simplifies to -30.\n", |
| 248 | + "\n", |
| 249 | + "8x = -30\n", |
| 250 | + "\n", |
| 251 | + "\n", |
| 252 | + "Step 3: To solve for x, divide both sides of the equation by 8, which is the coefficient of x.\n", |
| 253 | + "\n", |
| 254 | + "x = -30 / 8\n", |
| 255 | + "\n", |
| 256 | + "\n", |
| 257 | + "Step 4: Simplify the fraction -30/8 by finding the greatest common divisor, which is 2.\n", |
| 258 | + "\n", |
| 259 | + "x = -15 / 4\n", |
| 260 | + "\n", |
| 261 | + "\n", |
| 262 | + "Final answer:\n", |
| 263 | + "\n", |
| 264 | + "\n", |
| 265 | + "x = -15/4\n" |
| 266 | + ] |
| 267 | + } |
| 268 | + ], |
238 | 269 | "source": [
|
239 | 270 | "# Print results step by step\n",
|
240 |
| - "from IPython.display import Math, display\n", |
241 |
| - "\n", |
242 |
| - "def print_math_response(response):\n", |
243 |
| - " result = json.loads(response)\n", |
244 |
| - " steps = result['steps']\n", |
245 |
| - " final_answer = result['final_answer']\n", |
246 |
| - " for i in range(len(steps)):\n", |
247 |
| - " print(f\"Step {i+1}: {steps[i]['explanation']}\\n\")\n", |
248 |
| - " display(Math(steps[i]['output']))\n", |
249 |
| - " print(\"\\n\")\n", |
250 | 271 | "\n",
|
251 |
| - " print(\"Final answer:\\n\\n\")\n", |
252 |
| - " display(Math(final_answer))\n", |
| 272 | + "result = json.loads(result.content)\n", |
| 273 | + "steps = result['steps']\n", |
| 274 | + "final_answer = result['final_answer']\n", |
| 275 | + "for i in range(len(steps)):\n", |
| 276 | + " print(f\"Step {i+1}: {steps[i]['explanation']}\\n\")\n", |
| 277 | + " print(steps[i]['output'])\n", |
| 278 | + " print(\"\\n\")\n", |
253 | 279 | "\n",
|
254 |
| - "print_math_response(result.content)" |
| 280 | + "print(\"Final answer:\\n\\n\")\n", |
| 281 | + "print(final_answer)" |
255 | 282 | ]
|
256 | 283 | },
|
257 | 284 | {
|
|
276 | 303 | ""
|
277 | 304 | ]
|
278 | 305 | },
|
| 306 | + { |
| 307 | + "cell_type": "markdown", |
| 308 | + "metadata": {}, |
| 309 | + "source": [ |
| 310 | + "## Alternative: Using the SDK `parse` helper\n", |
| 311 | + "\n", |
| 312 | + "The new SDK version adds a `parse` helper, allowing you to use your own Pydantic model without defining a JSON schema." |
| 313 | + ] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": 9, |
| 318 | + "metadata": {}, |
| 319 | + "outputs": [], |
| 320 | + "source": [ |
| 321 | + "from pydantic import BaseModel\n", |
| 322 | + "\n", |
| 323 | + "class MathReasoning(BaseModel):\n", |
| 324 | + " class Step(BaseModel):\n", |
| 325 | + " explanation: str\n", |
| 326 | + " output: str\n", |
| 327 | + "\n", |
| 328 | + " steps: list[Step]\n", |
| 329 | + " final_answer: str\n", |
| 330 | + "\n", |
| 331 | + "def get_math_solution(question: str):\n", |
| 332 | + " response = client.beta.chat.completions.parse(\n", |
| 333 | + " model=openai_model,\n", |
| 334 | + " messages=[\n", |
| 335 | + " {\"role\": \"system\", \"content\": math_tutor_prompt},\n", |
| 336 | + " {\"role\": \"user\", \"content\": question},\n", |
| 337 | + " ],\n", |
| 338 | + " response_format=MathReasoning,\n", |
| 339 | + " )\n", |
| 340 | + "\n", |
| 341 | + " return response.choices[0].message" |
| 342 | + ] |
| 343 | + }, |
| 344 | + { |
| 345 | + "cell_type": "code", |
| 346 | + "execution_count": 10, |
| 347 | + "metadata": {}, |
| 348 | + "outputs": [ |
| 349 | + { |
| 350 | + "name": "stdout", |
| 351 | + "output_type": "stream", |
| 352 | + "text": [ |
| 353 | + "[Step(explanation='To isolate the term with the variable on one side of the equation, start by subtracting 7 from both sides.', output='8x = -23 - 7'), Step(explanation='Combine like terms on the right side to simplify the equation.', output='8x = -30'), Step(explanation='Divide both sides by 8 to solve for x.', output='x = -30 / 8'), Step(explanation='Simplify the fraction by dividing both the numerator and the denominator by their greatest common divisor, which is 2.', output='x = -15 / 4')]\n", |
| 354 | + "Final answer:\n", |
| 355 | + "x = -15/4\n" |
| 356 | + ] |
| 357 | + } |
| 358 | + ], |
| 359 | + "source": [ |
| 360 | + "result = get_math_solution(question).parsed\n", |
| 361 | + "\n", |
| 362 | + "print(result.steps)\n", |
| 363 | + "print(\"Final answer:\")\n", |
| 364 | + "print(result.final_answer)" |
| 365 | + ] |
| 366 | + }, |
| 367 | + { |
| 368 | + "cell_type": "markdown", |
| 369 | + "metadata": {}, |
| 370 | + "source": [ |
| 371 | + "## See your trace in Langfuse\n", |
| 372 | + "\n", |
| 373 | + "You can now see the trace and your supplied Pydantic model in Langfuse.\n", |
| 374 | + "\n", |
| 375 | + "[Example trace in Langfuse](https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/59c4376a-c8eb-4ecb-8780-2f028b87e7eb)" |
| 376 | + ] |
| 377 | + }, |
| 378 | + { |
| 379 | + "cell_type": "markdown", |
| 380 | + "metadata": {}, |
| 381 | + "source": [ |
| 382 | + "" |
| 383 | + ] |
| 384 | + }, |
279 | 385 | {
|
280 | 386 | "cell_type": "markdown",
|
281 | 387 | "metadata": {
|
|
297 | 403 | "name": "python3"
|
298 | 404 | },
|
299 | 405 | "language_info": {
|
300 |
| - "name": "python" |
| 406 | + "codemirror_mode": { |
| 407 | + "name": "ipython", |
| 408 | + "version": 3 |
| 409 | + }, |
| 410 | + "file_extension": ".py", |
| 411 | + "mimetype": "text/x-python", |
| 412 | + "name": "python", |
| 413 | + "nbconvert_exporter": "python", |
| 414 | + "pygments_lexer": "ipython3", |
| 415 | + "version": "3.9.18" |
301 | 416 | }
|
302 | 417 | },
|
303 | 418 | "nbformat": 4,
|
|
0 commit comments