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)[源代码]
基类:
object
The service function class.
- __init__(name: str, original_func: Callable, processed_func: Callable, json_schema: dict) None [源代码]
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[源代码]
基类:
object
A service toolkit class that turns service function into string prompt format.
- service_funcs: dict[str, ServiceFunction]
The registered functions in the service toolkit.
- add(service_func: Callable[[...], Any], **kwargs: Any) None [源代码]
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.
- 参数:
service_func (Callable[…, Any]) – The service function to be called.
kwargs (Any) – The arguments to be passed to the service function.
- 返回:
A tuple of tool function and a dict in JSON Schema format to describe the function.
- 返回类型:
Tuple(Callable[…, Any], dict)
备注
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.
示例
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 [源代码]
Parse, check the text and call the function.
- classmethod get(service_func: Callable[[...], Any], **kwargs: Any) Tuple[Callable[[...], Any], dict] [源代码]
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.
- 参数:
service_func (Callable[…, Any]) – The service function to be called.
kwargs (Any) – The arguments to be passed to the service function.
- 返回:
A tuple of tool function and a dict in JSON Schema format to describe the function.
- 返回类型:
Tuple(Callable[…, Any], dict)
备注
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.
示例
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[源代码]
基类:
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] [源代码]
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.
- 参数:
service_func (Callable[…, Any]) – The service function to be called.
kwargs (Any) – The arguments to be passed to the service function.
- 返回:
A tuple of tool function and a dict in JSON Schema format to describe the function.
- 返回类型:
Tuple(Callable[…, Any], dict)
备注
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.
示例
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