From 953d592669ddfdb615675a5324062a7b214f210b Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 30 Jul 2024 16:59:13 +0800 Subject: [PATCH 1/2] feat: Integrate AIGC & OpenAI with Moapi service --- aigc/aigc.go | 2 +- neo/neo.go | 10 +++++++--- openai/openai.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/aigc/aigc.go b/aigc/aigc.go index f20d0064..317a3b89 100644 --- a/aigc/aigc.go +++ b/aigc/aigc.go @@ -99,7 +99,7 @@ func (ai *DSL) newAI() (AI, error) { if ai.Connector == "" || strings.HasPrefix(ai.Connector, "moapi") { model := "gpt-3.5-turbo" - if ai.Connector != "" { + if strings.HasPrefix(ai.Connector, "moapi:") { model = strings.TrimPrefix(ai.Connector, "moapi:") } diff --git a/neo/neo.go b/neo/neo.go index 0b610c01..b3fc0826 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -343,12 +343,16 @@ func (neo *DSL) write(msg *message.JSON, w io.Writer, ctx command.Context, messa args := []interface{}{ctx, messages, msg, string(content)} p, err := process.Of(neo.Write, args...) if err != nil { - return err + log.Error("Neo custom write process error: %s", err.Error()) + msg.Write(w) + return nil } res, err := p.WithSID(ctx.Sid).Exec() if err != nil { - return err + log.Error("Neo custom write error: %s", err.Error()) + msg.Write(w) + return nil } if res == nil { @@ -549,7 +553,7 @@ func (neo *DSL) newAI() error { if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") { model := "gpt-3.5-turbo" - if neo.Connector != "" { + if strings.HasPrefix(neo.Connector, "moapi:") { model = strings.TrimPrefix(neo.Connector, "moapi:") } diff --git a/openai/openai.go b/openai/openai.go index 9ac7f94c..5c9e609a 100644 --- a/openai/openai.go +++ b/openai/openai.go @@ -35,6 +35,16 @@ type OpenAI struct { // New create a new OpenAI instance by connector id func New(id string) (*OpenAI, error) { + + // Moapi integration + if id == "" || strings.HasPrefix(id, "moapi") { + model := "gpt-3.5-turbo" + if strings.HasPrefix(id, "moapi:") { + model = strings.TrimPrefix(id, "moapi:") + } + return NewMoapi(model) + } + c, err := connector.Select(id) if err != nil { return nil, err From f451eb12a8d142e0c25921fb373e18d796a952c9 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 30 Jul 2024 17:44:17 +0800 Subject: [PATCH 2/2] feat: add utils.fmt.Printf and utils.fmt.ColorPrintf --- utils/fmt/fmt.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/process.go | 3 ++ 2 files changed, 80 insertions(+) create mode 100644 utils/fmt/fmt.go diff --git a/utils/fmt/fmt.go b/utils/fmt/fmt.go new file mode 100644 index 00000000..e79633af --- /dev/null +++ b/utils/fmt/fmt.go @@ -0,0 +1,77 @@ +package fmt + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/yaoapp/gou/process" +) + +// ProcessPrintf utils.fmt.Printf +func ProcessPrintf(process *process.Process) interface{} { + process.ValidateArgNums(1) + format := process.ArgsString(0) + if process.NumOfArgs() == 1 { + fmt.Print(format) + return nil + } + args := process.Args[1:] + fmt.Printf(format, args...) + return nil +} + +// ProcessColorPrintf utils.fmt.GreenPrintf +func ProcessColorPrintf(process *process.Process) interface{} { + process.ValidateArgNums(2) + colorName := strings.ToLower(process.ArgsString(0)) + format := process.ArgsString(1) + if process.NumOfArgs() == 1 { + fmt.Print(format) + return nil + } + + args := []interface{}{} + if process.NumOfArgs() > 2 { + args = process.Args[2:] + } + + switch colorName { + case "red": + fmt.Print(color.RedString(fmt.Sprintf(format, args...))) + case "green": + fmt.Print(color.GreenString(fmt.Sprintf(format, args...))) + case "yellow": + fmt.Print(color.YellowString(fmt.Sprintf(format, args...))) + case "blue": + fmt.Print(color.BlueString(fmt.Sprintf(format, args...))) + case "magenta": + fmt.Print(color.MagentaString(fmt.Sprintf(format, args...))) + case "cyan": + fmt.Print(color.CyanString(fmt.Sprintf(format, args...))) + case "white": + fmt.Print(color.WhiteString(fmt.Sprintf(format, args...))) + case "black": + fmt.Print(color.BlackString(fmt.Sprintf(format, args...))) + case "hired": + fmt.Print(color.HiRedString(fmt.Sprintf(format, args...))) + case "higreen": + fmt.Print(color.HiGreenString(fmt.Sprintf(format, args...))) + case "hiyellow": + fmt.Print(color.HiYellowString(fmt.Sprintf(format, args...))) + case "hiblue": + fmt.Print(color.HiBlueString(fmt.Sprintf(format, args...))) + case "himagenta": + fmt.Print(color.HiMagentaString(fmt.Sprintf(format, args...))) + case "hicyan": + fmt.Print(color.HiCyanString(fmt.Sprintf(format, args...))) + case "hiwhite": + fmt.Print(color.HiWhiteString(fmt.Sprintf(format, args...))) + case "hiblack": + fmt.Print(color.HiBlackString(fmt.Sprintf(format, args...))) + default: + fmt.Print(fmt.Sprintf(format, args...)) + } + + return nil +} diff --git a/utils/process.go b/utils/process.go index 52181748..16584c3d 100644 --- a/utils/process.go +++ b/utils/process.go @@ -3,6 +3,7 @@ package utils import ( "github.com/yaoapp/gou/process" "github.com/yaoapp/yao/utils/datetime" + "github.com/yaoapp/yao/utils/fmt" "github.com/yaoapp/yao/utils/json" "github.com/yaoapp/yao/utils/str" "github.com/yaoapp/yao/utils/tree" @@ -20,6 +21,8 @@ func Init() { // FMT process.Alias("xiang.helper.Print", "utils.fmt.Print") + process.Register("utils.fmt.Printf", fmt.ProcessPrintf) + process.Register("utils.fmt.ColorPrintf", fmt.ProcessColorPrintf) // ENV process.Alias("xiang.helper.EnvSet", "utils.env.Set")