-
Checked other resources
Commit to Help
Example Codetempalte "xxxxx"
template = template.format(dhead=df.head().to_markdown())
prompt = ChatPromptTemplate.from_messages(
[
("system", template),
MessagesPlaceholder(variable_name="agent_scratchpad"),
("human", "{input}"),
]
)
repl = PythonAstREPLTool(
locals={"df": df},
name="python_repl",
description="Runs code and returns the output of the final line",
args_schema=PythonInputs,
)
tools = [repl]
agent = OpenAIFunctionsAgent(llm=llm, prompt=prompt, tools=tools)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
max_iterations=10,
early_stopping_method="generate",
verbose=True,
extra_tools=[retriever_tool],
) | (lambda x: x["output"])
# Typing for playground inputs
class AgentInputs(BaseModel):
input: str
agent_executor = agent_executor.with_types(input_type=AgentInputs)
response = agent_executor.invoke({"input": output_query_str}) #### This may be need to be called in the langgraph
print(response)
import ast
import re
from langchain.agents.agent_toolkits import create_retriever_tool
####################### extra_tools or retriever_tool code #######################
# Function to execute a query and parse the results into a list of elements
def query_as_list(db, query):
res = db.run(query)
res = [el for sub in ast.literal_eval(res) for el in sub if el]
res = [re.sub(r"\b\d+\b", "", string).strip() for string in res]
return list(set(res))
# Create a vector store from the combined list of artists and albums
vector_db = FAISS.from_texts(names1, embeddings)
# Create a retriever from the vector store, retrieving the top 5 similar items
retriever = vector_db.as_retriever(search_kwargs={"k": 5})
# Description for the retriever tool
description = """Use to look up values to filter on. Input is an approximate spelling of the proper noun, output is \
valid proper nouns. Use the noun most similar to the search.
As an AI agent, you will be performing matching word cardinality. This involves comparing the number of times each word appears in two sets of text. You will count the occurrences of each word in both texts and determine the similarity based on these counts. Higher similarity indicates closer word usage patterns. This approach will help in tasks such as document comparison and plagiarism detection"""
# Create the retriever tool with the specified name and description
retriever_tool = create_retriever_tool(
retriever,
name="search_proper_nouns",
description=description,
) DescriptionI feel like I'm executing multiple tasks at the same time using the agent_executor in the code above. I really like the LangGraph agent concept because it’s cleaner and easier to understand. However, as a newcomer, I'm finding it challenging to understand the examples and apply them to my DataFrame situation. Could you help me by showing how to separate my extra_tools or retriever_tool function into a dedicated agent and create an agentic workflow? My goal is for the extra_tools or retriever_tool agent to process the DataFrame (df) first, refine the data, and then pass it to the agent_executor, which will be assigned to another agent. In essence, I want to break my large code into two agents and build a workflow where they work together. I would really appreciate some sample code to guide me. Thank you! System InfoName: langchain |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
I found two similar discussions that might be relevant to your question: To create an agentic workflow in LangChain where the Step 1: Define the Toolsfrom langchain.tools.retriever import create_retriever_tool
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Load documents and create a retriever
loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()
# Create retriever tool
retriever_tool = create_retriever_tool(
retriever,
"langsmith_search",
"Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
) Step 2: Create the First AgentThis agent will process the DataFrame. from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent
# Initialize the language model
model = ChatOpenAI(model="gpt-4")
# Create the first agent with the retriever tool
tools = [retriever_tool]
agent1 = create_tool_calling_agent(model, tools) Step 3: Create the Second AgentThis agent will execute further actions based on the refined data. # Create the second agent (you can add more tools if needed)
agent2 = create_tool_calling_agent(model, tools) Step 4: Combine Agents in a WorkflowUse from langchain.agents import AgentExecutor
# Create the first agent executor
agent_executor1 = AgentExecutor(agent=agent1, tools=tools)
# Create the second agent executor
agent_executor2 = AgentExecutor(agent=agent2, tools=tools)
# Define a function to process the DataFrame and pass it to the second agent
def process_and_execute(df):
# Process the DataFrame with the first agent
refined_data = agent_executor1.invoke({"input": df})
# Pass the refined data to the second agent
result = agent_executor2.invoke({"input": refined_data})
return result
# Example usage
df = "Your DataFrame here"
result = process_and_execute(df)
print(result) This setup ensures that the |
Beta Was this translation helpful? Give feedback.
-
@dosu... looks great. Here I haven't seen langgraph concepts like nodes and edges and workflow. Is that possible to convert this example to langgraph concepts. I like to have a langgraph template if possible for the same scenario. |
Beta Was this translation helpful? Give feedback.
-
@dosu This is good template. But how I convert this to input = pandas dataframe? I mean my input should be restricted to internal 'pandas dataframe'....any thoughts?
|
Beta Was this translation helpful? Give feedback.
-
@dosu... in the below function, where I ask my question? plaese help... the function you have given is below...: I used to ask a question like this in the past :
|
Beta Was this translation helpful? Give feedback.
I found two similar discussions that might be relevant to your question:
To create an agentic workflow in LangChain where the
extra_tools
orretriever_tool
agent processes the DataFrame (df
) first, refines the data, and then passes it to theagent_executor
, you can follow these steps:Step 1: Define the Tools