[add] Neo prepare hook
This commit is contained in:
parent
9a37b1c72b
commit
5e46dc74f5
3 changed files with 57 additions and 6 deletions
|
|
@ -318,7 +318,7 @@ func (req *Request) saveHistory(content []byte, messages []map[string]interface{
|
||||||
|
|
||||||
func (req *Request) error(err error, cb func(msg *message.JSON) int) {
|
func (req *Request) error(err error, cb func(msg *message.JSON) int) {
|
||||||
cb(req.msg().Text(err.Error()))
|
cb(req.msg().Text(err.Error()))
|
||||||
cb(message.New().Done())
|
cb(req.msg().Done())
|
||||||
// req.Done()
|
// req.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -400,7 +400,7 @@ func (req *Request) runScript(id string, args []interface{}, cb func(msg *messag
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(req.msg().Done())
|
cb(req.msg().Done())
|
||||||
req.Done()
|
// req.Done()
|
||||||
return v8go.Null(v8ctx.Isolate())
|
return v8go.Null(v8ctx.Isolate())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
58
neo/neo.go
58
neo/neo.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/yaoapp/gou/api"
|
"github.com/yaoapp/gou/api"
|
||||||
|
|
@ -143,7 +144,7 @@ func (neo *DSL) Answer(ctx command.Context, question string, answer Answer) erro
|
||||||
content := []byte{}
|
content := []byte{}
|
||||||
|
|
||||||
// get the chat messages
|
// get the chat messages
|
||||||
messages, err := neo.chatMessages(ctx.Sid, question)
|
messages, err := neo.chatMessages(ctx, question)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -242,15 +243,64 @@ func (neo *DSL) prompts() []map[string]interface{} {
|
||||||
return prompts
|
return prompts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepare the messages
|
||||||
|
func (neo *DSL) prepare(ctx command.Context, messages []map[string]interface{}) []map[string]interface{} {
|
||||||
|
if neo.Prepare == "" {
|
||||||
|
return []map[string]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
prompts := []map[string]interface{}{}
|
||||||
|
p, err := process.Of(neo.Prepare, ctx, messages)
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Neo prepare error: %s", err.Error())
|
||||||
|
return prompts
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := p.Exec()
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Neo prepare execute error: %s", err.Error())
|
||||||
|
return prompts
|
||||||
|
}
|
||||||
|
|
||||||
|
items, ok := data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
color.Red("Neo prepare response is not array")
|
||||||
|
return prompts
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, item := range items {
|
||||||
|
v, ok := item.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
color.Red("Neo prepare response [%d] is not map", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := v["role"]; !ok {
|
||||||
|
color.Red(`Neo prepare response [%d]["role"] required`, i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := v["content"]; !ok {
|
||||||
|
color.Red(`Neo prepare response [%d]["content"] required`, i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
prompts = append(prompts, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return prompts
|
||||||
|
}
|
||||||
|
|
||||||
// chatMessages get the chat messages
|
// chatMessages get the chat messages
|
||||||
func (neo *DSL) chatMessages(sid, 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()...)
|
messages := append([]map[string]interface{}{}, neo.prompts()...)
|
||||||
history, err := neo.Conversation.GetHistory(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(messages, history...)
|
messages = append(messages, history...)
|
||||||
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": sid})
|
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid})
|
||||||
return messages, nil
|
return messages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ type DSL struct {
|
||||||
Connector string `json:"connector"`
|
Connector string `json:"connector"`
|
||||||
ConversationSetting conversation.Setting `json:"conversation" yaml:"conversation"`
|
ConversationSetting conversation.Setting `json:"conversation" yaml:"conversation"`
|
||||||
Option map[string]interface{} `json:"option"`
|
Option map[string]interface{} `json:"option"`
|
||||||
|
Prepare string `json:"prepare,omitempty"`
|
||||||
Prompts []aigc.Prompt `json:"prompts,omitempty"`
|
Prompts []aigc.Prompt `json:"prompts,omitempty"`
|
||||||
Allows []string `json:"allows,omitempty"`
|
Allows []string `json:"allows,omitempty"`
|
||||||
AI aigc.AI `json:"-" yaml:"-"`
|
AI aigc.AI `json:"-" yaml:"-"`
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue