Iam creating my first programming using openai API but my code is creating some errors kindly anyone review my code

import openai

openai.api_key = ("API-KEY")

def sentiment_analysis(text):
    messages={
        {"role":"system","content":"""you are trained to analyze and detect the sentiment of the text.
         
         if you're of unsure of an answer, you can say "not sure" and recommend users to review manually"""},
         {"role":"user","content":f"""analyze the following text and determine if the sentiment is:positive or negative.return answer in single word as either positive or negative:{text}"""}
    }
    response=openai.chatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        max_tokens=1,
        n=1,
        stop=None,
        temperature=0,)
    response_text=response.choices[0].messages.content.strip().lower()
    return response_text
input="I am very happy today"
output=sentiment_analysis(input)
print(input,"this sentiment is",output)

Hi @AIgeek

Welcome to the community.

messages should be a list of message objects instead of what you’re doing in this code.

Use the boilerplate code from API reference.

Specifically: the acceptable messages format is a python list of dictionaries.

You will have a better AI semantics if you put a space after colons, such as at the end of the user role. This allows encoding as a common " word" token which includes a space in front like it would in the middle of paragraphs.

You may find the AI needs more than one token to output its word or the error you want, because you haven’t explicitly made clear the production of one word - it will like to chat unless extreme documentation is made to overcome its need for friendly human output… It is not “trained to”, you need to tell it it wasn’t trained to and here’s what it must do.

extra comma after temperature