Нужна помощь в сокращении затрат на OpenAI API [Python]: вот мой кодPython

Программы на Python
Anonymous
Нужна помощь в сокращении затрат на OpenAI API [Python]: вот мой код

Сообщение Anonymous »

Затем я вызываю примерно 500 раз функцию get_category_and_subcategory. Это стоило мне около 60 долларов, что слишком много для такого типа работы. Я делаю что-то не так и что можно улучшить?
Похоже, моя проблема в том, что категория_и_подкатегория огромна, что, похоже, приводит к самым большим затратам, токенам контекста. Я не знаю, как уменьшить эти затраты.
def get_chatgpt_response(prompt):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0.1, # Adjust the temperature for creativity
top_p=0.1,
response_format={"type": "json_object"}
)
return response.choices[0].message.content

def get_category_and_subcategory(title, description, prompt, category_and_subcategory):
product = {'title': title, 'description': description}
response = get_chatgpt_response(prompt.format(json.dumps(product, ensure_ascii=False),
json.dumps(category_and_subcategory, ensure_ascii=False)))
response = json.loads(response)
category, subcategory = response['category'], response['subcategory']
return category.strip(), subcategory.strip()

Вот моя подсказка
prompt = """
You are given product details and a dictionary of possible categories and subcategories. Your task is to categorize the product into the most appropriate category and subcategory based on these details.

Product:

{}

Choose the exact names of the Categories and Subcategories from the provided options:

{}

Ensure your response uses the exact names from the options provided. Format your response as JSON: `{{"category": "category", "subcategory": "subcategory"}}` and nothing else. Do not deviate from the provided options.
"""

title = "Sample Product"
description = "This is a sample product description."
category_and_subcategory = {
"Electronics": ["Mobile Phones", "Laptops"],
"Home Appliances": ["Refrigerators", "Washing Machines"]
}

category, subcategory = get_category_and_subcategory(title, description, category_and_subcategory)
print(f"Category: {category}, Subcategory: {subcategory}")

Вернуться в «Python»