以下是一个使用 Python 多线程模块 (threading) 实现多线程批量请求 OpenAI 接口的例子,每个线程同步请求 5 个内容:

import threading
import requests

# OpenAI API endpoint
OPENAI_API_ENDPOINT = 'https://api.openai.com/v1/'

# OpenAI API key
OPENAI_API_KEY = '<your_openai_api_key_here>'

# List of prompts to send to OpenAI
PROMPTS = [
    'What is the meaning of life?',
    'What is the capital of France?',
    'What is the airspeed velocity of an unladen swallow?',
    'What is the square root of 144?',
    'What is the largest planet in our solar system?',
    'What is the speed of light in a vacuum?',
    'What is the formula for water?',
    'What is the Pythagorean theorem?',
    'What is the atomic number of oxygen?',
    'What is the boiling point of water?',
    'What is the difference between a bird and a bat?',
    'What is the circumference of a circle?',
    'What is the meaning of the word 'ubiquitous'?',
    'What is the capital of Japan?',
    'What is the height of Mount Everest?',
    'What is the chemical formula for glucose?',
    'What is the distance from the Earth to the Moon?',
    'What is the temperature at which water freezes?',
    'What is the largest ocean on Earth?',
    'What is the difference between a herbivore and a carnivore?'
]

# Function to send a request to OpenAI API
def send_request(prompt):
    # Construct the request payload
    payload = {
        'prompt': prompt,
        'temperature': 0.5,
        'max_tokens': 50
    }
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {OPENAI_API_KEY}'
    }
    # Send the request to the OpenAI API endpoint
    response = requests.post(f'{OPENAI_API_ENDPOINT}/completions', json=payload, headers=headers)
    # Print the response text
    print(response.text)

# Function to send a batch of requests to OpenAI API
def send_batch_requests(prompts):
    # Create a list of threads to send each request in parallel
    threads = []
    for i in range(0, len(prompts), 5):
        batch_prompts = prompts[i:i+5]
        for prompt in batch_prompts:
            thread = threading.Thread(target=send_request, args=(prompt,))
            threads.append(thread)
    # Start all threads
    for thread in threads:
        thread.start()
    # Wait for all threads to finish
    for thread in threads:
        thread.join()

# Send batch of requests
send_batch_requests(PROMPTS)

在这个例子中,我们首先定义了 OpenAI API 的端点和密钥,以及一组要发送给 OpenAI 的提示。然后,我们定义了一个名为“send_request”的函数,该函数将单个提示作为参数,构造一个包含提示、温度和最大令牌数的请求有效负载,并将请求发送到 OpenAI API。最后,我们定义了一个名为“send_batch_requests”的函数,该函数将提示列表作为参数,将它们分成 5 个一组,然后创建一个线程来发送每个请求,并将这些线程放入一个列表中。然后,我们启动所有线程,并等待它们全部完成。最后,我们调用“send_batch_requests”函数,将提示列表作为参数来发送一批请求。

Python 多线程批量请求 OpenAI 接口,高效处理大量内容

原文地址: https://www.cveoy.top/t/topic/ndZe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录