#๐Ÿ”’ Error in Ollama when using "{", "}" in my prompt

4 messages ยท Page 1 of 1 (latest)

vernal frost
#
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 add_to_history(self, role, message):
        self.history.append((role, message))
        print(f"Added to history for role {role}")

    def clear_history(self):
        self.history = [self.history[0]] if "system" in self.history[0] else []
        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")
    bot.add_to_history("user", "{Raining,sunny}")
    bot.add_to_history("assistant", "Hello!")
    response = bot.prompt("Hello!")
    print(response)

Gives an error:

KeyError: "Input to ChatPromptTemplate is missing variables {'Raining,sunny'}.  Expected: ['Raining,sunny', 'input'] Received: ['input']"

However, the bot works if I substitute main by this:

if __name__ == "__main__":

    # Test the Bot class

    bot = Bot("phi3")
    bot.add_to_history("user", "Hello!")
    bot.add_to_history("assistant", "Hello!")
    response = bot.prompt("Hello!")
    print(response)
rapid loomBOT
#

@vernal frost

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.

rapid loomBOT
#

@vernal frost

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.