-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (74 loc) · 2.74 KB
/
Copy pathmain.py
File metadata and controls
87 lines (74 loc) · 2.74 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
from codegen.agent import Agent
from codegen.prompt.system_prompt import prompt
import os
import sys
from dotenv import load_dotenv
from codegen.tools.tools_schema import TOOLS_SCHEMA
from codegen.utils.persistent_kernel import PersistentKernel
from codegen.functions import FUNCTIONS
# from codegen.utils.initialize_code_rag import initialize_code_rag
# from codegen.utils.query_constructions import construct_query
def initialize_agent():
"""Initialize the CodeAct agent with configuration."""
load_dotenv()
## api key for anthropic
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
print("Error: ANTHROPIC_API_KEY not found in environment variables.")
print("Please set your API key in a .env file or environment variable.")
sys.exit(1)
## return agent with anthropic model
return Agent(
model="claude-4-sonnet-20250514",
api_key=api_key,
system_prompt=prompt,
base_url="https://api.anthropic.com/v1",
tools=TOOLS_SCHEMA,
kernel=PersistentKernel(namespace=FUNCTIONS, imports=""),
)
def main():
"""Main interactive loop for CodeAct."""
print("Welcome to CodeAct! 🤖")
print("Type your questions or commands. Use 'quit', 'exit', or Ctrl+C to stop.")
print("-" * 50)
## initialize agent and code rag
try:
agent = initialize_agent()
# initialize_code_rag()
except (ValueError, ConnectionError, OSError) as e:
print(f"Failed to initialize agent: {e}")
return
## main loop
while True:
try:
# Get user input
user_input = input("\nYou: ").strip()
# Check for exit commands
if user_input.lower() in ["quit", "exit", "q"]:
print("\nGoodbye! 👋")
break
# Skip empty input
if not user_input:
continue
# Add context to the user prompt
# constructed_query = construct_query(user_input)
# response = agent.query(constructed_query)
# Process the query
print("\nCodegen: \n", end="")
print("\n🔄 Processing...")
response = agent.query(user_input)
print("\n🤖 Response:")
print("-" * 30)
print(response["content"])
if response["tool_calls"]:
print(f"\n🛠️ Tool calls executed: {len(response['tool_calls'])}")
## handle keyboard interrupt
except KeyboardInterrupt:
print("\n\nGoodbye! 👋")
break
## handle end of file error
except EOFError:
print("\n\nGoodbye! 👋")
break
if __name__ == "__main__":
main()