Skip to content

Commit

Permalink
📄[DOC] Development - Build Custom Function Library
Browse files Browse the repository at this point in the history
  • Loading branch information
fairyshine committed Sep 24, 2024
1 parent 022a7d7 commit 6e80a26
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
105 changes: 105 additions & 0 deletions docs/Development/FunctionLibrary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
The external function (tool) set can be parsed by FastMindAPI in the form of a package, allowing the LLM to invoke these functions through natural language.

## 1 File Structure

### 1.1 Directory

A function set should be organized like this:

```
Custom_Python_Package/
__init__.py
requirements.txt
tools.json
ModuleA/
__init__.py
submodule1.py
ModuleB/
...
```

The root directory name is used as the `tool_package_name`.

### 1.2 tools.json

The file `tools.json` in the root directory is necessary for storing some metadata.

The "path" field provides the relative path of the function's file; please refer to the format in the following example.

```json
{
"TestFunc":{
"path": "ModuleA/submodule1.py"
},
...
}
```

### 1.3 Custom Function (tool)

The functions need to have:

- Docstrings; (`Name`, `Description`, `Parameters`, `Returns`)
- Type hints for input parameters. (Only native Python types are supported for parameters.)

Tips:

- The naming convention for Python functions mainly follows the PEP 8 style guide, using snake_case.

```python
# In ModuleA/submodule1.py

def test_func(Param1: str, Param2: List[int]):
"""
Name: test_func
Description: ...
Parameters:
Param1: str, ...
Param2: List[int], ...
Returns:
Output1: dict, ...
Other Information
"""
return Output1
```

## 2 Local Debugging

`pip install fastmindapi`

### 2.1 Load Sets

```python
from fastmindapi.modules import FunctionModule

Tool_Set_List = [
".../Tool_Package_1",
".../Tool_Package_2"
]

func_module = FunctionModule(Tool_Set_List)
```

### 2.2 Get Info

The format of tool information is the same as OpenAI's.

```python
func_module.get_all_tool_names_list()
func_module.get_tool_information_text_list(['Tool_Package_1/Tool_1', 'Tool_Package_2/Tool_1'])

```

### 2.3 Call a Function

```python
# Method 1
func_module.exec_tool_calling("Tool_Package_1/Tool_1(...)")
# Method 2
func_module.tool_pool_dict["Tool_Package_1"].call_tool("Tool_1",[])

# Example
func_module.exec_tool_calling("chem_lib/get_element_properties(element='Au')")
func_module.tool_pool_dict["chem_lib"].call_tool("get_element_properties",["Au"])
```

8 changes: 8 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ plugins:
paths: [src]
- search # built-in 搜索插件


markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.superfences

nav:
- Home:
- index.md
Expand All @@ -57,3 +63,5 @@ nav:
- User Guide: Quick_Start/user_guide.md
- Tutorial: Tutorial/tutorial.md
- Documentation: Documentation/API.md
- Development:
- Build Custom Function Library: Development/FunctionLibrary.md
1 change: 1 addition & 0 deletions src/fastmindapi/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .function import FunctionModule
2 changes: 1 addition & 1 deletion tests/funcmodule_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tests_settings # noqa: F401
from fastmindapi.modules.function import FunctionModule
from fastmindapi.modules import FunctionModule

package_list = [
"/Users/wumengsong/Code/PJLab/ChemAgent/chem_lib"
Expand Down

0 comments on commit 6e80a26

Please sign in to comment.