Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateCost(model string, usage *Usage, service_tier string) int64
- func CleanString(str string) string
- func FakeBackend(rawURL string, input []byte) (int, []byte)
- func GetFallbackChatModel(model string) string
- func GetMD5Hash(text string) string
- func Init(apikey string)
- func InitAPI(geminiKey, openaiKey string)
- func ToGeminiModel(model string) string
- func ToGeminiRequestJSON(req CompletionInput) ([]byte, error)
- func ToModel(model string) string
- func ToOpenAICompletionJSON(req CompletionInput) ([]byte, error)
- func ToOpenAIModel(model string) string
- type CompletionInput
- type CompletionOutput
- type CompletionReasoning
- type Embedding
- type EmbeddingOutput
- type Function
- type GeminiAPIResponse
- type GeminiContent
- type GeminiEmbeddingContent
- type GeminiEmbeddingPart
- type GeminiEmbeddingRequest
- type GeminiEmbeddingResponse
- type GeminiError
- type GeminiErrorDetail
- type GeminiFunctionCall
- type GeminiFunctionDeclaration
- type GeminiFunctionResponse
- type GeminiGenerationConfig
- type GeminiPart
- type GeminiPartInlineData
- type GeminiRankingResponse
- type GeminiRequest
- type GeminiRerankRecord
- type GeminiThinkingConfig
- type GeminiTool
- type LocalCandidate
- type LocalContent
- type LocalPart
- type LocalUsageMetadata
- type OpenAIChatMessage
- type OpenAIChatResponse
- type OpenAIChoice
- type OpenAIEmbeddingData
- type OpenAIEmbeddingRequest
- type OpenAIEmbeddingResponse
- type OpenAIError
- type OpenAIJSONSchema
- type OpenAIMessageContent
- type OpenAIMessageContentImageUrl
- type OpenAITool
- type PromptTokensDetails
- type RerankInput
- type RerankOutput
- type RerankRecord
- type RerankingResponse
- type ResponseFormat
- type ToolCall
- type ToolFunction
- type TotalCost
- type Usage
Constants ¶
const Gemini_2_5_flash = "gemini-2.5-flash"
const Gemini_2_5_flash_lite = "gemini-2.5-flash-lite"
const Gemini_2_5_pro = "gemini-2.5-pro"
const Gemini_3_1_flash_lite = "gemini-3.1-flash-lite-preview"
const Gemini_embedding_001 = "gemini-embedding-001"
const Gpt_4_1 = "gpt-4.1"
const Gpt_4_1_mini = "gpt-4.1-mini"
const Gpt_4_1_nano = "gpt-4.1-nano"
const Gpt_4o = "gpt-4o"
const Gpt_4o_mini = "gpt-4o-mini"
const Gpt_5 = "gpt-5"
const Gpt_5_4_mini = "gpt-5.4-mini"
const Gpt_5_4_nano = "gpt-5.4-nano"
const Gpt_5_mini = "gpt-5-mini"
const Gpt_5_nano = "gpt-5-nano"
const O3 = "o3"
const O4_mini = "o4-mini"
const Text_embedding_3_large = "text-embedding-3-large"
const Text_embedding_3_small = "text-embedding-3-small"
const Text_embedding_ada_002 = "text-embedding-ada-002"
Variables ¶
var BACKEND = "https://api.subiz.com.vn/4.1/ai"
Functions ¶
func CalculateCost ¶
return 1000 usd fpv
func CleanString ¶
badstring: UGjhuqduIGjGsOG7m25nIGThuqtuIGPDoGkgxJHhurd0IHThu7EgxJHhu5luZyBn4butaSBaTlMgdHLDqm4gU3ViaXouCiAzOkNo4buNbiBt4bqrdSB0aW4gWk5TW+KAi10oI2IlQzYlQjAlRTElQkIlOUJjLTMlRTElQkIlOERuLW0lRTElQkElQUJ1LXRpbi16bnMgIsSQxrDhu51uZyBk4bqrbiB0cuG7sWMgdGnhur9wIMSR4bq/biBixrDhu5tjLTPhu41uLW3huqt1LXRpbi16bnMiKQohW10oaHR0cHM6Ly92Y2RuLnN1Yml6LWNkbi5jb20vZmlsZS83ZjczNDllNWNjYTNmNzczYTlkODIwODIxZTg3ZmI1NTEwYzlhODgwNDc0MGQ0ZjMzOWY3YzlkMzdiN2IzNmZiX2FjcHhrZ3VtaWZ1b29mb29zYmxlKQojIyMgQsaw4bubYyA0OiBI4bq5biBs4buLY2ggZ+G7rWkgdOG7sSDEkeG7mW5nIFpOU1vigItdKCNiJUM2JUIwJUUxJUJCJTlCYy00LWglRTElQkElQjluLWwlRTElQkIlOEJjaC1nJUUxJUJCJUFEaS10JUUxJUJCJUIxLSVDNCU5MSVFMSVCQiU5OW5nLXpucyAixJDGsOG7nW5nIGThuqtuIHRy4buxYyB0aQDhur9w
func GetFallbackChatModel ¶
GetFallbackChatModel returns a fallback model for a given model. If the model is a GPT model, it returns the equivalent Gemini model. If the model is a Gemini model, it returns the equivalent OpenAI model. Otherwise, it returns a default Gemini model.
func GetMD5Hash ¶
func Init ¶
func Init(apikey string)
Init setups the Subiz API Key client. Only client should call this
func InitAPI ¶
func InitAPI(geminiKey, openaiKey string)
InitAPI setups API Keys for the server. Only server should call this
func ToGeminiModel ¶
ToGeminiModel converts a model name to its equivalent Gemini model.
func ToGeminiRequestJSON ¶
func ToGeminiRequestJSON(req CompletionInput) ([]byte, error)
ToGeminiRequestJSON converts an OpenAIChatRequest to a Gemini-compatible JSON request string.
func ToOpenAICompletionJSON ¶
func ToOpenAICompletionJSON(req CompletionInput) ([]byte, error)
func ToOpenAIModel ¶
ToOpenAIModel converts a model name to its equivalent OpenAI model.
Types ¶
type CompletionInput ¶
type CompletionInput struct {
Seed int `json:"seed,omitempty"`
PromptCacheKey string `json:"prompt_cache_key,omitempty"`
PromptCacheRetention string `json:"prompt_cache_retention,omitempty"`
Verbosity string `json:"verbosity,omitempty"`
Stop []string `json:"stop,omitempty"`
Model string `json:"model,omitempty"`
NoLog bool `json:"-"` // disable log
Instruct string `json:"instruct,omitempty"`
Messages []*header.LLMChatHistoryEntry `json:"messages,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
ToolChoice string `json:"tool_choice,omitempty"`
Reasoning *CompletionReasoning `json:"reasoning,omitempty"`
ReasoningEffort string `json:"reasoning_effort,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
Tools []OpenAITool `json:"tools,omitempty"`
ServiceTier string `json:"service_tier,omitempty"` // [auto, default], flex, priority, scale
StopAfterToolCalled bool `json:"stop_after_tool_called"`
}
type CompletionOutput ¶
type CompletionOutput struct {
Content string `json:"content"`
Refusal string `json:"refusal"`
Request []byte `json:"request"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
InputCachedTokens int64 `json:"input_cached_tokens"`
OuputCachedTokens int64 `json:"output_cached_tokens"`
Created int64 `json:"created"`
DurationMs int64 `json:"duration_ms"`
KfpvCostUSD int64 `json:"kfpv_cost_usd"` // 1 usd -> 1000_000_000 kfpvusd
}
func Complete ¶
func Complete(ctx context.Context, input CompletionInput) (string, CompletionOutput, error)
type CompletionReasoning ¶
type Embedding ¶
type Embedding struct {
Values []float32 `json:"values"`
}
Embedding represents the embedding values.
type EmbeddingOutput ¶
type EmbeddingOutput struct {
Text string `json:"text"`
Vector []float32 `json:"vector"`
TotalTokens int64 `json:"total_tokens"`
Created int64 `json:"created"`
DurationMs int64 `json:"duration_ms"`
KfpvCostUSD int64 `json:"kfpv_cost_usd"`
}
func GetEmbedding ¶
type Function ¶
type Function struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Parameters *header.JSONSchema `json:"parameters,omitempty"`
Handler func(ctx context.Context, arg, callid string, ctxm map[string]any) string `json:"-"` // "", true -> abandon completion
}
Function mimics the structure of a function in an OpenAI Chat Completion request.
type GeminiAPIResponse ¶
type GeminiAPIResponse struct {
Candidates []*LocalCandidate `json:"candidates"`
UsageMetadata *LocalUsageMetadata `json:"usageMetadata"`
ModelVersion string `json:"modelVersion"`
ResponseId string `json:"responseId"`
Error *GeminiError `json:"error"`
}
GeminiAPIResponse is a wrapper for the Gemini API response.
type GeminiContent ¶
type GeminiContent struct {
Parts []*GeminiPart `json:"parts"`
Role string `json:"role,omitempty"`
}
GeminiContent represents a content block in a Gemini request.
type GeminiEmbeddingContent ¶
type GeminiEmbeddingContent struct {
Parts []GeminiEmbeddingPart `json:"parts"`
}
GeminiEmbeddingContent represents the content for an embedding request.
type GeminiEmbeddingPart ¶
type GeminiEmbeddingPart struct {
Text string `json:"text"`
}
GeminiEmbeddingPart represents a part of the content for an embedding request.
type GeminiEmbeddingRequest ¶
type GeminiEmbeddingRequest struct {
Model string `json:"model"`
Content GeminiEmbeddingContent `json:"content"`
}
EmbeddingRequest represents the request to the embedding model.
type GeminiEmbeddingResponse ¶
type GeminiEmbeddingResponse struct {
Embedding *Embedding `json:"embedding"`
}
GeminiEmbeddingResponse represents the response from the embedding model.
type GeminiError ¶
type GeminiError struct {
Code int `json:"code"`
Message string `json:"message"`
Status string `json:"status"`
Details []GeminiErrorDetail `json:"details"`
}
GeminiError represents a Gemini API error.
type GeminiErrorDetail ¶
type GeminiErrorDetail struct {
Type string `json:"@type"`
FieldViolations []struct {
Field string `json:"field"`
Description string `json:"description"`
} `json:"fieldViolations"`
}
GeminiErrorDetail represents the details of a Gemini API error.
type GeminiFunctionCall ¶
GeminiFunctionCall is a local struct to avoid genai dependency.
type GeminiFunctionDeclaration ¶
type GeminiFunctionDeclaration struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters *header.JSONSchema `json:"parameters"`
}
GeminiFunctionDeclaration is a local struct to avoid genai dependency.
type GeminiFunctionResponse ¶
type GeminiFunctionResponse struct {
Name string `json:"name"`
Response map[string]any `json:"response"`
}
GeminiFunctionResponse is a local struct to avoid genai dependency.
type GeminiGenerationConfig ¶
type GeminiGenerationConfig struct {
StopSequences []string `json:"stopSequences,omitempty"`
// CandidateCount int `json:"candidateCount,omitempty"`
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"topP,omitempty"`
Seed int `json:"seed,omitempty"`
ResponseMIMEType string `json:"responseMimeType,omitempty"`
ResponseSchema *header.JSONSchema `json:"responseSchema,omitempty"`
ThinkingConfig *GeminiThinkingConfig `json:"thinkingConfig,omitempty"`
}
GeminiGenerationConfig represents the generation configuration.
type GeminiPart ¶
type GeminiPart struct {
Text *string `json:"text,omitempty"`
FunctionCall *GeminiFunctionCall `json:"functionCall,omitempty"`
FunctionResponse *GeminiFunctionResponse `json:"functionResponse,omitempty"`
InlineData *GeminiPartInlineData `json:"inline_data,omitempty"`
}
GeminiPart represents a part of a content block.
type GeminiPartInlineData ¶
type GeminiRankingResponse ¶
type GeminiRankingResponse struct {
Records []*GeminiRerankRecord `json:"records,omitempty"`
Error *GeminiError `json:"error"`
}
type GeminiRequest ¶
type GeminiRequest struct {
Model string `json:"model"`
SystemInstruction *GeminiContent `json:"systemInstruction,omitempty"`
Contents []*GeminiContent `json:"contents"`
Tools []*GeminiTool `json:"tools,omitempty"`
GenerationConfig *GeminiGenerationConfig `json:"generationConfig,omitempty"`
}
GeminiRequest represents the structure of a request to the Gemini API. https://ai.google.dev/api/generate-content#v1beta.GenerationConfig
type GeminiRerankRecord ¶
type GeminiThinkingConfig ¶
type GeminiThinkingConfig struct {
// The number of thoughts tokens that the model should generate.
ThinkingBudget int `json:"thinkingBudget,omitempty"`
// Indicates whether to include thoughts in the response. If true, thoughts are returned only when available.
IncludeThoughts bool `json:"includeThoughts"`
}
type GeminiTool ¶
type GeminiTool struct {
FunctionDeclarations []*GeminiFunctionDeclaration `json:"functionDeclarations"`
}
GeminiTool is a local struct to avoid genai dependency.
type LocalCandidate ¶
type LocalCandidate struct {
Content *LocalContent `json:"content"`
FinishReason string `json:"finishReason"` // Using string instead of enum
}
LocalCandidate mirrors genai.Candidate
type LocalContent ¶
LocalContent mirrors genai.Content
type LocalPart ¶
type LocalPart struct {
Text *string `json:"text,omitempty"`
FunctionCall *GeminiFunctionCall `json:"functionCall,omitempty"`
}
LocalPart is a struct to unmarshal different part types from JSON.
type LocalUsageMetadata ¶
type LocalUsageMetadata struct {
PromptTokenCount int32 `json:"promptTokenCount,omitempty"`
CandidatesTokenCount int32 `json:"candidatesTokenCount,omitempty"`
TotalTokenCount int32 `json:"totalTokenCount,omitempty"`
CachedContentTokenCount int32 `json:"cachedContentTokenCount,omitempty"`
ThoughtsTokenCount int32 `json:"thoughtsTokenCount,omitempty"`
}
LocalUsageMetadata mirrors genai.UsageMetadata
type OpenAIChatMessage ¶
type OpenAIChatMessage struct {
Role string `json:"role"`
Content *string `json:"content"` // Use pointer to allow for null content
Name string `json:"name,omitempty"`
Contents []OpenAIMessageContent `json:"contents,omitempty"` // Use pointer to allow for null content
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallId string `json:"tool_call_id,omitempty"`
Refusal string `json:"refusal,omitempty"`
}
OpenAIChatMessage mimics the structure of a message in an OpenAI Chat Completion request.
func (*OpenAIChatMessage) GetContent ¶
func (m *OpenAIChatMessage) GetContent() string
type OpenAIChatResponse ¶
type OpenAIChatResponse struct {
ID string `json:"id,omitempty"`
Created int64 `json:"created,omitempty"`
Object string `json:"object,omitempty"`
Model string `json:"model,omitempty"`
Choices []OpenAIChoice `json:"choices,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Error *OpenAIError `json:"error,omitempty"`
ServiceTier string `json:"service_tier,omitempty"`
}
OpenAIChatResponse mimics the structure of an OpenAI Chat Completion response.
func ChatCompleteAPI ¶
func ChatCompleteAPI(ctx context.Context, payload []byte) (OpenAIChatResponse, error)
type OpenAIChoice ¶
type OpenAIChoice struct {
Index int `json:"index"`
Message OpenAIChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
OpenAIChoice mimics the structure of a choice in an OpenAI Chat Completion response.
type OpenAIEmbeddingData ¶
type OpenAIEmbeddingRequest ¶
type OpenAIEmbeddingResponse ¶
type OpenAIEmbeddingResponse struct {
Object string `json:"object"`
Data []OpenAIEmbeddingData `json:"data"`
Model string `json:"model"`
Usage *Usage `json:"usage"`
Error *OpenAIError `json:"error,omitempty"`
}
func GetEmbeddingAPI ¶
func GetEmbeddingAPI(ctx context.Context, model, text string) (OpenAIEmbeddingResponse, error)
type OpenAIError ¶
type OpenAIError struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param"`
Code string `json:"code,omitempty"`
}
OpenAIError represents the structure of an error in an OpenAI Chat Completion response.
type OpenAIJSONSchema ¶
type OpenAIJSONSchema struct {
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"` // string, number, object, array, boolean, null
Description string `protobuf:"bytes,5,opt,name=description,proto3" json:"description,omitempty"`
Properties map[string]*OpenAIJSONSchema `` /* 146-byte string literal not displayed */
Items *OpenAIJSONSchema `protobuf:"bytes,7,opt,name=items,proto3" json:"items,omitempty"` // used for type array
MinItems int64 `protobuf:"varint,8,opt,name=minItems,proto3" json:"minItems,omitempty"`
UniqueItems bool `protobuf:"varint,9,opt,name=uniqueItems,proto3" json:"uniqueItems,omitempty"`
ExclusiveMinimum int64 `protobuf:"varint,10,opt,name=exclusiveMinimum,proto3" json:"exclusiveMinimum,omitempty"`
Required []string `protobuf:"bytes,11,rep,name=required,proto3" json:"required,omitempty"`
AdditionalProperties bool `json:"additionalProperties"` // chatgpt required this
Enum []string `json:"enum,omitempty"`
}
type OpenAIMessageContent ¶
type OpenAIMessageContent struct {
Text string `json:"text,omitempty"`
Type string `json:"type,omitempty"` // text, image_url
ImageUrl *OpenAIMessageContentImageUrl `json:"image_url,omitempty"`
}
type OpenAIMessageContentImageUrl ¶
type OpenAIMessageContentImageUrl struct {
Url string `json:"url,omitempty"`
}
type OpenAITool ¶
type OpenAITool struct {
Type string `json:"type"`
Function *Function `json:"function"`
Output string `json:"output,omitempty"` // our fake
}
OpenAITool mimics the structure of a tool in an OpenAI Chat Completion request.
type PromptTokensDetails ¶
type PromptTokensDetails struct {
CachedTokens int64 `json:"cached_tokens"`
}
PromptTokensDetails mimics the structure of the prompt_tokens_details field.
type RerankInput ¶
type RerankInput struct {
Seed int `json:"seed,omitempty"`
TopN int `json:"top_n,omitempty"`
Model string `json:"model,omitempty"`
Query string `json:"query,omitempty"`
Records []*RerankRecord `json:"records,omitempty"`
}
type RerankOutput ¶
type RerankOutput struct {
Records []*RerankRecord `json:"records,omitempty"`
Created int64 `json:"created,omitempty"`
DurationMs int64 `json:"duration_ms"`
KfpvCostUSD int64 `json:"kfpv_cost_usd"` // 1 usd -> 1000_000_000 kfpvusd
}
func Rerank ¶
func Rerank(ctx context.Context, model, query string, inrecords []*RerankRecord) (RerankOutput, error)
type RerankRecord ¶
type RerankingResponse ¶
type RerankingResponse struct {
Records []*RerankRecord `json:"records,omitempty"`
Created int64 `json:"created,omitempty"` // sec
Object string `json:"object,omitempty"`
Model string `json:"model,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Error *OpenAIError `json:"error,omitempty"`
}
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type,omitempty"`
JSONSchema *header.LLMResponseJSONSchemaFormat `json:"json_schema,omitempty"`
}
ResponseFormat specifies the format of the response.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
ToolCall represents a tool call in an OpenAI response.
type ToolFunction ¶
ToolFunction represents the function details in a tool call.
type TotalCost ¶
type TotalCost struct {
USD int64 `json:"usd"` // kfpvusd = usd*1_000_000_000 = fpvusd * 1000
}
type Usage ¶
type Usage struct {
PromptTokens int64 `json:"prompt_tokens"`
CompletionTokens int64 `json:"completion_tokens"`
TotalTokens int64 `json:"total_tokens"`
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
KFpvCostUSD int64 `json:"kfpv_cost_usd"` // our fields not openai
CostVND int64 `json:"cost_vnd"` // our fields not openai
}
Usage mimics the structure of the usage field in an OpenAI Chat Completion response.