-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (119 loc) · 4.99 KB
/
main.py
File metadata and controls
146 lines (119 loc) · 4.99 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
from typing import List, Optional
from dotenv import load_dotenv
from actions import PythonActionRegistry
from agent import Agent
from completions import generate_response
from environment import Environment
from goal import Goal, AgentFunctionCallingActionLanguage
from qa import agent_qa_function
from tool_data import register_tool
load_dotenv()
print(os.getenv('OPEN_IA_KEY'))
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# First, we'll define our tools using decorators
@register_tool(tags=["file_operations", "write"])
def write_file(
name: str,
content: str,
overwrite: bool = True,
append: bool = False,
create_parents: bool = True,
encoding: str = "utf-8",
) -> str:
"""
Writes text content to a file.
Args:
name: Target file path, e.g. "README.md"
content: Text to write
overwrite: If True, overwrite existing file (ignored if append is True)
append: If True, append to the file instead of overwriting
create_parents: Create parent directories if missing
encoding: File encoding to use
Returns:
A short status message describing what happened
"""
# Resolve mode
if append:
mode = "a"
else:
if not overwrite and os.path.exists(name):
raise FileExistsError(f"Refusing to overwrite existing file: {name}")
mode = "w"
# Make sure parent dirs exist if requested
parent = os.path.dirname(name)
if parent and create_parents:
os.makedirs(parent, exist_ok=True)
# Write the thing
with open(name, mode, encoding=encoding) as f:
written = f.write(content)
action = "appended" if append else ("overwrote" if overwrite else "created")
return f"{action} {written} bytes to {os.path.abspath(name)}"
@register_tool(tags=["file_operations", "read"])
def read_project_file(name: str) -> str:
"""Reads and returns the content of a specified project file.
Opens the file in read mode and returns its entire contents as a string.
Raises FileNotFoundError if the file doesn't exist.
Args:
name: The name of the file to read
Returns:
The contents of the file as a string
"""
with open(name, "r") as f:
return f.read()
@register_tool(tags=["qa-agent", "validation"])
def qa_agent(readme_content: Optional[str] = None,
readme_path: Optional[str] = None,) -> str:
return agent_qa_function(readme_content,readme_path)
@register_tool(tags=["file_operations", "list"])
def list_project_files() -> List[str]:
"""Lists all Python files in the current project directory.
Scans the current directory and returns a sorted list of all files
that end with '.py'.
Returns:
A sorted list of Python filenames
"""
return sorted([file for file in os.listdir(".")
if file.endswith(".py")])
@register_tool(tags=["system"], terminal=True)
def terminate(message: str) -> str:
"""Terminates the agent's execution with a final message.
Args:
message: The final message to return before terminating
Returns:
The message with a termination note appended
"""
return f"{message}\nTerminating..."
# Define the agent's goals
goals = [
Goal(
priority=1,
name="Write README",
description=(
"Draft a complete README for the project. "
"When ready, persist it to disk by calling write with "
"name='README2.md' and the README content. "
"After successfully writing, verify by calling read_project_file('README2.md'). "
" Then call agent_qa(readme_content=<loaded content>)."
" If QA passes (payload.passes == true), call terminate with a short success note."
" If QA fails, apply suggested_changes to improve the README and repeat once."
),
),
Goal(priority=1,
name="Terminate",
description="Call terminate when done and provide a complete README for the project in the message parameter")
]
# Create an agent instance with tag-filtered actions
agent = Agent(
goals=goals,
agent_language=AgentFunctionCallingActionLanguage(),
# The ActionRegistry now automatically loads tools with these tags
action_registry=PythonActionRegistry(tags=["file_operations","list","read","write","system","qa-agent","validation"]),
generate_response=generate_response,
environment=Environment()
)
# Run the agent with user input
user_input = "Write a README for this project."
final_memory = agent.run(user_input)
print(final_memory.get_memories())