Skip to content
57 changes: 54 additions & 3 deletions examples/tracing/openai/openai_tracing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"import os\n",
"\n",
"import openai\n",
"from pydantic import BaseModel\n",
"\n",
"# OpenAI env variables\n",
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY_HERE\"\n",
Expand Down Expand Up @@ -86,6 +87,14 @@
"That's it! Now you can continue using the traced OpenAI client normally. The data is automatically published to Openlayer and you can start creating tests around it!"
]
},
{
"cell_type": "markdown",
"id": "fb5ebdad",
"metadata": {},
"source": [
"### 3.1 Chat Completions API"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -104,12 +113,54 @@
"id": "abaf6987-c257-4f0d-96e7-3739b24c7206",
"metadata": {},
"outputs": [],
"source": []
"source": [
"class Person(BaseModel):\n",
" name: str\n",
" age: int\n",
" occupation: str\n",
"\n",
"# Parse method automatically returns structured Pydantic object\n",
"completion = openai_client.chat.completions.parse(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": \"Extract: John Doe is 30 years old and works as a software engineer\"}\n",
" ],\n",
" response_format=Person,\n",
")\n",
"\n",
"completion.choices[0].message.parsed"
]
},
{
"cell_type": "markdown",
"id": "4e6fb396",
"metadata": {},
"source": [
"### 3.2 Responses API"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21369c42",
"metadata": {},
"outputs": [],
"source": [
"# Responses API - new unified interface with enhanced metadata\n",
"response = openai_client.responses.create(\n",
" model=\"gpt-4o-mini\",\n",
" input=\"What is 3 + 3?\",\n",
" max_output_tokens=50\n",
")\n",
"\n",
"# Response is automatically traced\n",
"response"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -123,7 +174,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
Loading