Can I combine Agent and @tool and create a function for LangGraph? #27605
mraguth
announced in
Ask Dosu (Archived)
Replies: 1 comment 2 replies
-
Yes, you can combine the Agent and
Here's an example of how you might implement this: from langchain_core.tools import tool
from langgraph.graph import StateGraph
from langchain_core.agents import create_agent
from typing import Annotated
# Define the PythonREPL tool
repl = PythonREPL()
@tool
def python_repl(
code: Annotated[str, "The python code to execute to generate your chart."],
):
"""Use this to execute python code. If you want to see the output of a value,
you should print it out with `print(...)`. This is visible to the user."""
try:
result = repl.run(code)
except BaseException as e:
return f"Failed to execute. Error: {repr(e)}"
result_str = f"Successfully executed:\n\`\`\`python\n{code}\n\`\`\`\nStdout: {result}"
return (
result_str + "\n\nIf you have completed all tasks, respond with FINAL ANSWER."
)
# Create an agent using the tool
chart_agent = create_agent(
llm,
[python_repl],
system_message="Any charts you display will be visible by the user.",
)
# Define a function that uses the agent
def chart_generator_function(state):
# Logic to use the agent
return {"messages": ["Chart generated successfully"]}
# Create a new graph
workflow = StateGraph(AgentState)
# Add the function as a node in the workflow
workflow.add_node("chart_generator_node", chart_generator_function)
# Set the entry point and define edges as needed
workflow.set_entry_point("chart_generator_node") This setup allows you to encapsulate the logic of using the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am referencing this link : https://langchain-ai.github.io/langgraph/tutorials/multi_agent/multi-agent-collaboration/#define-tools
In this example 'chart_agent' is calling python_repl tool and performaing the action. However, I like to combine both tasks and create a function by that i can call in workflow.add_node. Is that possible? please help.
Tool
Agent:
Beta Was this translation helpful? Give feedback.
All reactions