-
Notifications
You must be signed in to change notification settings - Fork 46
Add Python customized tools guide, example, and SDK tool re-exports #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9a15d55
docs: add customized tools example and update guides
RealKai42 b20c0e6
docs: add "What's Next" section with links to Prompt API, Session API…
RealKai42 b7eeb6a
fix: lint error
RealKai42 60bcf67
docs: improve formatting in customized tools guide and session API do…
RealKai42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Example: Customized Tools | ||
|
|
||
| This example shows how to define a custom tool and load it through an agent file | ||
| when using the Kimi Agent SDK. | ||
|
|
||
| ## Run | ||
|
|
||
| ```sh | ||
| cd examples/python/customized-tools | ||
| uv sync --reinstall | ||
|
|
||
| # configure your API key | ||
| export KIMI_API_KEY=your-api-key | ||
| export KIMI_BASE_URL=https://api.moonshot.ai/v1 | ||
| export KIMI_MODEL_NAME=kimi-k2-thinking-turbo | ||
|
|
||
| uv run main.py | ||
| ``` | ||
|
|
||
| The agent file `myagent.yaml` registers the custom tool `my_tools.ls:Ls` and | ||
| reuses the default tool set. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import asyncio | ||
| from pathlib import Path | ||
|
|
||
| from kimi_agent_sdk import prompt | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| agent_file = Path(__file__).parent / "myagent.yaml" | ||
| async for msg in prompt( | ||
| "What tools do you have?", | ||
| agent_file=agent_file, | ||
| yolo=True, | ||
| ): | ||
| print(msg.extract_text(), end="", flush=True) | ||
| print() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from kimi_agent_sdk import CallableTool2, ToolError, ToolOk, ToolReturnValue | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class Params(BaseModel): | ||
| directory: str = Field(description="The directory to list files from.", default=".") | ||
|
|
||
|
|
||
| class Ls(CallableTool2): | ||
| name: str = "Ls" | ||
| description: str = "List files in a directory." | ||
| params: type[Params] = Params | ||
|
|
||
| async def __call__(self, params: Params) -> ToolReturnValue: | ||
| import os | ||
|
|
||
| try: | ||
| files = os.listdir(params.directory) | ||
| output = "\n".join(files) | ||
| return ToolOk(output=output) | ||
| except Exception as exc: | ||
| return ToolError( | ||
| output="", | ||
| message=str(exc), | ||
| brief="Failed to list files", | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| version: 1 | ||
| agent: | ||
| extend: default | ||
| tools: | ||
| - "kimi_cli.tools.multiagent:Task" | ||
| - "kimi_cli.tools.todo:SetTodoList" | ||
| - "kimi_cli.tools.shell:Shell" | ||
| - "kimi_cli.tools.file:ReadFile" | ||
| - "kimi_cli.tools.file:Glob" | ||
| - "kimi_cli.tools.file:Grep" | ||
| - "kimi_cli.tools.file:WriteFile" | ||
| - "kimi_cli.tools.file:StrReplaceFile" | ||
| - "kimi_cli.tools.web:SearchWeb" | ||
| - "kimi_cli.tools.web:FetchURL" | ||
| - "my_tools.ls:Ls" # custom tool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [project] | ||
| name = "customized-tools" | ||
| version = "0.1.0" | ||
| description = "Custom tool example for Kimi Agent SDK" | ||
| readme = "README.md" | ||
| requires-python = ">=3.12" | ||
| dependencies = ["kimi-agent-sdk"] | ||
|
|
||
| [tool.uv.sources] | ||
| kimi-agent-sdk = { path = "../../../python" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Customized Tools | ||
|
|
||
| Kimi Agent SDK is a thin wrapper around Kimi Code (Kimi CLI), so custom tools | ||
| are defined exactly the same way: write a Python tool class, register it in an | ||
| agent file, and pass that agent file to `prompt()` or `Session.create(...)`. | ||
|
|
||
| If you already have a Kimi CLI agent file and tools, you can reuse them as-is. | ||
|
|
||
| ## Step 1: Implement a tool | ||
|
|
||
| Create a tool class with a Pydantic parameter model and return `ToolOk` or | ||
| `ToolError`: | ||
|
|
||
| ```python | ||
| from kimi_agent_sdk import CallableTool2, ToolError, ToolOk, ToolReturnValue | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class Params(BaseModel): | ||
| directory: str = Field( | ||
| default=".", | ||
| description="The directory to list files from.", | ||
| ) | ||
|
|
||
|
|
||
| class Ls(CallableTool2): | ||
| name: str = "Ls" | ||
| description: str = "List files in a directory." | ||
| params: type[Params] = Params | ||
|
|
||
| async def __call__(self, params: Params) -> ToolReturnValue: | ||
| import os | ||
|
|
||
| try: | ||
| files = os.listdir(params.directory) | ||
| return ToolOk(output="\n".join(files)) | ||
| except Exception as exc: | ||
| return ToolError( | ||
| output="", | ||
| message=str(exc), | ||
| brief="Failed to list files", | ||
| ) | ||
| ``` | ||
|
|
||
| ## Step 2: Make the tool importable | ||
|
|
||
| Ensure your module is importable by the Python process running the SDK: | ||
|
|
||
| ``` | ||
| my_tools/ | ||
| __init__.py | ||
| ls.py | ||
| ``` | ||
|
|
||
| Options: | ||
|
|
||
| - Install your project package into the current environment. | ||
| - Or add the project root to `PYTHONPATH` when running your script. | ||
|
|
||
| ## Step 3: Register the tool in an agent file | ||
|
|
||
| Add your tool path (`module:ClassName`) to `tools`. Note that `tools` replaces | ||
| the inherited list, so include every tool you want to keep. | ||
|
|
||
| ```yaml | ||
| version: 1 | ||
| agent: | ||
| extend: default | ||
| tools: | ||
| - "kimi_cli.tools.multiagent:Task" | ||
| - "kimi_cli.tools.todo:SetTodoList" | ||
| - "kimi_cli.tools.shell:Shell" | ||
| - "kimi_cli.tools.file:ReadFile" | ||
| - "kimi_cli.tools.file:Glob" | ||
| - "kimi_cli.tools.file:Grep" | ||
| - "kimi_cli.tools.file:WriteFile" | ||
| - "kimi_cli.tools.file:StrReplaceFile" | ||
| - "kimi_cli.tools.web:SearchWeb" | ||
| - "kimi_cli.tools.web:FetchURL" | ||
| - "my_tools.ls:Ls" # custom tool | ||
| ``` | ||
|
|
||
| For full agent file format, see the | ||
| [Kimi Code agent docs](https://moonshotai.github.io/kimi-cli/en/customization/agents.html#custom-agent-files). | ||
|
|
||
| ## Step 4: Use the agent file in Python | ||
|
|
||
| Pass the agent file path to `prompt()` or `Session.create(...)`: | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from pathlib import Path | ||
|
|
||
| from kimi_agent_sdk import prompt | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| async for msg in prompt( | ||
| "What tools do you have?", | ||
| agent_file=Path("myagent.yaml"), | ||
| yolo=True, | ||
| ): | ||
| print(msg.extract_text(), end="", flush=True) | ||
| print() | ||
|
|
||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| If you prefer the low-level API, use `Session.create(agent_file=...)` instead. | ||
|
|
||
| For full code examples, see the [here](../../examples/python/customized-tools) | ||
RealKai42 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from kaos.path import KaosPath | ||
| from pydantic import BaseModel, Field | ||
| from kosong.tooling import ( | ||
| CallableTool2 as KosongCallableTool2, | ||
| ToolError as KosongToolError, | ||
| ToolOk as KosongToolOk, | ||
| ToolReturnValue as KosongToolReturnValue, | ||
| ) | ||
|
|
||
| from kimi_agent_sdk import ( | ||
| CallableTool2, | ||
| Config, | ||
| Session, | ||
| ToolError, | ||
| ToolOk, | ||
| ToolReturnValue, | ||
| ) | ||
|
|
||
|
|
||
| def test_tooling_exports_match_kosong() -> None: | ||
| assert CallableTool2 is KosongCallableTool2 | ||
| assert ToolOk is KosongToolOk | ||
| assert ToolError is KosongToolError | ||
| assert ToolReturnValue is KosongToolReturnValue | ||
|
|
||
|
|
||
| def test_custom_tool_uses_sdk_exports() -> None: | ||
| class Params(BaseModel): | ||
| directory: str = Field(default=".") | ||
|
|
||
| class Ls(CallableTool2): | ||
| name: str = "Ls" | ||
| description: str = "List files in a directory." | ||
| params: type[Params] = Params | ||
|
|
||
| async def __call__(self, params: Params) -> ToolReturnValue: | ||
| return ToolOk(output="ok") | ||
|
|
||
| result = asyncio.run(Ls()(Params())) | ||
| assert isinstance(result, ToolReturnValue) | ||
| assert result.is_error is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_agent_loads_custom_tool(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| tools_dir = tmp_path / "my_tools" | ||
| tools_dir.mkdir() | ||
| (tools_dir / "__init__.py").write_text("", encoding="utf-8") | ||
| (tools_dir / "ls.py").write_text( | ||
| "\n".join( | ||
| [ | ||
| "from pydantic import BaseModel, Field", | ||
| "from kimi_agent_sdk import CallableTool2, ToolOk, ToolReturnValue", | ||
| "", | ||
| "", | ||
| "class Params(BaseModel):", | ||
| " directory: str = Field(default='.')", | ||
| "", | ||
| "", | ||
| "class Ls(CallableTool2):", | ||
| " name: str = 'Ls'", | ||
| " description: str = 'List files in a directory.'", | ||
| " params: type[Params] = Params", | ||
| "", | ||
| " async def __call__(self, params: Params) -> ToolReturnValue:", | ||
| " return ToolOk(output='ok')", | ||
| "", | ||
| ] | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
| (tmp_path / "system.md").write_text("You are a test agent.", encoding="utf-8") | ||
| agent_file = tmp_path / "agent.yaml" | ||
| agent_file.write_text( | ||
| "\n".join( | ||
| [ | ||
| "version: 1", | ||
| "agent:", | ||
| " name: test-agent", | ||
| " system_prompt_path: ./system.md", | ||
| " tools:", | ||
| " - \"my_tools.ls:Ls\"", | ||
| "", | ||
| ] | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| monkeypatch.syspath_prepend(str(tmp_path)) | ||
|
|
||
| async with await Session.create( | ||
| work_dir=KaosPath(str(tmp_path)), | ||
| config=Config(), | ||
| agent_file=agent_file, | ||
| ) as session: | ||
| tool = session._cli.soul.agent.toolset.find("Ls") | ||
| assert tool is not None | ||
| assert tool.__class__.__module__ == "my_tools.ls" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.