ai_convert

package
v0.22.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpenAIChatCompletePath = "/chat/completions"
)

Variables

View Source
var (
	AIModelInputTokenLabel  = "ai_model_input_token"
	AIModelOutputTokenLabel = "ai_model_output_token"
	AIModelTotalTokenLabel  = "ai_model_total_token"
	AIModelModeLabel        = "ai_model_mode"
	AIModelLabel            = "ai_model"
	AIKeyLabel              = "ai_key"
	AIProviderLabel         = "ai_provider"
	AIProviderStatusesLabel = "ai_provider_statuses"
	AIModelStatusLabel      = "ai_model_status"
)
View Source
var (
	StatusNormal         = "normal"
	StatusInvalidRequest = "invalid request"
	StatusQuotaExhausted = "quota exhausted"
	StatusExpired        = "expired"
	StatusExceeded       = "exceeded"
	StatusInvalid        = "invalid"
	StatusTimeout        = "timeout"

	ParamAIModel = "param_ai_model"
)

Functions

func CheckKeySourceSkill

func CheckKeySourceSkill(skill string) bool

func CheckProviderSkill

func CheckProviderSkill(skill string) bool

func DelKeyResource

func DelKeyResource(provider string, resourceId string)

func DelProvider

func DelProvider(id string)

func GetAIKey

func GetAIKey(ctx eocontext.EoContext) string

func GetAIModel

func GetAIModel(ctx eocontext.EoContext) string

func GetAIModelInputToken

func GetAIModelInputToken(ctx eocontext.EoContext) int

func GetAIModelMode

func GetAIModelMode(ctx eocontext.EoContext) string

func GetAIModelOutputToken

func GetAIModelOutputToken(ctx eocontext.EoContext) int

func GetAIModelTotalToken

func GetAIModelTotalToken(ctx eocontext.EoContext) int

func GetAIProvider

func GetAIProvider(ctx eocontext.EoContext) string

func GetAIStatus

func GetAIStatus(ctx eocontext.EoContext) string

func GetAIStatusExceeded

func GetAIStatusExceeded(ctx eocontext.EoContext) string

func GetAIStatusExpired

func GetAIStatusExpired(ctx eocontext.EoContext) string

func GetAIStatusInvalid

func GetAIStatusInvalid(ctx eocontext.EoContext) string

func GetAIStatusInvalidRequest

func GetAIStatusInvalidRequest(ctx eocontext.EoContext) string

func GetAIStatusNormal

func GetAIStatusNormal(ctx eocontext.EoContext) string

func GetAIStatusQuotaExhausted

func GetAIStatusQuotaExhausted(ctx eocontext.EoContext) string

func GetAIStatusTimeout

func GetAIStatusTimeout(ctx eocontext.EoContext) string

func MapToStruct

func MapToStruct[T any](tmp map[string]interface{}) *T

MapToStruct 将 map 转换为结构体实例

func NewBalanceHandler

func NewBalanceHandler(id string, base string, timeout time.Duration) (eocontext.BalanceHandler, error)

func NewBaseNode

func NewBaseNode(id string, ip string, port int) *_BaseNode

func RegisterConverterCreateFunc

func RegisterConverterCreateFunc(id string, fn IConverterCreateFunc)

func ResponseConvert

func ResponseConvert(ctx eoscContext.EoContext, checkErr CheckError, errorCallback func(ctx http_service.IHttpContext, body []byte)) error

func SetAIKey

func SetAIKey(ctx eocontext.EoContext, key string)

func SetAIModel

func SetAIModel(ctx eocontext.EoContext, model string)

func SetAIModelInputToken

func SetAIModelInputToken(ctx eocontext.EoContext, token int)

func SetAIModelMode

func SetAIModelMode(ctx eocontext.EoContext, mode string)

func SetAIModelOutputToken

func SetAIModelOutputToken(ctx eocontext.EoContext, token int)

func SetAIModelTotalToken

func SetAIModelTotalToken(ctx eocontext.EoContext, token int)

func SetAIProvider

func SetAIProvider(ctx eocontext.EoContext, provider string)

func SetAIProviderStatuses

func SetAIProviderStatuses(ctx eocontext.EoContext, status AIProviderStatus)

func SetAIStatus

