Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import openai
- import json
- import os
- def get_a_quote(): # Define a function to get a quote when called
- # Set the OpenAI API key to the value of the `OPENAI_API_KEY` environment variable
- openai.api_key = os.getenv("OPENAI_API_KEY")
- # Set up JSON and error log file path
- json_file_path = "quotes.json"
- error_log_path = "errors.log"
- # Define prompt for the GPT-3 completion
- prompt = "Give a few words of wisdom for a submarine captain, in the form of a quote: "
- # Generate a completion using the OpenAI API
- try:
- response = openai.Completion.create(
- model="text-davinci-003",
- prompt=prompt,
- max_tokens=50,
- temperature=0.8,
- top_p=1,
- frequency_penalty=0,
- presence_penalty=0,
- stop=[" -"]
- )
- except Exception as e: # Log the error if there's a problem
- logging.basicConfig(filename=error_log_path, level=logging.ERROR, format='%(asctime)s %(message)s')
- logging.error('An error occurred: %s', e)
- return f"An error occurred!!! {e}"
- # Add the raw API response to a JSON file
- if os.path.exists(json_file_path):
- with open(json_file_path, "r") as f:
- data = json.load(f)
- else:
- data = []
- data.append(response)
- with open(json_file_path, "w") as f:
- json.dump(data, f)
- # function returns a nicely formatted quote
- return response["choices"][0]["text"].strip()
- if __name__ == "__main__": # Run this code as a script by calling the function
- print(f'Quote of the Day: {get_a_quote()}')
- input('\nPress enter or return to quit.') # optional debugging interrupt
Advertisement
Comments
-
- Example "data.json" containing some raw API returns:
- [{
- "id": "cmpl-6sbAvN5XvypZxkLrBtMA9OMZAZhMW",
- "object": "text_completion",
- "created": 1678470633,
- "model": "text-davinci-003",
- "choices": [{
- "text": "\n\n\"Leadership is not a rank, but rather a responsibility to those who depend on you.\"",
- "index": 0,
- "logprobs": null,
- "finish_reason": "stop"
- }],
- "usage": {
- "prompt_tokens": 18,
- "completion_tokens": 21,
- "total_tokens": 39
- }
- }, {
- "id": "cmpl-6sbB0gqKrSCwXnuKHYY8revi8zgaR",
- "object": "text_completion",
- "created": 1678470638,
- "model": "text-davinci-003",
- "choices": [{
- "text": "\n\n\"It is better to be a captain while on the surface, than a passenger while beneath it\"",
- "index": 0,
- "logprobs": null,
- "finish_reason": "stop"
- }],
- "usage": {
- "prompt_tokens": 18,
- "completion_tokens": 22,
- "total_tokens": 40
- }
- }, {
- "id": "cmpl-6sbB4gbJig59ZalJLWUk2TPRTa6iW",
- "object": "text_completion",
- "created": 1678470642,
- "model": "text-davinci-003",
- "choices": [{
- "text": "\n\n\"A good captain is one who learns from the past, lives in the present and plans for the future.\"",
- "index": 0,
- "logprobs": null,
- "finish_reason": "stop"
- }],
- "usage": {
- "prompt_tokens": 18,
- "completion_tokens": 24,
- "total_tokens": 42
- }
- }, {
- "id": "cmpl-6sbB8gWq0QXtzJTVtHZ07alSmPFJm",
- "object": "text_completion",
- "created": 1678470646,
- "model": "text-davinci-003",
- "choices": [{
- "text": "\n\n\"Success comes from taking calculated risks, but never forgetting to stay vigilant and prepared for the unknown.\"",
- "index": 0,
- "logprobs": null,
- "finish_reason": "stop"
- }],
- "usage": {
- "prompt_tokens": 18,
- "completion_tokens": 22,
- "total_tokens": 40
- }
- }, {
- "id": "cmpl-6sbBBgQubc6xPHLy82IJJD5mFQXV6",
- "object": "text_completion",
- "created": 1678470649,
- "model": "text-davinci-003",
- "choices": [{
- "text": "\n\n\"The sea is a good master and it teaches the duty of obedience.\"",
- "index": 0,
- "logprobs": null,
- "finish_reason": "stop"
- }],
- "usage": {
- "prompt_tokens": 18,
- "completion_tokens": 17,
- "total_tokens": 35
- }
- }]
Add Comment
Please, Sign In to add comment
Advertisement