Skip to content

Commit

Permalink
Fix error with inferring function parameters in agents, closes #816
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Nov 21, 2024
1 parent 94c05fb commit cdc6eee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/python/txtai/agent/tool/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ def fromdocs(target, config):
Tool
"""

name = target.__class__.__name__ if hasattr(target, "__call__") else target.__name__
target = target.__call__ if hasattr(target, "__call__") else target
# Get function name and target - use target if it's a function or method, else use target.__call__
name = target.__name__ if isinstance(target, (FunctionType, MethodType)) or not hasattr(target, "__call__") else target.__class__.__name__
target = target if isinstance(target, (FunctionType, MethodType)) or not hasattr(target, "__call__") else target.__call__

doc = inspect.getdoc(target).strip()
description, parameters, _ = chat_template_utils.parse_google_format_docstring(doc)
# Extract target documentation
doc = inspect.getdoc(target)
description, parameters, _ = chat_template_utils.parse_google_format_docstring(doc.strip()) if doc else (None, {}, None)

# Get list of required parameters
signature = inspect.signature(target)
Expand Down
8 changes: 6 additions & 2 deletions test/python/testagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,24 @@ def __call__(self, iso):

today = {"name": "today", "description": "Gets the current date and time", "target": DateTime()}

def current() -> str:
def current(iso) -> str:
"""
Gets the current date and time
Args:
iso: date will be converted to iso format if True
Returns:
current date and time
"""

return datetime.today().isoformat()
return datetime.today().isoformat() if iso else datetime.today()

agent = Agent(tools=[today, current, "websearch"], llm="hf-internal-testing/tiny-random-LlamaForCausalLM", max_iterations=1)

self.assertIsNotNone(agent)
self.assertIsInstance(agent.tools["today"](True), str)
self.assertIsInstance(agent.tools["current"](True), str)

def testToolsEmbeddings(self):
"""
Expand Down

0 comments on commit cdc6eee

Please sign in to comment.