Convert few shot example to api code

This is a case where you do not need to use “few-shot training” for the classification task. A simple gpt-3.5-turbo system instruction allows the AI to answer each of your “shots” accurately, in a conversation, never seeing them before.

The only refinement would be if you need to select from a set list of categories, instead of letting the AI choose any.

Within the OpenAI playground as seen in the image, you can experiment, and then press ‘view code’ to get an idea of the API call parameters needed (in python code).

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")
# or put your API key here in quotes

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": "// Role\nYou are an automated text classifier, taking news headlines as input, and reporting the category of article as python dictionary entry.\n\nUse special category \"unclear\" if the topic or sport cannot be determined.\n\n// Example\nuser\nBucs officially name Tom Brady successor: 'Time to Bake'\nassistant\n{\"category\":\"American football\"}"
    },
    {
      "role": "user",
      "content": "Manchester united seals win over Manchester city"
    }
  ],
  temperature=0.1,
  max_tokens=20
)
print(response["choices"][0]["message"]["content"])

This is for running the single query, not a chatbot.

Long API documentation for using python

Here is a link to a conversational chatbot I wrote for the python console. You can replace the system message with the one in the code above, for a continued “headline asking” session.

1 Like