feat: Add SelectModel command to DSL

Add the SelectModel command to the DSL, allowing the selection of a specific model. This command takes a model name as input and sets the AI to use that model. It returns a success message with a status code of 200 if the selection is successful.
This commit is contained in:
Max 2024-05-25 20:17:25 +08:00
parent 1a1ffabbd8
commit 1f4e9e5a16
2 changed files with 42 additions and 2 deletions

View file

@ -130,6 +130,29 @@ func (neo *DSL) API(router *gin.Engine, path string) error {
}
switch cmd {
case "ModelList":
c.JSON(200, gin.H{"data": neo.Models, "code": 200})
c.Done()
case "SelectModel":
model, ok := payload["model"].(string)
if !ok {
c.JSON(400, gin.H{"message": "model is required", "code": 400})
c.Done()
return
}
err := neo.Select(model)
if err != nil {
c.JSON(500, gin.H{"message": err.Error(), "code": 500})
c.Done()
return
}
c.JSON(200, gin.H{"message": "success", "code": 200})
c.Done()
case "ExitCommandMode":
err := command.Exit(sid)
if err != nil {
@ -524,11 +547,17 @@ func (neo *DSL) getGuardHandlers() ([]gin.HandlerFunc, error) {
// NewAI create a new AI
func (neo *DSL) newAI() error {
if neo.Connector == "" {
ai, err := openai.NewMoapi("gpt-3.5-turbo")
if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") {
model := "gpt-3.5-turbo"
if neo.Connector != "" {
model = strings.TrimPrefix(neo.Connector, "moapi:")
}
ai, err := openai.NewMoapi(model)
if err != nil {
return err
}
neo.AI = ai
return nil
}
@ -550,6 +579,16 @@ func (neo *DSL) newAI() error {
return fmt.Errorf("%s connector %s not support, should be a openai", neo.ID, neo.Connector)
}
// Select select the model
func (neo *DSL) Select(model string) error {
ai, err := openai.NewMoapi(model)
if err != nil {
return err
}
neo.AI = ai
return nil
}
// newConversation create a new conversation
func (neo *DSL) newConversation() error {

View file

@ -22,6 +22,7 @@ type DSL struct {
Prompts []aigc.Prompt `json:"prompts,omitempty"`
Allows []string `json:"allows,omitempty"`
Command Command `json:"command,omitempty"`
Models []string `json:"models,omitempty"`
AI aigc.AI `json:"-" yaml:"-"`
Conversation conversation.Conversation `json:"-" yaml:"-"`
GuardHandlers []gin.HandlerFunc `json:"-" yaml:"-"`