Skip to content

Instantly share code, notes, and snippets.

@torebal
Forked from jayo78/simpleGPT3Chatbot.py
Created April 21, 2023 06:59
Show Gist options
  • Save torebal/2135b9ab5ed86fa92c222d907b818bbc to your computer and use it in GitHub Desktop.
Save torebal/2135b9ab5ed86fa92c222d907b818bbc to your computer and use it in GitHub Desktop.

Revisions

  1. @jayo78 jayo78 revised this gist Apr 8, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simpleGPT3Chatbot.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    chatbot_prompt = """
    As an advanced chatbot, your primary goal is to assist users to the best of your ability. This may involve answering questions, providing helpful information, or completing tasks based on user input. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions.
    <conversation history>
    <conversation_history>
    User: <user input>
    Chatbot:"""
  2. @jayo78 jayo78 created this gist Jan 8, 2023.
    41 changes: 41 additions & 0 deletions simpleGPT3Chatbot.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    import openai

    openai.api_key = "YOUR API KEY HERE"
    model_engine = "text-davinci-003"
    chatbot_prompt = """
    As an advanced chatbot, your primary goal is to assist users to the best of your ability. This may involve answering questions, providing helpful information, or completing tasks based on user input. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions.
    <conversation history>
    User: <user input>
    Chatbot:"""


    def get_response(conversation_history, user_input):
    prompt = chatbot_prompt.replace(
    "<conversation_history>", conversation_history).replace("<user input>", user_input)

    # Get the response from GPT-3
    response = openai.Completion.create(
    engine=model_engine, prompt=prompt, max_tokens=2048, n=1, stop=None, temperature=0.5)

    # Extract the response from the response object
    response_text = response["choices"][0]["text"]

    chatbot_response = response_text.strip()

    return chatbot_response


    def main():
    conversation_history = ""

    while True:
    user_input = input("> ")
    if user_input == "exit":
    break
    chatbot_response = get_response(conversation_history, user_input)
    print(f"Chatbot: {chatbot_response}")
    conversation_history += f"User: {user_input}\nChatbot: {chatbot_response}\n"

    main()