Advertisement
rentacow

Quote of the Day

Mar 10th, 2023 (edited)
156
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | Writing | 0 0
  1. import logging
  2. import openai
  3. import json
  4. import os
  5.  
  6.  
  7. def get_a_quote():  # Define a function to get a quote when called
  8.  
  9.     # Set the OpenAI API key to the value of the `OPENAI_API_KEY` environment variable
  10.     openai.api_key = os.getenv("OPENAI_API_KEY")
  11.  
  12.     # Set up JSON and error log file path
  13.     json_file_path = "quotes.json"
  14.     error_log_path = "errors.log"
  15.  
  16.     # Define prompt for the GPT-3 completion
  17.     prompt = "Give a few words of wisdom for a submarine captain, in the form of a quote: "
  18.  
  19.     # Generate a completion using the OpenAI API
  20.     try:
  21.         response = openai.Completion.create(
  22.             model="text-davinci-003",
  23.             prompt=prompt,
  24.             max_tokens=50,
  25.             temperature=0.8,
  26.             top_p=1,
  27.             frequency_penalty=0,
  28.             presence_penalty=0,
  29.             stop=[" -"]
  30.         )
  31.     except Exception as e:  # Log the error if there's a problem
  32.         logging.basicConfig(filename=error_log_path, level=logging.ERROR, format='%(asctime)s %(message)s')
  33.         logging.error('An error occurred: %s', e)
  34.         return f"An error occurred!!! {e}"
  35.  
  36.     # Add the raw API response to a JSON file
  37.     if os.path.exists(json_file_path):
  38.         with open(json_file_path, "r") as f:
  39.             data = json.load(f)
  40.     else:
  41.         data = []
  42.     data.append(response)
  43.     with open(json_file_path, "w") as f:
  44.         json.dump(data, f)
  45.  
  46.     # function returns a nicely formatted quote
  47.     return response["choices"][0]["text"].strip()
  48.  
  49.  
  50. if __name__ == "__main__":  # Run this code as a script by calling the function
  51.     print(f'Quote of the Day: {get_a_quote()}')
  52.     input('\nPress enter or return to quit.')  # optional debugging interrupt
  53.  
Advertisement
Comments
  • rentacow
    1 year (edited)
    # JSON 2.13 KB | 0 0
    1. Example "data.json" containing some raw API returns:
    2.  
    3. [{
    4.     "id": "cmpl-6sbAvN5XvypZxkLrBtMA9OMZAZhMW",
    5.     "object": "text_completion",
    6.     "created": 1678470633,
    7.     "model": "text-davinci-003",
    8.     "choices": [{
    9.         "text": "\n\n\"Leadership is not a rank, but rather a responsibility to those who depend on you.\"",
    10.         "index": 0,
    11.         "logprobs": null,
    12.         "finish_reason": "stop"
    13.     }],
    14.     "usage": {
    15.         "prompt_tokens": 18,
    16.         "completion_tokens": 21,
    17.         "total_tokens": 39
    18.     }
    19. }, {
    20.     "id": "cmpl-6sbB0gqKrSCwXnuKHYY8revi8zgaR",
    21.     "object": "text_completion",
    22.     "created": 1678470638,
    23.     "model": "text-davinci-003",
    24.     "choices": [{
    25.         "text": "\n\n\"It is better to be a captain while on the surface, than a passenger while beneath it\"",
    26.         "index": 0,
    27.         "logprobs": null,
    28.         "finish_reason": "stop"
    29.     }],
    30.     "usage": {
    31.         "prompt_tokens": 18,
    32.         "completion_tokens": 22,
    33.         "total_tokens": 40
    34.     }
    35. }, {
    36.     "id": "cmpl-6sbB4gbJig59ZalJLWUk2TPRTa6iW",
    37.     "object": "text_completion",
    38.     "created": 1678470642,
    39.     "model": "text-davinci-003",
    40.     "choices": [{
    41.         "text": "\n\n\"A good captain is one who learns from the past, lives in the present and plans for the future.\"",
    42.         "index": 0,
    43.         "logprobs": null,
    44.         "finish_reason": "stop"
    45.     }],
    46.     "usage": {
    47.         "prompt_tokens": 18,
    48.         "completion_tokens": 24,
    49.         "total_tokens": 42
    50.     }
    51. }, {
    52.     "id": "cmpl-6sbB8gWq0QXtzJTVtHZ07alSmPFJm",
    53.     "object": "text_completion",
    54.     "created": 1678470646,
    55.     "model": "text-davinci-003",
    56.     "choices": [{
    57.         "text": "\n\n\"Success comes from taking calculated risks, but never forgetting to stay vigilant and prepared for the unknown.\"",
    58.         "index": 0,
    59.         "logprobs": null,
    60.         "finish_reason": "stop"
    61.     }],
    62.     "usage": {
    63.         "prompt_tokens": 18,
    64.         "completion_tokens": 22,
    65.         "total_tokens": 40
    66.     }
    67. }, {
    68.     "id": "cmpl-6sbBBgQubc6xPHLy82IJJD5mFQXV6",
    69.     "object": "text_completion",
    70.     "created": 1678470649,
    71.     "model": "text-davinci-003",
    72.     "choices": [{
    73.         "text": "\n\n\"The sea is a good master and it teaches the duty of obedience.\"",
    74.         "index": 0,
    75.         "logprobs": null,
    76.         "finish_reason": "stop"
    77.     }],
    78.     "usage": {
    79.         "prompt_tokens": 18,
    80.         "completion_tokens": 17,
    81.         "total_tokens": 35
    82.     }
    83. }]
Add Comment
Please, Sign In to add comment
Advertisement