-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent.py
More file actions
64 lines (51 loc) · 2.23 KB
/
agent.py
File metadata and controls
64 lines (51 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""File operations agent"""
import os
import tempfile
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.tools.file_tools import BashTool
from trpc_agent_sdk.tools.file_tools import EditTool
from trpc_agent_sdk.tools.file_tools import GlobTool
from trpc_agent_sdk.tools.file_tools import GrepTool
from trpc_agent_sdk.tools.file_tools import ReadTool
from trpc_agent_sdk.tools.file_tools import WriteTool
from .config import get_model_config
from .prompts import INSTRUCTION
def _create_model() -> LLMModel:
"""Create a model"""
api_key, url, model_name = get_model_config()
model = OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
return model
def create_agent(work_dir: str | None = None):
"""Create a file operations agent to demonstrate file operation tools.
Args:
work_dir: Working directory for file operations. If None, uses system temp directory.
Returns:
Configured LlmAgent instance
"""
# Create working directory if not provided
if work_dir is None:
system_temp = tempfile.gettempdir()
work_dir = os.path.join(system_temp, "file_tools_demo")
os.makedirs(work_dir, exist_ok=True)
# Create individual tools with working directory
read_tool = ReadTool(cwd=work_dir) # Read file contents
write_tool = WriteTool(cwd=work_dir) # Write or append to files
edit_tool = EditTool(cwd=work_dir) # Replace text blocks in files
grep_tool = GrepTool(cwd=work_dir) # Search for patterns using regex
bash_tool = BashTool(cwd=work_dir) # Execute shell commands
glob_tool = GlobTool(cwd=work_dir) # Find files matching glob patterns
return LlmAgent(
name="file_assistant",
description="File operations assistant with file operation tools",
model=_create_model(),
instruction=INSTRUCTION,
tools=[read_tool, write_tool, edit_tool, grep_tool, bash_tool, glob_tool],
)
root_agent = create_agent()