aaa
This commit is contained in:
parent
4f431d0640
commit
d497ed8599
48
gurt/api.py
48
gurt/api.py
@ -232,26 +232,44 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
|
||||
])
|
||||
|
||||
# --- 2. Prepare Tools ---
|
||||
# Wrap tools from TOOL_MAPPING to include the 'cog' instance
|
||||
# Create wrappers for tools to include the 'cog' instance
|
||||
prepared_tools = []
|
||||
for tool_name, tool_func in TOOL_MAPPING.items():
|
||||
# Use functools.partial to bind the first argument (cog)
|
||||
# Note: This assumes all tools in TOOL_MAPPING accept 'cog' as the first arg.
|
||||
try:
|
||||
# Create a partial function that includes the cog
|
||||
partial_tool_func = functools.partial(tool_func, cog)
|
||||
# Copy metadata (__name__, __doc__, __annotations__) for LangChain's schema generation
|
||||
functools.update_wrapper(partial_tool_func, tool_func)
|
||||
# Define a wrapper function that captures cog and calls the original tool
|
||||
# This wrapper will have the correct signature for Langchain to inspect
|
||||
def create_wrapper(func_to_wrap, current_cog):
|
||||
# Define the actual wrapper that Langchain will call
|
||||
# It accepts the arguments Langchain extracts based on the original function's signature
|
||||
def tool_wrapper(*args, **kwargs):
|
||||
# Call the original tool function with cog as the first argument
|
||||
# Pass through the arguments received from Langchain
|
||||
return func_to_wrap(current_cog, *args, **kwargs)
|
||||
# Copy metadata for Langchain schema generation from the original function
|
||||
functools.update_wrapper(tool_wrapper, func_to_wrap)
|
||||
# Ensure the wrapper has the correct name for the tool mapping/introspection
|
||||
# Note: Langchain might use the name attribute if wrapped in LangchainTool later
|
||||
tool_wrapper.__name__ = func_to_wrap.__name__
|
||||
return tool_wrapper
|
||||
|
||||
# LangchainAgent expects Tool objects or functions decorated with @tool
|
||||
# Wrap the partial function in a LangchainTool object
|
||||
# LangChain uses the function's __name__, __doc__, and type hints
|
||||
# Create the specific wrapper for this tool, capturing the current cog
|
||||
wrapped_tool_func = create_wrapper(tool_func, cog)
|
||||
|
||||
# Pass the wrapped function directly to the agent's tool list.
|
||||
# LangchainAgent should be able to handle functions directly.
|
||||
# We explicitly provide the name in the LangchainTool wrapper below for clarity if needed.
|
||||
# For now, let's try passing the function itself, relying on Langchain's introspection.
|
||||
# If issues persist, wrap in LangchainTool:
|
||||
prepared_tools.append(LangchainTool(
|
||||
name=tool_name, # Explicitly provide the name from mapping
|
||||
func=partial_tool_func,
|
||||
description=tool_func.__doc__ or f"Executes the {tool_name} tool.", # Use docstring or fallback
|
||||
# LangChain should infer args_schema from type hints on tool_func
|
||||
))
|
||||
name=tool_name, # Use the key from TOOL_MAPPING as the definitive name
|
||||
func=wrapped_tool_func, # Pass the wrapper function
|
||||
description=tool_func.__doc__ or f"Executes the {tool_name} tool.", # Use original docstring
|
||||
# LangChain should infer args_schema from the *original* tool_func's type hints
|
||||
# because functools.update_wrapper copies the signature.
|
||||
))
|
||||
# Simpler alternative (try if the above fails):
|
||||
# prepared_tools.append(wrapped_tool_func)
|
||||
|
||||
except Exception as tool_prep_e:
|
||||
logger.error(f"Error preparing tool '{tool_name}': {tool_prep_e}", exc_info=True)
|
||||
# Optionally skip this tool
|
||||
|
Loading…
x
Reference in New Issue
Block a user