I then call like 500 times the get_category_and_subcategory
function.. It cost me like 60$ which is too much for this type of job. Am I doing something wrong and what could be improved?
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()
Here is my prompt
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.
"""