Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pip install openai
- # pip install python-dotenv
- # .env file should contain OPENAI_API_KEY
- # przykład .env: OPENAI_API_KEY=sk-1234567890
- import csv
- from openai import OpenAI
- from dotenv import load_dotenv
- load_dotenv()
- client = OpenAI()
- def generate_description(model, producent, main_features, description):
- prompt = f"Model: {model}\nProducent: {producent}\nMain Features: {main_features}\nDescription: {description}\n\nGenerate a detailed product description:"
- response = client.chat.completions.create(
- model="gpt-3.5-turbo-1106",
- messages=[
- {"role": "system", "content": "You are a helpful assistant designed to generate product descriptions."},
- {"role": "user", "content": f"Write a detailed product description based on the following keyword:{prompt}."}
- ],
- max_tokens=150,
- )
- return response.choices[0].message.content.strip()
- def read_csv_and_generate_descriptions(csv_file_path):
- with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
- reader = csv.DictReader(csvfile)
- for row in reader:
- generated_description = generate_description(row['model'], row['producent'], row['main features'], row['description'])
- print(f"Generated Description for {row['model']}:\n{generated_description}\n")
- # Usage
- csv_file_path = '/home/jarekmor/python_projects/OpenAI_API/sample.csv'
- read_csv_and_generate_descriptions(csv_file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement