Skip to content

Commit 4d44f9c

Browse files
authored
[fel] update llama selector tools meta (#245)
* [fit]Update plugins metadata registration * 修改检视意见
1 parent 22a27a1 commit 4d44f9c

File tree

5 files changed

+173
-131
lines changed

5 files changed

+173
-131
lines changed

framework/fel/python/plugins/fel_llama_selector_tools/callable_registers.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

framework/fel/python/plugins/fel_llama_selector_tools/llama_selector.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
# Licensed under the MIT License. See License.txt in the project root for license information.
55
# ======================================================================================================================
66
import traceback
7-
from typing import Tuple, List, Any, Callable
7+
from typing import List
88

9-
from fitframework import fit_logger
9+
from fitframework import fit_logger, fitable
1010
from llama_index.core.base.base_selector import SingleSelection
1111
from llama_index.core.selectors import EmbeddingSingleSelector
1212
from llama_index.embeddings.openai import OpenAIEmbedding
1313

14-
from .callable_registers import register_callable_tool
14+
from .types.embedding_choice_selector import EmbeddingChoiceSelectorOptions
1515

1616

17-
def embedding_choice_selector(choice: List[str], query_str: str, **kwargs) -> List[SingleSelection]:
17+
@fitable("llama.tools.embedding_choice_selector", "default")
18+
def embedding_choice_selector(choice: List[str], query_str: str , options:EmbeddingChoiceSelectorOptions) -> List[SingleSelection]:
1819
""" Embedding selector that chooses one out of many options."""
1920
if len(choice) == 0:
2021
return []
21-
api_key = kwargs.get("api_key") or "EMPTY"
22-
model_name = kwargs.get("model_name") or "bge-large-zh"
23-
api_base = kwargs.get("api_base") or None
22+
api_key = options.api_key
23+
model_name = options.model_name
24+
api_base = options.api_base
2425

2526
embed_model = OpenAIEmbedding(model_name=model_name, api_base=api_base, api_key=api_key)
2627
selector = EmbeddingSingleSelector.from_defaults(embed_model=embed_model)
@@ -30,19 +31,3 @@ def embedding_choice_selector(choice: List[str], query_str: str, **kwargs) -> Li
3031
fit_logger.error("Invoke embedding choice selector failed.")
3132
traceback.print_exc()
3233
return []
33-
34-
35-
# Tuple 结构: (tool_func, config_args, return_description)
36-
selector_toolkit: List[Tuple[Callable[..., Any], List[str], str]] = [
37-
(embedding_choice_selector, ["model_name", "api_key", "api_base", "prompt", "mode"], "The selected choice."),
38-
]
39-
40-
for tool in selector_toolkit:
41-
register_callable_tool(tool, embedding_choice_selector.__module__, "llama_index.rag.toolkit")
42-
43-
if __name__ == '__main__':
44-
import time
45-
from .llama_schema_helper import dump_llama_schema
46-
47-
current_timestamp = time.strftime('%Y%m%d%H%M%S')
48-
dump_llama_schema(selector_toolkit, f"./llama_tool_schema-{str(current_timestamp)}.json")
Lines changed: 144 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,156 @@
11
{
2-
"tools": [
3-
{
4-
"tags": [
5-
"LlamaIndex"
6-
],
7-
"runnables": {
8-
"LlamaIndex": {
9-
"genericableId": "llama_index.rag.toolkit",
10-
"fitableId": "embedding_choice_selector"
11-
}
12-
},
13-
"schema": {
14-
"name": "embedding_choice_selector",
15-
"description": " Embedding selector that chooses one out of many options.",
16-
"parameters": {
17-
"type": "object",
18-
"properties": {
19-
"choice": {
20-
"title": "Choice",
21-
"type": "array",
22-
"items": {
23-
"type": "string"
24-
}
25-
},
26-
"query_str": {
27-
"title": "Query Str",
28-
"type": "string"
29-
},
30-
"model_name": {
31-
"type": "string",
32-
"description": "model_name"
33-
},
34-
"api_key": {
35-
"type": "string",
36-
"description": "api_key"
37-
},
38-
"api_base": {
39-
"type": "string",
40-
"description": "api_base"
2+
"version" : "1.0.0",
3+
"definitionGroups" : [ {
4+
"name" : "llm_selector_tools",
5+
"summary" : "LLM选择器工具组",
6+
"description" : "基于嵌入相似度的智能选择器工具,用于从多个选项中选择与查询最相关的选项。支持配置不同的嵌入模型和API端点。",
7+
"extensions" : { },
8+
"definitions" : [ {
9+
"schema" : {
10+
"name" : "EmbeddingChoiceSelectorTool",
11+
"description" : "嵌入选择器,从多个选项中选择一个。输入选项列表和查询字符串,返回选择结果列表。",
12+
"parameters" : {
13+
"type" : "object",
14+
"properties" : {
15+
"choice" : {
16+
"defaultValue" : "",
17+
"description" : "可选项列表",
18+
"name" : "choice",
19+
"type" : "array",
20+
"items" : {
21+
"type" : "string"
22+
},
23+
"example" : ""
4124
},
42-
"prompt": {
43-
"type": "string",
44-
"description": "prompt"
25+
"query_str" : {
26+
"defaultValue" : "",
27+
"description" : "查询字符串",
28+
"name" : "query_str",
29+
"type" : "string",
30+
"example" : ""
4531
},
46-
"mode": {
47-
"type": "string",
48-
"description": "mode"
32+
"options" : {
33+
"defaultValue" : "",
34+
"description" : "嵌入选择器配置",
35+
"name" : "options",
36+
"type" : "object",
37+
"properties" : {
38+
"api_key" : {
39+
"type" : "string"
40+
},
41+
"model_name" : {
42+
"type" : "string"
43+
},
44+
"api_base" : {
45+
"type" : "string"
46+
}
47+
},
48+
"example" : "",
49+
"required" : [ "api_key", "model_name", "api_base" ]
4950
}
5051
},
51-
"required": [
52-
"choice",
53-
"query_str"
54-
]
52+
"required" : [ "choice", "query_str", "options" ]
5553
},
56-
"return": {
57-
"title": "The Selected Choice.",
58-
"type": "array",
59-
"items": {
60-
"title": "SingleSelection",
61-
"description": "A single selection of a choice.",
62-
"type": "object",
63-
"properties": {
64-
"index": {
65-
"title": "Index",
66-
"type": "integer"
54+
"order" : [ "choice", "query_str", "options" ],
55+
"return" : {
56+
"type" : "array",
57+
"items" : {
58+
"type" : "object",
59+
"properties" : {
60+
"content" : {
61+
"type" : "string"
6762
},
68-
"reason": {
69-
"title": "Reason",
70-
"type": "string"
63+
"score" : {
64+
"type" : "number"
65+
}
66+
}
67+
},
68+
"convertor" : ""
69+
}
70+
}
71+
} ]
72+
} ],
73+
"toolGroups" : [ {
74+
"name" : "default",
75+
"summary" : "默认工具组",
76+
"description" : "包含嵌入选择器工具的默认工具组,提供基于语义相似度的智能选择功能。",
77+
"extensions" : { },
78+
"definitionGroupName" : "llm_selector_tools",
79+
"tools" : [ {
80+
"namespace" : "embedding_choice_selector",
81+
"schema" : {
82+
"name" : "EmbeddingChoiceSelectorTool",
83+
"description" : "基于嵌入相似度的智能选择器,通过计算查询字符串与选项列表的语义相似度,返回最相关的选项及其相似度分数。",
84+
"parameters" : {
85+
"type" : "object",
86+
"properties" : {
87+
"choice" : {
88+
"name" : "choice",
89+
"description" : "待选择的选项列表,工具将计算每个选项与查询字符串的语义相似度",
90+
"type" : "array",
91+
"items" : {
92+
"type" : "string"
7193
}
7294
},
73-
"required": [
74-
"index",
75-
"reason"
76-
]
77-
}
95+
"query_str" : {
96+
"name" : "query_str",
97+
"description" : "查询字符串,用于与选项列表进行语义相似度匹配",
98+
"type" : "string"
99+
},
100+
"options" : {
101+
"name" : "options",
102+
"description" : "嵌入模型配置参数,包括API密钥、模型名称和API基础URL",
103+
"type" : "object",
104+
"properties" : {
105+
"api_key" : {
106+
"description" : "用于访问嵌入模型的API密钥",
107+
"type" : "string"
108+
},
109+
"model_name" : {
110+
"description" : "要使用的嵌入模型名称",
111+
"type" : "string"
112+
},
113+
"api_base" : {
114+
"description" : "嵌入模型API的基础URL地址",
115+
"type" : "string"
116+
}
117+
},
118+
"required" : [ "api_key", "model_name", "api_base" ]
119+
}
120+
},
121+
"required" : [ ]
78122
},
79-
"parameterExtensions": {
80-
"config": [
81-
"model_name",
82-
"api_key",
83-
"api_base",
84-
"prompt",
85-
"mode"
86-
]
123+
"order" : [ "choice", "query_str", "options" ],
124+
"return" : {
125+
"name" : "selection_results",
126+
"description" : "返回选择结果列表,包含每个选项的内容和对应的相似度分数,按相似度降序排列",
127+
"type" : "array",
128+
"items" : {
129+
"type" : "object",
130+
"properties" : {
131+
"content" : {
132+
"description" : "选项的文本内容",
133+
"type" : "string"
134+
},
135+
"score" : {
136+
"description" : "该选项与查询字符串的语义相似度分数(0-1之间,分数越高表示越相似)",
137+
"type" : "number"
138+
}
139+
}
140+
},
141+
"convertor" : ""
87142
}
88-
}
89-
}
90-
]
143+
},
144+
"runnables" : {
145+
"FIT" : {
146+
"genericableId" : "llama.tools.embedding_choice_selector",
147+
"fitableId" : "default"
148+
}
149+
},
150+
"extensions" : {
151+
"tags" : [ "llama" ]
152+
},
153+
"definitionName" : "EmbeddingChoiceSelectorTool"
154+
} ]
155+
} ]
91156
}

framework/fel/python/plugins/fel_llama_selector_tools/types/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -- encoding: utf-8 --
2+
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
3+
# This file is a part of the ModelEngine Project.
4+
# Licensed under the MIT License. See License.txt in the project root for license information.
5+
# ======================================================================================================================
6+
class EmbeddingChoiceSelectorOptions(object):
7+
def __init__(self , model_name : str , api_key : str , api_base : str):
8+
self.model_name = model_name
9+
self.api_key = api_key
10+
self.api_base = api_base
11+
12+
def __eq__(self, other):
13+
if not isinstance(other, self.__class__):
14+
return False
15+
return self.__dict__ == other.__dict__
16+
17+
def __hash__(self):
18+
return hash(tuple(self.__dict__.values()))
19+
20+
def __repr__(self):
21+
return str((self.__dict__.values()))

0 commit comments

Comments
 (0)