0

I'm trying to get the emotion in a text using chatgpt API

def infer_feeling(text):
    prompt = f"What feeling is filled in the following text?\nText: {text}\nFeeling:"

    response = openai.ChatCompletion.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ]
    )

    reply = response.choices[0].message['content']


emotions = ["happiness", "sadness", "anger", "fear", "trust", "curiosity", "hope", "despair"]

What I want is getting the reply as an array element (emotions). Is it possible to match the response of gpt to the element of this array? I want it to return the best matching emotion in that array, and nothing else.

Thanks in advance for any help

mhmtemnacr
  • 185
  • 3
  • 18

1 Answers1

1

It looks like you are trying to do something like this, since you want GPT to choose one out of several given options: https://community.openai.com/t/gpt-function-calling-function-params-enum/281946

I am not an expert, but personally I would define a function call and use prompt engineering in the parameter description to stress that the parameter must be one of these options ("happiness", "sadness", "anger", ...), whichever fits best.