BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Developing AI Systems with the OpenAI API

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • 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.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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")
Kodu Düzenle ve Çalıştır