langgraph/how-tos/async/ #2523
Replies: 3 comments 8 replies
-
Thanks for this nice tutorial, it's so clean and simple. Works like magic! |
Beta Was this translation helpful? Give feedback.
-
Can you give more examples on how to define async tools? Here you showed how to define an async function to call the llm model, but the tool function is still sync. If I want to call the tools asynchronously, how to define the tool function or tool node? Can I just define an async tool funciton like the following:
Then can I just use the ToolNode class, or should I define a custom toolnode to handle the async calling? |
Beta Was this translation helpful? Give feedback.
-
I am not sure i I understood the answer correctly on how to make tool async here is my code from playwright.async_api import async_playwright
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent, ToolNode
from langchain_core.tools import tool
from agents.browser_agent.prompt import BROWSER_AGENT_PROMPT
# Simple global browser instance
playwright = None
browser = None
page = None
async def get_browser(): //NOT A TOOL
"""Get or create browser instance"""
global playwright, browser, page
# Check if we already have a working page
if page is not None:
try:
# Test if the page is still responsive
await page.title()
return page
except Exception as e:
# Page is broken, we'll create a new one
pass
# Create new browser session
# Clean up any existing resources
if page is not None:
try:
await page.close()
except:
pass
if browser is not None:
try:
await browser.close()
except:
pass
if playwright is not None:
try:
await playwright.stop()
except:
pass
# Create new session
playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless=False)
page = await browser.new_page()
return page
@tool
async def navigate_to_url(url: str) -> str:
"""Navigate to a URL"""
try:
page = await get_browser()
await page.goto(url)
title = await page.title()
return f"Successfully navigated to {url}"
except Exception as e:
return f"Error navigating to {url}: {str(e)}"
@tool
async def click_element_by_text(text: str) -> str:
"""Click an element with the given text"""
try:
page = await get_browser()
current_title = await page.title()
element = page.locator(f"text={text}")
count = await element.count()
if count > 0:
await element.first.click()
return f"Successfully clicked element with text: {text}"
else:
return f"No elements found with text: {text}"
except Exception as e:
return f"Error clicking element with text '{text}': {str(e)}"
@tool
async def get_page_content() -> str:
"""Get current page content to see what's available"""
try:
page = await get_browser()
title = await page.title()
all_text = await page.locator("body").inner_text()
return f"Page Title: {title}\nPage Text Preview: {all_text[:500]}..."
except Exception as e:
return f"Error getting page content: {str(e)}"
# Create ToolNode for all your async tools
browser_tool_node = ToolNode([navigate_to_url, click_element_by_text, get_page_content])
browser_agent = create_react_agent(
model=ChatOpenAI(model="gpt-4o", temperature=0.1),
tools=browser_tool_node, # Use ToolNode here
name="browser_agent",
prompt=BROWSER_AGENT_PROMPT,
).with_config(recursion_limit=20) When this agent is invoked via frontend stremable chat interface.... no tools are getting called/nothing happens i can see tool call in langsmith but it stucks in process no further flow. any suggestions? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
langgraph/how-tos/async/
Build language agents as graphs
https://langchain-ai.github.io/langgraph/how-tos/async/
Beta Was this translation helpful? Give feedback.
All reactions