langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.83k stars 15.12k forks source link

make sure that @tool decorator can be applied to a method (regression?) #27471

Open eyurtsev opened 3 days ago

eyurtsev commented 3 days ago

Privileged issue

Issue Content

from langchain_core.tools import tool

class MyAPI:
    def __init__(self):
        self.base_url = 'https://en.wikipedia.org/wiki/'

    @tool
    def lookup(self, page) -> str:
        """Look up a page on wikipedia. Include just the name of the page. Rest of the URL will be filled in."""
        url = self.base_url + item
        return request.get(url)        

api = MyAPI()
api.lookup.invoke('hello')
GlassesNoGlasses commented 3 days ago

Hey @eyurtsev! I'm part of a group of UofT developers interested in this issue. Can we tackle it? If so, what specific requirements and output are you looking for?

eyurtsev commented 3 days ago

Yes, you're welcome to try to tackle it. What kind of timeline do you have in mind?


We have a set of unit tests for tools: https://github.com/langchain-ai/langchain/blob/master/libs/core/tests/unit_tests/test_tools.py

We want to make sure that the @tool decorator has the correct behavior whether it's a standalone function or a method.

@tool
def foo(x: int, y: float) -> str:
  """bar"""
@tool
async def foo(x: int, y: float) -> str:
  """bar"""
class A:
  @tool
  def foo(self, x: int, y: float) -> str:
    """bar"""
class A:
  @tool
  async def foo(self, x: int, y: float) -> str:
    """bar"""

All behave the same.

GlassesNoGlasses commented 3 days ago

Thank you for the succinct requirements, that will really help us get started. Our timeline to get this finished is around 2 or 3 weeks from now, though the sooner the better. Does that work for you?

eyurtsev commented 22 hours ago

Could you check in before getting started. This is in core so we may end up prioritizing this fix