[add] neo custom write process
This commit is contained in:
parent
1b8afda828
commit
bd518610b9
4 changed files with 111 additions and 8 deletions
|
|
@ -75,6 +75,66 @@ func (json *JSON) Text(text string) *JSON {
|
||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map set from map
|
||||||
|
func (json *JSON) Map(msg map[string]interface{}) *JSON {
|
||||||
|
if msg == nil {
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
|
||||||
|
if text, ok := msg["text"].(string); ok {
|
||||||
|
json.Message.Text = text
|
||||||
|
}
|
||||||
|
|
||||||
|
if done, ok := msg["done"].(bool); ok {
|
||||||
|
json.Message.Done = done
|
||||||
|
}
|
||||||
|
|
||||||
|
if confirm, ok := msg["confirm"].(bool); ok {
|
||||||
|
json.Message.Confirm = confirm
|
||||||
|
}
|
||||||
|
|
||||||
|
if command, ok := msg["command"].(map[string]interface{}); ok {
|
||||||
|
json.Message.Command = &Command{}
|
||||||
|
if id, ok := command["id"].(string); ok {
|
||||||
|
json.Message.Command.ID = id
|
||||||
|
}
|
||||||
|
if name, ok := command["name"].(string); ok {
|
||||||
|
json.Message.Command.Name = name
|
||||||
|
}
|
||||||
|
if request, ok := command["request"].(string); ok {
|
||||||
|
json.Message.Command.Reqeust = request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if actions, ok := msg["actions"].([]interface{}); ok {
|
||||||
|
for _, action := range actions {
|
||||||
|
if v, ok := action.(map[string]interface{}); ok {
|
||||||
|
action := Action{}
|
||||||
|
if name, ok := v["name"].(string); ok {
|
||||||
|
action.Name = name
|
||||||
|
}
|
||||||
|
if t, ok := v["type"].(string); ok {
|
||||||
|
action.Type = t
|
||||||
|
}
|
||||||
|
if payload, ok := v["payload"].(map[string]interface{}); ok {
|
||||||
|
action.Payload = payload
|
||||||
|
}
|
||||||
|
|
||||||
|
if next, ok := v["next"].(string); ok {
|
||||||
|
action.Next = next
|
||||||
|
}
|
||||||
|
json.Message.Actions = append(json.Message.Actions, action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, ok := msg["data"].(map[string]interface{}); ok {
|
||||||
|
json.Message.Data = data
|
||||||
|
}
|
||||||
|
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
|
||||||
// Done set the done
|
// Done set the done
|
||||||
func (json *JSON) Done() *JSON {
|
func (json *JSON) Done() *JSON {
|
||||||
json.Message.Done = true
|
json.Message.Done = true
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ package message
|
||||||
|
|
||||||
// Message the message
|
// Message the message
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Text string `json:"text,omitempty"`
|
Text string `json:"text,omitempty"`
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
Done bool `json:"done,omitempty"`
|
Done bool `json:"done,omitempty"`
|
||||||
Confirm bool `json:"confirm,omitempty"`
|
Confirm bool `json:"confirm,omitempty"`
|
||||||
Command *Command `json:"command,omitempty"`
|
Command *Command `json:"command,omitempty"`
|
||||||
Actions []Action `json:"actions,omitempty"`
|
Actions []Action `json:"actions,omitempty"`
|
||||||
Data map[string]interface{}
|
Data map[string]interface{} `json:"-,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Action the action
|
// Action the action
|
||||||
|
|
|
||||||
44
neo/neo.go
44
neo/neo.go
|
|
@ -180,6 +180,7 @@ func (neo *DSL) Answer(ctx command.Context, question string, answer Answer) erro
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Trace("Command with AI: question: %s messages:%v", question, messages)
|
||||||
err = req.Run(messages, func(msg *message.JSON) int {
|
err = req.Run(messages, func(msg *message.JSON) int {
|
||||||
chanStream <- msg
|
chanStream <- msg
|
||||||
return 1
|
return 1
|
||||||
|
|
@ -193,6 +194,7 @@ func (neo *DSL) Answer(ctx command.Context, question string, answer Answer) erro
|
||||||
}
|
}
|
||||||
|
|
||||||
// chat with AI
|
// chat with AI
|
||||||
|
log.Trace("Chat with AI: question:%s messages:%v", question, messages)
|
||||||
_, ex := neo.AI.ChatCompletionsWith(ctx, messages, neo.Option, func(data []byte) int {
|
_, ex := neo.AI.ChatCompletionsWith(ctx, messages, neo.Option, func(data []byte) int {
|
||||||
chanStream <- message.NewOpenAI(data)
|
chanStream <- message.NewOpenAI(data)
|
||||||
return 1
|
return 1
|
||||||
|
|
@ -246,7 +248,12 @@ func (neo *DSL) Answer(ctx command.Context, question string, answer Answer) erro
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Write(w)
|
err := neo.write(msg, w, ctx, messages)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("Neo write process msg: %v error: %s", msg, err.Error())
|
||||||
|
msg.Write(w)
|
||||||
|
}
|
||||||
|
|
||||||
content = msg.Append(content)
|
content = msg.Append(content)
|
||||||
return !msg.IsDone()
|
return !msg.IsDone()
|
||||||
|
|
||||||
|
|
@ -302,6 +309,41 @@ func (neo *DSL) prompts() []map[string]interface{} {
|
||||||
return prompts
|
return prompts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// after the after hook
|
||||||
|
func (neo *DSL) write(msg *message.JSON, w io.Writer, ctx command.Context, messages []map[string]interface{}) error {
|
||||||
|
|
||||||
|
if neo.Write == "" {
|
||||||
|
msg.Write(w)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := process.Of(neo.Write, ctx, messages, msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := p.WithSID(ctx.Sid).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res == nil {
|
||||||
|
return fmt.Errorf("Neo custom write return null")
|
||||||
|
}
|
||||||
|
|
||||||
|
if messages, ok := res.([]interface{}); ok {
|
||||||
|
for _, new := range messages {
|
||||||
|
if v, ok := new.(map[string]interface{}); ok {
|
||||||
|
newMsg := message.New().Map(v)
|
||||||
|
newMsg.Write(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("Neo custom write return not map")
|
||||||
|
}
|
||||||
|
|
||||||
// prepare the messages
|
// prepare the messages
|
||||||
func (neo *DSL) prepare(ctx command.Context, messages []map[string]interface{}) []map[string]interface{} {
|
func (neo *DSL) prepare(ctx command.Context, messages []map[string]interface{}) []map[string]interface{} {
|
||||||
if neo.Prepare == "" {
|
if neo.Prepare == "" {
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ type DSL struct {
|
||||||
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"`
|
Prepare string `json:"prepare,omitempty"`
|
||||||
|
Write string `json:"write,omitempty"`
|
||||||
Prompts []aigc.Prompt `json:"prompts,omitempty"`
|
Prompts []aigc.Prompt `json:"prompts,omitempty"`
|
||||||
Allows []string `json:"allows,omitempty"`
|
Allows []string `json:"allows,omitempty"`
|
||||||
Command Command `json:"command,omitempty"`
|
Command Command `json:"command,omitempty"`
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue