This commit is contained in:
STARAI\Star 2024-09-07 23:05:11 +08:00
commit d8283562f9
5 changed files with 165 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.*
/go.sum

9
LICENSE Normal file
View File

@ -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.

91
agent.go Normal file
View File

@ -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
}

60
chat.go Normal file
View File

@ -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
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module apigo.cc/ai/agent
go 1.22