Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from openai import OpenAI
- # Initialize the client with your API key
- client = OpenAI(api_key='KEYHERE')
- # Function to query the model and write the response to a file
- def query_chatgpt_and_save(prompt):
- # Making the API call using the client instance
- response = client.completions.create(
- model="gpt-3.5-turbo-instruct", # Replace with your desired model
- prompt=prompt,
- temperature=0.5,
- max_tokens=200
- )
- # Extracting the text part of the response
- response_text = response.choices[0].text.strip()
- # Writing the response to a file
- with open('ai_response.txt', 'w') as file:
- file.write(response_text)
- # Example usage
- prompt_text = "write a 5 sentence synopsis of the book Mary Had a Little Lamb"
- query_chatgpt_and_save(prompt_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement