-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathagent.py
More file actions
59 lines (46 loc) · 1.68 KB
/
Copy pathagent.py
File metadata and controls
59 lines (46 loc) · 1.68 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
"""
Docker Troubleshooter Agent — an AI agent that diagnoses Docker issues on its own.
Run: python3 module-2/agent.py
"""
import subprocess
from langchain_ollama import ChatOllama
from langchain_core.tools import tool
from langchain.agents import create_agent as create_react_agent
@tool
def list_containers() -> str:
"""List all Docker containers (running and stopped)."""
result = subprocess.run(["docker", "ps", "-a"], capture_output=True, text=True)
return result.stdout or result.stderr
@tool
def get_logs(container_name: str) -> str:
"""Get the last 50 lines of logs from a Docker container."""
result = subprocess.run(
["docker", "logs", "--tail", "50", container_name],
capture_output=True, text=True,
)
return result.stdout + result.stderr
@tool
def inspect_container(container_name: str) -> str:
"""Get detailed info about a Docker container (state, config, network)."""
result = subprocess.run(
["docker", "inspect", container_name],
capture_output=True, text=True,
)
return result.stdout or result.stderr
llm = ChatOllama(model="gemma4", temperature=0)
tools = [list_containers, get_logs, inspect_container]
agent = create_react_agent(llm, tools)
print("\nDocker Troubleshooter Agent")
print("-" * 30)
print("Ask me about your Docker containers. Type 'quit' to exit.\n")
while True:
question = input("> ").strip()
if question.lower() in ("quit", "exit", "q"):
break
if not question:
continue
print("\nThinking...\n")
result = agent.invoke({"messages": [("user", question)]})
# The last message is the agent's final answer
print(result["messages"][-1].content)
print()