import json
import os
from dotenv import load_dotenv
import openai
from openai import OpenAI
= OpenAI()
client
load_dotenv()# openai key
= os.getenv("OPENAI_API_KEY")
OPENAI_API_KEY = OPENAI_API_KEY
openai.api_key from IPython.display import display, Markdown
API 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
= client.chat.completions.create(
response ="gpt-4-1106-preview",
model=[
messages"role": "user", "content": "What's the capital of Japan"},
{
],=0.3,
temperature=1,
seed )
0].message.content)) display(Markdown(response.choices[
The capital of Japan is Tokyo.
= client.chat.completions.create(
response ="gpt-4-1106-preview",
model=[
messages"role": "user", "content": "Tell me something about that city"},
{
],=0.3,
temperature=1,
seed )
0].message.content)) display(Markdown(response.choices[
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
= client.chat.completions.create(
response ="gpt-4-1106-preview",
model=[
messages"role": "user", "content": "reverse the string openaichatgpt"},
{
],=0.3,
temperature=1,
seed )
0].message.content)) display(Markdown(response.choices[
To reverse the string “openaichatgpt”, you would simply reverse the order of the characters to get “tpgtaahcianepo”.
"tpgtahciapeneo" == "openaichatgpt"[::-1]
False
= client.chat.completions.create(
response ="gpt-4-1106-preview",
model=[
messages"role": "user", "content": "what's todays date?"},
{
],=0.3,
temperature=1,
seed )
0].message.content)) display(Markdown(response.choices[
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
= client.chat.completions.create(
response ="gpt-4-1106-preview",
model=[
messages"role": "user", "content": "reverse the string openaichatgpt"},
{
],=0.3,
temperature=1,
seed )
Assistants API
= client.beta.assistants.create(
assistant ="Python Tutor",
name="You are a python tutor teaching someone who has experience in Java",
instructions=[{"type":"code_interpreter"}],
tools="gpt-4-1106-preview"
model )
assistant
Assistant(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')])
= client.beta.threads.create()
thread thread
Thread(id='thread_z12e0BsDI8SfaePTmRMsApXo', created_at=1705686038, metadata={}, object='thread')
= client.beta.threads.messages.create(
message =thread.id,
thread_id="user",
role="reverse the string openaichatgpt"
content )
= client.beta.threads.runs.create(
run =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"
}
]
}
= client.beta.threads.runs.retrieve(
run =thread.id,
thread_id=run.id
run_id )
run.status
'in_progress'
= client.beta.threads.messages.list(
messages =thread.id
thread_id
)
messages
SyncCursorPage[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
}
0].content[0].text.value)) display(Markdown(messages.data[
reverse the string openaichatgpt
= client.beta.threads.messages.create(
message =thread.id,
thread_id="user",
role="make the previous input as uppercase and tell me the length of the string"
content )
import time
# new run
= client.beta.threads.runs.create(
run =thread.id,
thread_id=assistant.id,
assistant_id
)
# wait for run to complete
while run.status == "queued" or run.status == "in_progress":
= client.beta.threads.runs.retrieve(
run =thread.id,
thread_id=run.id
run_id
)print(run.status)
2) time.sleep(
in_progress
in_progress
in_progress
completed
= client.beta.threads.messages.list(
messages =thread.id
thread_id )
0].content[0].text.value)) display(Markdown(messages.data[
The reversed string “tpgtahcianepo” in uppercase is “TPGTAHCIANEPO”. The length of this string is 13 characters.