fix: xml component working properly (#3822)

* 📝 (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>
This commit is contained in:
Cristhian Zanforlin Lousa 2024-09-18 11:56:58 -03:00 • committed by GitHub
commit 53c99e9857
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,9 +1,10 @@
from typing import List, Optional
from langchain.agents import create_xml_agent from langchain.agents import create_xml_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate
from langflow.base.agents.agent import LCToolsAgentComponent from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput from langflow.inputs import MultilineInput
from langflow.inputs.inputs import HandleInput from langflow.inputs.inputs import DataInput, HandleInput
from langflow.schema import Data
class XMLAgentComponent(LCToolsAgentComponent): class XMLAgentComponent(LCToolsAgentComponent):
@ -12,14 +13,14 @@ class XMLAgentComponent(LCToolsAgentComponent):
icon = "LangChain" icon = "LangChain"
beta = True beta = True
name = "XMLAgent" name = "XMLAgent"
inputs = LCToolsAgentComponent._base_inputs + [ inputs = LCToolsAgentComponent._base_inputs + [
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True), HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
DataInput(name="chat_history", display_name="Chat History", is_list=True, advanced=True),
MultilineInput( MultilineInput(
name="user_prompt", name="system_prompt",
display_name="Prompt", display_name="System Prompt",
value=""" info="System prompt for the agent.",
You are a helpful assistant. Help the user answer any questions. value="""You are a helpful assistant. Help the user answer any questions.
You have access to the following tools: You have access to the following tools:
@ -44,11 +45,22 @@ Question: {input}
{agent_scratchpad} {agent_scratchpad}
""", """,
), ),
MultilineInput(
name="user_prompt", display_name="Prompt", info="This prompt must contain 'input' key.", value="{input}"
),
] ]
def get_chat_history_data(self) -> Optional[List[Data]]:
return self.chat_history
def create_agent_runnable(self): def create_agent_runnable(self):
if "input" not in self.user_prompt:
raise ValueError("Prompt must contain 'input' key.")
messages = [ messages = [
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)) ("system", self.system_prompt),
("placeholder", "{chat_history}"),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
("ai", "{agent_scratchpad}"),
] ]
prompt = ChatPromptTemplate.from_messages(messages) prompt = ChatPromptTemplate.from_messages(messages)
return create_xml_agent(self.llm, self.tools, prompt) return create_xml_agent(self.llm, self.tools, prompt)