func SetAIStatus(ctx eocontext.EoContext, status string)

func SetAIStatusExceeded

func SetAIStatusExceeded(ctx eocontext.EoContext)

func SetAIStatusExpired

func SetAIStatusExpired(ctx eocontext.EoContext)

func SetAIStatusInvalid

func SetAIStatusInvalid(ctx eocontext.EoContext)

func SetAIStatusInvalidRequest

func SetAIStatusInvalidRequest(ctx eocontext.EoContext)

func SetAIStatusNormal

func SetAIStatusNormal(ctx eocontext.EoContext)

func SetAIStatusQuotaExhausted

func SetAIStatusQuotaExhausted(ctx eocontext.EoContext)

func SetAIStatusTimeout

func SetAIStatusTimeout(ctx eocontext.EoContext)

func SetKeyResource

func SetKeyResource(provider string, resource IKeyResource)

func SetProvider

func SetProvider(id string, p IProvider)

func TransformData

func TransformData(inputJSON string, mappingRule MappingRule) (map[string]interface{}, error)

TransformData 执行字段映射和类型转换

Types

type AIProviderStatus

type AIProviderStatus struct {
	Provider string `json:"provider"`
	Model    string `json:"model"`
	Key      string `json:"key"`
	Status   string `json:"status"`
}

func GetAIProviderStatuses

func GetAIProviderStatuses(ctx eocontext.EoContext) []AIProviderStatus

type BalanceManager

type BalanceManager struct {
	// contains filtered or unexported fields
}

func NewBalanceManager

func NewBalanceManager() *BalanceManager

func (*BalanceManager) Balances

func (m *BalanceManager) Balances() []IProvider

func (*BalanceManager) Del

func (m *BalanceManager) Del(id string) string

func (*BalanceManager) Get

func (m *BalanceManager) Get(provider string) (IProvider, bool)

func (*BalanceManager) SetProvider

func (m *BalanceManager) SetProvider(id string, p IProvider)

type CheckError

type CheckError func(ctx http_service.IHttpContext, body []byte) bool

type Error

type Error struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    string `json:"code"`
}

type FGenerateConfig

type FGenerateConfig func(cfg string) (map[string]interface{}, error)

type IChildConverter

type IChildConverter interface {
	IConverter
	Endpoint() string
}

type IConverter

type IConverter interface {
	RequestConvert(ctx eocontext.EoContext, extender map[string]interface{}) error
	ResponseConvert(ctx eocontext.EoContext) error
}

type IConverterCreateFunc

type IConverterCreateFunc func(cfg string) (IConverter, error)

func GetConverterCreateFunc

func GetConverterCreateFunc(id string) (IConverterCreateFunc, bool)

type IConverterFactory

type IConverterFactory interface {
	Create(cfg string) (IConverter, error)
}

type IKeyResource

type IKeyResource interface {
	ID() string
	Health() bool
	Priority() int
	// Up 上线
	Up()
	// Down 下线
	Down()

	IsBreaker() bool
	// Breaker 熔断
	Breaker()

	IConverter
}

func KeyResources

func KeyResources(provider string) ([]IKeyResource, bool)

type IManager

type IManager interface {
	Get(id string) (IConverterCreateFunc, bool)
	Set(id string, driver IConverterCreateFunc)
	Del(id string)
}

func NewManager

func NewManager() IManager

type IModelAccessConfig

type IModelAccessConfig interface {
	Provider() string
	Model() string
	Config() map[string]string
}

type IModelAccessConfigManager

type IModelAccessConfigManager interface {
	Get(id string) (IModelAccessConfig, bool)
	Set(id string, config IModelAccessConfig)
	Del(id string)
}

func NewModelAccessConfigManager

func NewModelAccessConfigManager() IModelAccessConfigManager

type IProvider

type IProvider interface {
	Provider() string
	Model() string
	ModelConfig() map[string]interface{}
	Priority() int
	Health() bool
	Down()
	BalanceHandler() eoscContext.BalanceHandler
	GenExtender(cfg string) (map[string]interface{}, error)
}

func Balances

func Balances() []IProvider

func GetProvider

func GetProvider(provider string) (IProvider, bool)

type KeyPool

type KeyPool eosc.Untyped[string, IKeyResource]

type KeyPoolManager

