Optimize Neo the prepare prompts
This commit is contained in:
parent
1176938ed1
commit
b477a29403
3 changed files with 79 additions and 68 deletions
|
|
@ -5,7 +5,6 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/yaoapp/gou/connector"
|
||||
"github.com/yaoapp/yao/aigc"
|
||||
"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
|
||||
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{}{}
|
||||
for _, prompt := range prompts {
|
||||
messages = append(messages, map[string]interface{}{
|
||||
"role": prompt.Role,
|
||||
"content": prompt.Content,
|
||||
})
|
||||
}
|
||||
// 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
|
||||
// })
|
||||
|
||||
messages = append(messages, map[string]interface{}{
|
||||
"role": "user",
|
||||
"content": content,
|
||||
})
|
||||
// if !has {
|
||||
// return "", fmt.Errorf("no related command found")
|
||||
// }
|
||||
|
||||
res, ex := driver.ai.ChatCompletions(messages, nil, nil)
|
||||
if ex != nil {
|
||||
return "", fmt.Errorf(ex.Message)
|
||||
}
|
||||
// messages := []map[string]interface{}{}
|
||||
// for _, prompt := range prompts {
|
||||
// messages = append(messages, map[string]interface{}{
|
||||
// "role": prompt.Role,
|
||||
// "content": prompt.Content,
|
||||
// })
|
||||
// }
|
||||
|
||||
bytes, err := jsoniter.Marshal(res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// messages = append(messages, map[string]interface{}{
|
||||
// "role": "user",
|
||||
// "content": content,
|
||||
// })
|
||||
|
||||
var data struct {
|
||||
Choices []struct{ Message struct{ Content string } }
|
||||
}
|
||||
err = jsoniter.Unmarshal(bytes, &data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// res, ex := driver.ai.ChatCompletions(messages, nil, nil)
|
||||
// if ex != nil {
|
||||
// return "", fmt.Errorf(ex.Message)
|
||||
// }
|
||||
|
||||
if len(data.Choices) == 0 {
|
||||
return "", fmt.Errorf("no related command found")
|
||||
}
|
||||
// bytes, err := jsoniter.Marshal(res)
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -52,13 +52,13 @@ func TestMemoryMatch(t *testing.T) {
|
|||
defer test.Clean()
|
||||
|
||||
mem := prepare(t)
|
||||
id, err := mem.Match(query.Param{Stack: "Table.Page.pet"}, "Generate table test data")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, "table.data", id)
|
||||
// id, err := mem.Match(query.Param{Stack: "Table.Page.pet"}, "Generate table test data")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// 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")
|
||||
}
|
||||
|
||||
|
|
|
|||
13
neo/neo.go
13
neo/neo.go
|
|
@ -339,15 +339,24 @@ func (neo *DSL) prepare(ctx command.Context, messages []map[string]interface{})
|
|||
|
||||
// chatMessages get the chat messages
|
||||
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)
|
||||
if err != nil {
|
||||
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, 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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue