Advertisement
rentacow

simple_chatbot

Feb 1st, 2023 (edited)
101
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | Software | 0 0
  1. import os
  2. import openai
  3.  
  4. # This script was first posted to pastebin at https://pastebin.com/UzN11MKx
  5. # You can find a detailed description of this script in the comments below.
  6. # Rev. 2.5.2
  7.  
  8. # Set the OpenAI API key to the value of the `OPENAI_API_KEY` environment variable
  9. openai.api_key = os.getenv("OPENAI_API_KEY")
  10.  
  11. # Define the starting sequence of the conversation
  12. start_sequence = "\nAI:"
  13. restart_sequence = "\nHuman: "
  14.  
  15. # Initialize the previous response to a pre-defined conversation start sequence
  16. previous_response = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by Scott on February 2nd at 05:56z. How can I help you today?"
  17.  
  18. # Set the maximum length of the prompt (conversation history) in characters
  19. max_prompt_length = 2750
  20.  
  21. # Start an infinite loop to continuously ask for user input and generate responses
  22. while True:
  23.     # Get the user's input
  24.     user_input = input("Human: ")
  25.     # Concatenate the previous response, the user's input, and the AI's response prompt
  26.     prompt = previous_response + restart_sequence + user_input + start_sequence
  27.  
  28.     # Check if the length of the prompt exceeds the maximum allowed length
  29.     if len(prompt) > max_prompt_length:
  30.         # If it does, truncate the previous response to meet the length requirement
  31.         previous_response = previous_response[-max_prompt_length:]
  32.  
  33.     # Send the prompt to OpenAI API for response generation
  34.     response = openai.Completion.create(
  35.         model="text-davinci-003",
  36.         prompt=prompt,
  37.         max_tokens=150,
  38.         temperature=0.7,
  39.         top_p=1,
  40.         frequency_penalty=0,
  41.         presence_penalty=0,
  42.         stop=[" Human:", " AI:"]
  43.     )
  44.  
  45.     # Get the response text from the API response
  46.     response_text = response["choices"][0]["text"].strip()
  47.     # Update the previous response to include the new conversation history
  48.     previous_response = prompt + response_text
  49.     # Print the response text
  50.     print(f"Chatbot: {response_text}")
  51.  
Advertisement
Comments
  • rentacow
    1 year (edited)
    Comment was deleted
  • rentacow
    1 year
    # text 2.51 KB | 0 0
    1. This code is a simple AI chatbot program that allows a user to interact with a text-generating AI. The code performs the following steps:
    2.  
    3. Imports the necessary modules: the os module, which allows access to the underlying operating system, and the openai module, which provides access to OpenAI's API.
    4.  
    5. Sets the OpenAI API key using the openai.api_key method and getting the API key from the environment variables using os.getenv("OPENAI_API_KEY").
    6.  
    7. Defines two strings, start_sequence and restart_sequence, to be used in the chatbot's responses later in the code.
    8.  
    9. Defines the string previous_response which is used to store the previous conversation between the user and the AI.
    10.  
    11. Sets the max_prompt_length variable to 6000, which sets the maximum length of the completion request, or the length of the chat history the AI will use to generate its responses.
    12.  
    13. Enters into an infinite loop using the while loop, which allows the chatbot to continue to respond to the user's inputs until the program is stopped.
    14.  
    15. Within the while loop, the code prompts the user for input using the input function, which stores the user's input in the user_input variable.
    16.  
    17. The prompt variable is then set to the previous conversation plus the current user input, separated by the restart_sequence string.
    18.  
    19. The code then checks if the length of the prompt exceeds the max_prompt_length by using an if statement and the len function. If the length of the prompt does exceed max_prompt_length, the previous conversation is truncated to meet the length requirement.
    20.  
    21. The code uses the openai.Completion.create method to generate a response from the AI, passing several parameters such as the model name, the prompt string, the maximum number of tokens (150), the temperature (0.7), and penalty values (0). The returned response is stored in the response variable.
    22.  
    23. The code then extracts the response text from the response variable and stores it in the response_text variable.
    24.  
    25. The previous conversation is updated to include the new response by concatenating the prompt, response_text, and start_sequence strings.
    26.  
    27. Finally, the code prints the AI's response using the print function and the f-string format, which allows the code to include variables within the string.
    28.  
    29. This code uses OpenAI's API to generate text responses in real-time, allowing the user to engage in a conversation with the AI. The program checks the length of the conversation history to ensure it does not exceed the max_prompt_length, and truncates the conversation history if necessary.
  • rentacow
    1 year
    # text 1.67 KB | 0 0
    1. Interacting directly with the GPT-3 API using code and using ChatGPT are two different ways of utilizing the powerful language model developed by OpenAI. Both methods have their unique advantages and disadvantages, which are worth exploring in detail.
    2.  
    3. When interacting directly with the GPT-3 API, the user has full control over the model's parameters and can customize the input and output as desired. This can be especially useful for developers who are looking to integrate the model into a larger application or build a more complex system. In addition, interacting directly with the API allows for a more streamlined interaction with the model, as the user can directly input and receive text from the model.
    4.  
    5. On the other hand, using ChatGPT eliminates the need for any code and provides a simpler interface for users who are not familiar with programming. This can be a great option for users who are simply looking to have a conversation with the model and don't require any additional customization or control over the model's parameters. Furthermore, using ChatGPT can also help to reduce the risk of errors in the code, as there is no need to worry about syntax or any other technical issues that may arise when working directly with the API.
    6.  
    7. In conclusion, the choice between interacting directly with the GPT-3 API and using ChatGPT will largely depend on the specific needs and skills of the user. For developers looking to build more complex systems, interacting directly with the API will likely be the preferred option. On the other hand, for users who are simply looking to have a conversation with the model, using ChatGPT will likely be the easier and more user-friendly option.
Add Comment
Please, Sign In to add comment
Advertisement