refactor: update LLM configuration to model ID in API and frontend
- Replaced LLMConfig with model_id in TextRelationExtractionRequest and FabriTextRequest interfaces. - Updated related API handlers to retrieve chat models based on model_id. - Adjusted frontend components to pass model_id instead of LLMConfig, enhancing clarity and simplifying the configuration process. These changes streamline the model configuration process across the application, improving maintainability and usability.
This commit is contained in:
@@ -412,7 +412,7 @@ export function testMultimodalFunction(testData: {
|
||||
export interface TextRelationExtractionRequest {
|
||||
text: string;
|
||||
tags: string[];
|
||||
llm_config: LLMConfig;
|
||||
model_id: string;
|
||||
}
|
||||
|
||||
export interface Node {
|
||||
@@ -426,13 +426,6 @@ export interface Relation {
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface LLMConfig {
|
||||
source: 'local' | 'remote';
|
||||
model_name: string;
|
||||
base_url: string;
|
||||
api_key: string;
|
||||
}
|
||||
|
||||
export interface TextRelationExtractionResponse {
|
||||
nodes: Node[];
|
||||
relations: Relation[];
|
||||
@@ -454,7 +447,7 @@ export function extractTextRelations(request: TextRelationExtractionRequest): Pr
|
||||
|
||||
export interface FabriTextRequest {
|
||||
tags: string[];
|
||||
llm_config: LLMConfig;
|
||||
model_id: string;
|
||||
}
|
||||
|
||||
export interface FabriTextResponse {
|
||||
@@ -476,7 +469,6 @@ export function fabriText(request: FabriTextRequest): Promise<FabriTextResponse>
|
||||
}
|
||||
|
||||
export interface FabriTagRequest {
|
||||
llm_config: LLMConfig;
|
||||
}
|
||||
|
||||
export interface FabriTagResponse {
|
||||
|
||||
@@ -150,6 +150,7 @@
|
||||
<GraphSettings
|
||||
v-if="formData"
|
||||
:graph-extract="formData.nodeExtractConfig"
|
||||
:model-id="formData.modelConfig.llmModelId"
|
||||
:all-models="allModels"
|
||||
@update:graphExtract="handleNodeExtractUpdate"
|
||||
/>
|
||||
|
||||
@@ -296,8 +296,7 @@
|
||||
import { ref, watch, onMounted, computed } from 'vue'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { extractTextRelations, fabriText, fabriTag, type Node, type Relation, type LLMConfig } from '@/api/initialization'
|
||||
import { listModels } from '@/api/model'
|
||||
import { extractTextRelations, fabriText, fabriTag, type Node, type Relation } from '@/api/initialization'
|
||||
import { getSystemInfo } from '@/api/system'
|
||||
|
||||
const { t } = useI18n()
|
||||
@@ -312,6 +311,7 @@ interface GraphExtractConfig {
|
||||
|
||||
interface Props {
|
||||
graphExtract: GraphExtractConfig
|
||||
modelId: string
|
||||
allModels?: any[]
|
||||
}
|
||||
|
||||
@@ -333,14 +333,6 @@ const tagFabring = ref(false)
|
||||
const textFabring = ref(false)
|
||||
const extracting = ref(false)
|
||||
|
||||
// 模型状态
|
||||
const modelStatus = ref({
|
||||
llm: {
|
||||
available: false,
|
||||
config: null as LLMConfig | null
|
||||
}
|
||||
})
|
||||
|
||||
// 系统信息
|
||||
const systemInfo = ref<any>(null)
|
||||
|
||||
@@ -438,16 +430,9 @@ const removeRelation = (index: number) => {
|
||||
|
||||
// 生成随机标签
|
||||
const handleFabriTag = async () => {
|
||||
if (!modelStatus.value.llm.available || !modelStatus.value.llm.config) {
|
||||
MessagePlugin.warning(t('graphSettings.completeModelConfig'))
|
||||
return
|
||||
}
|
||||
|
||||
tagFabring.value = true
|
||||
try {
|
||||
const response = await fabriTag({
|
||||
llm_config: modelStatus.value.llm.config
|
||||
})
|
||||
const response = await fabriTag({})
|
||||
localGraphExtract.value.tags = response.tags || []
|
||||
handleTagsChange()
|
||||
MessagePlugin.success(t('graphSettings.tagsGenerated'))
|
||||
@@ -461,7 +446,7 @@ const handleFabriTag = async () => {
|
||||
|
||||
// 生成随机文本
|
||||
const handleFabriText = async () => {
|
||||
if (!modelStatus.value.llm.available || !modelStatus.value.llm.config) {
|
||||
if (!props.modelId) {
|
||||
MessagePlugin.warning(t('graphSettings.completeModelConfig'))
|
||||
return
|
||||
}
|
||||
@@ -470,7 +455,7 @@ const handleFabriText = async () => {
|
||||
try {
|
||||
const response = await fabriText({
|
||||
tags: localGraphExtract.value.tags,
|
||||
llm_config: modelStatus.value.llm.config
|
||||
model_id: props.modelId
|
||||
})
|
||||
localGraphExtract.value.text = response.text || ''
|
||||
handleTextChange()
|
||||
@@ -485,7 +470,7 @@ const handleFabriText = async () => {
|
||||
|
||||
// 提取实体关系
|
||||
const handleExtract = async () => {
|
||||
if (!modelStatus.value.llm.available || !modelStatus.value.llm.config) {
|
||||
if (!props.modelId) {
|
||||
MessagePlugin.warning(t('graphSettings.completeModelConfig'))
|
||||
return
|
||||
}
|
||||
@@ -500,7 +485,7 @@ const handleExtract = async () => {
|
||||
const response = await extractTextRelations({
|
||||
text: localGraphExtract.value.text,
|
||||
tags: localGraphExtract.value.tags,
|
||||
llm_config: modelStatus.value.llm.config
|
||||
model_id: props.modelId
|
||||
})
|
||||
localGraphExtract.value.nodes = response.nodes || []
|
||||
localGraphExtract.value.relations = response.relations || []
|
||||
@@ -543,28 +528,6 @@ const clearExtractExample = () => {
|
||||
MessagePlugin.success(t('graphSettings.exampleCleared'))
|
||||
}
|
||||
|
||||
// 加载模型状态
|
||||
const loadModelStatus = async () => {
|
||||
try {
|
||||
const models = await listModels()
|
||||
|
||||
// 查找可用的KnowledgeQA模型(对话模型)
|
||||
const llmModels = models.filter((m: any) => m.type === 'KnowledgeQA')
|
||||
if (llmModels.length > 0) {
|
||||
const llmModel = llmModels[0]
|
||||
modelStatus.value.llm.available = true
|
||||
modelStatus.value.llm.config = {
|
||||
source: llmModel.source,
|
||||
model_name: llmModel.name,
|
||||
base_url: llmModel.parameters?.base_url || '',
|
||||
api_key: llmModel.parameters?.api_key || ''
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Failed to load model status:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载系统信息
|
||||
const loadSystemInfo = async () => {
|
||||
try {
|
||||
@@ -586,10 +549,7 @@ const handleOpenGraphGuide = () => {
|
||||
|
||||
// 初始化
|
||||
onMounted(async () => {
|
||||
await Promise.all([
|
||||
loadModelStatus(),
|
||||
loadSystemInfo()
|
||||
])
|
||||
await loadSystemInfo()
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1952,16 +1952,9 @@ func (h *InitializationHandler) testMultimodalWithDocReader(
|
||||
|
||||
// TextRelationExtractionRequest 文本关系提取请求结构
|
||||
type TextRelationExtractionRequest struct {
|
||||
Text string `json:"text" binding:"required"`
|
||||
Tags []string `json:"tags" binding:"required"`
|
||||
LLMConfig LLMConfig `json:"llm_config"`
|
||||
}
|
||||
|
||||
type LLMConfig struct {
|
||||
Source string `json:"source"`
|
||||
ModelName string `json:"model_name"`
|
||||
BaseUrl string `json:"base_url"`
|
||||
ApiKey string `json:"api_key"`
|
||||
Text string `json:"text" binding:"required"`
|
||||
Tags []string `json:"tags" binding:"required"`
|
||||
ModelID string `json:"model_id" binding:"required"`
|
||||
}
|
||||
|
||||
// TextRelationExtractionResponse 文本关系提取响应结构
|
||||
@@ -2009,8 +2002,16 @@ func (h *InitializationHandler) ExtractTextRelations(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 根据模型ID获取chat模型
|
||||
chatModel, err := h.modelService.GetChatModel(ctx, req.ModelID)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "获取模型失败", err)
|
||||
c.Error(errors.NewBadRequestError("获取模型失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 调用模型服务进行文本关系提取
|
||||
result, err := h.extractRelationsFromText(ctx, req.Text, req.Tags, req.LLMConfig)
|
||||
result, err := h.extractRelationsFromText(ctx, req.Text, req.Tags, chatModel)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "文本关系提取失败", err)
|
||||
c.Error(errors.NewInternalServerError("文本关系提取失败: " + err.Error()))
|
||||
@@ -2028,20 +2029,8 @@ func (h *InitializationHandler) extractRelationsFromText(
|
||||
ctx context.Context,
|
||||
text string,
|
||||
tags []string,
|
||||
llm LLMConfig,
|
||||
chatModel chat.Chat,
|
||||
) (*TextRelationExtractionResponse, error) {
|
||||
chatModel, err := chat.NewChat(&chat.ChatConfig{
|
||||
ModelID: "initialization",
|
||||
APIKey: llm.ApiKey,
|
||||
BaseURL: llm.BaseUrl,
|
||||
ModelName: llm.ModelName,
|
||||
Source: types.ModelSource(llm.Source),
|
||||
}, h.ollamaService)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "初始化模型服务失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
template := &types.PromptTemplateStructured{
|
||||
Description: h.config.ExtractManager.ExtractGraph.Description,
|
||||
Tags: tags,
|
||||
@@ -2066,8 +2055,8 @@ func (h *InitializationHandler) extractRelationsFromText(
|
||||
|
||||
// FabriTextRequest is a request for generating example text
|
||||
type FabriTextRequest struct {
|
||||
Tags []string `json:"tags"`
|
||||
LLMConfig LLMConfig `json:"llm_config"`
|
||||
Tags []string `json:"tags"`
|
||||
ModelID string `json:"model_id" binding:"required"`
|
||||
}
|
||||
|
||||
// FabriTextResponse is a response for generating example text
|
||||
@@ -2097,7 +2086,14 @@ func (h *InitializationHandler) FabriText(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.fabriText(ctx, req.Tags, req.LLMConfig)
|
||||
chatModel, err := h.modelService.GetChatModel(ctx, req.ModelID)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "获取模型失败", err)
|
||||
c.Error(errors.NewBadRequestError("获取模型失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.fabriText(ctx, req.Tags, chatModel)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "failed to generate fabri text", err)
|
||||
c.Error(errors.NewInternalServerError("failed to generate fabri text: " + err.Error()))
|
||||
@@ -2111,19 +2107,7 @@ func (h *InitializationHandler) FabriText(c *gin.Context) {
|
||||
}
|
||||
|
||||
// fabriText generates example text
|
||||
func (h *InitializationHandler) fabriText(ctx context.Context, tags []string, llm LLMConfig) (string, error) {
|
||||
chatModel, err := chat.NewChat(&chat.ChatConfig{
|
||||
ModelID: "initialization",
|
||||
APIKey: llm.ApiKey,
|
||||
BaseURL: llm.BaseUrl,
|
||||
ModelName: llm.ModelName,
|
||||
Source: types.ModelSource(llm.Source),
|
||||
}, h.ollamaService)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "初始化模型服务失败", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) fabriText(ctx context.Context, tags []string, chatModel chat.Chat) (string, error) {
|
||||
content := h.config.ExtractManager.FabriText.WithNoTag
|
||||
if len(tags) > 0 {
|
||||
tagStr, _ := json.Marshal(tags)
|
||||
@@ -2146,9 +2130,7 @@ func (h *InitializationHandler) fabriText(ctx context.Context, tags []string, ll
|
||||
}
|
||||
|
||||
// FabriTagRequest is a request for generating tags
|
||||
type FabriTagRequest struct {
|
||||
LLMConfig LLMConfig `json:"llm_config"`
|
||||
}
|
||||
type FabriTagRequest struct{}
|
||||
|
||||
// FabriTagResponse is a response for generating tags
|
||||
type FabriTagResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user