Skip to content

Commit 1037ee7

Browse files
authored
Merge pull request #27 from ks6088ts-labs/feature/issue-26_dify-tool
run Dify workflow
2 parents c496cfb + bb3fbde commit 1037ee7

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ QDRANT_URL="http://localhost:6333"
2929
## Elasticsearch Settings
3030
ELASTICSEARCH_URL="http://localhost:9200"
3131

32+
## Dify Settings
33+
DIFY_API_URL="https://api.dify.ai/v1"
34+
DIFY_API_KEY="xxx"
35+
3236
# ---------
3337
# Utilities
3438
# ---------

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
88
"elasticsearch>=9.1.0",
9+
"httpx>=0.28.1",
910
"langchain-community>=0.3.27",
1011
"langchain-openai>=0.3.28",
1112
"langchain-text-splitters>=0.3.9",

scripts/dify_operator.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import json
2+
import logging
3+
4+
import typer
5+
from dotenv import load_dotenv
6+
7+
from template_langgraph.loggers import get_logger
8+
from template_langgraph.tools.dify_tool import DifyClientWrapper
9+
10+
# Initialize the Typer application
11+
app = typer.Typer(
12+
add_completion=False,
13+
help="Dify operator CLI",
14+
)
15+
16+
# Set up logging
17+
logger = get_logger(__name__)
18+
19+
20+
@app.command()
21+
def run_workflow(
22+
requirements: str = typer.Option(
23+
"生成 AI のサービス概要を教えてください。日本語でお願いします",
24+
"--requirements",
25+
"-r",
26+
help="Requirements for running the Dify workflow",
27+
),
28+
verbose: bool = typer.Option(
29+
False,
30+
"--verbose",
31+
"-v",
32+
help="Enable verbose output",
33+
),
34+
):
35+
# Set up logging
36+
if verbose:
37+
logger.setLevel(logging.DEBUG)
38+
39+
logger.info("Running Dify workflow...")
40+
client = DifyClientWrapper()
41+
response = client.run_workflow(
42+
inputs={
43+
"inputs": {
44+
"requirements": requirements,
45+
},
46+
"response_mode": "blocking",
47+
"user": "abc-123",
48+
}
49+
)
50+
logger.info(
51+
json.dumps(
52+
response,
53+
indent=2,
54+
ensure_ascii=False,
55+
)
56+
)
57+
logger.info(f"Input: {response['data']['outputs']['requirements']}, Output: {response['data']['outputs']['text']}")
58+
59+
60+
if __name__ == "__main__":
61+
load_dotenv(
62+
override=True,
63+
verbose=True,
64+
)
65+
app()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from functools import lru_cache
2+
3+
import httpx
4+
from pydantic_settings import BaseSettings, SettingsConfigDict
5+
6+
7+
class Settings(BaseSettings):
8+
dify_base_url: str = "https://api.dify.ai/v1"
9+
dify_api_key: str = "<YOUR_API_KEY>"
10+
11+
model_config = SettingsConfigDict(
12+
env_file=".env",
13+
env_ignore_empty=True,
14+
extra="ignore",
15+
)
16+
17+
18+
@lru_cache
19+
def get_dify_settings() -> Settings:
20+
"""Get Dify settings."""
21+
return Settings()
22+
23+
24+
class DifyClientWrapper:
25+
def __init__(
26+
self,
27+
settings: Settings = None,
28+
):
29+
if settings is None:
30+
settings = get_dify_settings()
31+
self.base_url = settings.dify_base_url
32+
self.headers = {
33+
"Authorization": f"Bearer {settings.dify_api_key}",
34+
"Content-Type": "application/json",
35+
}
36+
37+
def run_workflow(
38+
self,
39+
inputs: dict,
40+
) -> dict:
41+
"""Run a Dify workflow."""
42+
with httpx.Client() as client:
43+
response = client.post(
44+
url=f"{self.base_url}/workflows/run",
45+
json=inputs,
46+
headers=self.headers,
47+
timeout=60 * 5, # Set a timeout for the request
48+
)
49+
response.raise_for_status()
50+
return response.json()

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)