发出 Azure AI API 请求,内容是一个大字典

问题描述 投票:0回答:1

我是新手,我需要发出如下例所示的请求,但我的问题是“my_dict”太大,我不断收到“400 Bad Request”错误,当我尝试拆分它并制作多次调用我收到“429 Too Many Requests”错误。另一个问题是,当我在较小的数据样本上测试它时,需要很长时间才能完成。

这是“my_dict”的结构,我使用 pandas 从 Excel 文件中获取它:

my_dict = {
    "key1": {
            0: "value1.1",
            1: "value2.1",
            ...
            1999: "value2000.1"
    },
    "key2": {
            0: "value1.2",
            1: "value2.2",
            ...
            1999: "value2000.2"
    },
    "key3": {
            0: "value1.3",
            1: "value2.3",
            ...
            1999: "value2000.3"
    },
    "key4": {
            0: "value1.4",
            1: "value2.4",
            ...
            1999: "value2000.4"
    },
}

我需要ai根据每个内部字典中的第一个项目返回一个值,然后根据第二个项目返回一个值,依此类推。

import requests

endpoint = 'https://resource-name.openai.azure.com/openai/deployments/deployment-id/chat/completions?api-version=2024-02-01'
api_key = '1q2w3e4r5t6y7u8i9o0p'

headers = {
    'Content-Type': 'application/json',
    'api-key': api_key
}

payload = {
    "messages": [
        {
            "role": "system",
            "content": "Prompt..."
                       f"Dictionary: {my_dict}"
        },
    ],
    "temperature": 0.7
}

response = requests.post(endpoint , headers=headers, json=payload)

print(response.json())

我将不胜感激任何帮助或建议。

python azure python-requests azure-openai azure-ai
1个回答
0
投票

我尝试使用下面的代码来管理一个大字典,方法是将其分成较小的块并向 OpenAI 的 GPT-4o 模型发出 API 请求。目标是根据索引的顺序(即索引 0、索引 1 和索引 1999)从每个内部字典返回值。

  • chunk_dict
    功能:根据指定的大小将大字典分割成更小的、可管理的块,以便于处理大型数据集。

  • process_large_dict
    功能:迭代每个块,将 POST 请求发送到 Azure OpenAI 端点。 API 提示指示模型按顺序处理并返回索引 0、1 和 1999 的值。

代码:

import requests
import time

endpoint = 'https://<openai_name>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview'
api_key = '<api_key>'

headers = {
    'Content-Type': 'application/json',
    'api-key': api_key
}

my_dict = {
    "key1": {
        0: "value1.1",
        1: "value2.1",
        # ...
        1999: "value2000.1"
    },
    "key2": {
        0: "value1.2",
        1: "value2.2",
        # ...
        1999: "value2000.2"
    },
    "key3": {
        0: "value1.3",
        1: "value2.3",
        # ...
        1999: "value2000.3"
    },
    "key4": {
        0: "value1.4",
        1: "value2.4",
        # ...
        1999: "value2000.4"
    },
}

def chunk_dict(d, chunk_size):
    """Split dictionary into smaller chunks."""
    items = list(d.items())
    for i in range(0, len(items), chunk_size):
        yield dict(items[i:i + chunk_size])

def make_request(chunk):
    """Make API request with chunk of data."""
    content = (
        f"Please process the following dictionary and return the values based on the order of their indices:"
        f"\n{chunk}\n\n"
        f"For each key, return the value for index 0 first, then for index 1, and finally for index 1999."
    )
    payload = {
        "messages": [
            {
                "role": "system",
                "content": content
            }
        ],
        "temperature": 0.7
    }
    try:
        response = requests.post(endpoint, headers=headers, json=payload)
        if response.status_code == 429:
            print("Rate limit exceeded. Waiting before retrying...")
            time.sleep(10)  
            return make_request(chunk) 
        elif response.status_code == 200:
            return response.json()
        else:
            print(f"Error: {response.status_code} - {response.text}")
            return None
    except requests.RequestException as e:
        print(f"Request exception: {e}")
        return None

def process_large_dict(my_dict, chunk_size):
    """Process the large dictionary in chunks."""
    chunks = chunk_dict(my_dict, chunk_size)
    results = []
    for chunk in chunks:
        result = make_request(chunk)
        if result:
            results.append(result)
        else:
            print("Failed to get response for chunk.")
    return results

chunk_size = 10 
results = process_large_dict(my_dict, chunk_size)
for result in results:
    print(result)

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.