1. create a handle_conversation() function
def handle_conversation():
context = ""
print("Welcome, local Ai-chatbot")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
break
result = chain.invoke({"context": context, "question": user_input})
print("ai-botie: ",result)
context += f"\nUser: {user_input}\nAI: {result}"
if __name__ == "__main__":
handle_conversation()
full script
from langchain_ollama import OllamaLLM
from langchain_core.prompts import ChatPromptTemplate
template = """
Answer the question below.
Here is the conversation history: {context}
Question: {question}
Answer
"""
model = OllamaLLM(model="llama3")
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | model
result = chain.invoke({"context":"","question":"Tu puedes hablar espanol?"})
def handle_conversation():
context = ""
print("Welcome, local ai-chatbot")
while True:
user_input = input("You: ")
if(user_input.lower() == "exit"):
break
result = chain.invoke({"context": context,"question":user_input})
print("ai-botie:", result)
context += f"\nUser: {user_input}\nAI: {result}"
if __name__ == "__main__":
handle_conversation()
I asked the following prompts, i think are useful
In the Python script below, where is the conversation context being stored? Is the context saved by Ollama or Llama 3,
or is it only stored in my Python variable? Please explain exactly what happens to the context during each call to
chain.invoke() and whether it persists after the program closes.