mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
248 lines
10 KiB
Python
248 lines
10 KiB
Python
from typing import Any, Optional
|
|
|
|
from core.ops.entities.config_entity import BaseTracingConfig
|
|
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)
|
|
.where(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).where(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 == "arize" 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://app.arize.com/"})
|
|
|
|
if tracing_provider == "phoenix" 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://app.phoenix.arize.com/projects/"})
|
|
|
|
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": f"{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/"})
|
|
|
|
if tracing_provider == "aliyun" 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://arms.console.aliyun.com/"})
|
|
|
|
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:
|
|
"""
|
|
try:
|
|
provider_config_map[tracing_provider]
|
|
except KeyError:
|
|
return {"error": f"Invalid tracing provider: {tracing_provider}"}
|
|
|
|
provider_config: dict[str, Any] = provider_config_map[tracing_provider]
|
|
config_class: type[BaseTracingConfig] = provider_config["config_class"]
|
|
other_keys: list[str] = provider_config["other_keys"]
|
|
|
|
default_config_instance: BaseTracingConfig = config_class(**tracing_config)
|
|
for key in other_keys:
|
|
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 in ("arize", "phoenix"):
|
|
try:
|
|
project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider)
|
|
except Exception:
|
|
project_url = None
|
|
elif tracing_provider == "langfuse":
|
|
try:
|
|
project_key = OpsTraceManager.get_trace_config_project_key(tracing_config, tracing_provider)
|
|
project_url = f"{tracing_config.get('host')}/project/{project_key}"
|
|
except Exception:
|
|
project_url = None
|
|
elif tracing_provider in ("langsmith", "opik"):
|
|
try:
|
|
project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider)
|
|
except Exception:
|
|
project_url = None
|
|
else:
|
|
project_url = None
|
|
|
|
# check if trace config already exists
|
|
trace_config_data: Optional[TraceAppConfig] = (
|
|
db.session.query(TraceAppConfig)
|
|
.where(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).where(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:
|
|
"""
|
|
try:
|
|
provider_config_map[tracing_provider]
|
|
except KeyError:
|
|
raise ValueError(f"Invalid tracing provider: {tracing_provider}")
|
|
|
|
# check if trace config already exists
|
|
current_trace_config = (
|
|
db.session.query(TraceAppConfig)
|
|
.where(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).where(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)
|
|
.where(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
|