Documentation
¶
Index ¶
- Variables
- func ChatBase(url string, model string, msg string, chatOpts ...ChatBaseOption) (string, error)
- func ChatBasedExtractData(url string, model string, msg string, fields map[string]any, ...) (map[string]any, error)
- func ChatWithStream(url string, model string, msg string, httpErrHandler func(err error), ...) (io.Reader, error)
- func DetailsToString(details []ChatDetail) string
- func ExtractFromResult(result string, fields map[string]any) (map[string]any, error)
- func GenerateJSONPrompt(msg string, fields map[string]any) string
- func GetBaseURLFromConfig(config *AIConfig, defaultRootUrl, defaultUri string) string
- func GetBaseURLFromConfigEx(config *AIConfig, defaultRootUrl, defaultUri string, openaiMode bool) string
- func GetRegisteredAIGateways() []string
- func Register(name string, gateway func() AIClient)
- func RegisteredAIGateways() []string
- func ShrinkAndSafeToFile(i any) string
- func StructuredStreamBase(url string, model string, msg string, ...) (chan *StructuredData, error)
- type AIClient
- type AIConfig
- type AIConfigOption
- func WithAPIKey(k string) AIConfigOption
- func WithBaseURL(baseURL string) AIConfigOption
- func WithChatImageContent(image ...any) AIConfigOption
- func WithContext(ctx context.Context) AIConfigOption
- func WithDebugStream(h ...bool) AIConfigOption
- func WithDomain(domain string) AIConfigOption
- func WithEnableThinking(t any) AIConfigOption
- func WithEnableThinkingEx(thinkField string, thinkValue any) AIConfigOption
- func WithExtraHeader(headers ...*ypb.HTTPHeader) AIConfigOption
- func WithExtraHeaderString(key string, value string) AIConfigOption
- func WithFunctionCallRetryTimes(times int) AIConfigOption
- func WithHTTPErrorHandler(h func(error)) AIConfigOption
- func WithHost(h string) AIConfigOption
- func WithImageBase64(b64 string) AIConfigOption
- func WithImageFile(i string) AIConfigOption
- func WithImageRaw(raw []byte) AIConfigOption
- func WithModel(model string) AIConfigOption
- func WithNoHTTPS(b bool) AIConfigOption
- func WithNoHttps(b bool) AIConfigOption
- func WithPort(p int) AIConfigOption
- func WithProxy(p string) AIConfigOption
- func WithReasonStreamHandler(h func(io.Reader)) AIConfigOption
- func WithStreamAndConfigHandler(h func(reader io.Reader, cfg *AIConfig)) AIConfigOption
- func WithStreamHandler(h func(io.Reader)) AIConfigOption
- func WithTimeout(timeout float64) AIConfigOption
- func WithType(t string) AIConfigOption
- type ChatBaseContext
- type ChatBaseOption
- func WithChatBase_DisableStream(b bool) ChatBaseOption
- func WithChatBase_EnableThinking(b bool) ChatBaseOption
- func WithChatBase_EnableThinkingEx(b bool, key string, value any) ChatBaseOption
- func WithChatBase_ErrHandler(b func(error)) ChatBaseOption
- func WithChatBase_Function(b []any) ChatBaseOption
- func WithChatBase_ImageRawInstance(images ...*ImageDescription) ChatBaseOption
- func WithChatBase_PoCOptions(b func() ([]poc.PocConfigOption, error)) ChatBaseOption
- func WithChatBase_ReasonStreamHandler(b func(reader io.Reader)) ChatBaseOption
- func WithChatBase_StreamHandler(b func(io.Reader)) ChatBaseOption
- func WithChatBase_ThinkingBudget(budget int64) ChatBaseOption
- type ChatChoice
- type ChatCompletion
- type ChatContent
- type ChatDetail
- func NewAIChatDetail(content string) ChatDetail
- func NewSystemChatDetail(content string) ChatDetail
- func NewToolChatDetail(name, content string) ChatDetail
- func NewToolChatDetailWithID(id, name, content string) ChatDetail
- func NewUserChatDetail(content string) ChatDetail
- func NewUserChatDetailEx(content any) ChatDetail
- type ChatDetails
- type ChatError
- type ChatMessage
- type ChatUsage
- type Chatter
- type Configurable
- type EmbeddingCaller
- type FuncReturn
- type FunctionCall
- type FunctionCaller
- type GeneralChatter
- type ImageDescription
- type ModelListCaller
- type ModelMeta
- type Parameters
- type Property
- type StructuredData
- type StructuredStreamer
- type ToolCall
- type UsageStatsInfo
Constants ¶
This section is empty.
Variables ¶
var EnableNewLoadOption = true
var ErrUnsupportedMethod = errors.New("unsupported method")
Functions ¶
func ChatBasedExtractData ¶
func ChatWithStream ¶
func DetailsToString ¶
func DetailsToString(details []ChatDetail) string
func ExtractFromResult ¶
func GetBaseURLFromConfig ¶
func GetBaseURLFromConfigEx ¶
func GetRegisteredAIGateways ¶
func GetRegisteredAIGateways() []string
func RegisteredAIGateways ¶ added in v1.3.3
func RegisteredAIGateways() []string
func ShrinkAndSafeToFile ¶
func StructuredStreamBase ¶
Types ¶
type AIClient ¶
type AIClient interface {
Chatter
FunctionCaller
Configurable
StructuredStreamer
ModelListCaller
}
type AIConfig ¶
type AIConfig struct {
// gateway network config
BaseURL string
Domain string `json:"domain" app:"name:domain,verbose:第三方加速域名,id:4"`
NoHttps bool `json:"no_https" app:"name:no_https,verbose:NoHttps,desc:是否禁用使用https请求api,id:3"`
// basic model
Model string `json:"model" app:"name:model,verbose:模型名称,id:2,type:list"`
Timeout float64 // `app:"name:请求超时时长"`
Deadline time.Time
APIKey string `json:"api_key" app:"name:api_key,verbose:ApiKey,desc:APIKey / Token,required:true,id:1"`
Proxy string `json:"proxy" app:"name:proxy,verbose:代理地址,id:5"`
Host string
Port int
StreamHandler func(io.Reader)
ReasonStreamHandler func(reader io.Reader)
Type string `json:"Type"`
Context context.Context
FunctionCallRetryTimes int
HTTPErrorHandler func(error)
Images []*ImageDescription
Headers []*ypb.HTTPHeader
EnableThinking bool
EnableThinkingField string
EnableThinkingValue any
}
func NewDefaultAIConfig ¶
func NewDefaultAIConfig(opts ...AIConfigOption) *AIConfig
type AIConfigOption ¶
type AIConfigOption func(*AIConfig)
func WithAPIKey ¶
func WithAPIKey(k string) AIConfigOption
func WithBaseURL ¶
func WithBaseURL(baseURL string) AIConfigOption
func WithChatImageContent ¶
func WithChatImageContent(image ...any) AIConfigOption
func WithContext ¶
func WithContext(ctx context.Context) AIConfigOption
func WithDebugStream ¶
func WithDebugStream(h ...bool) AIConfigOption
func WithDomain ¶
func WithDomain(domain string) AIConfigOption
func WithEnableThinking ¶
func WithEnableThinking(t any) AIConfigOption
func WithEnableThinkingEx ¶
func WithEnableThinkingEx(thinkField string, thinkValue any) AIConfigOption
func WithExtraHeader ¶
func WithExtraHeader(headers ...*ypb.HTTPHeader) AIConfigOption
func WithExtraHeaderString ¶
func WithExtraHeaderString(key string, value string) AIConfigOption
func WithFunctionCallRetryTimes ¶ added in v1.3.3
func WithFunctionCallRetryTimes(times int) AIConfigOption
func WithHTTPErrorHandler ¶
func WithHTTPErrorHandler(h func(error)) AIConfigOption
func WithHost ¶
func WithHost(h string) AIConfigOption
func WithImageBase64 ¶
func WithImageBase64(b64 string) AIConfigOption
func WithImageFile ¶
func WithImageFile(i string) AIConfigOption
func WithImageRaw ¶
func WithImageRaw(raw []byte) AIConfigOption
func WithModel ¶
func WithModel(model string) AIConfigOption
func WithNoHTTPS ¶
func WithNoHTTPS(b bool) AIConfigOption
func WithNoHttps ¶
func WithNoHttps(b bool) AIConfigOption
func WithPort ¶
func WithPort(p int) AIConfigOption
func WithProxy ¶
func WithProxy(p string) AIConfigOption
func WithReasonStreamHandler ¶
func WithReasonStreamHandler(h func(io.Reader)) AIConfigOption
func WithStreamAndConfigHandler ¶
func WithStreamAndConfigHandler(h func(reader io.Reader, cfg *AIConfig)) AIConfigOption
func WithStreamHandler ¶
func WithStreamHandler(h func(io.Reader)) AIConfigOption
func WithTimeout ¶
func WithTimeout(timeout float64) AIConfigOption
func WithType ¶
func WithType(t string) AIConfigOption
type ChatBaseContext ¶
type ChatBaseContext struct {
PoCOptionGenerator func() ([]poc.PocConfigOption, error)
EnableThinking bool
EnableThinkingField string
EnableThinkingValue any
ThinkingBudget int64
StreamHandler func(io.Reader)
ReasonStreamHandler func(reader io.Reader)
ErrHandler func(err error)
ImageUrls []*ImageDescription
DisableStream bool
}
func NewChatBaseContext ¶
func NewChatBaseContext(opts ...ChatBaseOption) *ChatBaseContext
type ChatBaseOption ¶
type ChatBaseOption func(c *ChatBaseContext)
func WithChatBase_DisableStream ¶
func WithChatBase_DisableStream(b bool) ChatBaseOption
func WithChatBase_EnableThinking ¶
func WithChatBase_EnableThinking(b bool) ChatBaseOption
func WithChatBase_EnableThinkingEx ¶
func WithChatBase_EnableThinkingEx(b bool, key string, value any) ChatBaseOption
func WithChatBase_ErrHandler ¶
func WithChatBase_ErrHandler(b func(error)) ChatBaseOption
func WithChatBase_Function ¶
func WithChatBase_Function(b []any) ChatBaseOption
func WithChatBase_ImageRawInstance ¶
func WithChatBase_ImageRawInstance(images ...*ImageDescription) ChatBaseOption
func WithChatBase_PoCOptions ¶
func WithChatBase_PoCOptions(b func() ([]poc.PocConfigOption, error)) ChatBaseOption
func WithChatBase_ReasonStreamHandler ¶
func WithChatBase_ReasonStreamHandler(b func(reader io.Reader)) ChatBaseOption
func WithChatBase_StreamHandler ¶
func WithChatBase_StreamHandler(b func(io.Reader)) ChatBaseOption
func WithChatBase_ThinkingBudget ¶
func WithChatBase_ThinkingBudget(budget int64) ChatBaseOption
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
Message ChatDetail `json:"message"`
FinishReason string `json:"finish_reason"`
}
type ChatCompletion ¶
type ChatContent ¶
type ChatContent struct {
Type string `json:"type"` // text / image_url
Text string `json:"text,omitempty"`
ImageUrl any `json:"image_url,omitempty"`
}
func NewUserChatContentImageUrl ¶
func NewUserChatContentImageUrl(u string) *ChatContent
func NewUserChatContentText ¶
func NewUserChatContentText(i string) *ChatContent
type ChatDetail ¶
type ChatDetail struct {
Role string `json:"role"`
Name string `json:"name,omitempty"`
Content any `json:"content"`
ToolCalls []*ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
}
func NewAIChatDetail ¶
func NewAIChatDetail(content string) ChatDetail
assistantMessage 根据传入的内容构造并返回一个 OpenAI 助手信息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ ```
func NewSystemChatDetail ¶
func NewSystemChatDetail(content string) ChatDetail
systemMessage 根据传入的内容构造并返回一个 OpenAI 系统信息 Example: ``` d = openai.ChatEx( [ openai.systemMessage("The weather in Boston is 72 degrees and sunny."), openai.userMessage("What is the weather like today?"), ], )~ ```
func NewToolChatDetail ¶
func NewToolChatDetail(name, content string) ChatDetail
toolMessage 根据传入的函数名,内容构造并返回一个 OpenAI 工具信息,用于指示工具返回结果 Example: ``` session = openai.NewSession( openai.proxy("http://127.0.0.1:7890") ) result = session.Chat(openai.userMessage("What is the weather like in Boston?"), openai.newFunction( "get_current_weather", "Get the current weather in a given location", openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location"), ), )~ result = session.Chat(openai.toolMessage("get_current_weather", `{"degree":72,"weather":"sunny"}`))~ println(result.String()) ```
func NewToolChatDetailWithID ¶
func NewToolChatDetailWithID(id, name, content string) ChatDetail
toolMessageWithID 根据传入的ID,函数名,内容构造并返回一个 OpenAI 工具信息,用于指示工具返回结果 Example: ``` session = openai.NewSession( openai.proxy("http://127.0.0.1:7890") ) result = session.Chat(openai.userMessage("What is the weather like in Boston?"), openai.newFunction( "get_current_weather", "Get the current weather in a given location", openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location"), ), )~ result = session.Chat(openai.toolMessage("get_current_weather", `{"degree":72,"weather":"sunny"}`))~ println(result.String()) ```
func NewUserChatDetail ¶
func NewUserChatDetail(content string) ChatDetail
userMessage 根据传入的内容构造并返回一个 OpenAI 用户信息 Example: ``` d = openai.ChatEx( [ openai.systemMessage("The weather in Boston is 72 degrees and sunny."), openai.userMessage("What is the weather like today?"), ], )~ ```
func NewUserChatDetailEx ¶
func NewUserChatDetailEx(content any) ChatDetail
func (ChatDetail) Clone ¶
func (detail ChatDetail) Clone() ChatDetail
type ChatDetails ¶
type ChatDetails []ChatDetail
func (ChatDetails) ChatMessages ¶
func (details ChatDetails) ChatMessages() []ChatDetail
ChatMessages 返回一个 ChatDetail 切片 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.ChatMessages()) ```
func (ChatDetails) Clone ¶
func (details ChatDetails) Clone() ChatDetails
func (ChatDetails) FirstString ¶
func (details ChatDetails) FirstString() string
String 返回第一个消息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.String()) ```
func (ChatDetails) FunctionCallResult ¶
func (details ChatDetails) FunctionCallResult() map[string]any
FunctionCallResult 返回函数调用的结果 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like in Boston?") ], openai.newFunction( "get_current_weather", "Get the current weather in a given location", openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location"), ), openai.proxy("http://127.0.0.1:7890"), )~ println(d.FunctionCallResult()) ```
func (ChatDetails) String ¶
func (details ChatDetails) String() string
String 返回消息切片中包含的所有消息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.String()) ```
type ChatMessage ¶
type ChatMessage struct {
Model string `json:"model"`
Messages []ChatDetail `json:"messages"`
Stream bool `json:"stream"`
EnableThinking bool `json:"enable_thinking,omitempty"`
}
func NewChatMessage ¶
func NewChatMessage(model string, messages []ChatDetail, dummy ...any) *ChatMessage
type Configurable ¶
type Configurable interface {
LoadOption(opt ...AIConfigOption)
BuildHTTPOptions() ([]poc.PocConfigOption, error)
CheckValid() error
}
type EmbeddingCaller ¶
type FuncReturn ¶
type FunctionCaller ¶
type GeneralChatter ¶
type GeneralChatter func(string, ...AIConfigOption) (string, error)
type ImageDescription ¶
type ImageDescription struct {
Url string `json:"url"`
}
type ModelListCaller ¶
type ModelMeta ¶
type ModelMeta struct {
Id string `json:"id"`
Object string `json:"object,omitempty"`
Created int64 `json:"created,omitempty"`
OwnedBy string `json:"owned_by,omitempty"`
}
func ListChatModels ¶
type Parameters ¶
type StructuredData ¶
type StructuredData struct {
Id string
Event string
DataSourceType string
DataRaw []byte
IsParsed bool
IsResponse bool
// parsed from node
OutputNodeName string
OutputNodeStatus string
OutputNodeId string
OutputNodeType string
OutputNodeExecTime string
OutputText string
OutputReason string
HaveUsage bool
ModelUsage []UsageStatsInfo
}
func (*StructuredData) Copy ¶
func (s *StructuredData) Copy() *StructuredData
type StructuredStreamer ¶
type StructuredStreamer interface {
SupportedStructuredStream() bool
StructuredStream(string, ...any) (chan *StructuredData, error)
}
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FuncReturn `json:"function"`
}