commit 471f13a27d02f9c51b3738ab11b13b60b76d5e3d Author: STARAI\Star Date: Sat Sep 7 23:13:36 2024 +0800 1 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f166f95 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 apigo + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/aigc.go b/aigc.go new file mode 100644 index 0000000..14e4b5a --- /dev/null +++ b/aigc.go @@ -0,0 +1,95 @@ +package openai + +import ( + "context" + "github.com/sashabaranov/go-openai" + "strings" +) + +func (ag *Agent) FastMakeImage(prompt, size, refImage string) ([]string, error) { + return ag.MakeImage(ModelDallE3Std, prompt, size, refImage) +} + +func (ag *Agent) BestMakeImage(prompt, size, refImage string) ([]string, error) { + return ag.MakeImage(ModelDallE3HD, prompt, size, refImage) +} + +func (ag *Agent) MakeImage(model, prompt, size, refImage string) ([]string, error) { + openaiConf := openai.DefaultConfig(ag.config.ApiKey) + if ag.config.Endpoint != "" { + openaiConf.BaseURL = ag.config.Endpoint + } + c := openai.NewClientWithConfig(openaiConf) + style := openai.CreateImageStyleVivid + if (!strings.Contains(prompt, "vivid") || !strings.Contains(prompt, "生动的")) && (strings.Contains(prompt, "natural") || strings.Contains(prompt, "自然的")) { + style = openai.CreateImageStyleNatural + } + quality := openai.CreateImageQualityStandard + if strings.HasSuffix(model, "-hd") { + quality = openai.CreateImageQualityHD + model = model[0 : len(model)-3] + } + r, err := c.CreateImage(context.Background(), openai.ImageRequest{ + Prompt: prompt, + Model: model, + Quality: quality, + Size: size, + Style: style, + ResponseFormat: openai.CreateImageResponseFormatURL, + }) + if err == nil { + results := make([]string, 0) + for _, item := range r.Data { + results = append(results, item.URL) + } + return results, nil + } else { + return nil, err + } +} + +func (ag *Agent) FastMakeVideo(prompt, size, refImage string) ([]string, []string, error) { + return ag.MakeVideo("", prompt, size, refImage) +} + +func (ag *Agent) BestMakeVideo(prompt, size, refImage string) ([]string, []string, error) { + return ag.MakeVideo("", prompt, size, refImage) +} + +func (ag *Agent) MakeVideo(model, prompt, size, refImage string) ([]string, []string, error) { + //c, err := openai.NewClient(openai.WithAPIKey(ag.config.ApiKey), openai.WithBaseURL(ag.config.Endpoint)) + //if err != nil { + // return nil, nil, err + //} + // + //cc := c.VideoGeneration(model).SetPrompt(prompt) + //if refImage != "" { + // cc.SetImageURL(refImage) + //} + // + //if resp, err := cc.Do(context.Background()); err == nil { + // for i := 0; i < 1200; i++ { + // r, err := c.AsyncResult(resp.ID).Do(context.Background()) + // if err != nil { + // return nil, nil, err + // } + // if r.TaskStatus == openai.VideoGenerationTaskStatusSuccess { + // covers := make([]string, 0) + // results := make([]string, 0) + // for _, item := range r.VideoResult { + // results = append(results, item.URL) + // covers = append(covers, item.CoverImageURL) + // } + // return results, covers, nil + // } + // if r.TaskStatus == openai.VideoGenerationTaskStatusFail { + // return nil, nil, errors.New("fail on task " + resp.ID) + // } + // time.Sleep(3 * time.Second) + // } + // return nil, nil, errors.New("timeout on task " + resp.ID) + //} else { + // return nil, nil, err + //} + return nil, nil, nil +} diff --git a/chat.go b/chat.go new file mode 100644 index 0000000..19a2558 --- /dev/null +++ b/chat.go @@ -0,0 +1,168 @@ +package openai + +import ( + "apigo.cc/ai/agent" + "context" + "github.com/sashabaranov/go-openai" + "github.com/ssgo/log" + "strings" +) + +func (ag *Agent) FastAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o_mini_2024_07_18, + }, callback) +} + +func (ag *Agent) LongAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4_32k_0613, + }, callback) +} + +func (ag *Agent) BatterAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4_turbo, + }, callback) +} + +func (ag *Agent) BestAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o_2024_08_06, + }, callback) +} + +func (ag *Agent) MultiAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o_mini_2024_07_18, + }, callback) +} + +func (ag *Agent) BestMultiAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o_2024_08_06, + }, callback) +} + +func (ag *Agent) CodeInterpreterAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o, + Tools: map[string]any{agent.ToolCodeInterpreter: nil}, + }, callback) +} + +func (ag *Agent) WebSearchAsk(messages []agent.ChatMessage, callback func(answer string)) (string, agent.TokenUsage, error) { + return ag.Ask(messages, &agent.ChatModelConfig{ + Model: ModelGPT_4o_mini_2024_07_18, + Tools: map[string]any{agent.ToolWebSearch: nil}, + }, callback) +} + +func (ag *Agent) Ask(messages []agent.ChatMessage, config *agent.ChatModelConfig, callback func(answer string)) (string, agent.TokenUsage, error) { + openaiConf := openai.DefaultConfig(ag.config.ApiKey) + if ag.config.Endpoint != "" { + openaiConf.BaseURL = ag.config.Endpoint + } + + if config == nil { + config = &agent.ChatModelConfig{} + } + config.SetDefault(&ag.config.DefaultChatModelConfig) + + agentMessages := make([]openai.ChatCompletionMessage, len(messages)) + for i, msg := range messages { + var contents []openai.ChatMessagePart + if msg.Contents != nil { + contents = make([]openai.ChatMessagePart, len(msg.Contents)) + for j, inPart := range msg.Contents { + part := openai.ChatMessagePart{} + part.Type = TypeMap[inPart.Type] + switch inPart.Type { + case agent.TypeText: + part.Text = inPart.Content + case agent.TypeImage: + part.ImageURL = &openai.ChatMessageImageURL{ + URL: inPart.Content, + Detail: openai.ImageURLDetailAuto, + } + } + contents[j] = part + } + } + agentMessages[i] = openai.ChatCompletionMessage{ + Role: RoleMap[msg.Role], + MultiContent: contents, + } + } + + opt := openai.ChatCompletionRequest{ + Model: config.GetModel(), + Messages: agentMessages, + MaxTokens: config.GetMaxTokens(), + Temperature: float32(config.GetTemperature()), + TopP: float32(config.GetTopP()), + StreamOptions: &openai.StreamOptions{ + IncludeUsage: true, + }, + } + + for name := range config.GetTools() { + switch name { + case agent.ToolCodeInterpreter: + opt.Tools = append(opt.Tools, openai.Tool{Type: "code_interpreter"}) + case agent.ToolWebSearch: + } + } + + c := openai.NewClientWithConfig(openaiConf) + if callback != nil { + opt.Stream = true + r, err := c.CreateChatCompletionStream(context.Background(), opt) + if err == nil { + results := make([]string, 0) + usage := agent.TokenUsage{} + for { + if r2, err := r.Recv(); err == nil { + if r2.Choices != nil { + for _, ch := range r2.Choices { + text := ch.Delta.Content + callback(text) + results = append(results, text) + } + } + if r2.Usage != nil { + usage.AskTokens += int64(r2.Usage.PromptTokens) + usage.AnswerTokens += int64(r2.Usage.CompletionTokens) + usage.TotalTokens += int64(r2.Usage.TotalTokens) + } + } else { + break + } + } + _ = r.Close() + return strings.Join(results, ""), usage, nil + } else { + log.DefaultLogger.Error(err.Error()) + return "", agent.TokenUsage{}, err + } + } else { + r, err := c.CreateChatCompletion(context.Background(), opt) + + if err == nil { + results := make([]string, 0) + if r.Choices != nil { + for _, ch := range r.Choices { + results = append(results, ch.Message.Content) + } + } + return strings.Join(results, ""), agent.TokenUsage{ + AskTokens: int64(r.Usage.PromptTokens), + AnswerTokens: int64(r.Usage.CompletionTokens), + TotalTokens: int64(r.Usage.TotalTokens), + }, nil + } else { + //fmt.Println(u.BMagenta(err.Error()), u.BMagenta(u.JsonP(r))) + return "", agent.TokenUsage{}, err + } + } +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..33e6cd4 --- /dev/null +++ b/config.go @@ -0,0 +1,81 @@ +package openai + +import ( + "apigo.cc/ai/agent" + "github.com/sashabaranov/go-openai" +) + +type Agent struct { + config agent.APIConfig +} + +var TypeMap = map[string]openai.ChatMessagePartType{ + agent.TypeText: openai.ChatMessagePartTypeText, + agent.TypeImage: openai.ChatMessagePartTypeImageURL, + //agent.TypeVideo: "video_url", +} +var RoleMap = map[string]string{ + agent.RoleSystem: openai.ChatMessageRoleSystem, + agent.RoleUser: openai.ChatMessageRoleUser, + agent.RoleAssistant: openai.ChatMessageRoleAssistant, + agent.RoleTool: openai.ChatMessageRoleTool, +} + +const ( + ModelGPT_4_32k_0613 = "gpt-4-32k-0613" + ModelGPT_4_32k_0314 = "gpt-4-32k-0314" + ModelGPT_4_32k = "gpt-4-32k" + ModelGPT_4_0613 = "gpt-4-0613" + ModelGPT_4_0314 = "gpt-4-0314" + ModelGPT_4o = "gpt-4o" + ModelGPT_4o_2024_05_13 = "gpt-4o-2024-05-13" + ModelGPT_4o_2024_08_06 = "gpt-4o-2024-08-06" + ModelGPT_4o_mini = "gpt-4o-mini" + ModelGPT_4o_mini_2024_07_18 = "gpt-4o-mini-2024-07-18" + ModelGPT_4_turbo = "gpt-4-turbo" + ModelGPT_4_turbo_2024_04_09 = "gpt-4-turbo-2024-04-09" + ModelGPT_4_0125_preview = "gpt-4-0125-preview" + ModelGPT_4_1106_preview = "gpt-4-1106-preview" + ModelGPT_4_turbo_preview = "gpt-4-turbo-preview" + ModelGPT_4_vision_preview = "gpt-4-vision-preview" + ModelGPT_4 = "gpt-4" + ModelGPT_3_5_turbo_0125 = "gpt-3.5-turbo-0125" + ModelGPT_3_5_turbo_1106 = "gpt-3.5-turbo-1106" + ModelGPT_3_5_turbo_0613 = "gpt-3.5-turbo-0613" + ModelGPT_3_5_turbo_0301 = "gpt-3.5-turbo-0301" + ModelGPT_3_5_turbo_16k = "gpt-3.5-turbo-16k" + ModelGPT_3_5_turbo_16k_0613 = "gpt-3.5-turbo-16k-0613" + ModelGPT_3_5_turbo = "gpt-3.5-turbo" + ModelGPT_3_5_turbo_instruct = "gpt-3.5-turbo-instruct" + ModelDavinci_002 = "davinci-002" + ModelCurie = "curie" + ModelCurie_002 = "curie-002" + ModelAda_002 = "ada-002" + ModelBabbage_002 = "babbage-002" + ModelCode_davinci_002 = "code-davinci-002" + ModelCode_cushman_001 = "code-cushman-001" + ModelCode_davinci_001 = "code-davinci-001" + ModelDallE2Std = "dall-e-2" + ModelDallE2HD = "dall-e-2-hd" + ModelDallE3Std = "dall-e-3" + ModelDallE3HD = "dall-e-3-hd" +) + +func (ag *Agent) Support() agent.Support { + return agent.Support{ + Ask: true, + AskWithImage: true, + AskWithVideo: false, + AskWithCodeInterpreter: true, + AskWithWebSearch: false, + MakeImage: true, + MakeVideo: false, + Models: []string{ModelGPT_4_32k_0613, ModelGPT_4_32k_0314, ModelGPT_4_32k, ModelGPT_4_0613, ModelGPT_4_0314, ModelGPT_4o, ModelGPT_4o_2024_05_13, ModelGPT_4o_2024_08_06, ModelGPT_4o_mini, ModelGPT_4o_mini_2024_07_18, ModelGPT_4_turbo, ModelGPT_4_turbo_2024_04_09, ModelGPT_4_0125_preview, ModelGPT_4_1106_preview, ModelGPT_4_turbo_preview, ModelGPT_4_vision_preview, ModelGPT_4, ModelGPT_3_5_turbo_0125, ModelGPT_3_5_turbo_1106, ModelGPT_3_5_turbo_0613, ModelGPT_3_5_turbo_0301, ModelGPT_3_5_turbo_16k, ModelGPT_3_5_turbo_16k_0613, ModelGPT_3_5_turbo, ModelGPT_3_5_turbo_instruct, ModelDavinci_002, ModelCurie, ModelCurie_002, ModelAda_002, ModelBabbage_002, ModelCode_davinci_002, ModelCode_cushman_001, ModelCode_davinci_001, ModelDallE2Std, ModelDallE2HD, ModelDallE3Std, ModelDallE3HD}, + } +} + +func init() { + agent.RegisterAgentMaker("openai", func(config agent.APIConfig) agent.Agent { + return &Agent{config: config} + }) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6176c14 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module apigo.cc/ai/openai + +go 1.22 + +require ( + apigo.cc/ai/agent v0.0.1 + github.com/sashabaranov/go-openai v1.29.1 + github.com/ssgo/log v1.7.7 +) + +require ( + github.com/ssgo/config v1.7.7 // indirect + github.com/ssgo/standard v1.7.7 // indirect + github.com/ssgo/u v1.7.7 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +)