Skip to content

Commit b32b527

Browse files
committed
chore(sdk-py): update README with agent tools and disk operations
1 parent ae6d4c2 commit b32b527

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

src/client/acontext-py/README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,144 @@ result = client.tools.rename_tool_name(
289289
print(result.status) # 0 for success
290290
```
291291

292+
### Agent Tools
293+
294+
The SDK provides agent tools that allow LLMs (OpenAI, Anthropic) to interact with Acontext disks through function calling. These tools can be converted to OpenAI or Anthropic tool schemas and executed when the LLM calls them.
295+
296+
#### Pre-configured Disk Tools
297+
298+
The SDK includes a pre-configured `DISK_TOOLS` pool with four disk operation tools:
299+
300+
- **`write_file`**: Write text content to a file
301+
- **`read_file`**: Read a text file with optional line offset and limit
302+
- **`replace_string`**: Replace strings in a file
303+
- **`list_artifacts`**: List files and directories in a path
304+
305+
#### Getting Tool Schemas for LLM APIs
306+
307+
Convert tools to the appropriate format for your LLM provider:
308+
309+
```python
310+
from acontext import AcontextClient
311+
from acontext.agent.disk import DISK_TOOLS
312+
313+
client = AcontextClient(api_key="sk-ac-your-root-api-bearer-token")
314+
315+
# Get OpenAI-compatible tool schemas
316+
openai_tools = DISK_TOOLS.to_openai_tool_schema()
317+
318+
# Get Anthropic-compatible tool schemas
319+
anthropic_tools = DISK_TOOLS.to_anthropic_tool_schema()
320+
321+
# Use with OpenAI API
322+
import openai
323+
openai_client = openai.OpenAI(api_key="your-openai-key")
324+
completion = openai_client.chat.completions.create(
325+
model="gpt-4",
326+
messages=[{"role": "user", "content": 'Write a file called hello.txt with "Hello, World!"'}],
327+
tools=openai_tools,
328+
)
329+
```
330+
331+
#### Executing Tools
332+
333+
When an LLM calls a tool, execute it using the tool pool:
334+
335+
```python
336+
from acontext import AcontextClient
337+
from acontext.agent.disk import DISK_TOOLS
338+
339+
client = AcontextClient(api_key="sk-ac-your-root-api-bearer-token")
340+
341+
# Create a disk for the tools to operate on
342+
disk = client.disks.create()
343+
344+
# Create a context for the tools
345+
ctx = DISK_TOOLS.format_context(client, disk.id)
346+
347+
# Execute a tool (e.g., after LLM returns a tool call)
348+
result = DISK_TOOLS.execute_tool(
349+
ctx,
350+
"write_file",
351+
{"filename": "hello.txt", "file_path": "/notes/", "content": "Hello, World!"}
352+
)
353+
print(result) # File 'hello.txt' written successfully to '/notes/hello.txt'
354+
355+
# Read the file
356+
read_result = DISK_TOOLS.execute_tool(
357+
ctx,
358+
"read_file",
359+
{"filename": "hello.txt", "file_path": "/notes/"}
360+
)
361+
print(read_result)
362+
363+
# List files in a directory
364+
list_result = DISK_TOOLS.execute_tool(
365+
ctx,
366+
"list_artifacts",
367+
{"file_path": "/notes/"}
368+
)
369+
print(list_result)
370+
371+
# Replace a string in a file
372+
replace_result = DISK_TOOLS.execute_tool(
373+
ctx,
374+
"replace_string",
375+
{
376+
"filename": "hello.txt",
377+
"file_path": "/notes/",
378+
"old_string": "Hello",
379+
"new_string": "Hi",
380+
}
381+
)
382+
print(replace_result)
383+
```
384+
385+
#### Creating Custom Tools
386+
387+
You can create custom tools by extending `BaseTool`:
388+
389+
```python
390+
from acontext.agent.base import BaseTool, BaseToolPool, BaseContext
391+
from typing import Dict, Any
392+
393+
class MyCustomTool(BaseTool):
394+
@property
395+
def name(self) -> str:
396+
return "my_custom_tool"
397+
398+
@property
399+
def description(self) -> str:
400+
return "A custom tool that does something"
401+
402+
@property
403+
def arguments(self) -> dict:
404+
return {
405+
"param1": {
406+
"type": "string",
407+
"description": "First parameter",
408+
}
409+
}
410+
411+
@property
412+
def required_arguments(self) -> list[str]:
413+
return ["param1"]
414+
415+
def execute(self, ctx: BaseContext, llm_arguments: dict) -> str:
416+
param1 = llm_arguments.get("param1")
417+
# Your custom logic here
418+
return f"Result: {param1}"
419+
420+
# Create a custom tool pool
421+
class MyToolPool(BaseToolPool):
422+
def format_context(self, *args, **kwargs) -> BaseContext:
423+
# Create and return your context
424+
return BaseContext()
425+
426+
my_pool = MyToolPool()
427+
my_pool.add_tool(MyCustomTool())
428+
```
429+
292430
### Blocks API
293431

294432
#### List blocks

0 commit comments

Comments
 (0)