Skip to content

Commit b81a5c5

Browse files
committed
wip
1 parent 2a3ac57 commit b81a5c5

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ uv run python -m template_langgraph.tasks.search_documents_on_elasticsearch
2424
# Run Kabuto Helpdesk Agent
2525
uv run python -m template_langgraph.tasks.run_kabuto_helpdesk_agent "KABUTOの起動時に、画面全体が紫色に点滅し、システムがフリーズします。"
2626
uv run python -m template_langgraph.tasks.run_kabuto_helpdesk_agent "KABUTOのマニュアルから禅モードに関する情報を教えて下さい"
27+
28+
# BasicWorkflowAgent
29+
uv run python -m template_langgraph.tasks.draw_basic_workflow_agent_mermaid_png "data/basic_workflow_agent.png"
30+
uv run python -m template_langgraph.tasks.run_basic_workflow_agent "KABUTOの起動時に、画面全体が紫色に点滅し、システムがフリーズします。"
2731
```
2832

2933
## References

template_langgraph/agents/basic_workflow_agent/__init__.py

Whitespace-only changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from langgraph.graph import END, START, StateGraph
2+
3+
from template_langgraph.agents.basic_workflow_agent.models import AgentInput, AgentOutput, AgentState
4+
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
5+
from template_langgraph.loggers import get_logger
6+
from template_langgraph.tools.elasticsearch_tool import search_elasticsearch
7+
from template_langgraph.tools.qdrants import search_qdrant
8+
9+
logger = get_logger(__name__)
10+
11+
12+
class BasicWorkflowAgent:
13+
def __init__(self, tools=None):
14+
if tools is None:
15+
# Default tool for searching Qdrant and Elasticsearch
16+
tools = [
17+
search_qdrant,
18+
search_elasticsearch,
19+
# Add other tools as needed
20+
]
21+
self.llm = AzureOpenAiWrapper().chat_model.bind_tools(
22+
tools=tools,
23+
)
24+
25+
def create_graph(self):
26+
"""Create the main graph for the agent."""
27+
# Create the workflow state graph
28+
workflow = StateGraph(AgentState)
29+
30+
# Create nodes
31+
workflow.add_node("initialize", self.initialize)
32+
workflow.add_node("do_something", self.do_something)
33+
34+
# Create edges
35+
workflow.add_edge(START, "initialize")
36+
workflow.add_edge("initialize", "do_something")
37+
workflow.add_edge("do_something", END)
38+
39+
# Compile the graph
40+
return workflow.compile()
41+
42+
def initialize(self, state: AgentState) -> AgentState:
43+
"""Initialize the agent with the given state."""
44+
logger.info(f"Initializing BasicWorkflowAgent with state: {state}")
45+
# Here you can add any initialization logic if needed
46+
return state
47+
48+
def do_something(self, state: AgentState) -> AgentState:
49+
"""Perform some action with the given state."""
50+
logger.info(f"Doing something with state: {state}")
51+
# Here you can add the logic for the action
52+
return state
53+
54+
def run_agent(self, input: AgentInput) -> AgentOutput:
55+
"""Run the agent with the given input."""
56+
logger.info(f"Running BasicWorkflowAgent with question: {input.model_dump_json(indent=2)}")
57+
app = self.create_graph()
58+
initial_state: AgentState = {
59+
"request": input.request,
60+
}
61+
final_state = app.invoke(initial_state)
62+
logger.info(f"Final state after running agent: {final_state}")
63+
return AgentOutput(
64+
response=final_state.get("response", "No response"),
65+
)
66+
67+
def draw_mermaid_png(self) -> bytes:
68+
"""Draw the graph in Mermaid format."""
69+
return self.create_graph().get_graph().draw_mermaid_png()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import TypedDict
2+
3+
from pydantic import BaseModel, Field
4+
5+
6+
class AgentInput(BaseModel):
7+
request: str = Field(..., description="ユーザーからのリクエスト")
8+
9+
10+
class AgentOutput(BaseModel):
11+
response: str = Field(..., description="エージェントの応答")
12+
13+
14+
class AgentState(TypedDict):
15+
request: str
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
3+
from template_langgraph.agents.basic_workflow_agent.agent import BasicWorkflowAgent
4+
5+
if __name__ == "__main__":
6+
png_path = "data/basic_workflow_agent.png"
7+
if len(sys.argv) > 1:
8+
png_path = sys.argv[1]
9+
10+
with open(png_path, "wb") as f:
11+
f.write(BasicWorkflowAgent().draw_mermaid_png())
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import logging
2+
import sys
3+
4+
from template_langgraph.agents.basic_workflow_agent.agent import AgentInput, BasicWorkflowAgent
5+
from template_langgraph.loggers import get_logger
6+
7+
logger = get_logger(__name__)
8+
logger.setLevel(logging.INFO)
9+
10+
if __name__ == "__main__":
11+
question = "「鬼灯」を実行すると、KABUTOが急に停止します。原因と対策を教えてください。"
12+
if len(sys.argv) > 1:
13+
# sys.argv[1] が最初の引数
14+
question = sys.argv[1]
15+
16+
# Agentのインスタンス化
17+
agent = BasicWorkflowAgent(
18+
tools=None, # ツールはカスタムせず、デフォルトのツールを使用
19+
)
20+
# AgentInputの作成
21+
agent_input = AgentInput(
22+
request=question,
23+
)
24+
25+
# エージェントの実行
26+
logger.info(f"Running BasicWorkflowAgent with input: {agent_input.model_dump_json(indent=2)}")
27+
agent_output = agent.run_agent(input=agent_input)
28+
logger.info(f"Agent output: {agent_output.model_dump_json(indent=2)}")

0 commit comments

Comments
 (0)