You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin.py 12KB

Introduce Plugins (#13836) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: xhe <xw897002528@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: takatost <takatost@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: AkaraChen <85140972+AkaraChen@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Hiroki Nagai <82458324+nagaihiroki-git@users.noreply.github.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: Ademílson Tonato <ademilson.tonato@refurbed.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: Yueh-Po Peng (Yabi) <94939112+y10ab1@users.noreply.github.com> Co-authored-by: Jason <ggbbddjm@gmail.com> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: yjc980121 <3898524+yjc980121@users.noreply.github.com> Co-authored-by: heyszt <36215648+hieheihei@users.noreply.github.com> Co-authored-by: Abdullah AlOsaimi <osaimiacc@gmail.com> Co-authored-by: Abdullah AlOsaimi <189027247+osaimi@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: zuodongxu <192560071+zuodongxu@users.noreply.github.com> Co-authored-by: Masashi Tomooka <tmokmss@users.noreply.github.com> Co-authored-by: aplio <ryo.091219@gmail.com> Co-authored-by: Obada Khalili <54270856+obadakhalili@users.noreply.github.com> Co-authored-by: Nam Vu <zuzoovn@gmail.com> Co-authored-by: Kei YAMAZAKI <1715090+kei-yamazaki@users.noreply.github.com> Co-authored-by: TechnoHouse <13776377+deephbz@users.noreply.github.com> Co-authored-by: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Co-authored-by: MaFee921 <31881301+2284730142@users.noreply.github.com> Co-authored-by: te-chan <t-nakanome@sakura-is.co.jp> Co-authored-by: HQidea <HQidea@users.noreply.github.com> Co-authored-by: Joshbly <36315710+Joshbly@users.noreply.github.com> Co-authored-by: xhe <xw897002528@gmail.com> Co-authored-by: weiwenyan-dev <154779315+weiwenyan-dev@users.noreply.github.com> Co-authored-by: ex_wenyan.wei <ex_wenyan.wei@tcl.com> Co-authored-by: engchina <12236799+engchina@users.noreply.github.com> Co-authored-by: engchina <atjapan2015@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: Kemal <kemalmeler@outlook.com> Co-authored-by: Lazy_Frog <4590648+lazyFrogLOL@users.noreply.github.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: Steven sun <98230804+Tuyohai@users.noreply.github.com> Co-authored-by: steven <sunzwj@digitalchina.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: Katy Tao <34019945+KatyTao@users.noreply.github.com> Co-authored-by: depy <42985524+h4ckdepy@users.noreply.github.com> Co-authored-by: 胡春东 <gycm520@gmail.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: MuYu <mr.muzea@gmail.com> Co-authored-by: Naoki Takashima <39912547+takatea@users.noreply.github.com> Co-authored-by: Summer-Gu <37869445+gubinjie@users.noreply.github.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: ybalbert001 <120714773+ybalbert001@users.noreply.github.com> Co-authored-by: Yuanbo Li <ybalbert@amazon.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Wu Jiayang <62842862+Wu-Jiayang@users.noreply.github.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: kimjion <45935338+kimjion@users.noreply.github.com> Co-authored-by: AugNSo <song.tiankai@icloud.com> Co-authored-by: llinvokerl <38915183+llinvokerl@users.noreply.github.com> Co-authored-by: liusurong.lsr <liusurong.lsr@alibaba-inc.com> Co-authored-by: Vasu Negi <vasu-negi@users.noreply.github.com> Co-authored-by: Hundredwz <1808096180@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. from flask_restful import Resource # type: ignore
  2. from controllers.console.wraps import setup_required
  3. from controllers.inner_api import api
  4. from controllers.inner_api.plugin.wraps import get_user_tenant, plugin_data
  5. from controllers.inner_api.wraps import plugin_inner_api_only
  6. from core.file.helpers import get_signed_file_url_for_plugin
  7. from core.model_runtime.utils.encoders import jsonable_encoder
  8. from core.plugin.backwards_invocation.app import PluginAppBackwardsInvocation
  9. from core.plugin.backwards_invocation.base import BaseBackwardsInvocationResponse
  10. from core.plugin.backwards_invocation.encrypt import PluginEncrypter
  11. from core.plugin.backwards_invocation.model import PluginModelBackwardsInvocation
  12. from core.plugin.backwards_invocation.node import PluginNodeBackwardsInvocation
  13. from core.plugin.backwards_invocation.tool import PluginToolBackwardsInvocation
  14. from core.plugin.entities.request import (
  15. RequestInvokeApp,
  16. RequestInvokeEncrypt,
  17. RequestInvokeLLM,
  18. RequestInvokeModeration,
  19. RequestInvokeParameterExtractorNode,
  20. RequestInvokeQuestionClassifierNode,
  21. RequestInvokeRerank,
  22. RequestInvokeSpeech2Text,
  23. RequestInvokeSummary,
  24. RequestInvokeTextEmbedding,
  25. RequestInvokeTool,
  26. RequestInvokeTTS,
  27. RequestRequestUploadFile,
  28. )
  29. from core.tools.entities.tool_entities import ToolProviderType
  30. from libs.helper import compact_generate_response
  31. from models.account import Account, Tenant
  32. from models.model import EndUser
  33. class PluginInvokeLLMApi(Resource):
  34. @setup_required
  35. @plugin_inner_api_only
  36. @get_user_tenant
  37. @plugin_data(payload_type=RequestInvokeLLM)
  38. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeLLM):
  39. def generator():
  40. response = PluginModelBackwardsInvocation.invoke_llm(user_model.id, tenant_model, payload)
  41. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  42. return compact_generate_response(generator())
  43. class PluginInvokeTextEmbeddingApi(Resource):
  44. @setup_required
  45. @plugin_inner_api_only
  46. @get_user_tenant
  47. @plugin_data(payload_type=RequestInvokeTextEmbedding)
  48. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
  49. try:
  50. return jsonable_encoder(
  51. BaseBackwardsInvocationResponse(
  52. data=PluginModelBackwardsInvocation.invoke_text_embedding(
  53. user_id=user_model.id,
  54. tenant=tenant_model,
  55. payload=payload,
  56. )
  57. )
  58. )
  59. except Exception as e:
  60. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  61. class PluginInvokeRerankApi(Resource):
  62. @setup_required
  63. @plugin_inner_api_only
  64. @get_user_tenant
  65. @plugin_data(payload_type=RequestInvokeRerank)
  66. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeRerank):
  67. try:
  68. return jsonable_encoder(
  69. BaseBackwardsInvocationResponse(
  70. data=PluginModelBackwardsInvocation.invoke_rerank(
  71. user_id=user_model.id,
  72. tenant=tenant_model,
  73. payload=payload,
  74. )
  75. )
  76. )
  77. except Exception as e:
  78. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  79. class PluginInvokeTTSApi(Resource):
  80. @setup_required
  81. @plugin_inner_api_only
  82. @get_user_tenant
  83. @plugin_data(payload_type=RequestInvokeTTS)
  84. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTTS):
  85. def generator():
  86. response = PluginModelBackwardsInvocation.invoke_tts(
  87. user_id=user_model.id,
  88. tenant=tenant_model,
  89. payload=payload,
  90. )
  91. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  92. return compact_generate_response(generator())
  93. class PluginInvokeSpeech2TextApi(Resource):
  94. @setup_required
  95. @plugin_inner_api_only
  96. @get_user_tenant
  97. @plugin_data(payload_type=RequestInvokeSpeech2Text)
  98. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
  99. try:
  100. return jsonable_encoder(
  101. BaseBackwardsInvocationResponse(
  102. data=PluginModelBackwardsInvocation.invoke_speech2text(
  103. user_id=user_model.id,
  104. tenant=tenant_model,
  105. payload=payload,
  106. )
  107. )
  108. )
  109. except Exception as e:
  110. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  111. class PluginInvokeModerationApi(Resource):
  112. @setup_required
  113. @plugin_inner_api_only
  114. @get_user_tenant
  115. @plugin_data(payload_type=RequestInvokeModeration)
  116. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeModeration):
  117. try:
  118. return jsonable_encoder(
  119. BaseBackwardsInvocationResponse(
  120. data=PluginModelBackwardsInvocation.invoke_moderation(
  121. user_id=user_model.id,
  122. tenant=tenant_model,
  123. payload=payload,
  124. )
  125. )
  126. )
  127. except Exception as e:
  128. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  129. class PluginInvokeToolApi(Resource):
  130. @setup_required
  131. @plugin_inner_api_only
  132. @get_user_tenant
  133. @plugin_data(payload_type=RequestInvokeTool)
  134. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTool):
  135. def generator():
  136. return PluginToolBackwardsInvocation.convert_to_event_stream(
  137. PluginToolBackwardsInvocation.invoke_tool(
  138. tenant_id=tenant_model.id,
  139. user_id=user_model.id,
  140. tool_type=ToolProviderType.value_of(payload.tool_type),
  141. provider=payload.provider,
  142. tool_name=payload.tool,
  143. tool_parameters=payload.tool_parameters,
  144. ),
  145. )
  146. return compact_generate_response(generator())
  147. class PluginInvokeParameterExtractorNodeApi(Resource):
  148. @setup_required
  149. @plugin_inner_api_only
  150. @get_user_tenant
  151. @plugin_data(payload_type=RequestInvokeParameterExtractorNode)
  152. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeParameterExtractorNode):
  153. try:
  154. return jsonable_encoder(
  155. BaseBackwardsInvocationResponse(
  156. data=PluginNodeBackwardsInvocation.invoke_parameter_extractor(
  157. tenant_id=tenant_model.id,
  158. user_id=user_model.id,
  159. parameters=payload.parameters,
  160. model_config=payload.model,
  161. instruction=payload.instruction,
  162. query=payload.query,
  163. )
  164. )
  165. )
  166. except Exception as e:
  167. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  168. class PluginInvokeQuestionClassifierNodeApi(Resource):
  169. @setup_required
  170. @plugin_inner_api_only
  171. @get_user_tenant
  172. @plugin_data(payload_type=RequestInvokeQuestionClassifierNode)
  173. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeQuestionClassifierNode):
  174. try:
  175. return jsonable_encoder(
  176. BaseBackwardsInvocationResponse(
  177. data=PluginNodeBackwardsInvocation.invoke_question_classifier(
  178. tenant_id=tenant_model.id,
  179. user_id=user_model.id,
  180. query=payload.query,
  181. model_config=payload.model,
  182. classes=payload.classes,
  183. instruction=payload.instruction,
  184. )
  185. )
  186. )
  187. except Exception as e:
  188. return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
  189. class PluginInvokeAppApi(Resource):
  190. @setup_required
  191. @plugin_inner_api_only
  192. @get_user_tenant
  193. @plugin_data(payload_type=RequestInvokeApp)
  194. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeApp):
  195. response = PluginAppBackwardsInvocation.invoke_app(
  196. app_id=payload.app_id,
  197. user_id=user_model.id,
  198. tenant_id=tenant_model.id,
  199. conversation_id=payload.conversation_id,
  200. query=payload.query,
  201. stream=payload.response_mode == "streaming",
  202. inputs=payload.inputs,
  203. files=payload.files,
  204. )
  205. return compact_generate_response(PluginAppBackwardsInvocation.convert_to_event_stream(response))
  206. class PluginInvokeEncryptApi(Resource):
  207. @setup_required
  208. @plugin_inner_api_only
  209. @get_user_tenant
  210. @plugin_data(payload_type=RequestInvokeEncrypt)
  211. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeEncrypt):
  212. """
  213. encrypt or decrypt data
  214. """
  215. try:
  216. return BaseBackwardsInvocationResponse(
  217. data=PluginEncrypter.invoke_encrypt(tenant_model, payload)
  218. ).model_dump()
  219. except Exception as e:
  220. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  221. class PluginInvokeSummaryApi(Resource):
  222. @setup_required
  223. @plugin_inner_api_only
  224. @get_user_tenant
  225. @plugin_data(payload_type=RequestInvokeSummary)
  226. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSummary):
  227. try:
  228. return BaseBackwardsInvocationResponse(
  229. data={
  230. "summary": PluginModelBackwardsInvocation.invoke_summary(
  231. user_id=user_model.id,
  232. tenant=tenant_model,
  233. payload=payload,
  234. )
  235. }
  236. ).model_dump()
  237. except Exception as e:
  238. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  239. class PluginUploadFileRequestApi(Resource):
  240. @setup_required
  241. @plugin_inner_api_only
  242. @get_user_tenant
  243. @plugin_data(payload_type=RequestRequestUploadFile)
  244. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestRequestUploadFile):
  245. # generate signed url
  246. url = get_signed_file_url_for_plugin(payload.filename, payload.mimetype, tenant_model.id, user_model.id)
  247. return BaseBackwardsInvocationResponse(data={"url": url}).model_dump()
  248. api.add_resource(PluginInvokeLLMApi, "/invoke/llm")
  249. api.add_resource(PluginInvokeTextEmbeddingApi, "/invoke/text-embedding")
  250. api.add_resource(PluginInvokeRerankApi, "/invoke/rerank")
  251. api.add_resource(PluginInvokeTTSApi, "/invoke/tts")
  252. api.add_resource(PluginInvokeSpeech2TextApi, "/invoke/speech2text")
  253. api.add_resource(PluginInvokeModerationApi, "/invoke/moderation")
  254. api.add_resource(PluginInvokeToolApi, "/invoke/tool")
  255. api.add_resource(PluginInvokeParameterExtractorNodeApi, "/invoke/parameter-extractor")
  256. api.add_resource(PluginInvokeQuestionClassifierNodeApi, "/invoke/question-classifier")
  257. api.add_resource(PluginInvokeAppApi, "/invoke/app")
  258. api.add_resource(PluginInvokeEncryptApi, "/invoke/encrypt")
  259. api.add_resource(PluginInvokeSummaryApi, "/invoke/summary")
  260. api.add_resource(PluginUploadFileRequestApi, "/upload/file/request")