Advertisement
Guest User

Untitled

a guest
Apr 16th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from openai import OpenAI
  2.  
  3. # Initialize the client with your API key
  4. client = OpenAI(api_key='KEYHERE')
  5.  
  6. # Function to query the model and write the response to a file
  7. def query_chatgpt_and_save(prompt):
  8.     # Making the API call using the client instance
  9.     response = client.completions.create(
  10.         model="gpt-3.5-turbo-instruct",  # Replace with your desired model
  11.         prompt=prompt,
  12.         temperature=0.5,
  13.         max_tokens=200
  14.     )
  15.  
  16.     # Extracting the text part of the response
  17.     response_text = response.choices[0].text.strip()
  18.  
  19.     # Writing the response to a file
  20.     with open('ai_response.txt', 'w') as file:
  21.         file.write(response_text)
  22.  
  23. # Example usage
  24. prompt_text = "write a 5 sentence synopsis of the book Mary Had a Little Lamb"
  25. query_chatgpt_and_save(prompt_text)
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement