-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
73 lines (58 loc) · 2.25 KB
/
Copy pathagent.py
File metadata and controls
73 lines (58 loc) · 2.25 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
"""
CodeLens – conversational agent (interactive CLI mode).
Fixes vs original:
- Correct import: langgraph.prebuilt.create_react_agent
- Env loading delegated to python-dotenv
- API key validation with friendly error message
- Config imported from config.py
"""
import os
import sys
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langgraph.prebuilt import create_react_agent
from config import LLM_MODEL
from tools import ALL_TOOLS
load_dotenv()
SYSTEM_PROMPT = (
"You are CodeLens, an expert software engineering agent.\n"
"You have full access to a codebase through the tools provided.\n\n"
"STRATEGY:\n"
"1. EXPLORE — Start with get_directory_tree to understand the project structure.\n"
"2. SEARCH — Use grep_search for specific identifiers; codebase_search for concepts.\n"
"3. UNDERSTAND — Use get_file_outline before reading a full file.\n"
"4. PLAN — Explain your findings and intended changes before writing anything.\n"
"5. EXECUTE — Use write_file for edits; run_terminal_command for tests/linting.\n\n"
"Always verify your work after making changes."
)
def build_agent():
"""Construct and return the CodeLens LangGraph agent."""
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
print("Error: GROQ_API_KEY not set. Add it to your .env file.")
sys.exit(1)
llm = ChatGroq(model_name=LLM_MODEL, groq_api_key=api_key, temperature=0)
return create_react_agent(model=llm, tools=ALL_TOOLS, prompt=SYSTEM_PROMPT)
def main():
agent = build_agent()
print("\n🔍 CodeLens Agent online! (type 'exit' to quit)\n")
while True:
try:
query = input("Your request: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye!")
break
if query.lower() in ("exit", "quit", "q"):
print("Goodbye!")
break
if not query:
continue
print("Thinking...\n")
try:
result = agent.invoke({"messages": [{"role": "user", "content": query}]})
answer = result["messages"][-1].content
print(f"Response:\n{answer}\n")
except Exception as exc:
print(f"Error: {exc}\n")
if __name__ == "__main__":
main()