Skip to content

state provider for @beta_tool and @async_beta_tool #1087

@marwq

Description

@marwq

Currently the only way to provide a request-scope state for tools in the beta runner is a wrapper:

def setup_tools(user_id: int):
    @beta_tool(name='get_user')
    def get_user(id: int) -> str:
        if user_id != id:
            return 'Error: User doesn't have permission'
        return ...

    return [get_user]

# in router/interactor
user_id = 123
client.beta.tool_runner(
    ...,
    tools=setup_tools(user_id)
)

I suggest to add feature for passing context for tools:

@beta_tool(name='get_user')
def get_user(id: int, ctx: Context) -> str:
    if ctx['user_id'] != id:
        return 'Error: User doesn't have permission'
    return ...

client.beta.tool_runner(
    ...,
    tools=[get_user],
    context={'user_id': 1},
)

Or at least support for methods:

class FetchProduct
    def __init__(self, ctx: dict):
        self.ctx = ctx

    @beta_tool(name='fetch_product')
    def __call__(self, id: int) -> str:
        return self.ctx['session'].get(Product, id)

tools = [FetchProduct(ctx)]

Currently, it doesn't work because an error would be raised on the self argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions