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
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ QDRANT_URL="http://localhost:6333"
## Elasticsearch Settings
ELASTICSEARCH_URL="http://localhost:9200"

## Dify Settings
DIFY_API_URL="https://api.dify.ai/v1"
DIFY_API_KEY="xxx"

# ---------
# Utilities
# ---------
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"elasticsearch>=9.1.0",
"httpx>=0.28.1",
"langchain-community>=0.3.27",
"langchain-openai>=0.3.28",
"langchain-text-splitters>=0.3.9",
Expand Down
65 changes: 65 additions & 0 deletions scripts/dify_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import json
import logging

import typer
from dotenv import load_dotenv

from template_langgraph.loggers import get_logger
from template_langgraph.tools.dify_tool import DifyClientWrapper

# Initialize the Typer application
app = typer.Typer(
add_completion=False,
help="Dify operator CLI",
)

# Set up logging
logger = get_logger(__name__)


@app.command()
def run_workflow(
requirements: str = typer.Option(
"生成 AI のサービス概要を教えてください。日本語でお願いします",
"--requirements",
"-r",
help="Requirements for running the Dify workflow",
),
verbose: bool = typer.Option(
False,
"--verbose",
"-v",
help="Enable verbose output",
),
):
# Set up logging
if verbose:
logger.setLevel(logging.DEBUG)

logger.info("Running Dify workflow...")
client = DifyClientWrapper()
response = client.run_workflow(
inputs={
"inputs": {
"requirements": requirements,
},
"response_mode": "blocking",
"user": "abc-123",
}
)
logger.info(
json.dumps(
response,
indent=2,
ensure_ascii=False,
)
)
logger.info(f"Input: {response['data']['outputs']['requirements']}, Output: {response['data']['outputs']['text']}")


if __name__ == "__main__":
load_dotenv(
override=True,
verbose=True,
)
app()
50 changes: 50 additions & 0 deletions template_langgraph/tools/dify_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from functools import lru_cache

import httpx
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
dify_base_url: str = "https://api.dify.ai/v1"
dify_api_key: str = "<YOUR_API_KEY>"

model_config = SettingsConfigDict(
env_file=".env",
env_ignore_empty=True,
extra="ignore",
)


@lru_cache
def get_dify_settings() -> Settings:
"""Get Dify settings."""
return Settings()


class DifyClientWrapper:
def __init__(
self,
settings: Settings = None,
):
if settings is None:
settings = get_dify_settings()
self.base_url = settings.dify_base_url
self.headers = {
"Authorization": f"Bearer {settings.dify_api_key}",
"Content-Type": "application/json",
}

def run_workflow(
self,
inputs: dict,
) -> dict:
"""Run a Dify workflow."""
with httpx.Client() as client:
response = client.post(
url=f"{self.base_url}/workflows/run",
json=inputs,
headers=self.headers,
timeout=60 * 5, # Set a timeout for the request
)
response.raise_for_status()
return response.json()
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.