Gom lô

Phát triển hệ thống AI với OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Giới hạn tốc độ truy cập là gì

Một người lái xe bị cảnh sát chặn lại

Phát triển hệ thống AI với OpenAI API

Khi nào xảy ra giới hạn tốc độ

 

  • Quá nhiều yêu cầu

    Nhiều bong bóng thoại đại diện cho nhiều tin nhắn

 

  • Quá nhiều văn bản trong yêu cầu

    Biểu tượng bong bóng thoại lớn có dấu chấm trên nền trắng, biểu thị một tin nhắn dài
Phát triển hệ thống AI với OpenAI API

Tránh giới hạn tốc độ

 

  • Thử lại
    • Chờ ngắn giữa các yêu cầu

 

  • Gom lô
    • Xử lý nhiều tin nhắn trong một yêu cầu

 

  • Giảm token
    • Đo và cắt giảm số lượng token
Phát triển hệ thống AI với OpenAI API

Thử lại

 

from tenacity import (
    retry,
    stop_after_attempt,
    wait_random_exponential
)

@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
Phát triển hệ thống AI với OpenAI API

Thử lại

 

@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
Phát triển hệ thống AI với OpenAI API

Gom lô

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]
Phát triển hệ thống AI với OpenAI API

Gom lô

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
Phát triển hệ thống AI với OpenAI API

Giảm số token

 

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
Phát triển hệ thống AI với OpenAI API

Ayo berlatih!

Phát triển hệ thống AI với OpenAI API

Preparing Video For Download...