æ¬è¨äºã¯ Qiita Dify Advent Calendar 2024 ã® 15æ¥ç®ã®è¨äºã§ãã
åç½®ã
ä¼ç¤¾ã§ Dify Communitiyçï¼ã»ã«ããã¹ãç°å¢ï¼ã AWS ã§åããã¦ãã¾ãã
æ§æ㯠Dify on AWS with CDK ã«éããªãè¿ãã§ãã
- Terraform ã§æ§ç¯
- ElastiCache Valkey ãå©ç¨ï¼Redis ãã移è¡ãã¾ããï¼
ã大ããªéãã§ããããã
ãã¨ã¯ã社å¡éå®ã¢ã¯ã»ã¹ã¨ãããã ALB 㧠Google èªè¨¼ããã¦ã¾ãã
ã¨ããããªæãã®ç°å¢ã§ç¤¾å
ã§æ´»çºã«å©ç¨ããå§ãã¾ããã
ã¿ãªããã«ã©ãã©ãã¢ããªãä½ã£ã¦ãããããã®ã§åºæ¬çã« ã¨ãã£ã¿ã¼
権éãä»ä¸ãã¦ãã¾ãã
ããããã¨ãä¸é¨ã®é¨ç½²ããï¼è¥å¹²ï¼æ©å¯æ§ã®ããè³æããã¬ãã¸ã«ããã¦å©ç¨ãããã¨ããè¦æãããã¾ããã
Difyä¸ã§ããã¬ãã¸ã¸ã®ã¢ã¯ã»ã¹ã¯ãã¬ãã¸åä½ã§å¶å¾¡ã§ããè¨å®ã¯ããã®ã§è¯ãã®ã§ãããä¼è©±ãã°ãä»ã®äººã«è¦ãããããªãã¨ã®ãã¨ã
ç¾ç¶ã® Dify ã§ã¯ ã¨ãã£ã¿ã¼
権éããã°ãä»ã®äººãä½ã£ãã¢ããªã®ä¼è©±ãã°ãè¦ãã¦ãã¾ãã¾ãã
ãã®è¨äºã§ã¯ããã®ä¼è©±ãã°ã¸ã®ã¢ã¯ã»ã¹ã
- Difyã®ã½ã¼ã¹ã¯å¤æ´ããã«ããªã¼ãã¼ã»ç®¡çè ã»ã¢ããªä½æè ã«å¶éããä»çµã¿
ã«ã¤ãã¦æ¸ãã¾ãã
ãªããä¸æ¦ç»é¢ã使ã£ã¦ã®ã¢ã¯ã»ã¹ã®ã¿ã«ã¤ãã¦å¯¾å¦ãããã®ã§ãã
äºå注æäºé
æ¬è¨äºã§ã®æ¹æ³ã¯ v0.13.2 ã®æç¹ã§åä½ã§ãã¦ãããã®ã§ãããå°æ¥ã«ããããã®åä½ãä¿è¨¼ããããã®ã§ã¯ããã¾ããã
ã¾ããæ¬è¨äºãåèã«åçãªãã®ãä½æããããã«ãã£ã¦çãããããªãçµæãå½æ¹ã§ã¯è²¬ä»»ã¯ã¨ãã¾ããã
ä»çµã¿
ããã¾ããªä»çµã¿ã¯ä»¥ä¸ã®éãã§ãã
- API ã®å段ã«ãªã¼ãã¼ã¹ããã¯ã·ã¨ã㦠Nginxï¼å®é㯠OpenRestyï¼ãé ç½®
/console/api/apps/<uuid>/chat-conversations
ã¸ã®ã¢ã¯ã»ã¹æã« Lua ãåãã- Lua ã§DBã«ã¢ã¯ã»ã¹ããé²è¦§ãã¦è¯ã人ãã©ãããå¤æãã
- OKãªã API ã³ã³ããã«ãªãã¼ã¹ããã¯ã·ãã
- NGãªã 403 Forbidden ã«ãã
æ§æãã¤ã¡ã¼ã¸åããã¨ä»¥ä¸ã®ãããªæãã§ã
以ä¸ã§ãããå°ã詳ããæ¸ãã¦ããã¾ãã
ãªãã¼ã¹ããã¯ã·
ä»åãLua ãå©ç¨ããã®ã§OpenRestyã³ã³ãããå©ç¨ãã¦ãã¾ãã ããã§ã以ä¸ã®ãããªè¨å®ããã¦ããã¾ãã
location ~ /console/api/apps/(?<app_id>[^/]+)/chat-conversations { access_by_lua_file '/usr/local/openresty/lua-scripts/auth.lua'; proxy_pass http://localhost:5001; include proxy.conf; }
localhost:5001
ã API ã³ã³ããã®ã¢ã¯ã»ã¹å
ã«ãªãã¾ãã
ECSã«ãã㦠API ã³ã³ãã㨠OpenResty ã³ã³ãããåä¸ã¿ã¹ã¯å
ã§å®ç¾©ãã¦ããã®ã§ããã®ãããªã¢ã¯ã»ã¹æ¹æ³ã¨ãªã£ã¦ãã¾ãã
ã¾ãããã°ï¼ã¢ãã¦ã³ã¹
ã¯
/app/<uuid>/logs
ã¨ãããã¹ã§ããããã®ä¸ã®å¦çã§å®éã®ä¼è©±ãã°ãåå¾ãããããã«ãªã£ã¦ããããã®éã®ã¢ã¯ã»ã¹å ã
/console/api/apps/<uuid>/chat-conversations
ã¨ãããããªãã®ãªã®ã§ãããã¸ã®ã¢ã¯ã»ã¹ããã£ããã« Lua ãå®è¡ãããããã«ãªã£ã¦ãã¾ã
Lua
Lua ã§ãã£ã¦ãããã¨ã¯ãããã以ä¸ã®éãã§ãï¼ã½ã¼ã¹å ¬éã¯æ§ãããã¦ããã ãã¾ãã以ä¸ãæç´ã«å®è£ ãã¦ããã ãã§ãï¼
Authorization
ãããã¼ããã³ã¼ããã¦ãuser_id ãå¾ãã- ãªã¯ã¨ã¹ãURIã«å«ã¾ããã¢ããªã±ã¼ã·ã§ã³IDããã¼ã«DBã«åãåãããã¢ããªã±ã¼ã·ã§ã³ã®ä½è
ã® id ãå¾ãã
- ã¢ããªã±ã¼ã·ã§ã³ä½è
ã®å ´åã¯
許å¯
ã¨ãªã
- ã¢ããªã±ã¼ã·ã§ã³ä½è
ã®å ´åã¯
- user_id ããã¼ã«ãã¦DBã«åãåããããã®ã¦ã¼ã¶ã¼ã®ãã¼ã«ãåå¾ããã
- ãã¼ã«ã
owner
oradmin
ã§ããã°ã許å¯
ã¨ãªã
- ãã¼ã«ã
- ä¸è¨ã«è©²å½ããªãå ´å㯠403 Forbidden ã¨ãªã
ãããã®ãã¤ã³ãã¨ãªãæ å ±ãå¾ã¯èª¬æãã¦çµããã«ãã¾ã
user_id ã®åå¾
ã¨ã³ããã¤ã³ãã¸ã®ã¢ã¯ã»ã¹æã«ã
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNDRmNTVlOTUtMDA3OC00YTJhLWIzMjMtYWQzNDZmNDYzMjkyIiwiZXhwIjoxNzMwMzQ2MzkzLCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.ymnvkzc96HnWX26CZ7i3vCAl9h8knLZNmTB6lMwGro0
ã®ãããªãªã¯ã¨ã¹ããããã¼ãä»ä¸ããã¾ãã
ãã®ãã¼ã¯ã³ã¯ https://github.com/langgenius/dify/blob/0.13.2/api/extensions/ext_login.py#L23-L37 ã«ããããã«
decoded = PassportService().verify(auth_token)
user_id = decoded.get("user_id")
ã«ã¦ãã³ã¼ããã㦠user_id ãåå¾ã§ãããã®ã§ã
ããã¦ãã³ã¼ãèªä½ã¯ https://github.com/langgenius/dify/blob/0.13.2/api/libs/passport.py#L16 ã«ããããã«
jwt.decode(token, self.sk, algorithms=["HS256"])
JWTã©ã¤ãã©ãªã使ã£ã¦ãã³ã¼ãããã¦ãã¾ã
self.sk
ã¯ç°å¢å¤æ°ã§ä¸ãã¦ãã SECRET_KEY
ãªã®ã§ãåçãªãã³ã¼ãã®å¦çã Lua ã§è¡ããã¨ã«ãã user-id ãåå¾ã§ããã¨ãããã¨ã§ã
ã¢ããªã±ã¼ã·ã§ã³ä½æè
ã¢ããªã±ã¼ã·ã§ã³ã®æ
å ±ã¯ apps
ãã¼ãã«ã«ããã¾ã
Table "public.apps" Column | Type | Collation | Nullable | Default -------------------------+-----------------------------+-----------+----------+----------------------------- id | uuid | | not null | uuid_generate_v4() tenant_id | uuid | | not null | name | character varying(255) | | not null | mode | character varying(255) | | not null | icon | character varying(255) | | | icon_background | character varying(255) | | | app_model_config_id | uuid | | | status | character varying(255) | | not null | 'normal'::character varying enable_site | boolean | | not null | enable_api | boolean | | not null | api_rpm | integer | | not null | 0 api_rph | integer | | not null | 0 is_demo | boolean | | not null | false is_public | boolean | | not null | false created_at | timestamp without time zone | | not null | CURRENT_TIMESTAMP(0) updated_at | timestamp without time zone | | not null | CURRENT_TIMESTAMP(0) is_universal | boolean | | not null | false workflow_id | uuid | | | description | text | | not null | ''::character varying tracing | text | | | max_active_requests | integer | | | icon_type | character varying(255) | | | created_by | uuid | | | updated_by | uuid | | | use_icon_as_answer_icon | boolean | | not null | false Indexes: "app_pkey" PRIMARY KEY, btree (id) "app_tenant_id_idx" btree (tenant_id) Referenced by: TABLE "tool_published_apps" CONSTRAINT "tool_published_apps_app_id_fkey" FOREIGN KEY (app_id) REFERENCES apps(id)
URLã®pathã«å«ã¾ãã UUID ããã®ãã¼ãã«ã® id
ã®å¤ãªã®ã§ããããã create_by
ã«ã©ã ãè¦ãã°ä½è
ã® user_id ããããã¾ãã
ããã¦ãèªè¨¼æ
å ±ã«å«ã¾ãã user_id ã¨æ¯è¼ããã°ãã¢ã¯ã»ã¹ãã¦ãã人ãã¢ããªã±ã¼ã·ã§ã³ã®ä½è
ãã©ããå¤æã§ããã¨ãããã¨ã§ãã
ãªã¼ãã¼ã管çè
ãã¼ã«ã¯ tenant_account_joins
ã§è¨å®ããã¦ããããã§ã
Table "public.tenant_account_joins" Column | Type | Collation | Nullable | Default ------------+-----------------------------+-----------+----------+----------------------------- id | uuid | | not null | uuid_generate_v4() tenant_id | uuid | | not null | account_id | uuid | | not null | role | character varying(16) | | not null | 'normal'::character varying invited_by | uuid | | | created_at | timestamp without time zone | | not null | CURRENT_TIMESTAMP(0) updated_at | timestamp without time zone | | not null | CURRENT_TIMESTAMP(0) current | boolean | | not null | false Indexes: "tenant_account_join_pkey" PRIMARY KEY, btree (id) "tenant_account_join_account_id_idx" btree (account_id) "tenant_account_join_tenant_id_idx" btree (tenant_id) "unique_tenant_account_join" UNIQUE CONSTRAINT, btree (tenant_id, account_id)
account_id
ã user_id ãªã®ã§ãããããã¢ã¯ã»ã¹ãã¦ãã人ã®ãã¼ã«ãåå¾ã§ãã¾ã
ã¾ã¨ã
Dify Communityçã«ããã¦ãDifyæ¬ä½ãä¿®æ£ããã«ãä¼è©±ãã°ã®é²è¦§ãå¶éããä»çµã¿ãä½ã£ã¦ã¿ã¾ãã
- OpenResty + Lua ãæ¡ç¨
- ãªã¯ã¨ã¹ãã«å«ã¾ããèªè¨¼æ å ±ããã¨ã« Dify DB ã«åãåããã¦ãé²è¦§æ¨©éã®è¨±å¯ãå¤æãã
ã¨ããä»çµã¿ã§ãã
以ä¸ãæå¾ã¾ã§ã覧ããã ããããã¨ããããã¾ãã