File tree Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ QDRANT_URL="http://localhost:6333"
29
29
# # Elasticsearch Settings
30
30
ELASTICSEARCH_URL = " http://localhost:9200"
31
31
32
+ # # Dify Settings
33
+ DIFY_API_URL = " https://api.dify.ai/v1"
34
+ DIFY_API_KEY = " xxx"
35
+
32
36
# ---------
33
37
# Utilities
34
38
# ---------
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ readme = "README.md"
6
6
requires-python = " >=3.10"
7
7
dependencies = [
8
8
" elasticsearch>=9.1.0" ,
9
+ " httpx>=0.28.1" ,
9
10
" langchain-community>=0.3.27" ,
10
11
" langchain-openai>=0.3.28" ,
11
12
" langchain-text-splitters>=0.3.9" ,
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments