support Scan & Edit

This commit is contained in:
Star 2024-11-03 11:30:14 +08:00
parent 205d635b7a
commit df79a87a90
4 changed files with 240 additions and 23 deletions

101
agent.go
View File

@ -15,11 +15,19 @@ type Agent struct {
EmbeddingConfigs map[string]*EmbeddingConfig
ImageConfigs map[string]*ImageConfig
VideoConfigs map[string]*VideoConfig
Chat func(aiConf *AIConfig, messages []ChatMessage, callback func(string), chatConf ChatConfig) (ChatResult, error)
Embedding func(aiConf *AIConfig, text string, embeddingConf EmbeddingConfig) (EmbeddingResult, error)
MakeImage func(aiConf *AIConfig, imageConf ImageConfig) (ImageResult, error)
MakeVideo func(aiConf *AIConfig, videoConf VideoConfig) (string, error)
EditConfigs map[string]*map[string]any
ScanConfigs map[string]*map[string]any
AsrConfigs map[string]*AsrConfig
TtsConfigs map[string]*TtsConfig
Chat func(aiConf *AIConfig, messages []ChatMessage, callback func(string), conf ChatConfig) (ChatResult, error)
Embedding func(aiConf *AIConfig, text string, conf EmbeddingConfig) (EmbeddingResult, error)
MakeImage func(aiConf *AIConfig, conf ImageConfig) (ImageResult, error)
MakeVideo func(aiConf *AIConfig, conf VideoConfig) (string, error)
GetVideoResult func(aiConf *AIConfig, taskId string, waitSeconds int) (VideoResult, error)
Edit func(aiConf *AIConfig, from string, conf map[string]any) (StringResult, error)
Scan func(aiConf *AIConfig, image []byte, conf map[string]any) (ScanResult, error)
Asr func(aiConf *AIConfig, url string, conf AsrConfig) (ScanResult, error)
Tts func(aiConf *AIConfig, text string, conf TtsConfig) (StringResult, error)
}
type agentObj struct {
@ -28,6 +36,10 @@ type agentObj struct {
embeddingConfig *EmbeddingConfig
imageConfig *ImageConfig
videoConfig *VideoConfig
editConfig *map[string]any
scanConfig *map[string]any
asrConfig *AsrConfig
ttsConfig *TtsConfig
agent *Agent
}
@ -97,6 +109,68 @@ func (ag *agentObj) GetVideoResult(argsIn goja.FunctionCall, vm *goja.Runtime) g
return vm.ToValue(gojs.MakeMap(r))
}
func (ag *agentObj) Edit(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
from := args.Str(0)
conf := ag.getMapArgs(args.Map(1), *ag.editConfig)
r, err := ag.agent.Edit(ag.config, from, conf)
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(gojs.MakeMap(r))
}
func (ag *agentObj) Scan(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
image := args.Bytes(0)
conf := ag.getMapArgs(args.Map(1), *ag.scanConfig)
r, err := ag.agent.Scan(ag.config, image, conf)
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(gojs.MakeMap(r))
}
func (ag *agentObj) Asr(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
url := args.Str(0)
conf := AsrConfig{}
u.Convert(args.Map(1), &conf)
// if conf.Model == "" {
// conf.Model = ag.asrConfig.Model
// }
// Uid string
// Format string
// Codec string
// Rate int
// Bits int
// Channel int
// Language string
// Itn bool
// Punc bool
// Ddc bool
// Extra map[string]any
conf.Extra = ag.getMapArgs(conf.Extra, ag.asrConfig.Extra)
r, err := ag.agent.Asr(ag.config, url, conf)
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(gojs.MakeMap(r))
}
func (ag *agentObj) Tts(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
text := args.Str(0)
conf := TtsConfig{}
u.Convert(args.Map(1), &conf)
conf.Extra = ag.getMapArgs(conf.Extra, ag.asrConfig.Extra)
r, err := ag.agent.Tts(ag.config, text, conf)
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(gojs.MakeMap(r))
}
func (ag *agentObj) getAskArgs(thisArg goja.Value, vm *goja.Runtime, args []goja.Value) ([]ChatMessage, ChatConfig, func(string)) {
conf := ChatConfig{}
var callback func(answer string)
@ -200,9 +274,9 @@ func (ag *agentObj) getVideoArgs(args []goja.Value) VideoConfig {
if conf.Height == 0 {
conf.Height = ag.videoConfig.Height
}
if ag.imageConfig.Extra != nil {
if ag.videoConfig.Extra != nil {
extra := make(map[string]any)
for k, v := range ag.imageConfig.Extra {
for k, v := range ag.videoConfig.Extra {
extra[k] = v
}
if conf.Extra != nil {
@ -215,6 +289,21 @@ func (ag *agentObj) getVideoArgs(args []goja.Value) VideoConfig {
return conf
}
func (ag *agentObj) getMapArgs(setConf map[string]any, defaultConf map[string]any) map[string]any {
conf := map[string]any{}
if defaultConf != nil {
for k, v := range defaultConf {
conf[k] = v
}
}
if setConf != nil {
for k, v := range setConf {
conf[k] = v
}
}
return conf
}
func makeChatMessages(args []goja.Value) []ChatMessage {
out := make([]ChatMessage, 0)
if len(args) > 0 {

View File

@ -45,6 +45,10 @@ type AILoadConfig struct {
Embedding map[string]*EmbeddingConfig
Image map[string]*ImageConfig
Video map[string]*VideoConfig
Edit map[string]*map[string]any
Scan map[string]*map[string]any
Asr map[string]*AsrConfig
Tts map[string]*TtsConfig
Extra map[string]any
}
@ -112,6 +116,11 @@ type ImageResult struct {
UsedTime int64
}
type StringResult struct {
Result string
UsedTime int64
}
type VideoConfig struct {
Prompt string
GenerateCount int
@ -130,3 +139,27 @@ type VideoResult struct {
IsProcessing bool
UsedTime int64
}
type ScanResult struct {
Result string
Detail map[string]any
UsedTime int64
}
type AsrConfig struct {
Uid string
Format string
Codec string
Rate int
Bits int
Channel int
Language string
Itn bool
Punc bool
Ddc bool
Extra map[string]any
}
type TtsConfig struct {
Extra map[string]any
}

View File

@ -1,21 +1,33 @@
export default {
//----{{- range $aiName, $aiConf := .}}
//----{{$aiName}}: {
//---- {{- range $chatName, $chatConf := $aiConf.Chat}}
//---- {{$chatName}}(messages: any, callback?: (answer: string) => void, config?: ChatConfig): ChatResult { return null as any },
//---- {{- range $name, $conf := $aiConf.Chat}}
//---- {{$name}}(messages: any, callback?: (answer: string) => void, config?: ChatConfig): ChatResult { return null as any },
//---- {{- end }}
//---- {{- range $embeddingName, $embeddingConf := $aiConf.Embedding}}
//---- {{$embeddingName}}(messages: any, config?: EmbeddingConfig): EmbeddingResult { return null as any },
//---- {{- range $name, $conf := $aiConf.Embedding}}
//---- {{$name}}(messages: any, config?: EmbeddingConfig): EmbeddingResult { return null as any },
//---- {{- end }}
//---- {{- range $imageName, $imageConf := $aiConf.Image}}
//---- {{$imageName}}(config?: ImageConfig): ImageResult { return null as any },
//---- {{- range $name, $conf := $aiConf.Image}}
//---- {{$name}}(config?: ImageConfig): ImageResult { return null as any },
//---- {{- end }}
//---- {{- range $videlName, $videlConf := $aiConf.Video}}
//---- {{$videlName}}(config?: VideoConfig): string { return '' },
//---- {{- range $name, $conf := $aiConf.Video}}
//---- {{$name}}(config?: VideoConfig): string { return '' },
//---- {{- end }}
//---- {{- if $aiConf.Video}}
//---- getVideoResult(taskId: string, waitSeconds?:number): VideoResult { return null as any },
//---- {{- end }}
//---- {{- range $name, $conf := $aiConf.Edit}}
//---- {{$name}}(image:string, config?: Object): StringResult { return null as any },
//---- {{- end }}
//---- {{- range $name, $conf := $aiConf.Scan}}
//---- {{$name}}(image:any, config?: Object): ScanResult { return null as any },
//---- {{- end }}
//---- {{- range $name, $conf := $aiConf.Asr}}
//---- {{$name}}(audio:any, config?: AsrConfig): AsrResult { return null as any },
//---- {{- end }}
//---- {{- range $name, $conf := $aiConf.Tts}}
//---- {{$name}}(text:string, config?: TtsConfig): StringResult { return null as any },
//---- {{- end }}
//----},
//----{{- end }}
similarity
@ -74,6 +86,11 @@ interface ImageResult {
usedTime: number
}
interface StringResult {
result: string
usedTime: number
}
interface VideoConfig {
prompt: string
generateCount: number
@ -90,3 +107,9 @@ interface VideoResult {
previews: string[]
usedTime: number
}
interface ScanResult {
result: string
detail: Object
usedTime: number
}

90
gojs.go
View File

@ -76,6 +76,38 @@ func makeAIList() {
aiConf.Video[confName] = conf
}
}
if aiConf.Edit == nil {
aiConf.Edit = map[string]*map[string]any{}
}
for confName, conf := range agent.EditConfigs {
if aiConf.Edit[confName] == nil {
aiConf.Edit[confName] = conf
}
}
if aiConf.Scan == nil {
aiConf.Scan = map[string]*map[string]any{}
}
for confName, conf := range agent.ScanConfigs {
if aiConf.Scan[confName] == nil {
aiConf.Scan[confName] = conf
}
}
if aiConf.Asr == nil {
aiConf.Asr = map[string]*AsrConfig{}
}
for confName, conf := range agent.AsrConfigs {
if aiConf.Asr[confName] == nil {
aiConf.Asr[confName] = conf
}
}
if aiConf.Tts == nil {
aiConf.Tts = map[string]*TtsConfig{}
}
for confName, conf := range agent.TtsConfigs {
if aiConf.Tts[confName] == nil {
aiConf.Tts[confName] = conf
}
}
}
}
}
@ -119,46 +151,86 @@ func makeJsObj(vm *goja.Runtime) gojs.Map {
// 生成Chat方法
for confName, conf := range aiLoadConf.Chat {
chatObj := &agentObj{
obj := &agentObj{
config: &aiConf,
chatConfig: conf,
agent: agent,
}
aiObj[confName] = chatObj.Chat
aiObj[confName] = obj.Chat
}
// 生成Embedding方法
for confName, conf := range aiLoadConf.Embedding {
chatObj := &agentObj{
obj := &agentObj{
config: &aiConf,
embeddingConfig: conf,
}
aiObj[confName] = chatObj.Embedding
aiObj[confName] = obj.Embedding
}
// 生成MakeImage方法
for confName, conf := range aiLoadConf.Image {
chatObj := &agentObj{
obj := &agentObj{
config: &aiConf,
imageConfig: conf,
agent: agent,
}
aiObj[confName] = chatObj.MakeImage
aiObj[confName] = obj.MakeImage
}
// 生成MakeVideo方法
for confName, conf := range aiLoadConf.Video {
chatObj := &agentObj{
obj := &agentObj{
config: &aiConf,
videoConfig: conf,
agent: agent,
}
aiObj[confName] = chatObj.MakeVideo
aiObj[confName] = obj.MakeVideo
if aiObj["getVideoResult"] == nil {
aiObj["getVideoResult"] = chatObj.GetVideoResult
aiObj["getVideoResult"] = obj.GetVideoResult
}
}
// 生成Edit方法
for confName, conf := range aiLoadConf.Edit {
obj := &agentObj{
config: &aiConf,
editConfig: conf,
agent: agent,
}
aiObj[confName] = obj.Edit
}
// 生成Scan方法
for confName, conf := range aiLoadConf.Scan {
obj := &agentObj{
config: &aiConf,
scanConfig: conf,
agent: agent,
}
aiObj[confName] = obj.Scan
}
// 生成Asr方法
for confName, conf := range aiLoadConf.Asr {
obj := &agentObj{
config: &aiConf,
asrConfig: conf,
agent: agent,
}
aiObj[confName] = obj.Asr
}
// 生成Tts方法
for confName, conf := range aiLoadConf.Tts {
obj := &agentObj{
config: &aiConf,
ttsConfig: conf,
agent: agent,
}
aiObj[confName] = obj.Tts
}
jsObj[aiName] = aiObj
}