Skip to content

Commit 4f2c9de

Browse files
authored
Merge pull request #29 from ks6088ts-labs/feature/issue-26_dify-tool
support dify tool
2 parents 1037ee7 + d9d98b7 commit 4f2c9de

File tree

11 files changed

+188
-63
lines changed

11 files changed

+188
-63
lines changed

docs/index.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# template-langgraph
22

3-
## Operations
4-
5-
see [test_all.sh](../scripts/test_all.sh) for all operations
3+
## Overview
64

75
```shell
8-
# Start Docker containers
6+
# Set up environment
97
docker compose up -d
8+
```
9+
10+
## Testing
1011

12+
see [test_all.sh](../scripts/test_all.sh) for all operations
13+
14+
```shell
1115
# Test all scripts
1216
bash scripts/test_all.sh
1317
```

notebooks/chat_with_tools.ipynb

Lines changed: 120 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/templates.ipynb

Lines changed: 0 additions & 45 deletions
This file was deleted.
File renamed without changes.

scripts/dify_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def run_workflow(
5454
ensure_ascii=False,
5555
)
5656
)
57-
logger.info(f"Input: {response['data']['outputs']['requirements']}, Output: {response['data']['outputs']['text']}")
57+
logger.info(f"Output: {response['data']['outputs']['text']}")
5858

5959

6060
if __name__ == "__main__":

scripts/qdrant_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
88
from template_langgraph.loggers import get_logger
9-
from template_langgraph.tools.qdrants import QdrantClientWrapper
9+
from template_langgraph.tools.qdrant_tool import QdrantClientWrapper
1010
from template_langgraph.utilities.csv_loaders import CsvLoaderWrapper
1111

1212
# Initialize the Typer application

scripts/test_all.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/bash
22

3+
# Stop on errors, undefined variables, and pipe failures
4+
set -euo pipefail
5+
6+
# Run Docker Compose to set up the environment
7+
docker compose up -d --wait
8+
39
# Qdrant
410
uv run python scripts/qdrant_operator.py --help
511
uv run python scripts/qdrant_operator.py delete-collection --collection-name qa_kabuto --verbose
@@ -24,7 +30,7 @@ AGENT_NAMES=(
2430
"task_decomposer_agent"
2531
)
2632
for AGENT_NAME in "${AGENT_NAMES[@]}"; do
27-
uv run python scripts/agent_runner.py png --name "$AGENT_NAME" --verbose --output "generated/${AGENT_NAME}.png"
33+
uv run python scripts/agent_operator.py png --name "$AGENT_NAME" --verbose --output "generated/${AGENT_NAME}.png"
2834
done
2935

3036
## Run agents
@@ -38,5 +44,8 @@ NAME_QUESTION_ARRAY=(
3844
for NAME_QUESTION in "${NAME_QUESTION_ARRAY[@]}"; do
3945
IFS=':' read -r AGENT_NAME QUESTION <<< "$NAME_QUESTION"
4046
echo "Running agent: $AGENT_NAME with question: $QUESTION"
41-
uv run python scripts/agent_runner.py run --name "$AGENT_NAME" --verbose --question "$QUESTION"
47+
uv run python scripts/agent_operator.py run --name "$AGENT_NAME" --verbose --question "$QUESTION"
4248
done
49+
50+
# Clean up Docker Compose environment
51+
docker compose down --remove-orphans

template_langgraph/agents/chat_with_tools_agent/agent.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from template_langgraph.agents.chat_with_tools_agent.models import AgentState
77
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
88
from template_langgraph.loggers import get_logger
9+
from template_langgraph.tools.dify_tool import run_dify_workflow
910
from template_langgraph.tools.elasticsearch_tool import search_elasticsearch
10-
from template_langgraph.tools.qdrants import search_qdrant
11+
from template_langgraph.tools.qdrant_tool import search_qdrant
1112

1213
logger = get_logger(__name__)
1314

@@ -39,6 +40,11 @@ def __call__(self, inputs: dict):
3940
class ChatWithToolsAgent:
4041
def __init__(self):
4142
self.llm = AzureOpenAiWrapper().chat_model
43+
self.tools = [
44+
run_dify_workflow,
45+
search_qdrant,
46+
search_elasticsearch,
47+
]
4248

4349
def create_graph(self):
4450
"""Create the main graph for the agent."""
@@ -50,10 +56,7 @@ def create_graph(self):
5056
workflow.add_node(
5157
"tools",
5258
BasicToolNode(
53-
tools=[
54-
search_qdrant,
55-
search_elasticsearch,
56-
]
59+
tools=self.tools,
5760
),
5861
)
5962

@@ -78,10 +81,7 @@ def chat_with_tools(self, state: AgentState) -> AgentState:
7881
"""Chat with tools using the state."""
7982
logger.info(f"Chatting with tools using state: {state}")
8083
llm_with_tools = self.llm.bind_tools(
81-
tools=[
82-
search_qdrant,
83-
search_elasticsearch,
84-
],
84+
tools=self.tools,
8585
)
8686
return {
8787
"messages": [

template_langgraph/agents/kabuto_helpdesk_agent/agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
44
from template_langgraph.loggers import get_logger
5+
from template_langgraph.tools.dify_tool import run_dify_workflow
56
from template_langgraph.tools.elasticsearch_tool import search_elasticsearch
6-
from template_langgraph.tools.qdrants import search_qdrant
7+
from template_langgraph.tools.qdrant_tool import search_qdrant
78

89
logger = get_logger(__name__)
910

@@ -13,6 +14,7 @@ def __init__(self, tools=None):
1314
if tools is None:
1415
# Default tool for searching Qdrant
1516
tools = [
17+
run_dify_workflow,
1618
search_qdrant,
1719
search_elasticsearch,
1820
# Add other tools as needed

template_langgraph/tools/dify_tool.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from functools import lru_cache
22

33
import httpx
4+
from langchain.tools import tool
5+
from pydantic import BaseModel, Field
46
from pydantic_settings import BaseSettings, SettingsConfigDict
57

68

@@ -48,3 +50,36 @@ def run_workflow(
4850
)
4951
response.raise_for_status()
5052
return response.json()
53+
54+
55+
class DifyWorkflowInput(BaseModel):
56+
requirements: str = Field(
57+
default="生成 AI のサービス概要を教えてください。日本語でお願いします",
58+
description="Requirements for running the Dify workflow",
59+
)
60+
61+
62+
class DifyWorkflowOutput(BaseModel):
63+
response: dict = Field(description="Output data from the Dify workflow")
64+
65+
66+
@tool(args_schema=DifyWorkflowInput)
67+
def run_dify_workflow(
68+
requirements: str = "生成 AI のサービス概要を教えてください。日本語でお願いします",
69+
) -> DifyWorkflowOutput:
70+
"""
71+
Difyワークフローを実行します。
72+
指定された入力パラメータでワークフローを実行し、結果を返します。
73+
"""
74+
wrapper = DifyClientWrapper()
75+
response = wrapper.run_workflow(
76+
inputs={
77+
"inputs": {
78+
"requirements": requirements,
79+
},
80+
"response_mode": "blocking",
81+
"user": "abc-123",
82+
}
83+
)
84+
85+
return DifyWorkflowOutput(response=response)

0 commit comments

Comments
 (0)