Difficulty Upgrading to gpt-3.5-turbo

Hello all!

I’m trying to upgrade to ‘gpt-3.5-turbo’ before it is retired but I am having some difficulty. I apologize in advance for anything obvious that I am overlooking. Any help would be very much appreciated.

If this is not working because I’m still on ‘Usage tier 1’, I will gladly spend the $50 but I feel like the issue is due to my code.

Thank you in advance!


This is the working code I’m currently using for “text-davinci-003”:

openai.api_key = os.environ.get(‘TEST’)
client = openai.Client(api_key=openai.api_key)

def evaluate_business_name_with_ai(business_name, industry):
try:
response = client.completions.create(
model=“text-davinci-003”,
prompt=f"Prompt example here.“,
max_tokens=150
)
return response.choices[0].text.strip()
except Exception as e:
print(f"Error in OpenAI call: {e}”)
return “N/A”


This is the code I am trying to update with:

openai.api_key = os.environ.get(‘TEST’)
client = openai.Client(api_key=openai.api_key)

def evaluate_business_name_with_ai(business_name, industry):
try:
response = openai.Completion.create(
model=“gpt-3.5-turbo”,
prompt=f"Prompt example here.“,
)
return response.choices[0].text.strip()
except Exception as e:
print(f"Error in OpenAI call: {e}”)
return “N/A”

1 Like

Welcome!

You’ll want to check out the model endpoint compatibility docs…

Basically, there’s two types of endpoint now… Completion (old style - might go away?) and Chat Completion (ie ChatML)…

If you want to continue using the LLM completion style (and likely not change your code too much), you’ll want the gpt-3.5-turbo-instruct model.

Hope this makes sense!

1 Like

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.

Thank you for taking the time to help!! Really appreciate it.

1 Like

You’re the man! Thank you so very much for your help. Your detailed and thorough response means the world to me as I embark on learning more about OpenAI.

It is working perfectly now! No more errors.

Thank you again! Wishing you an amazing new year ahead! :vulcan_salute:

1 Like