diff --git a/framework/fel/python/plugins/fel_llama_index_tools/callable_registers.py b/framework/fel/python/plugins/fel_llama_index_tools/callable_registers.py deleted file mode 100644 index 0cde3122..00000000 --- a/framework/fel/python/plugins/fel_llama_index_tools/callable_registers.py +++ /dev/null @@ -1,29 +0,0 @@ -# -- encoding: utf-8 -- -# Copyright (c) 2024 Huawei Technologies Co., Ltd. All Rights Reserved. -# This file is a part of the ModelEngine Project. -# Licensed under the MIT License. See License.txt in the project root for license information. -# ====================================================================================================================== -import functools -from inspect import signature -from typing import Callable, Any, Tuple, List - -from fitframework import fit_logger -from fitframework.core.repo.fitable_register import register_fitable - - -def __invoke_tool(input_args: dict, tool_func: Callable[..., Any], **kwargs) -> Any: - return tool_func(**input_args, **kwargs) - - -def register_callable_tool(tool: Tuple[Callable[..., Any], List[str], str], module: str, generic_id: str): - func = tool[0] - fitable_id = f"{func.__name__}" - - tool_invoke = functools.partial(__invoke_tool, tool_func=func) - tool_invoke.__module__ = module - tool_invoke.__annotations__ = { - 'input_args': dict, - 'return': signature(func).return_annotation - } - register_fitable(generic_id, fitable_id, False, [], tool_invoke) - fit_logger.info("register: generic_id = %s, fitable_id = %s", generic_id, fitable_id, stacklevel=2) diff --git a/framework/fel/python/plugins/fel_llama_index_tools/llama_rag_basic_toolkit.py b/framework/fel/python/plugins/fel_llama_index_tools/llama_rag_basic_toolkit.py index 3a84e07b..8c7de347 100644 --- a/framework/fel/python/plugins/fel_llama_index_tools/llama_rag_basic_toolkit.py +++ b/framework/fel/python/plugins/fel_llama_index_tools/llama_rag_basic_toolkit.py @@ -6,24 +6,25 @@ import os import traceback from enum import Enum, unique -from typing import List, Callable, Any, Tuple +from typing import List -from fitframework import fit_logger -from fitframework.core.repo.fitable_register import register_fitable +from fitframework import fit_logger, fitable from llama_index.core.base.base_selector import SingleSelection from llama_index.core.postprocessor import SimilarityPostprocessor, SentenceEmbeddingOptimizer, LLMRerank, \ LongContextReorder, FixedRecencyPostprocessor from llama_index.core.postprocessor.types import BaseNodePostprocessor from llama_index.core.prompts import PromptType, PromptTemplate -from llama_index.core.prompts.default_prompts import DEFAULT_CHOICE_SELECT_PROMPT_TMPL from llama_index.core.selectors import LLMSingleSelector, LLMMultiSelector from llama_index.core.selectors.prompts import DEFAULT_SINGLE_SELECT_PROMPT_TMPL, DEFAULT_MULTI_SELECT_PROMPT_TMPL from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.llms.openai import OpenAI -from .callable_registers import register_callable_tool -from .node_utils import document_to_query_node, query_node_to_document from .types.document import Document +from .types.llm_rerank_options import LLMRerankOptions +from .types.embedding_options import EmbeddingOptions +from .types.retriever_options import RetrieverOptions +from .types.llm_choice_selector_options import LLMChoiceSelectorOptions +from .node_utils import document_to_query_node, query_node_to_document os.environ["no_proxy"] = "*" @@ -42,49 +43,50 @@ def __invoke_postprocessor(postprocessor: BaseNodePostprocessor, nodes: List[Doc return nodes -def similarity_filter(nodes: List[Document], query_str: str, **kwargs) -> List[Document]: +@fitable("llama.tools.similarity_filter", "default") +def similarity_filter(nodes: List[Document], query_str: str, options: RetrieverOptions) -> List[Document]: """Remove documents that are below a similarity score threshold.""" - similarity_cutoff = float(kwargs.get("similarity_cutoff") or 0.3) - postprocessor = SimilarityPostprocessor(similarity_cutoff=similarity_cutoff) + if options is None: + options = RetrieverOptions() + postprocessor = SimilarityPostprocessor(similarity_cutoff=options.similarity_cutoff) return __invoke_postprocessor(postprocessor, nodes, query_str) -def sentence_embedding_optimizer(nodes: List[Document], query_str: str, **kwargs) -> List[Document]: +@fitable("llama.tools.sentence_embedding_optimizer", "default") +def sentence_embedding_optimizer(nodes: List[Document], query_str: str, options: EmbeddingOptions) -> List[Document]: """Optimization of a text chunk given the query by shortening the input text.""" - api_key = kwargs.get("api_key") or "EMPTY" - model_name = kwargs.get("model_name") or "bce-embedding-base_v1" - api_base = kwargs.get("api_base") or ("http://51.36.139.24:8010/v1" if api_key == "EMPTY" else None) - percentile_cutoff = kwargs.get("percentile_cutoff") - threshold_cutoff = kwargs.get("threshold_cutoff") - percentile_cutoff = percentile_cutoff if percentile_cutoff is None else float(percentile_cutoff) - threshold_cutoff = threshold_cutoff if threshold_cutoff is None else float(threshold_cutoff) - - embed_model = OpenAIEmbedding(model_name=model_name, api_base=api_base, api_key=api_key) - optimizer = SentenceEmbeddingOptimizer(embed_model=embed_model, percentile_cutoff=percentile_cutoff, - threshold_cutoff=threshold_cutoff) + if options is None: + options = EmbeddingOptions() + api_base = options.api_base + embed_model = OpenAIEmbedding(model_name=options.model_name, api_base=api_base, api_key=options.api_key) + optimizer = SentenceEmbeddingOptimizer(embed_model=embed_model, percentile_cutoff=options.percentile_cutoff, + threshold_cutoff=options.threshold_cutoff) return __invoke_postprocessor(optimizer, nodes, query_str) -def llm_rerank(nodes: List[Document], query_str: str, **kwargs) -> List[Document]: +@fitable("llama.tools.llm_rerank", "default") +def llm_rerank(nodes: List[Document], query_str: str, options: LLMRerankOptions) -> List[Document]: """ Re-order nodes by asking the LLM to return the relevant documents and a score of how relevant they are. Returns the top N ranked nodes. """ - api_key = kwargs.get("api_key") or "EMPTY" - model_name = kwargs.get("model_name") or "Qwen1.5-14B-Chat" - api_base = kwargs.get("api_base") or ("http://80.11.128.62:8000/v1" if api_key == "EMPTY" else None) - prompt = kwargs.get("prompt") or DEFAULT_CHOICE_SELECT_PROMPT_TMPL - choice_batch_size = int(kwargs.get("choice_batch_size") or 10) - top_n = int(kwargs.get("top_n") or 10) - - llm = OpenAI(model=model_name, api_base=api_base, api_key=api_key, max_tokens=4096) + if options is None: + options = LLMRerankOptions() + + api_base = options.api_base + + prompt = options.prompt + + llm = OpenAI(model=options.model_name, api_base=api_base, api_key=options.api_key) choice_select_prompt = PromptTemplate(prompt, prompt_type=PromptType.CHOICE_SELECT) - llm_rerank_obj = LLMRerank(llm=llm, choice_select_prompt=choice_select_prompt, choice_batch_size=choice_batch_size, - top_n=top_n) + llm_rerank_obj = LLMRerank(llm=llm, choice_select_prompt=choice_select_prompt, + choice_batch_size=options.choice_batch_size, + top_n=options.top_n) return __invoke_postprocessor(llm_rerank_obj, nodes, query_str) -def long_context_rerank(nodes: List[Document], query_str: str, **kwargs) -> List[Document]: +@fitable("llama.tools.long_context_rerank", "default") +def long_context_rerank(nodes: List[Document], query_str: str) -> List[Document]: """Re-order the retrieved nodes, which can be helpful in cases where a large top-k is needed.""" return __invoke_postprocessor(LongContextReorder(), nodes, query_str) @@ -95,24 +97,23 @@ class SelectorMode(Enum): MULTI = "multi" -def llm_choice_selector(choice: List[str], query_str: str, **kwargs) -> List[SingleSelection]: +@fitable("llama.tools.llm_choice_selector", "default") +def llm_choice_selector(choice: List[str], query_str: str, options: LLMChoiceSelectorOptions) -> List[SingleSelection]: """LLM-based selector that chooses one or multiple out of many options.""" if len(choice) == 0: return [] - api_key = kwargs.get("api_key") or "EMPTY" - model_name = kwargs.get("model_name") or "Qwen1.5-14B-Chat" - api_base = kwargs.get("api_base") or ("http://80.11.128.62:8000/v1" if api_key == "EMPTY" else None) - prompt = kwargs.get("prompt") - mode = str(kwargs.get("mode") or SelectorMode.SINGLE.value) - if mode.lower() not in [m.value for m in SelectorMode]: - raise ValueError(f"Invalid mode {mode}.") - - llm = OpenAI(model=model_name, api_base=api_base, api_key=api_key, max_tokens=4096) - if mode.lower() == SelectorMode.SINGLE.value: - selector_prompt = prompt or DEFAULT_SINGLE_SELECT_PROMPT_TMPL + if options is None: + options = LLMChoiceSelectorOptions() + api_base = options.api_base + if options.mode.lower() not in [m.value for m in SelectorMode]: + raise ValueError(f"Invalid mode {options.mode}.") + + llm = OpenAI(model=options.model_name, api_base=api_base, api_key=options.api_key, max_tokens=4096) + if options.mode.lower() == SelectorMode.SINGLE.value: + selector_prompt = options.prompt or DEFAULT_SINGLE_SELECT_PROMPT_TMPL selector = LLMSingleSelector.from_defaults(llm=llm, prompt_template_str=selector_prompt) else: - multi_selector_prompt = prompt or DEFAULT_MULTI_SELECT_PROMPT_TMPL + multi_selector_prompt = options.prompt or DEFAULT_MULTI_SELECT_PROMPT_TMPL selector = LLMMultiSelector.from_defaults(llm=llm, prompt_template_str=multi_selector_prompt) try: return selector.select(choice, query_str).selections @@ -122,34 +123,10 @@ def llm_choice_selector(choice: List[str], query_str: str, **kwargs) -> List[Sin return [] -def fixed_recency(nodes: List[Document], tok_k: int, date_key: str, query_str: str, **kwargs) -> List[Document]: +@fitable("llama.tools.fixed_recency", "default") +def fixed_recency(nodes: List[Document], top_k: int, date_key: str, query_str: str) -> List[Document]: """This postprocessor returns the top K nodes sorted by date""" postprocessor = FixedRecencyPostprocessor( - tok_k=tok_k, date_key=date_key if date_key else "date" + top_k=top_k, date_key=date_key if date_key else "date" ) return __invoke_postprocessor(postprocessor, nodes, query_str) - - -# Tuple 结构: (tool_func, config_args, return_description) -rag_basic_toolkit: List[Tuple[Callable[..., Any], List[str], str]] = [ - (similarity_filter, ["similarity_cutoff"], "The filtered documents."), - (sentence_embedding_optimizer, ["model_name", "api_key", "api_base", "percentile_cutoff", "threshold_cutoff"], - "The optimized documents."), - (llm_rerank, ["model_name", "api_key", "api_base", "prompt", "choice_batch_size", "top_n"], - "The re-ordered documents."), - (long_context_rerank, [], "The re-ordered documents."), - (llm_choice_selector, ["model_name", "api_key", "api_base", "prompt", "mode"], "The selected choice."), - (fixed_recency, ["nodes", "tok_k", "date_key", "query_str"], "The fixed recency postprocessor") -] - - -for tool in rag_basic_toolkit: - register_callable_tool(tool, llm_choice_selector.__module__, "llama_index.rag.toolkit") - - -if __name__ == '__main__': - import time - from .llama_schema_helper import dump_llama_schema - - current_timestamp = time.strftime('%Y%m%d%H%M%S') - dump_llama_schema(rag_basic_toolkit, f"./llama_tool_schema-{str(current_timestamp)}.json") diff --git a/framework/fel/python/plugins/fel_llama_index_tools/tools.json b/framework/fel/python/plugins/fel_llama_index_tools/tools.json index 62283787..928d9a07 100644 --- a/framework/fel/python/plugins/fel_llama_index_tools/tools.json +++ b/framework/fel/python/plugins/fel_llama_index_tools/tools.json @@ -1,685 +1,1218 @@ { - "tools": [ - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "similarity_filter" + "version" : "1.0.0", + "definitionGroups" : [ { + "name" : "llama_index_tools", + "summary" : "", + "description" : "", + "extensions" : { }, + "definitions" : [ { + "schema" : { + "name" : "SimilarityFilterTool", + "description" : "用于移除相似度分数低于阈值的文档节点。输入文档列表和查询字符串,返回过滤后的文档列表。", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "defaultValue" : "", + "description" : "待过滤的文档列表", + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + }, + "example" : "" }, - "schema": { - "name": "similarity_filter", - "description": "Remove documents that are below a similarity score threshold.", - "parameters": { - "type": "object", - "properties": { - "nodes": { - "title": "Nodes", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "query_str": { - "title": "Query Str", - "type": "string" - }, - "similarity_cutoff": { - "type": "string", - "description": "similarity_cutoff" - } - }, - "required": [ - "nodes", - "query_str" - ] - }, - "return": { - "title": "The Filtered Documents.", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "parameterExtensions": { - "config": [ - "similarity_cutoff" - ] + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" + }, + "options" : { + "defaultValue" : "", + "description" : "检索器配置", + "name" : "options", + "type" : "object", + "properties" : { + "similarity_cutoff" : { + "type" : "number", + "description" : "相似度阈值,范围0-1,小于此值的文档将被过滤" } + }, + "example" : "", + "required" : [ "similarity_cutoff" ] } + }, + "required" : [ "nodes", "query_str", "options" ] }, - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "sentence_embedding_optimizer" + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + } + }, { + "schema" : { + "name" : "SentenceEmbeddingOptimizerTool", + "description" : "通过缩短输入文本来优化给定查询的文本块。输入文档列表和查询字符串,返回优化后的文档列表。", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "defaultValue" : "", + "description" : "待优化的文档列表", + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + }, + "example" : "" }, - "schema": { - "name": "sentence_embedding_optimizer", - "description": "Optimization of a text chunk given the query by shortening the input text.", - "parameters": { - "type": "object", - "properties": { - "nodes": { - "title": "Nodes", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "query_str": { - "title": "Query Str", - "type": "string" - }, - "model_name": { - "type": "string", - "description": "model_name" - }, - "api_key": { - "type": "string", - "description": "api_key" - }, - "api_base": { - "type": "string", - "description": "api_base" - }, - "percentile_cutoff": { - "type": "string", - "description": "percentile_cutoff" - }, - "threshold_cutoff": { - "type": "string", - "description": "threshold_cutoff" - } - }, - "required": [ - "nodes", - "query_str" - ] - }, - "return": { - "title": "The Optimized Documents.", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "parameterExtensions": { - "config": [ - "model_name", - "api_key", - "api_base", - "percentile_cutoff", - "threshold_cutoff" - ] + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" + }, + "options" : { + "defaultValue" : "", + "description" : "嵌入模型配置", + "name" : "options", + "type" : "object", + "properties" : { + "model_name" : { + "type" : "string", + "description" : "嵌入模型名称" + }, + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "percentile_cutoff" : { + "type" : "number", + "description" : "百分位阈值,范围0-1,只保留前N%的相关句子" + }, + "threshold_cutoff" : { + "type" : "number", + "description" : "分数阈值,范围0-1,只保留相似度高于此值的句子" } + }, + "example" : "", + "required" : [ "model_name", "api_key", "api_base", "percentile_cutoff", "threshold_cutoff" ] } + }, + "required" : [ "nodes", "query_str", "options" ] }, - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "llm_rerank" + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + } + }, { + "schema" : { + "name" : "LLMRerankTool", + "description" : "通过让 LLM 返回相关文档和相关性分数来重新排序节点。输入文档列表和查询字符串,返回重排序后的文档列表。", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "defaultValue" : "", + "description" : "待重排序的文档列表", + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + }, + "example" : "" + }, + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" }, - "schema": { - "name": "llm_rerank", - "description": "\n Re-order nodes by asking the LLM to return the relevant documents and a score of how relevant they are.\n Returns the top N ranked nodes.\n ", - "parameters": { - "type": "object", - "properties": { - "nodes": { - "title": "Nodes", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "query_str": { - "title": "Query Str", - "type": "string" - }, - "model_name": { - "type": "string", - "description": "model_name" - }, - "api_key": { - "type": "string", - "description": "api_key" - }, - "api_base": { - "type": "string", - "description": "api_base" - }, - "prompt": { - "type": "string", - "description": "prompt" - }, - "choice_batch_size": { - "type": "string", - "description": "choice_batch_size" - }, - "top_n": { - "type": "string", - "description": "top_n" - } + "options" : { + "defaultValue" : "", + "description" : "LLM重排序配置", + "name" : "options", + "type" : "object", + "properties" : { + "model_name" : { + "type" : "string", + "description" : "LLM模型名称" + }, + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "prompt" : { + "type" : "string", + "description" : "LLM提示模板" + }, + "choice_batch_size" : { + "type" : "integer", + "description" : "每批处理的候选数量" + }, + "top_n" : { + "type" : "integer", + "description" : "返回前N个最相关文档" + } + }, + "example" : "", + "required" : [ "model_name", "api_key", "api_base", "prompt", "choice_batch_size", "top_n" ] + } + }, + "required" : [ "nodes", "query_str", "options" ] + }, + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + } + }, { + "schema" : { + "name" : "LongContextRerankTool", + "description" : "重新排序检索到的节点,在需要大量 top-k 的情况下很有帮助。输入文档列表和查询字符串,返回重排序后的文档列表。", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "defaultValue" : "", + "description" : "待重排序的文档列表", + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } }, - "required": [ - "nodes", - "query_str" - ] - }, - "return": { - "title": "The Re-Ordered Documents.", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "parameterExtensions": { - "config": [ - "model_name", - "api_key", - "api_base", - "prompt", - "choice_batch_size", - "top_n" - ] + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + }, + "example" : "" + }, + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" } + }, + "required" : [ "nodes", "query_str" ] }, - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "long_context_rerank" + "order" : [ "nodes", "query_str" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + } + }, { + "schema" : { + "name" : "LLMChoiceSelectorTool", + "description" : "基于 LLM 的选择器,从多个选项中选择一个或多个。输入选项列表和查询字符串,返回选中的选项列表。", + "parameters" : { + "type" : "object", + "properties" : { + "choice" : { + "defaultValue" : "", + "description" : "可选项列表", + "name" : "choice", + "type" : "array", + "items" : { + "type" : "string" + }, + "example" : "" + }, + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" + }, + "options" : { + "defaultValue" : "", + "description" : "LLM选择器配置", + "name" : "options", + "type" : "object", + "properties" : { + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "model_name" : { + "type" : "string", + "description" : "LLM模型名称" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "prompt" : { + "type" : "string", + "description" : "提示模板" + }, + "mode" : { + "type" : "string", + "description" : "选择模式" } + }, + "example" : "", + "required" : [ "api_key", "model_name", "api_base", "prompt", "mode" ] + } + }, + "required" : [ "choice", "query_str", "options" ] + }, + "order" : [ "choice", "query_str", "options" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "index" : { + "type" : "integer", + "description" : "选择的索引" + }, + "reason" : { + "type" : "string", + "description" : "选择的原因" + } + } + }, + "convertor" : "" + } + } + }, { + "schema" : { + "name" : "FixedRecencyTool", + "description" : "按日期排序返回前 K 个节点的后处理器。输入文档列表、topK、日期字段和查询字符串,返回按时间排序的文档列表。", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "defaultValue" : "", + "description" : "待排序的文档列表", + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "example" : "" + }, + "top_k" : { + "defaultValue" : "", + "description" : "返回前K个文档", + "name" : "top_k", + "type" : "integer", + "example" : "" }, - "schema": { - "name": "long_context_rerank", - "description": "Re-order the retrieved nodes, which can be helpful in cases where a large top-k is needed.", - "parameters": { - "type": "object", - "properties": { - "nodes": { - "title": "Nodes", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "query_str": { - "title": "Query Str", - "type": "string" - } + "date_key" : { + "defaultValue" : "", + "description" : "日期字段名", + "name" : "date_key", + "type" : "string", + "example" : "" + }, + "query_str" : { + "defaultValue" : "", + "description" : "查询字符串", + "name" : "query_str", + "type" : "string", + "example" : "" + } + }, + "required" : [ "nodes", "top_k", "date_key", "query_str" ] + }, + "order" : [ "nodes", "top_k", "date_key", "query_str" ], + "return" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + } + } ] + } ], + "toolGroups" : [ { + "name" : "llama_index_tools_impl", + "summary" : "", + "description" : "", + "extensions" : { }, + "definitionGroupName" : "llama_index_tools", + "tools" : [ { + "namespace" : "similarity_filter", + "schema" : { + "name" : "SimilarityFilterTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } }, - "required": [ - "nodes", - "query_str" - ] - }, - "return": { - "title": "The Re-Ordered Documents.", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + } + }, + "query_str" : { + "name" : "query_str", + "type" : "string" + }, + "options" : { + "name" : "options", + "type" : "object", + "properties" : { + "similarity_cutoff" : { + "type" : "number", + "description" : "相似度阈值,范围0-1,小于此值的文档将被过滤" } + }, + "required" : [ "similarity_cutoff" ] } + }, + "required" : [ ] }, - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "llm_choice_selector" + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "name" : "", + "description" : "返回过滤后的文档列表,移除相似度分数低于阈值的文档节点。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.similarity_filter", + "fitableId" : "default" + } + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "SimilarityFilterTool" + }, { + "namespace" : "sentence_embedding_optimizer", + "schema" : { + "name" : "SentenceEmbeddingOptimizerTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + } + }, + "query_str" : { + "name" : "query_str", + "type" : "string" }, - "schema": { - "name": "llm_choice_selector", - "description": "LLM-based selector that chooses one or multiple out of many options.", - "parameters": { - "type": "object", - "properties": { - "choice": { - "title": "Choice", - "type": "array", - "items": { - "type": "string" - } - }, - "query_str": { - "title": "Query Str", - "type": "string" - }, - "model_name": { - "type": "string", - "description": "model_name" - }, - "api_key": { - "type": "string", - "description": "api_key" - }, - "api_base": { - "type": "string", - "description": "api_base" - }, - "prompt": { - "type": "string", - "description": "prompt" - }, - "mode": { - "type": "string", - "description": "mode" - } + "options" : { + "name" : "options", + "type" : "object", + "properties" : { + "model_name" : { + "type" : "string", + "description" : "嵌入模型名称" + }, + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "percentile_cutoff" : { + "type" : "number", + "description" : "百分位阈值,范围0-1,只保留前N%的相关句子" + }, + "threshold_cutoff" : { + "type" : "number", + "description" : "分数阈值,范围0-1,只保留相似度高于此值的句子" + } + }, + "required" : [ "model_name", "api_key", "api_base", "percentile_cutoff", "threshold_cutoff" ] + } + }, + "required" : [ ] + }, + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "name" : "", + "description" : "返回优化后的文档列表,通过缩短输入文本来优化给定查询的文本块。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.sentence_embedding_optimizer", + "fitableId" : "default" + } + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "SentenceEmbeddingOptimizerTool" + }, { + "namespace" : "llm_rerank", + "schema" : { + "name" : "LLMRerankTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } }, - "required": [ - "choice", - "query_str" - ] - }, - "return": { - "title": "The Selected Choice.", - "type": "array", - "items": { - "title": "SingleSelection", - "description": "A single selection of a choice.", - "type": "object", - "properties": { - "index": { - "title": "Index", - "type": "integer" - }, - "reason": { - "title": "Reason", - "type": "string" - } - }, - "required": [ - "index", - "reason" - ] - } - }, - "parameterExtensions": { - "config": [ - "model_name", - "api_key", - "api_base", - "prompt", - "mode" - ] + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + } + }, + "query_str" : { + "name" : "query_str", + "type" : "string" + }, + "options" : { + "name" : "options", + "type" : "object", + "properties" : { + "model_name" : { + "type" : "string", + "description" : "LLM模型名称" + }, + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "prompt" : { + "type" : "string", + "description" : "LLM提示模板" + }, + "choice_batch_size" : { + "type" : "integer", + "description" : "每批处理的候选数量" + }, + "top_n" : { + "type" : "integer", + "description" : "返回前N个最相关文档" } + }, + "required" : [ "model_name", "api_key", "api_base", "prompt", "choice_batch_size", "top_n" ] } + }, + "required" : [ ] }, - { - "runnables": { - "LlamaIndex": { - "genericableId": "llama_index.rag.toolkit", - "fitableId": "fixed_recency" + "order" : [ "nodes", "query_str", "options" ], + "return" : { + "name" : "", + "description" : "返回重排序后的文档列表,通过让 LLM 返回相关文档和相关性分数来重新排序节点。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.llm_rerank", + "fitableId" : "default" + } + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "LLMRerankTool" + }, { + "namespace" : "long_context_rerank", + "schema" : { + "name" : "LongContextRerankTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + } + }, + "query_str" : { + "name" : "query_str", + "type" : "string" + } + }, + "required" : [ ] + }, + "order" : [ "nodes", "query_str" ], + "return" : { + "name" : "", + "description" : "返回重排序后的文档列表,重新排序检索到的节点,在需要大量 top-k 的情况下很有帮助。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } + } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.long_context_rerank", + "fitableId" : "default" + } + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "LongContextRerankTool" + }, { + "namespace" : "llm_choice_selector", + "schema" : { + "name" : "LLMChoiceSelectorTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "choice" : { + "name" : "choice", + "type" : "array", + "items" : { + "type" : "string" + } }, - "schema": { - "name": "fixed_recency", - "description": "This postprocessor returns the top K nodes sorted by date", - "parameters": { - "type": "object", - "properties": { - "nodes": { - "type": "string", - "description": "nodes" - }, - "tok_k": { - "type": "string", - "description": "tok_k" - }, - "date_key": { - "type": "string", - "description": "date_key" - }, - "query_str": { - "type": "string", - "description": "query_str" - } + "query_str" : { + "name" : "query_str", + "type" : "string" + }, + "options" : { + "name" : "options", + "type" : "object", + "properties" : { + "api_key" : { + "type" : "string", + "description" : "API密钥" + }, + "model_name" : { + "type" : "string", + "description" : "LLM模型名称" + }, + "api_base" : { + "type" : "string", + "description" : "API基础URL" + }, + "prompt" : { + "type" : "string", + "description" : "提示模板" + }, + "mode" : { + "type" : "string", + "description" : "选择模式" + } + }, + "required" : [ "api_key", "model_name", "api_base", "prompt", "mode" ] + } + }, + "required" : [ ] + }, + "order" : [ "choice", "query_str", "options" ], + "return" : { + "name" : "", + "description" : "返回选中的选项列表,基于 LLM 的选择器,从多个选项中选择一个或多个。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "index" : { + "type" : "integer", + "description" : "选择的索引" + }, + "reason" : { + "type" : "string", + "description" : "选择的原因" + } + } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.llm_choice_selector", + "fitableId" : "default" + } + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "LLMChoiceSelectorTool" + }, { + "namespace" : "fixed_recency", + "schema" : { + "name" : "FixedRecencyTool", + "description" : "", + "parameters" : { + "type" : "object", + "properties" : { + "nodes" : { + "name" : "nodes", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } }, - "required": [ - "nodes", - "tok_k", - "date_key", - "query_str" - ] - }, - "return": { - "title": "The Fixed Recency Postprocessor", - "type": "array", - "items": { - "title": "Document", - "description": "Document.", - "type": "object", - "properties": { - "content": { - "title": "Content", - "type": "string" - }, - "media": { - "title": "Media", - "description": "Media.", - "type": "object", - "properties": { - "mime": { - "title": "Mime", - "type": "string" - }, - "data": { - "title": "Data", - "type": "string" - } - }, - "required": [ - "mime", - "data" - ] - }, - "metadata": { - "title": "Metadata", - "type": "object" - } - }, - "required": [ - "content", - "metadata" - ] - } - }, - "parameterExtensions": { - "config": [ - "nodes", - "tok_k", - "date_key", - "query_str" - ] + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + } + }, + "top_k" : { + "name" : "top_k", + "type" : "integer" + }, + "date_key" : { + "name" : "date_key", + "type" : "string" + }, + "query_str" : { + "name" : "query_str", + "type" : "string" + } + }, + "required" : [ ] + }, + "order" : [ "nodes", "top_k", "date_key", "query_str" ], + "return" : { + "name" : "", + "description" : "返回按时间排序的文档列表,按日期排序返回前 K 个节点的后处理器。", + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "content" : { + "type" : "string", + "description" : "文档内容" + }, + "media" : { + "type" : "object", + "properties" : { + "mime" : { + "type" : "string", + "description" : "媒体类型" + }, + "data" : { + "type" : "string", + "description" : "媒体数据" + } + }, + "description" : "媒体信息" + }, + "metadata" : { + "type" : "object", + "description" : "元数据" + } } + }, + "convertor" : "" + } + }, + "runnables" : { + "FIT" : { + "genericableId" : "llama.tools.fixed_recency", + "fitableId" : "default" } - ] + }, + "extensions" : { + "tags" : [ "llama" ] + }, + "definitionName" : "FixedRecencyTool" + } ] + } ] } \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/document.py b/framework/fel/python/plugins/fel_llama_index_tools/types/document.py index 4989999f..9e42af47 100644 --- a/framework/fel/python/plugins/fel_llama_index_tools/types/document.py +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/document.py @@ -1,22 +1,25 @@ # -- encoding: utf-8 -- -# Copyright (c) 2024 Huawei Technologies Co., Ltd. All Rights Reserved. +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. # This file is a part of the ModelEngine Project. # Licensed under the MIT License. See License.txt in the project root for license information. # ====================================================================================================================== import typing -from .serializable import Serializable from .media import Media +class Document(object): + def __init__(self, content: str, media: Media , metadata: typing.Dict[str, object] ): + self.content = content + self.media = media + self.metadata = metadata + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ -class Document(Serializable): - """ - Document. - """ - content: str - media: Media = None - metadata: typing.Dict[str, object] + def __hash__(self): + return hash(tuple(self.__dict__.values())) - class Config: - frozen = True - smart_union = True + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/embedding_options.py b/framework/fel/python/plugins/fel_llama_index_tools/types/embedding_options.py new file mode 100644 index 00000000..88e28e97 --- /dev/null +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/embedding_options.py @@ -0,0 +1,27 @@ +# -- encoding: utf-8 -- +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +# This file is a part of the ModelEngine Project. +# Licensed under the MIT License. See License.txt in the project root for license information. +# ====================================================================================================================== +import typing + +from .media import Media + +class EmbeddingOptions(object): + def __init__(self, model_name: str,api_key: str,api_base: str,percentile_cutoff: float,threshold_cutoff: float): + self.model_name = model_name + self.api_key = api_key + self.api_base = api_base + self.percentile_cutoff = percentile_cutoff + self.threshold_cutoff = threshold_cutoff + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ + + def __hash__(self): + return hash(tuple(self.__dict__.values())) + + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/llm_choice_selector_options.py b/framework/fel/python/plugins/fel_llama_index_tools/types/llm_choice_selector_options.py new file mode 100644 index 00000000..bd1ebde1 --- /dev/null +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/llm_choice_selector_options.py @@ -0,0 +1,27 @@ +# -- encoding: utf-8 -- +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +# This file is a part of the ModelEngine Project. +# Licensed under the MIT License. See License.txt in the project root for license information. +# ====================================================================================================================== +import typing + +from .media import Media + +class LLMChoiceSelectorOptions(object): + def __init__(self, api_key: str, model_name: str,api_base: str, prompt: str ,mode: str = 'single'): + self.api_key = api_key + self.model_name = model_name + self.api_base = api_base + self.prompt = prompt + self.mode = mode + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ + + def __hash__(self): + return hash(tuple(self.__dict__.values())) + + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/llm_rerank_options.py b/framework/fel/python/plugins/fel_llama_index_tools/types/llm_rerank_options.py new file mode 100644 index 00000000..3c3b0ab9 --- /dev/null +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/llm_rerank_options.py @@ -0,0 +1,28 @@ +# -- encoding: utf-8 -- +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +# This file is a part of the ModelEngine Project. +# Licensed under the MIT License. See License.txt in the project root for license information. +# ====================================================================================================================== +import typing + +from .media import Media + +class LLMRerankOptions(object): + def __init__(self, model_name: str ,api_key: str ,api_base: str ,prompt: str ,choice_batch_size: int, top_n: int): + self.model_name = model_name + self.api_key = api_key + self.api_base = api_base + self.prompt = prompt + self.choice_batch_size = choice_batch_size + self.top_n = top_n + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ + + def __hash__(self): + return hash(tuple(self.__dict__.values())) + + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/media.py b/framework/fel/python/plugins/fel_llama_index_tools/types/media.py index b1bdb54a..15786034 100644 --- a/framework/fel/python/plugins/fel_llama_index_tools/types/media.py +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/media.py @@ -1,18 +1,21 @@ # -- encoding: utf-8 -- -# Copyright (c) 2024 Huawei Technologies Co., Ltd. All Rights Reserved. +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. # This file is a part of the ModelEngine Project. # Licensed under the MIT License. See License.txt in the project root for license information. # ====================================================================================================================== -from .serializable import Serializable +class Media(object): + def __init__(self, mime: str, data: str): + self.mime = mime + self.data = data + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ -class Media(Serializable): - """ - Media. - """ - mime: str - data: str + def __hash__(self): + return hash(tuple(self.__dict__.values())) - class Config: - frozen = True - smart_union = True + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/retriever_options.py b/framework/fel/python/plugins/fel_llama_index_tools/types/retriever_options.py new file mode 100644 index 00000000..603cfa4c --- /dev/null +++ b/framework/fel/python/plugins/fel_llama_index_tools/types/retriever_options.py @@ -0,0 +1,22 @@ +# -- encoding: utf-8 -- +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +# This file is a part of the ModelEngine Project. +# Licensed under the MIT License. See License.txt in the project root for license information. +# ====================================================================================================================== +import typing + +from .media import Media +class RetrieverOptions(object): + def __init__(self, similarity_cutoff: float): + self.similarity_cutoff = similarity_cutoff + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return self.__dict__ == other.__dict__ + + def __hash__(self): + return hash(tuple(self.__dict__.values())) + + def __repr__(self): + return str((self.__dict__.values())) \ No newline at end of file diff --git a/framework/fel/python/plugins/fel_llama_index_tools/types/serializable.py b/framework/fel/python/plugins/fel_llama_index_tools/types/serializable.py deleted file mode 100644 index 4522897f..00000000 --- a/framework/fel/python/plugins/fel_llama_index_tools/types/serializable.py +++ /dev/null @@ -1,25 +0,0 @@ -# -- encoding: utf-8 -- -# Copyright (c) 2024 Huawei Technologies Co., Ltd. All Rights Reserved. -# This file is a part of the ModelEngine Project. -# Licensed under the MIT License. See License.txt in the project root for license information. -# ====================================================================================================================== -import typing - -try: - import pydantic - - if pydantic.__version__.startswith("1."): - raise ImportError - import pydantic.v1 as pydantic -except ImportError: - import pydantic - - -class Serializable(pydantic.BaseModel): - def json(self, **kwargs: typing.Any) -> str: - kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs} - return super().json(**kwargs_with_defaults) - - def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]: - kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs} - return super().dict(**kwargs_with_defaults) \ No newline at end of file