agentscope.service.service_toolkit module

Service Toolkit for service function usage.

class agentscope.service.service_toolkit.ServiceFunction(name: str, original_func: Callable, processed_func: Callable, json_schema: dict)[source]

Bases: object

The service function class.

__init__(name: str, original_func: Callable, processed_func: Callable, json_schema: dict) None[source]

Initialize the service function object.

name: str

The name of the service function.

original_func: Callable

The original function before processing.

processed_func: Callable

The processed function that can be called by the model directly.

json_schema: dict

The JSON schema description of the service function.

require_args: bool

Whether calling the service function requests arguments. Some arguments may have default values, so it is not necessary to provide all arguments.

class agentscope.service.service_toolkit.ServiceToolkit[source]

Bases: object

A service toolkit class that turns service function into string prompt format.

__init__() None[source]

Initialize the service toolkit with a list of service functions.

service_funcs: dict[str, ServiceFunction]

The registered functions in the service toolkit.

add(service_func: Callable[[...], Any], **kwargs: Any) None[source]

Add a service function to the toolkit, which will be processed into a tool function that can be called by the model directly, and registered in processed_funcs.

Parameters:
  • service_func (Callable[…, Any]) – The service function to be called.

  • kwargs (Any) – The arguments to be passed to the service function.

Returns:

A tuple of tool function and a dict in JSON Schema format to describe the function.

Return type:

Tuple(Callable[…, Any], dict)

Note

The description of the function and arguments are extracted from its docstring automatically, which should be well-formatted in Google style. Otherwise, their descriptions in the returned dictionary will be empty.

Suggestions:

1. The name of the service function should be self-explanatory, so that the agent can understand the function and use it properly. 2. The typing of the arguments should be provided when defining the function (e.g. def func(a: int, b: str, c: bool)), so that the agent can specify the arguments properly. 3. The execution results should be a ServiceResponse object.

Example

def bing_search(query: str, api_key: str, num_results=10):
    """Search the query in Bing search engine.

        Args:
            query: (`str`):
                The string query to search.
            api_key: (`str`):
                The API key for Bing search.
            num_results: (`int`, optional):
                The number of results to return, default to 10.
    """

    # ... Your implementation here ...
    return ServiceResponse(status, output)
property json_schemas: dict

The json schema descriptions of the processed service funcs.

property tools_calling_format: str

The calling format of the tool functions.

property tools_instruction: str

The instruction of the tool functions.

parse_and_call_func(text_cmd: list[dict] | str, raise_exception: bool = False) Msg[source]

Parse, check the text and call the function.

classmethod get(service_func: Callable[[...], Any], **kwargs: Any) Tuple[Callable[[...], Any], dict][source]

Convert a service function into a tool function that agent can use, and generate a dictionary in JSON Schema format that can be used in OpenAI API directly. While for open-source model, developers should handle the conversation from json dictionary to prompt.

Parameters:
  • service_func (Callable[…, Any]) – The service function to be called.

  • kwargs (Any) – The arguments to be passed to the service function.

Returns:

A tuple of tool function and a dict in JSON Schema format to describe the function.

Return type:

Tuple(Callable[…, Any], dict)

Note

The description of the function and arguments are extracted from its docstring automatically, which should be well-formatted in Google style. Otherwise, their descriptions in the returned dictionary will be empty.

Suggestions:

1. The name of the service function should be self-explanatory, so that the agent can understand the function and use it properly. 2. The typing of the arguments should be provided when defining the function (e.g. def func(a: int, b: str, c: bool)), so that the agent can specify the arguments properly.

Example

def bing_search(query: str, api_key: str, num_results: int=10):
    '''Search the query in Bing search engine.

    Args:
        query (str):
            The string query to search.
        api_key (str):
            The API key for Bing search.
        num_results (int):
            The number of results to return, default to 10.
    '''
    pass
class agentscope.service.service_toolkit.ServiceFactory[source]

Bases: object

A service factory class that turns service function into string prompt format.

classmethod get(service_func: Callable[[...], Any], **kwargs: Any) Tuple[Callable[[...], Any], dict][source]

Convert a service function into a tool function that agent can use, and generate a dictionary in JSON Schema format that can be used in OpenAI API directly. While for open-source model, developers should handle the conversation from json dictionary to prompt.

Parameters:
  • service_func (Callable[…, Any]) – The service function to be called.

  • kwargs (Any) – The arguments to be passed to the service function.

Returns:

A tuple of tool function and a dict in JSON Schema format to describe the function.

Return type:

Tuple(Callable[…, Any], dict)

Note

The description of the function and arguments are extracted from its docstring automatically, which should be well-formatted in Google style. Otherwise, their descriptions in the returned dictionary will be empty.

Suggestions:

1. The name of the service function should be self-explanatory, so that the agent can understand the function and use it properly. 2. The typing of the arguments should be provided when defining the function (e.g. def func(a: int, b: str, c: bool)), so that the agent can specify the arguments properly.

Example

def bing_search(query: str, api_key: str, num_results: int=10):
    '''Search the query in Bing search engine.

    Args:
        query (str):
            The string query to search.
        api_key (str):
            The API key for Bing search.
        num_results (int):
            The number of results to return, default to 10.
    '''
    pass