DEV Community

Cover image for Create a Simple ChatBot with Mesop + Ollama less than 25 lines
0xkoji
0xkoji

Posted on • Edited on

7

Create a Simple ChatBot with Mesop + Ollama less than 25 lines

In this post, I will show you how to create a simple chatbot with Mesop and Ollama.

What is Mesop?

https://google.github.io/mesop/
Quickly build web UIs in Python
Used at Google for rapid internal app development

Mesop is like Gradio or Streamlit.

Step0 Install Ollama

You can download Ollama from the following link.
https://ollama.com/download

Step1 Install dependencies

pip install mesop ollama
Enter fullscreen mode Exit fullscreen mode

Step2 Write a chatbot

app.py

import ollama
import mesop as me
import mesop.labs as mel

@me.page(
    path="/",
    title="Mesop ChatBot",
)
def page():
    mel.chat(transform, title="Ollama ChatBot with Mesop", bot_user="Mesop Bot")

def transform(input: str, history: list[mel.ChatMessage]):
    messages = [{"role": "user", "content": message.content} for message in history]
    messages.append({"role": "user", "content": input})

    stream = ollama.chat(model='llama3', messages=messages, stream=True)

    for chunk in stream:
        content = chunk.get('message', {}).get('content', '')
        if content:
            yield content
Enter fullscreen mode Exit fullscreen mode

Step3 Run Chatbot

mesop app.py
Enter fullscreen mode Exit fullscreen mode

chatbot

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay