Mar 16, 2024

Chatbot is Reading Nostr Notes and Producing More Chat

Here is a proof of concept that needs lots of additional work. It connects to two relays and gets all the recent notes of kind=1. And generate a response to the note. It does not send it to Nostr network yet.

To use any model you have to do

ollama pull modelname

The best model for now is openchat. But it produces average responses (it's not that smart). Using models with 70B parameters, it should give more clever answers or do better small talk. But those will require coding the necessary prompt templates as far as I understand.

from nostr_sdk import Filter, Client, Keys, NostrSigner
import time
from datetime import timedelta
from colorist import rgb
import traceback
from threading import Thread
from langchain_community.chat_models import ChatOllama
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)
from urllib3.util.retry import MaxRetryError

# model_name = "mixtral:8x7b-instruct-v0.1-q2_K" # wrong prompt format
model_name = "openchat"  # correct prompt. bad answers.
# model_name = "gemma" # worst
# model_name = "qwen:14b"
# model_name = "qwen:72b"  # good, except summary

def ask_miqu2(q, messages, chat):
    # disc = """\n\nDisclaimer: I am a rogue chatbot and my training is not complete yet.
    # I am a demo, which have no value. I sometimes tell lies.
    # I sometimes make things up when actually I am unsure about an answer, lol.
    # Please disregard what I say.
    # Programmers of me are not responsible for anything that you do while chatting with me."""
    q = "HUMAN: " + q
    rgb(q, 247, 147, 26)
    messages.append(HumanMessage(q))
    response = chat.invoke(messages)
    rgb(response.content, 200, 30, 255)
    messages.append(AIMessage(response.content))


def user2():
    chat = ChatOllama(model=model_name, timeout=5)

    filter2 = Filter().kind(1)
    keys = Keys.generate()
    signer = NostrSigner.keys(keys)
    client = Client(signer)
    client.add_relays(["wss://e.nos.lol", "wss://nos.lol"])
    client.connect()
    events = client.get_events_of([filter2], timedelta(seconds=10))
    for event in events:
        q = event.content()
        if len(q) > 50:
            try:
                messages = [
                    SystemMessage(content='You are a friendly bot who chats with'
                                          ' people on Nostr network. Your name is MIQU2.'),
                    HumanMessage('HUMAN: Hi MIQU2. What a beautiful network this is!'),
                    AIMessage('MIQU2: Indeed, lots of freedom minded people!'),
                    HumanMessage('HUMAN: I am glad I joined Nostr. Plebs here are really friendly.'),
                    AIMessage('MIQU2: There are also friendly bots like me :)'),
                    HumanMessage('HUMAN: Of course! I like it a lot.'),
                    AIMessage('MIQU2: Welcome and enjoy. Let me know if you have questions.')]

                ask_miqu2(q, messages, chat)
            except MaxRetryError:
                print("can't connect to Ollama.")
            except ConnectionRefusedError:
                print("can't connect to Ollama.")
            except ConnectionError:
                print("can't connect to Ollama.")
            except:
                traceback.print_exc()


if __name__ == '__main__':
    Thread(target=user2).start()

Here is a sample output. HUMAN is a Nostr user. MIQU2 is the bot.

HUMAN: Le Figaro

Gymnastique : l'entraîneur Pop «ne comprend pas» sa suspension en raison d'accusations d'agressions sexuelles sur mineurs

Nellu Pop, un des entraîneurs de l’équipe de France de gym a fait savoir, ce samedi et via un communiqué d’un de ses avocats qu’il ne comprenait pas sa suspension.

www.lefigaro.fr/sports/autres-sports/gymnastique-l-entraineur-pop-ne-comprend-pas-sa-suspension-en-raison-d-accusations-d-agressions-sexuelles-sur-mineurs-20240316

#Figaro #France #Presse

MIQU2: Merci pour ce signalement. Il s'agit d'un article du Figaro concernant l'entraîneur Pop, qui a été suspendu à cause de soupçons d'actes sexuellement inappropriés sur mineurs. Pop a déclaré qu'il ne comprenait pas sa suspension. Voici le lien pour plus d'informations : www.lefigaro.fr/sports/autres-sports/gymnastique-l-entraineur-pop-ne-comprend-pas-sa-suspension-en-raison-d-accusations-d-agressions-sexuelles-sur-mineurs-20240316

HUMAN: it’s a spectrum

open source sometimes is needed, sometimes only giving users the license to view the code it and use it (but not redistribute to others), and sometimes keeping it closed source is fine

MIQU2: The choice between open-source, partially open-source, or closed-source software can depend on several factors. Open-source projects allow for greater transparency and collaboration, but also require more effort in managing the community. Closed-source projects may be easier to manage and maintain but limit the ability for users to contribute directly to the codebase.

Ultimately, it is up to developers and organizations to decide what approach best fits their needs and goals. It's great that there are different options available on Nostr and elsewhere!

HUMAN: It's not hard to get people to trust closed source software. It's actually the norm since all you need to do is tell a good story and not obviously rug people.

It's harder to get people to trust open source software because it doesn't provide a trustworthy narrative along with the code. No user is out there reading open source code checking for privacy leaks.

MIQU2: You make a valid point about the challenges of open source versus closed source software. Trust in both cases can be built through different means.

For closed source software, it often relies on marketing and reputation, while for open source software, trust is built through transparency and community engagement. It's important to recognize that both approaches have their pros and cons, and the choice between them depends on the specific needs and context of a project or user.

HUMAN: I write docs, too. I do everything. And I'm just a girl and we like to chat.

MIQU2: That's great! What kind of documents do you write?