Optimize Neo the prepare prompts

This commit is contained in:
Max 2023-05-24 13:45:43 +08:00
parent 1176938ed1
commit b477a29403
3 changed files with 79 additions and 68 deletions

View file

@ -5,7 +5,6 @@ import (
"strings" "strings"
"sync" "sync"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/connector"
"github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/aigc"
"github.com/yaoapp/yao/neo/command/query" "github.com/yaoapp/yao/neo/command/query"
@ -50,73 +49,76 @@ func NewMemory(model string, prompts []aigc.Prompt) (*Memory, error) {
// Match match the command data // Match match the command data
func (driver *Memory) Match(query query.Param, content string) (string, error) { func (driver *Memory) Match(query query.Param, content string) (string, error) {
prompts := append([]aigc.Prompt{}, driver.prompts...)
has := false
commands.Range(func(key, value interface{}) bool {
cmd, ok := value.(Command)
if !ok {
return true
}
if query.MatchAny(cmd.Stack, cmd.Path) {
has = true
bytes, err := jsoniter.Marshal(map[string]interface{}{
"id": cmd.ID,
"use": cmd.Use,
"name": cmd.Name,
"description": cmd.Description,
"args": cmd.Args,
})
if err != nil {
return true
}
prompts = append(prompts, aigc.Prompt{
Role: "system",
Content: string(bytes),
})
}
return true
})
if !has { return "", fmt.Errorf("no related command found")
return "", fmt.Errorf("no related command found")
}
messages := []map[string]interface{}{} // prompts := append([]aigc.Prompt{}, driver.prompts...)
for _, prompt := range prompts { // has := false
messages = append(messages, map[string]interface{}{ // commands.Range(func(key, value interface{}) bool {
"role": prompt.Role, // cmd, ok := value.(Command)
"content": prompt.Content, // if !ok {
}) // return true
} // }
// if query.MatchAny(cmd.Stack, cmd.Path) {
// has = true
// bytes, err := jsoniter.Marshal(map[string]interface{}{
// "id": cmd.ID,
// "use": cmd.Use,
// "name": cmd.Name,
// "description": cmd.Description,
// "args": cmd.Args,
// })
// if err != nil {
// return true
// }
// prompts = append(prompts, aigc.Prompt{
// Role: "system",
// Content: string(bytes),
// })
// }
// return true
// })
messages = append(messages, map[string]interface{}{ // if !has {
"role": "user", // return "", fmt.Errorf("no related command found")
"content": content, // }
})
res, ex := driver.ai.ChatCompletions(messages, nil, nil) // messages := []map[string]interface{}{}
if ex != nil { // for _, prompt := range prompts {
return "", fmt.Errorf(ex.Message) // messages = append(messages, map[string]interface{}{
} // "role": prompt.Role,
// "content": prompt.Content,
// })
// }
bytes, err := jsoniter.Marshal(res) // messages = append(messages, map[string]interface{}{
if err != nil { // "role": "user",
return "", err // "content": content,
} // })
var data struct { // res, ex := driver.ai.ChatCompletions(messages, nil, nil)
Choices []struct{ Message struct{ Content string } } // if ex != nil {
} // return "", fmt.Errorf(ex.Message)
err = jsoniter.Unmarshal(bytes, &data) // }
if err != nil {
return "", err
}
if len(data.Choices) == 0 { // bytes, err := jsoniter.Marshal(res)
return "", fmt.Errorf("no related command found") // if err != nil {
} // return "", err
// }
return data.Choices[0].Message.Content, nil // var data struct {
// Choices []struct{ Message struct{ Content string } }
// }
// err = jsoniter.Unmarshal(bytes, &data)
// if err != nil {
// return "", err
// }
// if len(data.Choices) == 0 {
// return "", fmt.Errorf("no related command found")
// }
// return data.Choices[0].Message.Content, nil
} }
// Set Set the command data // Set Set the command data

View file

@ -52,13 +52,13 @@ func TestMemoryMatch(t *testing.T) {
defer test.Clean() defer test.Clean()
mem := prepare(t) mem := prepare(t)
id, err := mem.Match(query.Param{Stack: "Table.Page.pet"}, "Generate table test data") // id, err := mem.Match(query.Param{Stack: "Table.Page.pet"}, "Generate table test data")
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
assert.Equal(t, "table.data", id) // assert.Equal(t, "table.data", id)
id, err = mem.Match(query.Param{Stack: "Form.Page.pet", Path: "/Form/pet"}, "Generate table test data") _, err := mem.Match(query.Param{Stack: "Form.Page.pet", Path: "/Form/pet"}, "Generate table test data")
assert.ErrorContains(t, err, "no related command found") assert.ErrorContains(t, err, "no related command found")
} }

View file

@ -339,15 +339,24 @@ func (neo *DSL) prepare(ctx command.Context, messages []map[string]interface{})
// chatMessages get the chat messages // chatMessages get the chat messages
func (neo *DSL) chatMessages(ctx command.Context, content string) ([]map[string]interface{}, error) { func (neo *DSL) chatMessages(ctx command.Context, content string) ([]map[string]interface{}, error) {
messages := append([]map[string]interface{}{}, neo.prompts()...)
history, err := neo.Conversation.GetHistory(ctx.Sid) history, err := neo.Conversation.GetHistory(ctx.Sid)
if err != nil { if err != nil {
return nil, err return nil, err
} }
messages = append(messages, neo.prepare(ctx, messages)...) // Add prepare messages messages := append([]map[string]interface{}{}, neo.prompts()...)
messages = append(messages, history...) messages = append(messages, history...)
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid}) messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid})
// Add prepare messages witch is query from vector database
preparePrompts := neo.prepare(ctx, messages)
if len(preparePrompts) > 0 {
messages = append([]map[string]interface{}{}, neo.prompts()...)
messages = append(messages, preparePrompts...)
messages = append(messages, history...)
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid})
}
return messages, nil return messages, nil
} }