diff --git a/docs/index.md b/docs/index.md index 3061561..82b3f2b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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/) diff --git a/template_langgraph/agents/basic_workflow_agent/agent.py b/template_langgraph/agents/basic_workflow_agent/agent.py index 79a4f21..61645dc 100644 --- a/template_langgraph/agents/basic_workflow_agent/agent.py +++ b/template_langgraph/agents/basic_workflow_agent/agent.py @@ -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 @@ -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 @@ -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}") @@ -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.""" diff --git a/template_langgraph/agents/basic_workflow_agent/models.py b/template_langgraph/agents/basic_workflow_agent/models.py index e5d4132..6e1c6d0 100644 --- a/template_langgraph/agents/basic_workflow_agent/models.py +++ b/template_langgraph/agents/basic_workflow_agent/models.py @@ -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="抽出されたプロファイル情報")