-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchatbot.py
More file actions
28 lines (21 loc) · 849 Bytes
/
chatbot.py
File metadata and controls
28 lines (21 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from langchain.chains.llm import LLMChain
from langchain_openai import OpenAI
from langchain.prompts import ChatPromptTemplate
# Define the prompt template
prompt_template = ChatPromptTemplate.from_template("You said: {user_input}. Here's a response:")
# Create an instance of the OpenAI LLM
llm = OpenAI()
# Create a chatbot chain
chatbot_chain = LLMChain(llm=llm, prompt=prompt_template)
# Define a function to handle user input
def handle_user_input(user_input):
# Create a chat prompt
chat_prompt = prompt_template.format_prompt(user_input=user_input).to_messages()
# Generate a response using the chatbot chain
response = chatbot_chain.invoke(chat_prompt)
# Return the response
return response['text']
# Test the chatbot
user_input = "Hello, how are you?"
response = handle_user_input(user_input)
print(response)