बैचिंग

OpenAI API के साथ AI सिस्टम बनाना

Francesca Donadoni

Curriculum Manager, DataCamp

रेट लिमिट्स क्या हैं

एक व्यक्ति कार चला रहा है, पुलिसकर्मी ने रोका है

OpenAI API के साथ AI सिस्टम बनाना

रेट लिमिट्स क्यों लगते हैं

 

  • बहुत अधिक रिक्वेस्ट्स

    कई संदेशों का समूह, अनेक मैसेज दर्शाते हुए

 

  • रिक्वेस्ट में बहुत ज़्यादा टेक्स्ट

    लंबे संदेश का संकेत देता बड़ा स्पीच बबल आइकन
OpenAI API के साथ AI सिस्टम बनाना

रेट लिमिट्स से बचना

 

  • Retry
    • रिक्वेस्ट्स के बीच छोटा इंतज़ार

 

  • Batching
    • कई मैसेज एक ही रिक्वेस्ट में प्रोसेस करना

 

  • टोकन्स घटाना
    • टोकन्स की गिनती करके उन्हें कम करना
OpenAI API के साथ AI सिस्टम बनाना

रीट्राई करना

 

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 के साथ AI सिस्टम बनाना

रीट्राई करना

 

@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 के साथ AI सिस्टम बनाना

बैचिंग

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 के साथ AI सिस्टम बनाना

बैचिंग

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 के साथ AI सिस्टम बनाना

टोकन्स घटाना

 

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 के साथ AI सिस्टम बनाना

अभ्यास करते हैं!

OpenAI API के साथ AI सिस्टम बनाना

Preparing Video For Download...