DEV Community

artydev
artydev

Posted on

1

The smallest chatbot implementation using Langchain Ollama, and Python

Thanks to : Understand Ollama and LangChain Chat History in 10 minutes

from langchain_community.llms import Ollama
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.prompts import ChatPromptTemplate,  MessagesPlaceholder

llm = Ollama(model = "llama3")

chat_history = []

chat_history = [
    ("human","My name is John")
]

prompt_template = ChatPromptTemplate.from_messages([
    ("system","your name is HAL, greeet user and answer questions with simple responses"),
    MessagesPlaceholder(variable_name= "chat_history"),
    ("human","{input}")
])

chain = prompt_template | llm

def  main ():
    while True:
        question = input("You : ")
        if question == "done":
            return
        response = chain.invoke({"input" : question,  "chat_history": chat_history})
        chat_history.append(HumanMessage(content=question))
        chat_history.append(AIMessage(content=response))

        print("AI  : " + response)

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Example of discussion:

Image description

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay