diff --git a/dingo/model/llm/rag/llm_rag_answer_relevancy.py b/dingo/model/llm/rag/llm_rag_answer_relevancy.py index 24c7f6e0..78d08aee 100644 --- a/dingo/model/llm/rag/llm_rag_answer_relevancy.py +++ b/dingo/model/llm/rag/llm_rag_answer_relevancy.py @@ -22,7 +22,7 @@ # 用于embedding的模型,支持OpenAI和HuggingFace class EmbeddingModel: """Embedding模型接口,支持OpenAI和HuggingFace模型""" - def __init__(self, model_name: str = "text-embedding-3-large", is_openai: bool = True): + def __init__(self, model_name: str = "text-embedding-3-large", is_openai: bool = True, api_key: str = None, base_url: str = None): self.is_openai = is_openai self.model_name = model_name @@ -32,8 +32,8 @@ def __init__(self, model_name: str = "text-embedding-3-large", is_openai: bool = from openai import OpenAI self.client = OpenAI( - api_key="API-KEY", - base_url="API-KEY-BASE-URL" + api_key=api_key, + base_url=base_url ) else: # 使用HuggingFace Embeddings @@ -127,7 +127,18 @@ def init_embedding_model(cls, model_name: str = "text-embedding-3-large"): """初始化embedding模型""" # 检查是否是OpenAI模型 is_openai = model_name.startswith("text-embedding-") - cls.embedding_model = EmbeddingModel(model_name, is_openai) + api_key = None + base_url = None + if is_openai: + # 从配置中获取API密钥和base_url + if not cls.dynamic_config.key: + raise ValueError("key cannot be empty in llm config.") + elif not cls.dynamic_config.api_url: + raise ValueError("api_url cannot be empty in llm config.") + else: + api_key = cls.dynamic_config.key + base_url = cls.dynamic_config.api_url + cls.embedding_model = EmbeddingModel(model_name, is_openai, api_key, base_url) @classmethod def build_messages(cls, input_data: Data) -> List: @@ -265,7 +276,7 @@ def eval(cls, input_data: Data) -> ModelRes: result = ModelRes() result.score = score - # 根据分数判断是否通过(默认阈值5,满分10分) + # 根据分数判断是否通过,默认阈值为5 threshold = 5 if hasattr(cls, 'dynamic_config') and cls.dynamic_config.parameters: threshold = cls.dynamic_config.parameters.get('threshold', 5) diff --git a/dingo/model/llm/rag/llm_rag_context_precision.py b/dingo/model/llm/rag/llm_rag_context_precision.py index 05dd4f0f..85a514e3 100644 --- a/dingo/model/llm/rag/llm_rag_context_precision.py +++ b/dingo/model/llm/rag/llm_rag_context_precision.py @@ -254,7 +254,7 @@ def process_response(cls, responses: List[str]) -> ModelRes: result = ModelRes() result.score = score - # 根据分数判断是否通过(默认阈值5,满分10分) + # 根据分数判断是否通过,默认阈值为5 threshold = 5 if hasattr(cls, 'dynamic_config') and cls.dynamic_config.parameters: threshold = cls.dynamic_config.parameters.get('threshold', 5) diff --git a/dingo/model/llm/rag/llm_rag_context_recall.py b/dingo/model/llm/rag/llm_rag_context_recall.py index bd2e1842..0b6019b5 100644 --- a/dingo/model/llm/rag/llm_rag_context_recall.py +++ b/dingo/model/llm/rag/llm_rag_context_recall.py @@ -49,6 +49,7 @@ class LLMRAGContextRecall(BaseOpenAI): prompt = """上下文召回评估提示词,用于分类陈述归因""" + @staticmethod def context_recall_prompt(question: str, context: str, answer: str) -> str: """ 生成上下文召回评估的提示词 @@ -200,7 +201,7 @@ def process_response(cls, response: str) -> ModelRes: result = ModelRes() result.score = score - # 根据分数判断是否通过(默认阈值5,满分10分) + # 根据分数判断是否通过,默认阈值为5 threshold = 5 if hasattr(cls, 'dynamic_config') and cls.dynamic_config.parameters: threshold = cls.dynamic_config.parameters.get('threshold', 5) diff --git a/dingo/model/llm/rag/llm_rag_context_relevancy.py b/dingo/model/llm/rag/llm_rag_context_relevancy.py index 4e481add..734f7314 100644 --- a/dingo/model/llm/rag/llm_rag_context_relevancy.py +++ b/dingo/model/llm/rag/llm_rag_context_relevancy.py @@ -45,6 +45,7 @@ class LLMRAGContextRelevancy(BaseOpenAI): "source_frameworks": "Ragas + DeepEval + TruLens" } + @staticmethod def context_relevance_judge1_prompt(query: str, context: str) -> str: """ First judge template for context relevance evaluation (Chinese version). @@ -80,6 +81,7 @@ def context_relevance_judge1_prompt(query: str, context: str) -> str: 请不要尝试解释。 分析上下文和问题后,相关性分数为 """ + @staticmethod def context_relevance_judge2_prompt(query: str, context: str) -> str: """ Second judge template for context relevance evaluation (Chinese version). @@ -200,7 +202,7 @@ def process_response(cls, response: str) -> ModelRes: result = ModelRes() result.score = score - # 根据分数判断是否通过(默认阈值5,满分10分) + # 根据分数判断是否通过,默认阈值为5 threshold = 5 if hasattr(cls, 'dynamic_config') and cls.dynamic_config.parameters: threshold = cls.dynamic_config.parameters.get('threshold', 5) diff --git a/dingo/model/llm/rag/llm_rag_faithfulness.py b/dingo/model/llm/rag/llm_rag_faithfulness.py index 2ded8fac..c31a5a50 100644 --- a/dingo/model/llm/rag/llm_rag_faithfulness.py +++ b/dingo/model/llm/rag/llm_rag_faithfulness.py @@ -43,6 +43,7 @@ class LLMRAGFaithfulness(BaseOpenAI): "source_frameworks": "Ragas + DeepEval" } + @staticmethod def statement_generator_prompt(question: str, answer: str) -> str: """ Prompt to generate statements from answer (Chinese version). @@ -67,18 +68,19 @@ def statement_generator_prompt(question: str, answer: str) -> str: 请以JSON格式返回结果,格式如下: ```json -{ +{{ "statements": [ "陈述1", "陈述2", "陈述3" ] -} +}} ``` 请不要输出其他内容,只返回JSON格式的结果。 """ + @staticmethod def faithfulness_judge_prompt(context: str, statements: List[str]) -> str: """ Prompt to judge faithfulness of statements (Chinese version). @@ -103,15 +105,15 @@ def faithfulness_judge_prompt(context: str, statements: List[str]) -> str: 请以JSON格式返回结果,格式如下: ```json -{ +{{ "statements": [ - { + {{ "statement": "原始陈述,一字不差", "reason": "判断理由", "verdict": 0或1 - } + }} ] -} +}} ``` 请不要输出其他内容,只返回JSON格式的结果。 @@ -284,7 +286,7 @@ def process_response(cls, response: str) -> ModelRes: result = ModelRes() result.score = score - # 根据分数判断是否通过(默认阈值5,满分10分) + # 根据分数判断是否通过,默认阈值为5 threshold = 5 if hasattr(cls, 'dynamic_config') and cls.dynamic_config.parameters: threshold = cls.dynamic_config.parameters.get('threshold', 5)