Toplu işleme

OpenAI API ile AI Sistemleri Geliştirme

Francesca Donadoni

Curriculum Manager, DataCamp

Oran sınırlamaları nedir

Bir araba süren kişi, bir polis tarafından durduruluyor

OpenAI API ile AI Sistemleri Geliştirme

Oran sınırı nasıl oluşur

 

  • Çok fazla istek

    Birden çok mesajı temsil eden kalabalık konuşma balonları

 

  • İstekte çok fazla metin

    Uzun bir mesajı temsil eden noktalı büyük konuşma balonu simgesi
OpenAI API ile AI Sistemleri Geliştirme

Oran sınırlarından kaçınma

 

  • Yeniden dene
    • İstekler arasında kısa bekleme

 

  • Toplu işleme
    • Bir istekte birden çok mesaj işleme

 

  • Token azaltma
    • Token sayısını ölçüp düşürme
OpenAI API ile AI Sistemleri Geliştirme

Yeniden deneme

 

from tenacity import (
    retry,
    stop_after_attempt,
    wait_random_exponential
)

@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
OpenAI API ile AI Sistemleri Geliştirme

Yeniden deneme

 

@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))

def get_response(model, message): response = client.chat.completions.create( model=model, messages=[message], response_format={"type": "json_object"} ) return response.choices[0].message.content
OpenAI API ile AI Sistemleri Geliştirme

Toplu işleme

countries = ["United States", "Ireland", "India"]

message=[
    {
    "role": "system",
    "content": """You are given a series of countries and are asked to return the 
    country and capital city. Provide each of the questions with an answer in the 
    response as separate content.""",
    }]


[message.append({"role": "user", "content": i }) for i in countries]
OpenAI API ile AI Sistemleri Geliştirme

Toplu işleme

response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=message
    )

print(response.choices[0].message.content)
United States: Washington D.C.
Ireland: Dublin
India: New Delhi
OpenAI API ile AI Sistemleri Geliştirme

Token azaltma

 

import tiktoken


encoding = tiktoken.encoding_for_model("gpt-4o-mini")
prompt = "Tokens can be full words, or groups of characters commonly grouped together: tokenization."
num_tokens = len(encoding.encode(prompt))
print("Number of tokens in prompt:", num_tokens)
Number of tokens in prompt: 17
OpenAI API ile AI Sistemleri Geliştirme

Hadi pratik yapalım!

OpenAI API ile AI Sistemleri Geliştirme

Preparing Video For Download...