I’ll jump right into this.
First, you demonstrate use of an older openai python library, while a new version was released November 6, and has seen rapid changes. Let’s install that, at an administrator or root command prompt if you have a system “for all users” Python install:
pip install --upgrade openai
Your Python should also be version 3.8 - 3.11 for best compatibility.
Then, instead of needing to obtain a “TEST” API key from the environment, you can set OPENAI_API_KEY
as the environment variable containing an API key from your account, and it will automatically be used by new client code as a default.
Then we code. Although there is still a completion model that can replace text-davinci-003
, I’ll show using the messages that a chat model requires and new API parameters.
I’m also going to add example stuff that makes your function actually evaluate business names…
"""An OpenAI API script, starting with messages to AI"""
import openai
client = openai.OpenAI()
system = "You are Business Boss AI, a business brainstorming assistant."
user_template = (
f"Rate the quality of the business name {{business_name}}, "
f"in the field of {{industry}}, on a scale 0-10."
)
def evaluate_business_name_with_ai(business_name, industry):
"""Then a function that accepts parameters and calls API"""
user = user_template.format(
business_name=business_name,
industry=industry
)
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user},
],
max_tokens=100, top_p=0.1,
)
except Exception as e:
print(f"Error in OpenAI call: {e}")
return "N/A"
return response.choices[0].message.content
if __name__ == "__main__":
"""Finally some code that will demonstrate usage"""
bus = "TriplePAQ"
ind = "internet travel agency"
score = evaluate_business_name_with_ai(bus, ind)
print(score) # AI language results
In all, we “programmed” the operation of the AI with a system message, made the user message a template, and then fill in the template with values passed to the function. Let’s see it work on my terrible made-up business name:
I would rate the business name TriplePAQ a 7 out of 10 in the field of internet travel agency. The name is catchy and has a modern feel to it. It suggests a focus on providing multiple travel packages to customers. However, it may not immediately convey the idea of an online travel agency, and could be confused with a shipping or packaging company.
The chatty bot is kind of polite about my name, I guess. The actual messages and function are now up to you.