76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
// Package aiwire 定义 AI 网关的线格式类型:OpenAI Responses / Anthropic Messages /
|
|
// Embeddings 与通用记账、错误、模型列表结构。上游统一为 OCI OpenAI 兼容面。
|
|
package aiwire
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// StringList 兼容 OpenAI stop 的 string 与 []string 两种线格式。
|
|
type StringList []string
|
|
|
|
func (s *StringList) UnmarshalJSON(b []byte) error {
|
|
if len(b) > 0 && b[0] == '"' {
|
|
var one string
|
|
if err := json.Unmarshal(b, &one); err != nil {
|
|
return err
|
|
}
|
|
*s = StringList{one}
|
|
return nil
|
|
}
|
|
var many []string
|
|
if err := json.Unmarshal(b, &many); err != nil {
|
|
return err
|
|
}
|
|
*s = many
|
|
return nil
|
|
}
|
|
|
|
// Usage 是 token 用量。
|
|
type Usage struct {
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
// PromptTokensDetails 携带缓存命中细分;OCI 为隐式 prompt 缓存,只有读取计数
|
|
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
|
|
}
|
|
|
|
// PromptTokensDetails 是输入 token 细分(OpenAI prompt_tokens_details 同构)。
|
|
type PromptTokensDetails struct {
|
|
CachedTokens int `json:"cached_tokens"`
|
|
}
|
|
|
|
// CachedTokens 返回缓存命中读取的 token 数;无细分时为 0。
|
|
func (u *Usage) CachedTokens() int {
|
|
if u == nil || u.PromptTokensDetails == nil {
|
|
return 0
|
|
}
|
|
return u.PromptTokensDetails.CachedTokens
|
|
}
|
|
|
|
// Model 是 /v1/models 列表项。
|
|
type Model struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object"`
|
|
Created int64 `json:"created"`
|
|
OwnedBy string `json:"owned_by"`
|
|
}
|
|
|
|
// ModelList 是 /v1/models 响应体。
|
|
type ModelList struct {
|
|
Object string `json:"object"`
|
|
Data []Model `json:"data"`
|
|
}
|
|
|
|
// ErrorBody 是 OpenAI 格式错误响应。
|
|
type ErrorBody struct {
|
|
Error ErrorDetail `json:"error"`
|
|
}
|
|
|
|
// ErrorDetail 是错误明细。
|
|
type ErrorDetail struct {
|
|
Message string `json:"message"`
|
|
Type string `json:"type"`
|
|
Code string `json:"code,omitempty"`
|
|
}
|