-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_agent.py
More file actions
72 lines (56 loc) · 1.75 KB
/
github_agent.py
File metadata and controls
72 lines (56 loc) · 1.75 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
import os
import re
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.agent_toolkits.github.toolkit import GitHubToolkit
from langchain_community.utilities.github import GitHubAPIWrapper
from langchain.agents import create_agent
load_dotenv()
required_vars = [
"GOOGLE_API_KEY",
"GITHUB_APP_ID",
"GITHUB_REPOSITORY",
"GITHUB_APP_PRIVATE_KEY_PATH",
]
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
key_path = os.getenv("GITHUB_APP_PRIVATE_KEY_PATH")
with open(key_path, "r") as f:
private_key = f.read()
os.environ["GITHUB_APP_PRIVATE_KEY"] = private_key
def sanitize_tool_name(name: str) -> str:
name = name.lower()
name = re.sub(r"[^a-z0-9_]+", "_", name)
return name.strip("_")
github = GitHubAPIWrapper()
toolkit = GitHubToolkit.from_github_api_wrapper(github)
tools = toolkit.get_tools()
for tool in tools:
tool.name = sanitize_tool_name(tool.name)
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash-lite",
temperature=0,
)
system_prompt = """
You are a GitHub AI assistant.
You help users interact with a GitHub repository using the available tools.
Use tools whenever repository actions are required.
Explain actions briefly and clearly.
"""
agent = create_agent(
llm,
tools=tools,
system_prompt=system_prompt,
)
print("GitHub Agent Ready Type 'exit' to quit.\n")
while True:
user_input = input("You: ")
if user_input.lower() in {"exit", "quit"}:
break
result = agent.invoke({
"messages": [
{"role": "user", "content": user_input}
]
})
print(result.get("output", result))