Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: xml component working properly #3822

Merged
merged 6 commits into from
Sep 18, 2024
Merged

fix: xml component working properly #3822

merged 6 commits into from
Sep 18, 2024

Conversation

Cristhianzl
Copy link
Member

📝 (XMLAgent.py): Add support for chat history data input in XMLAgentComponent to enhance agent functionality and interaction with Language Model.

…omponent to enhance agent functionality and interaction with Language Model.
@Cristhianzl Cristhianzl self-assigned this Sep 16, 2024
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request python Pull requests that update Python code labels Sep 16, 2024
@github-actions github-actions bot added bug Something isn't working and removed enhancement New feature or request labels Sep 16, 2024
Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-3822.dmtpw4p5recq1.amplifyapp.com

Comment on lines 22 to 33
value="""You are a helpful assistant. Help the user answer any questions.
You have access to the following tools:

{tools}

In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>

For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:

<tool>search</tool><tool_input>weather in SF</tool_input>

<observation>64 degrees</observation>

When you are done, respond with a final answer between <final_answer></final_answer>. For example:

<final_answer>The weather in SF is 64 degrees</final_answer>

Begin!

Question: {input}

{agent_scratchpad}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the prompt shouldn't be necessary and is probably not a good idea.

Copy link
Member Author

@Cristhianzl Cristhianzl Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogabrielluiz Actually, I will just remove the first "enter" space to improve the UX.

image

src/backend/base/langflow/components/agents/XMLAgent.py Outdated Show resolved Hide resolved
…ontent for better clarity and consistency

🐛 (XMLAgent.py): Ensure user_prompt contains 'input' key before creating agent runnable to prevent errors
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Sep 16, 2024
@zcasanova
Copy link

I updated it to work for me. Only thing I"m uncertain of is the chat history:

from langchain.agents import create_xml_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput
from langflow.inputs.inputs import HandleInput


class XMLAgentComponent(LCToolsAgentComponent):
    display_name: str = "XML Agent"
    description: str = "Agent that uses tools formatting instructions as xml to the Language Model."
    icon = "LangChain"
    beta = True
    name = "XMLAgent"

    inputs = LCToolsAgentComponent._base_inputs + [
        HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
        MultilineInput(
            name="chat_history",
            display_name="Chat History",
            value=""),
        MultilineInput(
            name="user_prompt",
            display_name="Prompt",
            value="""
You are a helpful assistant. Help the user answer any questions.

You have access to the following tools:

{tools}

In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>

For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:

<tool>search</tool><tool_input>weather in SF</tool_input>

<observation>64 degrees</observation>

When you are done, respond with a final answer between <final_answer></final_answer>. For example:

<final_answer>The weather in SF is 64 degrees</final_answer>

 Begin!

Previous Conversation:
{chat_history}

Question: {input}
{agent_scratchpad}
            """,
        ),
    ]

    def create_agent_runnable(self):
        messages = [
            ("placeholder", "{chat_history}"),
            HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
            ("ai", "{agent_scratchpad}"),
        ]
        prompt = ChatPromptTemplate.from_messages(messages)
        return create_xml_agent(self.llm, self.tools, prompt)

@Cristhianzl
Copy link
Member Author

I updated it to work for me. Only thing I"m uncertain of is the chat history:

from langchain.agents import create_xml_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput
from langflow.inputs.inputs import HandleInput


class XMLAgentComponent(LCToolsAgentComponent):
    display_name: str = "XML Agent"
    description: str = "Agent that uses tools formatting instructions as xml to the Language Model."
    icon = "LangChain"
    beta = True
    name = "XMLAgent"

    inputs = LCToolsAgentComponent._base_inputs + [
        HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
        MultilineInput(
            name="chat_history",
            display_name="Chat History",
            value=""),
        MultilineInput(
            name="user_prompt",
            display_name="Prompt",
            value="""
You are a helpful assistant. Help the user answer any questions.

You have access to the following tools:

{tools}

In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>

For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:

<tool>search</tool><tool_input>weather in SF</tool_input>

<observation>64 degrees</observation>

When you are done, respond with a final answer between <final_answer></final_answer>. For example:

<final_answer>The weather in SF is 64 degrees</final_answer>

 Begin!

Previous Conversation:
{chat_history}

Question: {input}
{agent_scratchpad}
            """,
        ),
    ]

    def create_agent_runnable(self):
        messages = [
            ("placeholder", "{chat_history}"),
            HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
            ("ai", "{agent_scratchpad}"),
        ]
        prompt = ChatPromptTemplate.from_messages(messages)
        return create_xml_agent(self.llm, self.tools, prompt)

hi @zcasanova
the chat history takes on count olders chat conversation in that session..
works like a memory.. but you have to add the chat history component on the flow

@anovazzi1 anovazzi1 self-requested a review September 18, 2024 13:04
Copy link
Contributor

@anovazzi1 anovazzi1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approve for test running
requested by @Cristhianzl

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Sep 18, 2024
@ogabrielluiz ogabrielluiz merged commit 53c99e9 into main Sep 18, 2024
35 of 36 checks passed
@ogabrielluiz ogabrielluiz deleted the cz/fixXML branch September 18, 2024 14:57
diogocabral pushed a commit to headlinevc/langflow that referenced this pull request Nov 26, 2024
* 📝 (XMLAgent.py): Add support for chat history data input in XMLAgentComponent to enhance agent functionality and interaction with Language Model.

* [autofix.ci] apply automated fixes

* 📝 (XMLAgent.py): Update user_prompt to system_prompt and adjust its content for better clarity and consistency
🐛 (XMLAgent.py): Ensure user_prompt contains 'input' key before creating agent runnable to prevent errors

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lgtm This PR has been approved by a maintainer python Pull requests that update Python code size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants