Advertisement
jarekmor

opis_produktów_z_csv

Dec 9th, 2023
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. # pip install openai
  2. # pip install python-dotenv
  3.  
  4. # .env file should contain OPENAI_API_KEY
  5. # przykład .env: OPENAI_API_KEY=sk-1234567890
  6.  
  7. import csv
  8. from openai import OpenAI
  9.  
  10. from dotenv import load_dotenv
  11. load_dotenv()
  12.  
  13. client = OpenAI()
  14.  
  15. def generate_description(model, producent, main_features, description):
  16.     prompt = f"Model: {model}\nProducent: {producent}\nMain Features: {main_features}\nDescription: {description}\n\nGenerate a detailed product description:"
  17.     response = client.chat.completions.create(
  18.         model="gpt-3.5-turbo-1106",
  19.         messages=[
  20.         {"role": "system", "content": "You are a helpful assistant designed to generate product descriptions."},
  21.         {"role": "user", "content": f"Write a detailed product description based on the following keyword:{prompt}."}
  22.     ],
  23.     max_tokens=150,
  24.  
  25.     )
  26.     return response.choices[0].message.content.strip()
  27.  
  28. def read_csv_and_generate_descriptions(csv_file_path):
  29.    
  30.     with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
  31.         reader = csv.DictReader(csvfile)
  32.         for row in reader:
  33.             generated_description = generate_description(row['model'], row['producent'], row['main features'], row['description'])
  34.             print(f"Generated Description for {row['model']}:\n{generated_description}\n")
  35.  
  36. # Usage
  37. csv_file_path = '/home/jarekmor/python_projects/OpenAI_API/sample.csv'
  38. read_csv_and_generate_descriptions(csv_file_path)
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement