| from transformers import pipeline, Conversation | |
| def chat_with_bot(message, conversation_history=None): | |
| """ | |
| Has a conversation with a chatbot using a pretrained model. | |
| """ | |
| chatbot_pipeline = pipeline("conversational") | |
| if conversation_history: | |
| conversation = Conversation(text=message, past_user_inputs=conversation_history['past_user_inputs'], generated_responses=conversation_history['generated_responses']) | |
| else: | |
| conversation = Conversation(text=message) | |
| result = chatbot_pipeline(conversation) | |
| return result.generated_responses[-1], {'past_user_inputs': result.past_user_inputs, 'generated_responses': result.generated_responses} | |
| if __name__ == '__main__': | |
| user_message = "Hello, how are you?" | |
| response, history = chat_with_bot(user_message) | |
| print("Bot:", response) | |
| user_message = "What is the weather like today?" | |
| response, history = chat_with_bot(user_message, conversation_history=history) | |
| print("Bot:", response) | |