#๐Ÿ”’ First local LLM - suggestions?

26 messages ยท Page 1 of 1 (latest)

meager crest
#

Hello! I am experimenting with LLMs that can run locally and I coded this Bot. I was just wondering if this is good/clean or if there's any improvement I could do. It seems to be working correctly so idk. Am I missing something? Thank you!

from langchain_community.llms import Ollama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


class Bot:
    def __init__(self, model="phi3", system=None):
        self.llm = Ollama(model=model)
        self.history = [("system", system)] if system else []
        print("Bot created")

    def addToHistory(self, role, message):
        self.history.append((role, message))
        print(f"Added to history: ({role}: {message})")

    def removeFromHistory(self, index):
        self.history.pop(index)
        print(f"Removed from history index {index}")

    def clearHistory(self):
        self.history = []
        print("History cleared")

    def prompt(self, prompt):
        output_parser = StrOutputParser()

        messages = self.history.copy()
        messages.append(("user", "{input}"))
        template = ChatPromptTemplate.from_messages(messages)

        chain = template | self.llm | output_parser
        output = chain.invoke({"input": prompt })

        print(f"Prompted and got response")

        return output.strip()


if __name__ == "__main__":

    # Test the Bot class

    bot = Bot("phi3", "You are a clown.")
    bot.addToHistory("user", "Please use bananas in your responses.")
    bot.addToHistory("assistant", "Ok! I will mention bananas in the next response.")
    response = bot.prompt("Hello! Create one-sentence story.")
    print(response)```
somber vigilBOT
#

@meager crest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tawdry dragon
#

removeFromHistory does not sounds very useful
clearHistory ignores the system prompt if you passed one when creating the bot
python style guidelines almost always recommend for you to use snake_case like remove_from_history instead of removeFromHistory

not sure about the prompt() method itself though, I haven't really used LangChain nor phi3 myself

meager crest
#

let me fix clearHistory

#

also why would i use snake case

#

@tawdry dragon


    def clear_history(self):
        self.history = [self.history[0]]
        print("History cleared")

should work

tawdry dragon
somber vigilBOT
#
PEP 8

PEP 8 is the official style guide for Python. It includes comprehensive guidelines for code formatting, variable naming, and making your code easy to read. Professional Python developers are usually required to follow the guidelines, and will often use code-linters like flake8 to verify that the code they're writing complies with the style guide.

More information:

tawdry dragon
meager crest
meager crest
#
    def clear_history(self):
        self.history = [self.history[0]] if "system" in self.history[0] else []
        print("History cleared")
#

ez

tawdry dragon
tawdry dragon
meager crest
#

you just add user and assistant

tawdry dragon
#

there are a bunch of workflows that rely on multiple assistents interacting with each other (each having their own system prompt), but you may as well use different Bot instances if you want to replicate that

meager crest
meager crest
#

thank you!

#

and if someone else could check the prompt part! thank u

tawdry dragon
somber vigilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.