[Python] Fit the chat response into a list in GPT API

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página