Untitled

 avatar
unknown
python
2 years ago
981 B
23
Indexable
import openai

# Set up your OpenAI API credentials
openai.api_key = 'YOUR_API_KEY'

# Define a function for generating a response from ChatGPT
def generate_response(prompt):
    response = openai.Completion.create(
        engine='text-davinci-003',  # Use the GPT-3.5 engine
        prompt=prompt,
        max_tokens=50,  # Define the maximum length of the generated response
        temperature=0.7,  # Control the randomness of the generated response
        n=1,  # Generate a single response
        stop=None  # Allow the model to generate a full response without a specific stopping token
    )
    return response.choices[0].text.strip()

# Main loop for the chatbot
while True:
    user_input = input("User: ")

    # Exit the loop if the user enters 'quit'
    if user_input.lower() == 'quit':
        break

    # Generate a response from ChatGPT based on the user's input
    chatbot_response = generate_response(user_input)

    print("ChatGPT: " + chatbot_response)