type KeyPoolManager struct {
	// contains filtered or unexported fields
}

func NewKeyPoolManager

func NewKeyPoolManager() *KeyPoolManager

func (*KeyPoolManager) Del

func (m *KeyPoolManager) Del(id string)

func (*KeyPoolManager) DelKeySource

func (m *KeyPoolManager) DelKeySource(id, resourceId string)

func (*KeyPoolManager) KeyResources

func (m *KeyPoolManager) KeyResources(id string) ([]IKeyResource, bool)

func (*KeyPoolManager) Set

func (m *KeyPoolManager) Set(id string, resource IKeyResource)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func (*Manager) Del

func (m *Manager) Del(id string)

func (*Manager) Get

func (m *Manager) Get(id string) (IConverterCreateFunc, bool)

func (*Manager) Set

func (m *Manager) Set(id string, driver IConverterCreateFunc)

type MappingRule

type MappingRule map[string]*ValueRule

MappingRule 定义映射规则结构

type Message

type Message struct {
	// Role indicates the role of the message sender (e.g., "system", "user", "assistant").
	Role string `json:"role"`
	// Content contains the actual text of the message.
	Content string `json:"content"`
}

Message represents a single message in the conversation.

type ModelConfig

type ModelConfig struct {
	MaxTokens           int                                  `json:"max_tokens,omitempty"`
	MaxCompletionTokens int                                  `json:"max_completion_tokens,omitempty"`
	Temperature         float32                              `json:"temperature,omitempty"`
	TopP                float32                              `json:"top_p,omitempty"`
	N                   int                                  `json:"n,omitempty"`
	Stream              bool                                 `json:"stream,omitempty"`
	Stop                []string                             `json:"stop,omitempty"`
	PresencePenalty     float32                              `json:"presence_penalty,omitempty"`
	ResponseFormat      *openai.ChatCompletionResponseFormat `json:"response_format,omitempty"`
	Seed                *int                                 `json:"seed,omitempty"`
	FrequencyPenalty    float32                              `json:"frequency_penalty,omitempty"`
	LogProbs            bool                                 `json:"logprobs,omitempty"`
	TopLogProbs         int                                  `json:"top_logprobs,omitempty"`
}

type OpenAIConvert

type OpenAIConvert struct {
	// contains filtered or unexported fields
}

func NewOpenAIConvert

func NewOpenAIConvert(apikey string, baseUrl string, timeout time.Duration, checkErr CheckError, errorCallback func(ctx http_service.IHttpContext, body []byte)) (*OpenAIConvert, error)

func (*OpenAIConvert) RequestConvert

func (o *OpenAIConvert) RequestConvert(ctx eoscContext.EoContext, extender map[string]interface{}) error

func (*OpenAIConvert) ResponseConvert

func (o *OpenAIConvert) ResponseConvert(ctx eoscContext.EoContext) error

type Request

type Request struct {
	Model    string                         `json:"model"`
	Messages []openai.ChatCompletionMessage `json:"messages"`
	// MaxTokens The maximum number of tokens that can be generated in the chat completion.
	// This value can be used to control costs for text generated via API.
	// This value is now deprecated in favor of max_completion_tokens, and is not compatible with o1 series models.
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat-create-max_tokens
	MaxTokens int `json:"max_tokens,omitempty"`
	// MaxCompletionTokens An upper bound for the number of tokens that can be generated for a completion,
	// including visible output tokens and reasoning tokens https://platform.openai.com/docs/guides/reasoning
	MaxCompletionTokens int      `json:"max_completion_tokens,omitempty"`
	Temperature         float32  `json:"temperature,omitempty"`
	TopP                float32  `json:"top_p,omitempty"`
	N                   int      `json:"n,omitempty"`
	Stream              bool     `json:"stream,omitempty"`
	Stop                []string `json:"stop,omitempty"`
	PresencePenalty     float32  `json:"presence_penalty,omitempty"`
}

Request 定义客户端统一输入请求格式

type Response

type Response struct {
	*openai.ChatCompletionResponse `json:"response,omitempty"`
	Error                          *Error `json:"error,omitempty"`
}

Response 定义客户端统一输出响应格式

type ValueRule

type ValueRule struct {
	Value string `json:"value"`
	Type  string `json:"type"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL