#๐Ÿ”’ Escaping string issue

15 messages ยท Page 1 of 1 (latest)

warm ruin
#

I have a string taken from a PDF, which could have some jumbled inside:

znyr}~twprt}ryot}sn}{snyr}y{nstywnprtyty}nrynn}s{prypnpsryt}tyrnpsnynyot}~rn}rprynyn

and it's giving me this error of course:

ValueError: Single '}' encountered in format string
Single '}' encountered in format string

using re.escape(text) is not helping.
Are there other workarounds I can try?

leaden pulsarBOT
#

@warm ruin

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.

rose swift
#

can you send your code?

warm ruin
#
class Workflow(BaseModel, ABC):
    metadata: ClassVar[WorkflowMetadata] = WorkflowMetadata()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        Workflow.metadata.register(cls)

    def __call__(self) -> "Workflow":
        return self

    @property
    @abstractmethod
    def chat_interface(self) -> BaseChatModel:
        raise NotImplementedError("Subclasses must implement the 'chat_interface' property")

    def create_answer_chain(self, context_prompt: str):
        prompt = ChatPromptTemplate.from_messages([
            ("system", re.escape(context_prompt)),
            ("human", "{input}")
        ])
        return prompt | self.chat_interface

    def execute_answer_chain(
        self,
        question: str,
        context_prompt: str,
        config: Optional[RunnableConfig] = None
    ) -> str:
        print(question, context_prompt)
        answer_chain = self.create_answer_chain(context_prompt)
        return str(answer_chain.invoke({"input": question}, config).content)

rose swift
#

and your complete error please

warm ruin
rose swift
#

oh wow

warm ruin
#

yeah...

#

ultimately it's just the string being reparsed internally by langchain, so I'm trying to figure out a way to workaround this issue from the initial argument

#

The easiest fix is to:

("system", re.escape(context_prompt).replace("}", "").replace("{", "")),

But I don't want to compromise on data integrity

warm ruin
# warm ruin ultimately it's just the string being reparsed internally by langchain, so I'm t...

I re read what I wrote and figured it out... today is just one of those days i guess...

    def create_answer_chain(self):
        prompt = ChatPromptTemplate.from_messages([
            ("system", "{system_prompt}"),
            ("human", "{input}")
        ])
        return prompt | self.chat_interface

    def execute_answer_chain(
        self,
        question: str,
        context_prompt: str,
        config: Optional[RunnableConfig] = None
    ) -> str:
        print(question, context_prompt)
        answer_chain = self.create_answer_chain()
        return str(answer_chain.invoke({
            "system_prompt": context_prompt,
            "input": question
        }, config).content)
#

If it's being re parsed internally because the from_message expects a format string, then just pass the format string instead of the string variable

#

!close

leaden pulsarBOT
#
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.