1717package dao
1818
1919import (
20+ "fmt"
2021 "ragflow/internal/entity"
2122)
2223
@@ -28,6 +29,16 @@ func NewTenantLLMDAO() *TenantLLMDAO {
2829 return & TenantLLMDAO {}
2930}
3031
32+ // GetByID get tenant LLM by primary key ID
33+ func (dao * TenantLLMDAO ) GetByID (id int64 ) (* entity.TenantLLM , error ) {
34+ var tenantLLM entity.TenantLLM
35+ err := DB .Where ("id = ?" , id ).First (& tenantLLM ).Error
36+ if err != nil {
37+ return nil , err
38+ }
39+ return & tenantLLM , nil
40+ }
41+
3142// GetByTenantAndModelName get tenant LLM by tenant ID and model name
3243func (dao * TenantLLMDAO ) GetByTenantAndModelName (tenantID , providerName string , modelName string ) (* entity.TenantLLM , error ) {
3344 var tenantLLM entity.TenantLLM
@@ -38,6 +49,16 @@ func (dao *TenantLLMDAO) GetByTenantAndModelName(tenantID, providerName string,
3849 return & tenantLLM , nil
3950}
4051
52+ // GetByTenantNameAndType get tenant LLM by tenant ID, model name, and model type
53+ func (dao * TenantLLMDAO ) GetByTenantNameAndType (tenantID , modelName string , modelType entity.ModelType ) (* entity.TenantLLM , error ) {
54+ var tenantLLM entity.TenantLLM
55+ err := DB .Where ("tenant_id = ? AND llm_name = ? AND model_type = ?" , tenantID , modelName , modelType ).First (& tenantLLM ).Error
56+ if err != nil {
57+ return nil , err
58+ }
59+ return & tenantLLM , nil
60+ }
61+
4162// GetByTenantAndType get tenant LLM by tenant ID and model type
4263func (dao * TenantLLMDAO ) GetByTenantAndType (tenantID string , modelType entity.ModelType ) (* entity.TenantLLM , error ) {
4364 var tenantLLM entity.TenantLLM
@@ -268,3 +289,50 @@ func (dao *TenantLLMDAO) GetByTenantIDLLMNameAndFactory(tenantID, llmName, facto
268289 }
269290 return & tenantLLM , nil
270291}
292+
293+ // LookupTenantLLMByID looks up a TenantLLM record by ID and returns the record plus composite model name.
294+ func LookupTenantLLMByID (tenantLLMDao * TenantLLMDAO , id int64 ) (* entity.TenantLLM , string , error ) {
295+ tenantLLM , err := tenantLLMDao .GetByID (id )
296+ if err != nil {
297+ return nil , "" , fmt .Errorf ("failed to get tenant_llm by id %d: %w" , id , err )
298+ }
299+ if tenantLLM == nil || tenantLLM .LLMName == nil || * tenantLLM .LLMName == "" {
300+ return nil , "" , fmt .Errorf ("tenant_llm record not found for id %d" , id )
301+ }
302+ compositeName := fmt .Sprintf ("%s@%s" , * tenantLLM .LLMName , tenantLLM .LLMFactory )
303+ return tenantLLM , compositeName , nil
304+ }
305+
306+ // LookupTenantLLMByName looks up a TenantLLM record by tenant name and model type.
307+ func LookupTenantLLMByName (tenantLLMDao * TenantLLMDAO , tenantID , name string , modelType entity.ModelType ) (* entity.TenantLLM , string , error ) {
308+ // Parse factory from name if present (e.g., "model@Factory")
309+ modelName , factory := splitModelNameAndFactory (name )
310+
311+ // If factory is found, use factory-based lookup
312+ if factory != "" {
313+ return LookupTenantLLMByFactory (tenantLLMDao , tenantID , factory , modelName , modelType )
314+ }
315+
316+ tenantLLM , err := tenantLLMDao .GetByTenantNameAndType (tenantID , modelName , modelType )
317+ if err != nil {
318+ return nil , "" , fmt .Errorf ("failed to get tenant_llm by name %s: %w" , name , err )
319+ }
320+ if tenantLLM == nil || tenantLLM .LLMName == nil || * tenantLLM .LLMName == "" {
321+ return nil , "" , fmt .Errorf ("tenant_llm record not found for name %s" , name )
322+ }
323+ compositeName := fmt .Sprintf ("%s@%s" , * tenantLLM .LLMName , tenantLLM .LLMFactory )
324+ return tenantLLM , compositeName , nil
325+ }
326+
327+ // LookupTenantLLMByFactory looks up a TenantLLM record by tenant, factory, and model name.
328+ func LookupTenantLLMByFactory (tenantLLMDao * TenantLLMDAO , tenantID , factory , name string , modelType entity.ModelType ) (* entity.TenantLLM , string , error ) {
329+ tenantLLM , err := tenantLLMDao .GetByTenantFactoryAndModelName (tenantID , factory , name )
330+ if err != nil {
331+ return nil , "" , fmt .Errorf ("failed to get tenant_llm by factory %s and name %s: %w" , factory , name , err )
332+ }
333+ if tenantLLM == nil || tenantLLM .LLMName == nil || * tenantLLM .LLMName == "" {
334+ return nil , "" , fmt .Errorf ("tenant_llm record not found for factory %s and name %s" , factory , name )
335+ }
336+ compositeName := fmt .Sprintf ("%s@%s" , * tenantLLM .LLMName , tenantLLM .LLMFactory )
337+ return tenantLLM , compositeName , nil
338+ }
0 commit comments