npi-ai / npi

Action library for AI Agent
https://www.npi.ai
Apache License 2.0
191 stars 7 forks source link

refactor(core): introduce `npi_tool` decorator #3

Closed idiotWu closed 7 months ago

idiotWu commented 7 months ago

This PR introduces a new decorator npi_tool so that we can register tool functions more elegantly:

Before

Tool definition and registration are coded in separated sections, making it difficult to maintain.

class MyApp(App):
    def get_functions(self) -> List[FunctionRegistration]:
         return [
            FunctionRegistration(
                fn=self.search_emails,
                Params=SearchEmailsParameters,
                description='Search for emails with a query'
            ),
         ]

    def search_emails(self, params: SearchEmailsParameters):
         ...

After

Tool definition and registration are tightly bound.

class MyApp(App):
    # Default: `Params` is inferred from type hints, and `description` is inferred from docstring
    @npi_tool
    def search_emails(self, params: SearchEmailsParameters):
         """Search for emails with a query"""
         ...

    # You can also explicitly set the `Param` and `description`
    @npi_tool(
        Params=WaitForReplyParameters,
        description='Wait for a reply from the last email sent in the previous chats'
    )
    def wait_for_reply(self, params):
         ...