“openai” is a python library. There’s a quickstart you could have read, but let’s jump in.
If you have Python 3.8-3.11 installed on your system for compatibility, you can, at your command line or shell:
pip install --upgrade openai
to install the latest version of the openai python library (wheel) and its dependencies.
You then can run Python scripts, applications, or more advanced uses with the new v1.1 client object programming style introduced November 6:
from openai import OpenAI
client = OpenAI()
system = [{"role": "system", "content":
"You are Jbot, a helpful AI assistant."}]
user = [{"role": "user", "content":
"introduce Jbot"}]
chat = []
while not user[0]['content'] == "exit":
try:
response = client.chat.completions.create(
messages=system + chat[-20:] + user,
model="gpt-3.5-turbo",
max_tokens=1000, top_p=0.9,
stream = True,
)
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
reply = ""
for part in response:
word = part.choices[0].delta.content or ""
reply += word
print(word, end ="")
chat += user + [{"role": "assistant", "content": reply}]
user = [{"role": "user", "content": input("\nPrompt: ")}]
Don’t save your programs as openai.py
, as that will break everything. Save this as streaming_chatbot.py
.
You must then finally obtain an API key from your (funded) API account, and set the key value as a OS environment variable OPENAI_API_KEY.