Aan de slagGa gratis aan de slag

Setting token limits

An e-commerce platform just hired you to improve the performance of their customer service bot built using the OpenAI API. You've decided to start by ensuring that the input messages do not cause any rate limit issue by setting a limit of 100 tokens, and test it with a sample input message.

The tiktoken library has been preloaded.

Deze oefening maakt deel uit van de cursus

Developing AI Systems with the OpenAI API

Cursus bekijken

Oefeninstructies

  • Use the tiktoken library to create an encoding for the gpt-4o-mini model.
  • Check for the expected number of tokens in the input message.
  • Print the response if the message passes both checks.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

client = OpenAI(api_key="")
input_message = {"role": "user", "content": "I'd like to buy a shirt and a jacket. Can you suggest two color pairings for these items?"}

# Use tiktoken to create the encoding for your model
encoding = tiktoken.____(____)
# Check for the number of tokens
num_tokens = ____

# Run the chat completions function and print the response
if num_tokens <= ____:
    response = client.chat.completions.create(model="gpt-4o-mini", messages=[input_message])
    print(____)
else:
    print("Message exceeds token limit")
Code bewerken en uitvoeren