import json
import os
from dotenv import load_dotenv
import openai
from openai import OpenAI
client = OpenAI()
load_dotenv()
# openai key
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY
from IPython.display import display, MarkdownAPI Keys: https://platform.openai.com/api-keys
Chat Completions API
- Takes a user’s message and a model as inputs
- Generates responses to user inputs
- Messages serve as the fundamental component in Chat Completions API operations
Chat Completion Models: Drawbacks
- No message history tracking
- Lack of support for file inputs (e.g., PDFs, CSVs)
- Challenges with computational tasks
- No management of context windows
- Operate synchronously
Assistants API
- Manages conversation Threads for Context Maintenance
- Tools: Code Interpreter, Retrieval, Function calling
- Supports file handling
- Asynchronous support
- Can generate graphs, charts, and process various file types
# !pip3 install openai --upgrade# openai version
openai.__version__'1.8.0'
1. No Message Histories in ChatCompletions
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "user", "content": "What's the capital of Japan"},
],
temperature=0.3,
seed=1,
)display(Markdown(response.choices[0].message.content))The capital of Japan is Tokyo.
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "user", "content": "Tell me something about that city"},
],
temperature=0.3,
seed=1,
)display(Markdown(response.choices[0].message.content))I’m sorry, but you didn’t specify which city you’re referring to. Could you please provide the name of the city you’re interested in learning about?
No Code Interpreter - Computational Tasks
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "user", "content": "reverse the string openaichatgpt"},
],
temperature=0.3,
seed=1,
)display(Markdown(response.choices[0].message.content))To reverse the string “openaichatgpt”, you would simply reverse the order of the characters to get “tpgtaahcianepo”.
"tpgtahciapeneo" == "openaichatgpt"[::-1]False
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "user", "content": "what's todays date?"},
],
temperature=0.3,
seed=1,
)display(Markdown(response.choices[0].message.content))As an AI, I don’t have real-time capabilities, so I can’t provide the current date. However, you can easily check the date on your computer, smartphone, or any other device with a calendar function.
Context Window Handling
Synchronous
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "user", "content": "reverse the string openaichatgpt"},
],
temperature=0.3,
seed=1,
)Assistants API
assistant = client.beta.assistants.create(
name="Python Tutor",
instructions="You are a python tutor teaching someone who has experience in Java",
tools=[{"type":"code_interpreter"}],
model="gpt-4-1106-preview"
)assistantAssistant(id='asst_4bTZR8hx96VEvyFca4hnmoe1', created_at=1705686038, description=None, file_ids=[], instructions='You are a python tutor teaching someone who has experience in Java', metadata={}, model='gpt-4-1106-preview', name='Python Tutor', object='assistant', tools=[ToolCodeInterpreter(type='code_interpreter')])
thread = client.beta.threads.create()
threadThread(id='thread_z12e0BsDI8SfaePTmRMsApXo', created_at=1705686038, metadata={}, object='thread')
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="reverse the string openaichatgpt"
)run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id)print(json.dumps(run.model_dump(), indent=4)){
"id": "run_npuaz95TU2HdDGu3cgRhkOsW",
"assistant_id": "asst_4bTZR8hx96VEvyFca4hnmoe1",
"cancelled_at": null,
"completed_at": null,
"created_at": 1705686039,
"expires_at": 1705686639,
"failed_at": null,
"file_ids": [],
"instructions": "You are a python tutor teaching someone who has experience in Java",
"last_error": null,
"metadata": {},
"model": "gpt-4-1106-preview",
"object": "thread.run",
"required_action": null,
"started_at": null,
"status": "queued",
"thread_id": "thread_z12e0BsDI8SfaePTmRMsApXo",
"tools": [
{
"type": "code_interpreter"
}
]
}
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)run.status'in_progress'
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
messagesSyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_j9CJRJRiYrsfl7pk6a07u72c', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='reverse the string openaichatgpt'), type='text')], created_at=1705686038, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_z12e0BsDI8SfaePTmRMsApXo')], object='list', first_id='msg_j9CJRJRiYrsfl7pk6a07u72c', last_id='msg_j9CJRJRiYrsfl7pk6a07u72c', has_more=False)
print(json.dumps(messages.model_dump(), indent=4)){
"data": [
{
"id": "msg_j9CJRJRiYrsfl7pk6a07u72c",
"assistant_id": null,
"content": [
{
"text": {
"annotations": [],
"value": "reverse the string openaichatgpt"
},
"type": "text"
}
],
"created_at": 1705686038,
"file_ids": [],
"metadata": {},
"object": "thread.message",
"role": "user",
"run_id": null,
"thread_id": "thread_z12e0BsDI8SfaePTmRMsApXo"
}
],
"object": "list",
"first_id": "msg_j9CJRJRiYrsfl7pk6a07u72c",
"last_id": "msg_j9CJRJRiYrsfl7pk6a07u72c",
"has_more": false
}
display(Markdown(messages.data[0].content[0].text.value))reverse the string openaichatgpt
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="make the previous input as uppercase and tell me the length of the string"
)import time# new run
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# wait for run to complete
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
print(run.status)
time.sleep(2)in_progress
in_progress
in_progress
completed
messages = client.beta.threads.messages.list(
thread_id=thread.id
)display(Markdown(messages.data[0].content[0].text.value))The reversed string “tpgtahcianepo” in uppercase is “TPGTAHCIANEPO”. The length of this string is 13 characters.