mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-22 04:28:33 +08:00

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>
207 lines
8.2 KiB
Python
207 lines
8.2 KiB
Python
from typing import Optional
|
|
|
|
from core.ops.ops_trace_manager import OpsTraceManager, provider_config_map
|
|
from extensions.ext_database import db
|
|
from models.model import App, TraceAppConfig
|
|
|
|
|
|
class OpsService:
|
|
@classmethod
|
|
def get_tracing_app_config(cls, app_id: str, tracing_provider: str):
|
|
"""
|
|
Get tracing app config
|
|
:param app_id: app id
|
|
:param tracing_provider: tracing provider
|
|
:return:
|
|
"""
|
|
trace_config_data: Optional[TraceAppConfig] = (
|
|
db.session.query(TraceAppConfig)
|
|
.filter(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
|
|
.first()
|
|
)
|
|
|
|
if not trace_config_data:
|
|
return None
|
|
|
|
# decrypt_token and obfuscated_token
|
|
app = db.session.query(App).filter(App.id == app_id).first()
|
|
if not app:
|
|
return None
|
|
tenant_id = app.tenant_id
|
|
decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config(
|
|
tenant_id, tracing_provider, trace_config_data.tracing_config
|
|
)
|
|
new_decrypt_tracing_config = OpsTraceManager.obfuscated_decrypt_token(tracing_provider, decrypt_tracing_config)
|
|
|
|
if tracing_provider == "langfuse" and (
|
|
"project_key" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_key")
|
|
):
|
|
try:
|
|
project_key = OpsTraceManager.get_trace_config_project_key(decrypt_tracing_config, tracing_provider)
|
|
new_decrypt_tracing_config.update(
|
|
{
|
|
"project_url": "{host}/project/{key}".format(
|
|
host=decrypt_tracing_config.get("host"), key=project_key
|
|
)
|
|
}
|
|
)
|
|
except Exception:
|
|
new_decrypt_tracing_config.update(
|
|
{"project_url": "{host}/".format(host=decrypt_tracing_config.get("host"))}
|
|
)
|
|
|
|
if tracing_provider == "langsmith" and (
|
|
"project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
|
|
):
|
|
try:
|
|
project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
|
|
new_decrypt_tracing_config.update({"project_url": project_url})
|
|
except Exception:
|
|
new_decrypt_tracing_config.update({"project_url": "https://smith.langchain.com/"})
|
|
|
|
if tracing_provider == "opik" and (
|
|
"project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
|
|
):
|
|
try:
|
|
project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
|
|
new_decrypt_tracing_config.update({"project_url": project_url})
|
|
except Exception:
|
|
new_decrypt_tracing_config.update({"project_url": "https://www.comet.com/opik/"})
|
|
if tracing_provider == "weave" and (
|
|
"project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url")
|
|
):
|
|
try:
|
|
project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider)
|
|
new_decrypt_tracing_config.update({"project_url": project_url})
|
|
except Exception:
|
|
new_decrypt_tracing_config.update({"project_url": "https://wandb.ai/"})
|
|
trace_config_data.tracing_config = new_decrypt_tracing_config
|
|
return trace_config_data.to_dict()
|
|
|
|
@classmethod
|
|
def create_tracing_app_config(cls, app_id: str, tracing_provider: str, tracing_config: dict):
|
|
"""
|
|
Create tracing app config
|
|
:param app_id: app id
|
|
:param tracing_provider: tracing provider
|
|
:param tracing_config: tracing config
|
|
:return:
|
|
"""
|
|
if tracing_provider not in provider_config_map and tracing_provider:
|
|
return {"error": f"Invalid tracing provider: {tracing_provider}"}
|
|
|
|
config_class, other_keys = (
|
|
provider_config_map[tracing_provider]["config_class"],
|
|
provider_config_map[tracing_provider]["other_keys"],
|
|
)
|
|
# FIXME: ignore type error
|
|
default_config_instance = config_class(**tracing_config) # type: ignore
|
|
for key in other_keys: # type: ignore
|
|
if key in tracing_config and tracing_config[key] == "":
|
|
tracing_config[key] = getattr(default_config_instance, key, None)
|
|
|
|
# api check
|
|
if not OpsTraceManager.check_trace_config_is_effective(tracing_config, tracing_provider):
|
|
return {"error": "Invalid Credentials"}
|
|
|
|
# get project url
|
|
if tracing_provider == "langfuse":
|
|
project_key = OpsTraceManager.get_trace_config_project_key(tracing_config, tracing_provider)
|
|
project_url = "{host}/project/{key}".format(host=tracing_config.get("host"), key=project_key)
|
|
elif tracing_provider in ("langsmith", "opik"):
|
|
project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider)
|
|
else:
|
|
project_url = None
|
|
|
|
# check if trace config already exists
|
|
trace_config_data: Optional[TraceAppConfig] = (
|
|
db.session.query(TraceAppConfig)
|
|
.filter(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
|
|
.first()
|
|
)
|
|
|
|
if trace_config_data:
|
|
return None
|
|
|
|
# get tenant id
|
|
app = db.session.query(App).filter(App.id == app_id).first()
|
|
if not app:
|
|
return None
|
|
tenant_id = app.tenant_id
|
|
tracing_config = OpsTraceManager.encrypt_tracing_config(tenant_id, tracing_provider, tracing_config)
|
|
if project_url:
|
|
tracing_config["project_url"] = project_url
|
|
trace_config_data = TraceAppConfig(
|
|
app_id=app_id,
|
|
tracing_provider=tracing_provider,
|
|
tracing_config=tracing_config,
|
|
)
|
|
db.session.add(trace_config_data)
|
|
db.session.commit()
|
|
|
|
return {"result": "success"}
|
|
|
|
@classmethod
|
|
def update_tracing_app_config(cls, app_id: str, tracing_provider: str, tracing_config: dict):
|
|
"""
|
|
Update tracing app config
|
|
:param app_id: app id
|
|
:param tracing_provider: tracing provider
|
|
:param tracing_config: tracing config
|
|
:return:
|
|
"""
|
|
if tracing_provider not in provider_config_map:
|
|
raise ValueError(f"Invalid tracing provider: {tracing_provider}")
|
|
|
|
# check if trace config already exists
|
|
current_trace_config = (
|
|
db.session.query(TraceAppConfig)
|
|
.filter(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
|
|
.first()
|
|
)
|
|
|
|
if not current_trace_config:
|
|
return None
|
|
|
|
# get tenant id
|
|
app = db.session.query(App).filter(App.id == app_id).first()
|
|
if not app:
|
|
return None
|
|
tenant_id = app.tenant_id
|
|
tracing_config = OpsTraceManager.encrypt_tracing_config(
|
|
tenant_id, tracing_provider, tracing_config, current_trace_config.tracing_config
|
|
)
|
|
|
|
# api check
|
|
# decrypt_token
|
|
decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config(tenant_id, tracing_provider, tracing_config)
|
|
if not OpsTraceManager.check_trace_config_is_effective(decrypt_tracing_config, tracing_provider):
|
|
raise ValueError("Invalid Credentials")
|
|
|
|
current_trace_config.tracing_config = tracing_config
|
|
db.session.commit()
|
|
|
|
return current_trace_config.to_dict()
|
|
|
|
@classmethod
|
|
def delete_tracing_app_config(cls, app_id: str, tracing_provider: str):
|
|
"""
|
|
Delete tracing app config
|
|
:param app_id: app id
|
|
:param tracing_provider: tracing provider
|
|
:return:
|
|
"""
|
|
trace_config = (
|
|
db.session.query(TraceAppConfig)
|
|
.filter(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider)
|
|
.first()
|
|
)
|
|
|
|
if not trace_config:
|
|
return None
|
|
|
|
db.session.delete(trace_config)
|
|
db.session.commit()
|
|
|
|
return True
|