commit d8283562f90c02c7ffe96e48f091369474d6ed00 Author: STARAI\Star Date: Sat Sep 7 23:05:11 2024 +0800 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a7d55d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.* +/go.sum diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d894367 --- /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/agent.go b/agent.go new file mode 100644 index 0000000..bfaafe0 --- /dev/null +++ b/agent.go @@ -0,0 +1,91 @@ +package agent + +import "sync" + +const ( + TypeText = "text" + TypeImage = "image" + TypeVideo = "video" + RoleSystem = "system" + RoleUser = "user" + RoleAssistant = "assistant" + RoleTool = "tool" + ToolCodeInterpreter = "codeInterpreter" + ToolWebSearch = "webSearch" +) + +type Support struct { + Ask bool + AskWithImage bool + AskWithVideo bool + AskWithCodeInterpreter bool + AskWithWebSearch bool + MakeImage bool + MakeVideo bool + Models []string +} + +type Agent interface { + Support() Support + Ask(messages []ChatMessage, config *ChatModelConfig, callback func(answer string)) (string, TokenUsage, error) + FastAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + LongAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + BatterAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + BestAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + MultiAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + BestMultiAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + CodeInterpreterAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + WebSearchAsk(messages []ChatMessage, callback func(answer string)) (string, TokenUsage, error) + MakeImage(model, prompt, size, refImage string) ([]string, error) + FastMakeImage(prompt, size, refImage string) ([]string, error) + BestMakeImage(prompt, size, refImage string) ([]string, error) + MakeVideo(model, prompt, size, refImage string) ([]string, []string, error) + FastMakeVideo(prompt, size, refImage string) ([]string, []string, error) + BestMakeVideo(prompt, size, refImage string) ([]string, []string, error) +} + +type APIConfig struct { + Endpoint string + ApiKey string + DefaultChatModelConfig ChatModelConfig +} + +type TokenUsage struct { + AskTokens int64 + AnswerTokens int64 + TotalTokens int64 +} + +var agentMakers = map[string]func(APIConfig) Agent{} +var agentMakersLock = sync.RWMutex{} + +var agents = map[string]Agent{} +var agentsLock = sync.RWMutex{} + +func RegisterAgentMaker(agentId string, maker func(APIConfig) Agent) { + agentMakersLock.Lock() + agentMakers[agentId] = maker + agentMakersLock.Unlock() +} + +func CreateAgent(name, agentId string, config APIConfig) Agent { + agentMakersLock.RLock() + maker := agentMakers[agentId] + agentMakersLock.RUnlock() + + if maker != nil { + agent := maker(config) + agentsLock.Lock() + agents[name] = agent + agentsLock.Unlock() + return agent + } + return nil +} + +func GetAgent(name string) Agent { + agentsLock.RLock() + agent := agents[name] + agentsLock.RUnlock() + return agent +} diff --git a/chat.go b/chat.go new file mode 100644 index 0000000..f6c605e --- /dev/null +++ b/chat.go @@ -0,0 +1,60 @@ +package agent + +type ChatMessage struct { + Role string + Contents []ChatMessageContent +} + +type ChatMessageContent struct { + Type string // text, image, audio, video + Content string +} + +type ChatModelConfig struct { + defaultConfig *ChatModelConfig + Model string + Ratio float64 + MaxTokens int + Temperature float64 + TopP float64 + Tools map[string]any +} + +func (cmc *ChatModelConfig) SetDefault(config *ChatModelConfig) { + cmc.defaultConfig = config +} + +func (cmc *ChatModelConfig) GetModel() string { + if cmc.Model == "" && cmc.defaultConfig != nil { + return cmc.defaultConfig.Model + } + return cmc.Model +} + +func (cmc *ChatModelConfig) GetMaxTokens() int { + if cmc.MaxTokens == 0 && cmc.defaultConfig != nil { + return cmc.defaultConfig.MaxTokens + } + return cmc.MaxTokens +} + +func (cmc *ChatModelConfig) GetTemperature() float64 { + if cmc.Temperature == 0 && cmc.defaultConfig != nil { + return cmc.defaultConfig.Temperature + } + return cmc.Temperature +} + +func (cmc *ChatModelConfig) GetTopP() float64 { + if cmc.TopP == 0 && cmc.defaultConfig != nil { + return cmc.defaultConfig.TopP + } + return cmc.TopP +} + +func (cmc *ChatModelConfig) GetTools() map[string]any { + if cmc.Tools == nil && cmc.defaultConfig != nil { + return cmc.defaultConfig.Tools + } + return cmc.Tools +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..24bd389 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module apigo.cc/ai/agent + +go 1.22