refactor: select model type by env LLM_MODEL_USE

This commit is contained in:
lilong.129
2025-04-29 23:14:12 +08:00
parent 3ffa5d96d2
commit cc9a527274
5 changed files with 19 additions and 20 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2504292233 v5.0.0-beta-2504292314
+9 -7
View File
@@ -49,10 +49,9 @@ func WithCVService(service CVServiceType) AIServiceOption {
type LLMServiceType string type LLMServiceType string
const ( const (
LLMServiceTypeUITARS LLMServiceType = "ui-tars" LLMServiceTypeUITARS LLMServiceType = "ui-tars"
LLMServiceTypeGPT4o LLMServiceType = "gpt-4o" LLMServiceTypeGPT LLMServiceType = "gpt"
LLMServiceTypeGPT4Vision LLMServiceType = "gpt-4-vision" LLMServiceTypeQwenVL LLMServiceType = "qwen-vl"
LLMServiceTypeQwenVL LLMServiceType = "qwen-vl"
) )
// ILLMService 定义了 LLM 服务接口,包括规划和断言功能 // ILLMService 定义了 LLM 服务接口,包括规划和断言功能
@@ -67,7 +66,7 @@ func WithLLMService(modelType LLMServiceType) AIServiceOption {
var planner IPlanner var planner IPlanner
var err error var err error
switch modelType { switch modelType {
case LLMServiceTypeGPT4o: case LLMServiceTypeGPT:
// TODO: implement gpt-4o planner and asserter // TODO: implement gpt-4o planner and asserter
planner, err = NewPlanner(context.Background()) planner, err = NewPlanner(context.Background())
case LLMServiceTypeUITARS: case LLMServiceTypeUITARS:
@@ -79,9 +78,9 @@ func WithLLMService(modelType LLMServiceType) AIServiceOption {
} }
// init asserter // init asserter
asserter, err := NewAsserter(context.Background(), modelType) asserter, err := NewAsserter(context.Background())
if err != nil { if err != nil {
log.Error().Err(err).Msgf("init %s asserter failed", modelType) log.Error().Err(err).Msg("init asserter failed")
os.Exit(code.GetErrorCode(err)) os.Exit(code.GetErrorCode(err))
} }
@@ -115,11 +114,14 @@ const (
EnvModelName = "LLM_MODEL_NAME" EnvModelName = "LLM_MODEL_NAME"
) )
var EnvModelUse string
// GetOpenAIModelConfig get OpenAI config // GetOpenAIModelConfig get OpenAI config
func GetOpenAIModelConfig() (*openai.ChatModelConfig, error) { func GetOpenAIModelConfig() (*openai.ChatModelConfig, error) {
if err := config.LoadEnv(); err != nil { if err := config.LoadEnv(); err != nil {
return nil, errors.Wrap(code.LoadEnvError, err.Error()) return nil, errors.Wrap(code.LoadEnvError, err.Error())
} }
EnvModelUse = os.Getenv("LLM_MODEL_USE")
openaiBaseURL := os.Getenv(EnvOpenAIBaseURL) openaiBaseURL := os.Getenv(EnvOpenAIBaseURL)
if openaiBaseURL == "" { if openaiBaseURL == "" {
+7 -10
View File
@@ -43,14 +43,12 @@ type Asserter struct {
model model.ToolCallingChatModel model model.ToolCallingChatModel
systemPrompt string systemPrompt string
history ConversationHistory history ConversationHistory
modelType LLMServiceType
} }
// NewAsserter creates a new Asserter instance // NewAsserter creates a new Asserter instance
func NewAsserter(ctx context.Context, modelType LLMServiceType) (*Asserter, error) { func NewAsserter(ctx context.Context) (*Asserter, error) {
asserter := &Asserter{ asserter := &Asserter{
ctx: ctx, ctx: ctx,
modelType: modelType,
systemPrompt: defaultAssertionPrompt, systemPrompt: defaultAssertionPrompt,
} }
@@ -59,12 +57,11 @@ func NewAsserter(ctx context.Context, modelType LLMServiceType) (*Asserter, erro
return nil, err return nil, err
} }
switch modelType { if strings.Contains(EnvModelUse, string(LLMServiceTypeUITARS)) {
case LLMServiceTypeUITARS:
asserter.systemPrompt += "\n\n" + uiTarsAssertionResponseFormat asserter.systemPrompt += "\n\n" + uiTarsAssertionResponseFormat
case LLMServiceTypeQwenVL: } else if strings.Contains(EnvModelUse, string(LLMServiceTypeQwenVL)) {
asserter.systemPrompt += "\n\n" + defaultAssertionResponseJsonFormat asserter.systemPrompt += "\n\n" + defaultAssertionResponseJsonFormat
case LLMServiceTypeGPT4Vision, LLMServiceTypeGPT4o: } else if strings.Contains(EnvModelUse, string(LLMServiceTypeGPT)) {
// define output format // define output format
type OutputFormat struct { type OutputFormat struct {
Thought string `json:"thought"` Thought string `json:"thought"`
@@ -86,8 +83,8 @@ func NewAsserter(ctx context.Context, modelType LLMServiceType) (*Asserter, erro
Strict: false, Strict: false,
}, },
} }
default: } else {
return nil, errors.New("not supported model type for asserter") return nil, fmt.Errorf("model type %s not supported for asserter", EnvModelUse)
} }
asserter.model, err = openai.NewChatModel(ctx, config) asserter.model, err = openai.NewChatModel(ctx, config)
@@ -144,7 +141,7 @@ Here is the assertion. Please tell whether it is truthy according to the screens
startTime := time.Now() startTime := time.Now()
resp, err := a.model.Generate(a.ctx, a.history) resp, err := a.model.Generate(a.ctx, a.history)
log.Info().Float64("elapsed(s)", time.Since(startTime).Seconds()). log.Info().Float64("elapsed(s)", time.Since(startTime).Seconds()).
Str("model", string(a.modelType)).Msg("call model service for assertion") Str("model", EnvModelUse).Msg("call model service for assertion")
if err != nil { if err != nil {
return nil, errors.Wrap(code.LLMRequestServiceError, err.Error()) return nil, errors.Wrap(code.LLMRequestServiceError, err.Error())
} }
+1 -1
View File
@@ -10,7 +10,7 @@ import (
) )
func createAsserter(t *testing.T) *Asserter { func createAsserter(t *testing.T) *Asserter {
asserter, err := NewAsserter(context.Background(), LLMServiceTypeUITARS) asserter, err := NewAsserter(context.Background())
require.NoError(t, err) require.NoError(t, err)
return asserter return asserter
} }
+1 -1
View File
@@ -30,7 +30,7 @@ func NewPlanner(ctx context.Context) (*Planner, error) {
return &Planner{ return &Planner{
ctx: ctx, ctx: ctx,
model: model, model: model,
modelType: LLMServiceTypeGPT4o, modelType: LLMServiceTypeGPT,
systemPrompt: uiTarsPlanningPrompt, // TODO: change prompt with function calling systemPrompt: uiTarsPlanningPrompt, // TODO: change prompt with function calling
}, nil }, nil
} }