quiz3 / q4
rashid01's picture
Create q4
726663f verified
import openai
def chatbot(api_key, model="text-davinci-003", max_tokens=150, temperature=0.5):
"""
Simulate a simple chatbot interaction using LangChain (GPT-3 model).
Parameters:
- api_key (str): Your OpenAI API key.
- model (str): The GPT-3 model to use. Defaults to 'text-davinci-003'.
- max_tokens (int): Maximum number of tokens (words) in the generated response. Defaults to 150.
- temperature (float): Controls randomness in text generation. Higher values make the output more diverse. Defaults to 0.5.
"""
# Set the OpenAI API key
openai.api_key = api_key
print("Welcome! Ask me anything or type 'exit' to end the conversation.")
while True:
# Get user input
user_input = input("You: ")
# Check for termination command
if user_input.lower() == 'exit':
print("Chatbot: Goodbye!")
break
# Generate response using LangChain
response = openai.Completion.create(
engine=model,
prompt=user_input,
max_tokens=max_tokens,
temperature=temperature
)
# Extract and print the generated response
chatbot_response = response['choices'][0]['text'].strip()
print("Chatbot:", chatbot_response)
# Example usage:
def main():
# Replace 'your_openai_api_key_here' with your actual API key
api_key = 'your_openai_api_key_here'
# Start the chatbot
chatbot(api_key)
if __name__ == "__main__":
main()