assistants/deep-dive.md +42 −44
21 21
22```python22```python
23file = client.files.create(23file = client.files.create(
2424 file=open("revenue-forecast.csv", "rb"), file=open("revenue-forecast.csv", "rb"), purpose="assistants"
25 purpose='assistants'
26)25)
27```26```
28 27
49 description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",48 description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",
50 model="gpt-4o",49 model="gpt-4o",
51 tools=[{"type": "code_interpreter"}],50 tools=[{"type": "code_interpreter"}],
5251 tool_resources={ tool_resources={"code_interpreter": {"file_ids": [file.id]}},
53 "code_interpreter": {
54 "file_ids": [file.id]
55 }
56 }
57)52)
58```53```
59 54
107 "role": "user",102 "role": "user",
108 "content": "Create 3 data visualizations based on the trends in this file.",103 "content": "Create 3 data visualizations based on the trends in this file.",
109 "attachments": [104 "attachments": [
110105 { {"file_id": file.id, "tools": [{"type": "code_interpreter"}]}
111106 "file_id": file.id, ],
112 "tools": [{"type": "code_interpreter"}]
113 }
114 ]
115 }107 }
116 ]108 ]
117)109)
165Tools cannot access image content unless specified. To pass image files to Code Interpreter, add the file ID in the message `attachments` list to allow the tool to read and analyze the input. Image URLs cannot be downloaded in Code Interpreter today.157Tools cannot access image content unless specified. To pass image files to Code Interpreter, add the file ID in the message `attachments` list to allow the tool to read and analyze the input. Image URLs cannot be downloaded in Code Interpreter today.
166 158
167```python159```python
168160file = client.files.create(file = client.files.create(file=open("myimage.png", "rb"), purpose="vision")
169 file=open("myimage.png", "rb"),
170 purpose="vision"
171)
172thread = client.beta.threads.create(161thread = client.beta.threads.create(
173 messages=[162 messages=[
174 {163 {
176 "content": [165 "content": [
177 {166 {
178 "type": "text",167 "type": "text",
179168 "text": "What is the difference between these images?" "text": "What is the difference between these images?",
180 },169 },
181 {170 {
182 "type": "image_url",171 "type": "image_url",
183172 "image_url": {"url": "https://example.com/image.png"} "image_url": {
173 "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"
184 },174 },
185 {
186 "type": "image_file",
187 "image_file": {"file_id": file.id}
188 },175 },
176 {"type": "image_file", "image_file": {"file_id": file.id}},
189 ],177 ],
190 }178 }
191 ]179 ]
209 },198 },
210 {199 {
211 "type": "image_url",200 "type": "image_url",
212201 "image_url": {"url": "https://example.com/image.png"} "image_url": {"url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"}
213 },202 },
214 {203 {
215 "type": "image_file",204 "type": "image_file",
245},234},
246{235{
247"type": "image_url",236"type": "image_url",
248237"image_url": {"url": "https://example.com/image.png"}"image_url": {"url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"}
249},238},
250{239{
251"type": "image_file",240"type": "image_file",
271 {260 {
272 "role": "user",261 "role": "user",
273 "content": [262 "content": [
274263 { {"type": "text", "text": "What is this an image of?"},
275 "type": "text",
276 "text": "What is this an image of?"
277 },
278 {264 {
279 "type": "image_url",265 "type": "image_url",
280 "image_url": {266 "image_url": {
281267 "url": "https://example.com/image.png", "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png",
282268 "detail": "high" "detail": "high",
283269 } },
284 },270 },
285 ],271 ],
286 }272 }
301 {287 {
302 "type": "image_url",288 "type": "image_url",
303 "image_url": {289 "image_url": {
304290 "url": "https://example.com/image.png", "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png",
305 "detail": "high"291 "detail": "high"
306 }292 }
307 },293 },
328 {314 {
329 "type": "image_url",315 "type": "image_url",
330 "image_url": {316 "image_url": {
331317 "url": "https://example.com/image.png", "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png",
332 "detail": "high"318 "detail": "high"
333 }319 }
334 },320 },
373When annotations are present in the Message object, you'll see illegible model-generated substrings in the text that you should replace with the annotations. These strings may look something like `【13†source】` or `sandbox:/mnt/data/file.csv`. Here’s an example python code snippet that replaces these strings with the annotations.359When annotations are present in the Message object, you'll see illegible model-generated substrings in the text that you should replace with the annotations. These strings may look something like `【13†source】` or `sandbox:/mnt/data/file.csv`. Here’s an example python code snippet that replaces these strings with the annotations.
374 360
375```python361```python
362import os
363from pathlib import Path
364
365thread_id = os.environ["OPENAI_THREAD_ID"]
366message_id = os.environ["OPENAI_MESSAGE_ID"]
367downloads = Path("downloads")
368downloads.mkdir(exist_ok=True)
369
376# Retrieve the message object370# Retrieve the message object
377message = client.beta.threads.messages.retrieve(371message = client.beta.threads.messages.retrieve(
378372 thread_id="...", thread_id=thread_id,
379373 message_id="..." message_id=message_id,
380)374)
381 375
382# Extract the message content376# Extract the message content
387 381
388# Iterate over the annotations and add footnotes382# Iterate over the annotations and add footnotes
389 383
390384for index, annotation in enumerate(annotations): # Replace the text with a footnotefor index, annotation in enumerate(annotations):
391385message_content.value = message_content.value.replace(annotation.text, f' [{index}]') # Replace the text with a footnote.
386 message_content.value = message_content.value.replace(
387 annotation.text, f" [{index}]"
388 )
392 389
393 # Gather citations based on annotation attributes390 # Gather citations based on annotation attributes
394391 if (file_citation := getattr(annotation, 'file_citation', None)): if file_citation := getattr(annotation, "file_citation", None):
395 cited_file = client.files.retrieve(file_citation.file_id)392 cited_file = client.files.retrieve(file_citation.file_id)
396393 citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}') citations.append(f"[{index}] {file_citation.quote} from {cited_file.filename}")
397394 elif (file_path := getattr(annotation, 'file_path', None)): elif file_path := getattr(annotation, "file_path", None):
398 cited_file = client.files.retrieve(file_path.file_id)395 cited_file = client.files.retrieve(file_path.file_id)
399396 citations.append(f'[{index}] Click <here> to download {cited_file.filename}') file_content = client.files.content(file_path.file_id)
400397 # Note: File download functionality not implemented above for brevity output_path = downloads / Path(cited_file.filename).name
398 output_path.write_bytes(file_content.read())
399 citations.append(f"[{index}] Downloaded {output_path}")
401 400
402# Add footnotes to the end of the message before displaying to user401# Add footnotes to the end of the message before displaying to user
403 402
404403message_content.value += '\n' + '\n'.join(citations)message_content.value += "\n" + "\n".join(citations)
405```404```
406 405
407 406
412```python411```python
413run = client.beta.threads.runs.create(412run = client.beta.threads.runs.create(
414 thread_id=thread.id,413 thread_id=thread.id,
415414 assistant_id=assistant.id assistant_id=assistant.id,
416)415)
417```416```
418 417
442 assistant_id=assistant.id,441 assistant_id=assistant.id,
443 model="gpt-4o",442 model="gpt-4o",
444 instructions="New instructions that override the Assistant instructions",443 instructions="New instructions that override the Assistant instructions",
445444 tools=[{"type": "code_interpreter"}, {"type": "file_search"}] tools=[{"type": "code_interpreter"}, {"type": "file_search"}],
446)445)
447```446```
448 447