|
39 | 39 | "outputs": [], |
40 | 40 | "source": [ |
41 | 41 | "import os\n", |
42 | | - "\n", |
43 | 42 | "import openai\n", |
44 | 43 | "\n", |
| 44 | + "from pydantic import BaseModel\n", |
| 45 | + "\n", |
45 | 46 | "# OpenAI env variables\n", |
46 | 47 | "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY_HERE\"\n", |
47 | 48 | "\n", |
|
86 | 87 | "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!" |
87 | 88 | ] |
88 | 89 | }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "id": "fb5ebdad", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "### 3.1 Chat Completions API" |
| 96 | + ] |
| 97 | + }, |
89 | 98 | { |
90 | 99 | "cell_type": "code", |
91 | 100 | "execution_count": null, |
|
104 | 113 | "id": "abaf6987-c257-4f0d-96e7-3739b24c7206", |
105 | 114 | "metadata": {}, |
106 | 115 | "outputs": [], |
107 | | - "source": [] |
| 116 | + "source": [ |
| 117 | + "class Person(BaseModel):\n", |
| 118 | + " name: str\n", |
| 119 | + " age: int\n", |
| 120 | + " occupation: str\n", |
| 121 | + "\n", |
| 122 | + "# Parse method automatically returns structured Pydantic object\n", |
| 123 | + "completion = openai_client.chat.completions.parse(\n", |
| 124 | + " model=\"gpt-4o\",\n", |
| 125 | + " messages=[\n", |
| 126 | + " {\"role\": \"user\", \"content\": \"Extract: John Doe is 30 years old and works as a software engineer\"}\n", |
| 127 | + " ],\n", |
| 128 | + " response_format=Person,\n", |
| 129 | + ")\n", |
| 130 | + "\n", |
| 131 | + "completion.choices[0].message.parsed" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "id": "4e6fb396", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### 3.2 Responses API" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "id": "21369c42", |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "# Responses API - new unified interface with enhanced metadata\n", |
| 150 | + "response = openai_client.responses.create(\n", |
| 151 | + " model=\"gpt-4o-mini\",\n", |
| 152 | + " input=\"What is 3 + 3?\",\n", |
| 153 | + " max_output_tokens=50\n", |
| 154 | + ")\n", |
| 155 | + "\n", |
| 156 | + "# Response is automatically traced\n", |
| 157 | + "response" |
| 158 | + ] |
108 | 159 | } |
109 | 160 | ], |
110 | 161 | "metadata": { |
111 | 162 | "kernelspec": { |
112 | | - "display_name": "Python 3 (ipykernel)", |
| 163 | + "display_name": "base", |
113 | 164 | "language": "python", |
114 | 165 | "name": "python3" |
115 | 166 | }, |
|
123 | 174 | "name": "python", |
124 | 175 | "nbconvert_exporter": "python", |
125 | 176 | "pygments_lexer": "ipython3", |
126 | | - "version": "3.9.18" |
| 177 | + "version": "3.12.7" |
127 | 178 | } |
128 | 179 | }, |
129 | 180 | "nbformat": 4, |
|
0 commit comments