Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ uv run python -m template_langgraph.tasks.run_kabuto_helpdesk_agent "KABUTOの
# BasicWorkflowAgent
uv run python -m template_langgraph.tasks.draw_basic_workflow_agent_mermaid_png "data/basic_workflow_agent.png"
uv run python -m template_langgraph.tasks.run_basic_workflow_agent "KABUTOの起動時に、画面全体が紫色に点滅し、システムがフリーズします。"
uv run python -m template_langgraph.tasks.run_basic_workflow_agent "私の名前はフグ田 サザエ。東京都世田谷区桜新町あさひが丘3丁目に住んでいる 24 歳の主婦です。夫のノリスケと子供のタラちゃんがいます。"
```

## References

### Sample Codes

- [「現場で活用するためのAIエージェント実践入門」リポジトリ](https://github.com/masamasa59/genai-agent-advanced-book)

### Models

- [AzureOpenAIEmbeddings](https://python.langchain.com/docs/integrations/text_embedding/azureopenai/)
Expand Down
20 changes: 17 additions & 3 deletions template_langgraph/agents/basic_workflow_agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from langchain_core.messages import AIMessage
from langgraph.graph import END, START, StateGraph

from template_langgraph.agents.basic_workflow_agent.models import AgentInput, AgentOutput, AgentState
from template_langgraph.agents.basic_workflow_agent.models import AgentInput, AgentOutput, AgentState, Profile
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
from template_langgraph.loggers import get_logger

Expand All @@ -20,12 +20,14 @@ def create_graph(self):
# Create nodes
workflow.add_node("initialize", self.initialize)
workflow.add_node("do_something", self.do_something)
workflow.add_node("extract_profile", self.extract_profile)
workflow.add_node("finalize", self.finalize)

# Create edges
workflow.add_edge(START, "initialize")
workflow.add_edge("initialize", "do_something")
workflow.add_edge("do_something", "finalize")
workflow.add_edge("do_something", "extract_profile")
workflow.add_edge("extract_profile", "finalize")
workflow.add_edge("finalize", END)

# Compile the graph
Expand Down Expand Up @@ -55,6 +57,15 @@ def do_something(self, state: AgentState) -> AgentState:

return state

def extract_profile(self, state: AgentState) -> AgentState:
"""Extract profile information from the state."""
logger.info(f"Extracting profile from state: {state}")
profile = self.llm.with_structured_output(Profile).invoke(
input=state["messages"],
)
state["profile"] = profile
return state

def finalize(self, state: AgentState) -> AgentState:
"""Finalize the agent's work and prepare the output."""
logger.info(f"Finalizing BasicWorkflowAgent with state: {state}")
Expand All @@ -75,7 +86,10 @@ def run_agent(self, input: AgentInput) -> AgentOutput:
}
final_state = app.invoke(initial_state)
logger.info(f"Final state after running agent: {final_state}")
return AgentOutput(response=final_state["messages"][-1].content)
return AgentOutput(
response=final_state["messages"][-1].content,
profile=final_state["profile"],
)

def draw_mermaid_png(self) -> bytes:
"""Draw the graph in Mermaid format."""
Expand Down
9 changes: 9 additions & 0 deletions template_langgraph/agents/basic_workflow_agent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
from pydantic import BaseModel, Field


class Profile(BaseModel):
first_name: str = Field(..., description="名")
last_name: str = Field(..., description="姓")
age: int | None = Field(None, description="年齢")
address: str | None = Field(None, description="住所")


class AgentInput(BaseModel):
request: str = Field(..., description="ユーザーからのリクエスト")


class AgentOutput(BaseModel):
response: str = Field(..., description="エージェントの応答")
profile: Profile | None = Field(None, description="抽出されたプロファイル情報")


class AgentState(TypedDict):
messages: Annotated[list, add_messages]
profile: Profile | None = Field(None, description="抽出されたプロファイル情報")