Skip to content

Binder

%load_ext autoreload
%autoreload 2

LLaMaBot's SimpleBot in under 5 minutes

Let's say we have the text of a blog...

with open("../../data/blog_text.txt", "r+") as f:
    blog_text = f.read()
blog_text[0:100] + "..."

And we'd like to create a function that takes in the text and gives us a draft LinkedIn post, complete with emojis, that is designed to entice others to read the blog post. LLaMaBot's SimpleBot lets us build that function easily.

from llamabot import SimpleBot

system_prompt = """You are a LinkedIn post generator bot.
A human will give you the text of a blog post that they've authored,
and you will compose a LinkedIn post that advertises it.
The post is intended to hook a reader into reading the blog post.
The LinkedIn post should be written with one line per sentence.
Each sentence should begin with an emoji appropriate to that sentence.
The post should be written in professional English and in first-person tone for the human.
"""

linkedin = SimpleBot(
    system_prompt=system_prompt,
    stream_target="stdout",  # this is the default!,
    model_name="gpt-4-0125-preview",
)

Note that SimpleBot by default will always stream. All that you need to configure is where you want to stream to.

With linkedin, we can now pass in the blog text and - voila! - get back a draft LinkedIn post.

linkedin_post = linkedin(blog_text)

Now, you can edit it to your hearts content! :-)

Next up, we have streaming that is compatible with Panel's Chat interface, which expects the text to be returned in its entirety as it is being built up.

linkedin_panel = SimpleBot(
    system_prompt=system_prompt,
    stream_target="panel",
)
linkedin_post = linkedin_panel(blog_text)
for post in linkedin_post:
    print(post)

And finally, we have streaming via the API. We return a generator that yields individual parts of text as they are being generated.

linkedin_api = SimpleBot(
    system_prompt=system_prompt,
    stream_target="api",
)

linkedin_post = linkedin_api(blog_text)
for post in linkedin_post:
    print(post, end="")

If you have an Ollama server running, you can hit the API using SimpleBot. The pre-requisite is that you have already run ollama pull <modelname> to download the model to the Ollama server.

print(system_prompt)
import os
from dotenv import load_dotenv

load_dotenv()

linkedin_ollama = SimpleBot(
    model_name="ollama/mistral",  # Specifying Ollama via the model_name argument is necessary!s
    system_prompt=system_prompt,
    stream_target="stdout",  # this is the default!
    api_base=f"http://{os.getenv('OLLAMA_SERVER')}:11434",
)
linkedin_post = linkedin_ollama(blog_text)