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.

ops_service.py 10KB

feat: Add W&B Weave Tracing Integration (#14262) Signed-off-by: Yuichiro Utsumi <utsumi.yuichiro@fujitsu.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: ChengZi <chen.zhang@zilliz.com> Signed-off-by: cl <cailue@apache.org> Co-authored-by: Yu Chun Chang <changyuchun159630@gmail.com> Co-authored-by: Kyle Chang <kylechang@91app.com> Co-authored-by: Lick-liu <51771897+Lick-liu@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: Yuichiro Utsumi <81412151+utsumi-fj@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: DDDDD12138 <43703884+DDDDD12138@users.noreply.github.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Lam <scau_ljw@126.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Mars <524574386@qq.com> Co-authored-by: mars <linjx2@by-health.com> Co-authored-by: Joe <79627742+ZhouhaoJiang@users.noreply.github.com> Co-authored-by: Rafael Carvalho <r.carvalho@me.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: codingjaguar <codingjaguar@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: Arcaner <52057416+lrhan321@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: XiaoBa <94062266+XiaoBa-Yu@users.noreply.github.com> Co-authored-by: Xiaoba Yu <xb1823725853@gmail.com> Co-authored-by: zhangyuhang <2827528315@qq.com> Co-authored-by: yuhang2.zhang <yuhang2.zhang@ly.com> Co-authored-by: 诗浓 <nyaashino@gmail.com> Co-authored-by: RookieAgent <42060616+Sakura4036@users.noreply.github.com> Co-authored-by: sho-takano-dev <shota.takano.dev@gmail.com> Co-authored-by: 過世秋風 <1040926235@qq.com> Co-authored-by: Yi Feng <66539215+bigyifeng@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: ShadowJobs <794878115@qq.com> Co-authored-by: LinYing <linying@momenta.ai> Co-authored-by: Benjamin <benjaminx@gmail.com> Co-authored-by: LiuBodong <liubodong2010@126.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: csurong <csurong1@gmail.com> Co-authored-by: 傻笑zz <43721571+shaxiaozz@users.noreply.github.com> Co-authored-by: L8ng <straydragonl@foxmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: LittleFish-15 <58618983+LittleFish-15@users.noreply.github.com> Co-authored-by: 诗浓 <844670992@qq.com> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: StoneFancyX <53338920+StoneFancyX@users.noreply.github.com> Co-authored-by: StoneFancyX <kindbin@qq.com> Co-authored-by: Naoki KOBAYASHI <naotama@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: cyflhn <cyflhn@163.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: jimmyfen <757343258@qq.com> Co-authored-by: Xuetao Song <xuetaomagicsong@gmail.com> Co-authored-by: Panpan <wurui.dev@gmail.com> Co-authored-by: wyy-holding <59436937+wyy-holding@users.noreply.github.com> Co-authored-by: リイノ Lin <sorphwer@gmail.com> Co-authored-by: Ning <accelerator314@gmail.com> Co-authored-by: Linh Nguyen <55907715+batman0911@users.noreply.github.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: Ron <svcvit@gmail.com> Co-authored-by: Novice <novice12185727@gmail.com> Co-authored-by: NanoNova <kid1412621@gmail.com> Co-authored-by: JaydenZhou <380774082@qq.com> Co-authored-by: dotdotdot <823150982@qq.com> Co-authored-by: Good Wood <slm_1990@126.com> Co-authored-by: Ryosei Karaki <38310693+karamaru-alpha@users.noreply.github.com> Co-authored-by: chenhuan0728 <54611342+chenhuan0728@users.noreply.github.com> Co-authored-by: chenhuan <huan.chen0728@foxmail> Co-authored-by: lenbo <islenbo@qq.com> Co-authored-by: Jiang <65766008+AlwaysBluer@users.noreply.github.com> Co-authored-by: jiangzhijie <jiangzhijie.jzj@alibaba-inc.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: zhangkun-21 <sephiroth0932@gmail.com> Co-authored-by: hsiong <37357447+hsiong@users.noreply.github.com> Co-authored-by: 李远军 <4842@9ji.com> Co-authored-by: yourchanges <yourchanges@gmail.com> Co-authored-by: David <guyuezhuying@126.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: taokuizu <taokuizu@qq.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: JimintheBox <gjwlals111@gmail.com> Co-authored-by: wlleiiwang <1025164922@qq.com> Co-authored-by: wlleiiwang <wlleiiwang@tencent.com> Co-authored-by: Alex <32982705+AlexYuan997@users.noreply.github.com> Co-authored-by: yuanlong <yuanlong@boco.com.cn> Co-authored-by: wanttobeamaster <45583625+wanttobeamaster@users.noreply.github.com> Co-authored-by: xiaozhiqing.xzq <xiaozhiqing.xzq@alibaba-inc.com> Co-authored-by: Chenhe Gu <guchenhe@gmail.com> Co-authored-by: tyounami <vkbo@qq.com> Co-authored-by: bo.zhao <bo.zhao@iglooinsure.com> Co-authored-by: ClSlaid <cailue@apache.org> Co-authored-by: adru <106513264+adpanru@users.noreply.github.com> Co-authored-by: horochx <32632779+horochx@users.noreply.github.com>
6 ay önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from typing import Any
  2. from core.ops.entities.config_entity import BaseTracingConfig
  3. from core.ops.ops_trace_manager import OpsTraceManager, provider_config_map
  4. from extensions.ext_database import db
  5. from models.model import App, TraceAppConfig
  6. class OpsService:
  7. @classmethod
  8. def get_tracing_app_config(cls, app_id: str, tracing_provider: str):
  9. """
  10. Get tracing app config
  11. :param app_id: app id
  12. :param tracing_provider: tracing provider
  13. :return:
  14. """
  15. trace_config_data: TraceAppConfig | None = (
  16. db.session.query(TraceAppConfig)
  17. .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
  18. .first()
  19. )
  20. if not trace_config_data:
  21. return None
  22. # decrypt_token and obfuscated_token
  23. app = db.session.query(App).where(App.id == app_id).first()
  24. if not app:
  25. return None
  26. tenant_id = app.tenant_id
  27. decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config(
  28. tenant_id, tracing_provider, trace_config_data.tracing_config
  29. )
  30. new_decrypt_tracing_config = OpsTraceManager.obfuscated_decrypt_token(tracing_provider, decrypt_tracing_config)
  31. if tracing_provider == "arize" and (
  32. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  33. ):
  34. try:
  35. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  36. new_decrypt_tracing_config.update({"project_url": project_url})
  37. except Exception:
  38. new_decrypt_tracing_config.update({"project_url": "https://app.arize.com/"})
  39. if tracing_provider == "phoenix" and (
  40. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  41. ):
  42. try:
  43. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  44. new_decrypt_tracing_config.update({"project_url": project_url})
  45. except Exception:
  46. new_decrypt_tracing_config.update({"project_url": "https://app.phoenix.arize.com/projects/"})
  47. if tracing_provider == "langfuse" and (
  48. "project_key" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_key")
  49. ):
  50. try:
  51. project_key = OpsTraceManager.get_trace_config_project_key(decrypt_tracing_config, tracing_provider)
  52. new_decrypt_tracing_config.update(
  53. {
  54. "project_url": "{host}/project/{key}".format(
  55. host=decrypt_tracing_config.get("host"), key=project_key
  56. )
  57. }
  58. )
  59. except Exception:
  60. new_decrypt_tracing_config.update({"project_url": f"{decrypt_tracing_config.get('host')}/"})
  61. if tracing_provider == "langsmith" and (
  62. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  63. ):
  64. try:
  65. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  66. new_decrypt_tracing_config.update({"project_url": project_url})
  67. except Exception:
  68. new_decrypt_tracing_config.update({"project_url": "https://smith.langchain.com/"})
  69. if tracing_provider == "opik" and (
  70. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  71. ):
  72. try:
  73. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  74. new_decrypt_tracing_config.update({"project_url": project_url})
  75. except Exception:
  76. new_decrypt_tracing_config.update({"project_url": "https://www.comet.com/opik/"})
  77. if tracing_provider == "weave" and (
  78. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  79. ):
  80. try:
  81. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  82. new_decrypt_tracing_config.update({"project_url": project_url})
  83. except Exception:
  84. new_decrypt_tracing_config.update({"project_url": "https://wandb.ai/"})
  85. if tracing_provider == "aliyun" and (
  86. "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
  87. ):
  88. try:
  89. project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
  90. new_decrypt_tracing_config.update({"project_url": project_url})
  91. except Exception:
  92. new_decrypt_tracing_config.update({"project_url": "https://arms.console.aliyun.com/"})
  93. trace_config_data.tracing_config = new_decrypt_tracing_config
  94. return trace_config_data.to_dict()
  95. @classmethod
  96. def create_tracing_app_config(cls, app_id: str, tracing_provider: str, tracing_config: dict):
  97. """
  98. Create tracing app config
  99. :param app_id: app id
  100. :param tracing_provider: tracing provider
  101. :param tracing_config: tracing config
  102. :return:
  103. """
  104. try:
  105. provider_config_map[tracing_provider]
  106. except KeyError:
  107. return {"error": f"Invalid tracing provider: {tracing_provider}"}
  108. provider_config: dict[str, Any] = provider_config_map[tracing_provider]
  109. config_class: type[BaseTracingConfig] = provider_config["config_class"]
  110. other_keys: list[str] = provider_config["other_keys"]
  111. default_config_instance: BaseTracingConfig = config_class(**tracing_config)
  112. for key in other_keys:
  113. if key in tracing_config and tracing_config[key] == "":
  114. tracing_config[key] = getattr(default_config_instance, key, None)
  115. # api check
  116. if not OpsTraceManager.check_trace_config_is_effective(tracing_config, tracing_provider):
  117. return {"error": "Invalid Credentials"}
  118. # get project url
  119. if tracing_provider in ("arize", "phoenix"):
  120. try:
  121. project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider)
  122. except Exception:
  123. project_url = None
  124. elif tracing_provider == "langfuse":
  125. try:
  126. project_key = OpsTraceManager.get_trace_config_project_key(tracing_config, tracing_provider)
  127. project_url = f"{tracing_config.get('host')}/project/{project_key}"
  128. except Exception:
  129. project_url = None
  130. elif tracing_provider in ("langsmith", "opik"):
  131. try:
  132. project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider)
  133. except Exception:
  134. project_url = None
  135. else:
  136. project_url = None
  137. # check if trace config already exists
  138. trace_config_data: TraceAppConfig | None = (
  139. db.session.query(TraceAppConfig)
  140. .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
  141. .first()
  142. )
  143. if trace_config_data:
  144. return None
  145. # get tenant id
  146. app = db.session.query(App).where(App.id == app_id).first()
  147. if not app:
  148. return None
  149. tenant_id = app.tenant_id
  150. tracing_config = OpsTraceManager.encrypt_tracing_config(tenant_id, tracing_provider, tracing_config)
  151. if project_url:
  152. tracing_config["project_url"] = project_url
  153. trace_config_data = TraceAppConfig(
  154. app_id=app_id,
  155. tracing_provider=tracing_provider,
  156. tracing_config=tracing_config,
  157. )
  158. db.session.add(trace_config_data)
  159. db.session.commit()
  160. return {"result": "success"}
  161. @classmethod
  162. def update_tracing_app_config(cls, app_id: str, tracing_provider: str, tracing_config: dict):
  163. """
  164. Update tracing app config
  165. :param app_id: app id
  166. :param tracing_provider: tracing provider
  167. :param tracing_config: tracing config
  168. :return:
  169. """
  170. try:
  171. provider_config_map[tracing_provider]
  172. except KeyError:
  173. raise ValueError(f"Invalid tracing provider: {tracing_provider}")
  174. # check if trace config already exists
  175. current_trace_config = (
  176. db.session.query(TraceAppConfig)
  177. .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
  178. .first()
  179. )
  180. if not current_trace_config:
  181. return None
  182. # get tenant id
  183. app = db.session.query(App).where(App.id == app_id).first()
  184. if not app:
  185. return None
  186. tenant_id = app.tenant_id
  187. tracing_config = OpsTraceManager.encrypt_tracing_config(
  188. tenant_id, tracing_provider, tracing_config, current_trace_config.tracing_config
  189. )
  190. # api check
  191. # decrypt_token
  192. decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config(tenant_id, tracing_provider, tracing_config)
  193. if not OpsTraceManager.check_trace_config_is_effective(decrypt_tracing_config, tracing_provider):
  194. raise ValueError("Invalid Credentials")
  195. current_trace_config.tracing_config = tracing_config
  196. db.session.commit()
  197. return current_trace_config.to_dict()
  198. @classmethod
  199. def delete_tracing_app_config(cls, app_id: str, tracing_provider: str):
  200. """
  201. Delete tracing app config
  202. :param app_id: app id
  203. :param tracing_provider: tracing provider
  204. :return:
  205. """
  206. trace_config = (
  207. db.session.query(TraceAppConfig)
  208. .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
  209. .first()
  210. )
  211. if not trace_config:
  212. return None
  213. db.session.delete(trace_config)
  214. db.session.commit()
  215. return True