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

Different behaviour between tool usage and directly calling a function #2734

Closed
4 tasks done
fede-bello opened this issue Dec 12, 2024 · 4 comments
Closed
4 tasks done

Comments

@fede-bello
Copy link

fede-bello commented Dec 12, 2024

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

I was doing a small personal project where the objective is to given a prompt with a country,get an agent to display a map of the country.

I am using langchain and langgraph to create the agent, earth engine to get the data and geemap to display the map.

Going to simplify a bit the code for the sake of the example. The function I was using was something like this:

import ee
import geemap
from langchain_cohere import ChatCohere
from langchain.tools import tool
from IPython.display import display
from langgraph.prebuilt import create_react_agent
import asyncio

ee.Authenticate()
ee.Initialize(project="unicef-geospatial")

def get_uruguay_map():
    """Displays a map of Uruguay"""
    country_boundries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
    uruguay_boundries = country_boundries.filter(ee.Filter.eq("country_na", "Uruguay"))

    country_map = geemap.Map()
    country_map.center_object(uruguay_boundries)
    country_map.add_layer(uruguay_boundries, {}, "Uruguay Boundaries")
    display(country_map)
    return "success"

If I call the function directly, it works as expected.
image

When changing the function to a tool and hooking it up to the agent, it doesn't work as expected. The code was having at the start was:

@tool
def get_uruguay_map():
    """Displays a map of Uruguay"""
    country_boundries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
    uruguay_boundries = country_boundries.filter(ee.Filter.eq("country_na", "Uruguay"))

    country_map = geemap.Map()
    country_map.center_object(uruguay_boundries)
    country_map.add_layer(uruguay_boundries, {}, "Uruguay Boundaries")
    display(country_map)
    return "success"

llm = ChatCohere(temperature=0.0)
tools = [get_uruguay_map]

graph = create_react_agent(
    tools=tools,
    model=llm,
)

# %%
inputs = {
    "messages": [
        {
            "role": "user",
            "content": "Show me the map of Uruguay",
        }
    ]
}
graph.invoke(inputs)

With that code, I was getting the following output:

ToolMessage(content='Error: RuntimeError("There is no current event loop in thread \'ThreadPoolExecutor-3_0\'.")\n Please fix your mistakes.', name='get_uruguay_map', id='4bba79a1-c59a-4fbb-b065-f7e51122db5c', tool_call_id='eec8f8104e7a44a4ba63044c6a06dfa5', status='error'),

Which I couldn't diagnose but I found that it could be fixed by setting the event loop.

@tool
def get_uruguay_map():
    """Displays a map of Uruguay"""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    country_boundries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
    uruguay_boundries = country_boundries.filter(ee.Filter.eq("country_na", "Uruguay"))

    country_map = geemap.Map()
    country_map.center_object(uruguay_boundries)
    country_map.add_layer(uruguay_boundries, {}, "Uruguay Boundaries")
    display(country_map)
    return "success"

llm = ChatCohere(temperature=0.0)
tools = [get_uruguay_map]

graph = create_react_agent(
    tools=tools,
    model=llm,
)

# %%
inputs = {
    "messages": [
        {
            "role": "user",
            "content": "Show me the map of Uruguay",
        }
    ]
}
graph.invoke(inputs)

However, now instead of getting the map centered on Uruguay, I am getting the map centered on the world, even though the layer is properly displayed.

image

I am not sure why this is happening or how to properly diagnose/fix the issue.

Any thoughts?

System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 24.1.0: Thu Oct 10 21:00:32 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6030
> Python Version:  3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information
-------------------
> langchain_core: 0.3.21
> langchain: 0.3.9
> langchain_community: 0.3.9
> langsmith: 0.1.147
> langchain_cohere: 0.3.3
> langchain_experimental: 0.3.3
> langchain_openai: 0.2.11
> langchain_text_splitters: 0.3.2
> langgraph_sdk: 0.1.43

Optional packages not installed
-------------------------------
> langserve

Other Dependencies
------------------
> aiohttp: 3.11.10
> async-timeout: Installed. No version info available.
> cohere: 5.13.3
> dataclasses-json: 0.6.7
> httpx: 0.28.1
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> langsmith-pyo3: Installed. No version info available.
> numpy: 1.26.4
> openai: 1.57.0
> orjson: 3.10.12
> packaging: 24.2
> pandas: 2.2.3
> pydantic: 2.10.3
> pydantic-settings: 2.6.1
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.36
> tabulate: 0.9.0
> tenacity: 9.0.0
> tiktoken: 0.8.0
> typing-extensions: 4.12.2
@gbaian10
Copy link
Contributor

It looks like you're running in a Jupyter environment, which already has an event loop running. I suspect this might be causing the issue.
If you're in Jupyter, can the same code run in a .py file?

Or try using nest_asyncio to see if it solves the problem.

import nest_asyncio
nest_asyncio.apply()

@fede-bello
Copy link
Author

Running it with nest_asyncio solves the first issue, but still the map is not being centered when I use it as a tool

@fede-bello
Copy link
Author

Seems to be a geemap bug that for some reason appears when using the tool.
Thanks

@fede-bello
Copy link
Author

For anyone having a similar issue, it's being tracked here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants