DEV Community

artydev
artydev

Posted on • Edited on

1

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 = []

prompt_template = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are an AI named Mike, you answer questions with simple answers and no funny stuff.",
        ),
        MessagesPlaceholder(variable_name="chat_history"),
        ("human", "{input}"),
    ]
)

chain = prompt_template | llm


def start_app():
    while True:
        question = input("You: ")
        if question == "done":
            return

        # response = llm.invoke(question)
        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__":
    start_app()

Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more