mp_common

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Embeddings added in v0.1.5

func Embeddings(ctx context.Context, provider, apiKey, url string, req map[string]interface{}, headers ...Header) ([]byte, error)

func Ocr added in v0.1.5

func Ocr(ctx *gin.Context, provider, apiKey, url string, req *OcrReq, headers ...Header) ([]byte, error)

func Rerank added in v0.1.5

func Rerank(ctx context.Context, provider, apiKey, url string, req map[string]interface{}, headers ...Header) ([]byte, error)

Types

type Document added in v0.1.3

type Document struct {
	Text string `json:"text"`
}

type EmbeddingData added in v0.1.3

type EmbeddingData struct {
	Object    string    `json:"object"`
	Embedding []float64 `json:"embedding" validate:"required,min=1"`
	Index     int       `json:"index"`
}

type EmbeddingReq added in v0.1.3

type EmbeddingReq struct {
	Model          string   `json:"model" validate:"required"`
	Input          []string `json:"input" validate:"required"`
	EncodingFormat *string  `json:"encoding_format,omitempty"`
}

func (*EmbeddingReq) Check added in v0.1.3

func (req *EmbeddingReq) Check() error

func (*EmbeddingReq) Data added in v0.1.5

func (req *EmbeddingReq) Data() (map[string]interface{}, error)

type EmbeddingResp added in v0.1.3

type EmbeddingResp struct {
	Id      *string         `json:"id,omitempty"`
	Model   string          `json:"model" validate:"required"`
	Object  *string         `json:"object,omitempty"`
	Data    []EmbeddingData `json:"data" validate:"required,dive"`
	Usage   Usage           `json:"usage"`
	Created *int            `json:"created,omitempty"`
}

type FCType added in v0.1.4

type FCType string
const (
	FCTypeFunctionCall FCType = "functionCall"
	FCTypeNoSupport    FCType = "noSupport"
	FCTypeToolCall     FCType = "toolCall"
)

type FunctionCall added in v0.1.4

type FunctionCall struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}
type Header struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type IEmbeddingReq

type IEmbeddingReq interface {
	Data() map[string]interface{}
}

func NewEmbeddingReq

func NewEmbeddingReq(data map[string]interface{}) IEmbeddingReq

type IEmbeddingResp

type IEmbeddingResp interface {
	String() string
	Data() (map[string]interface{}, bool)
	ConvertResp() (*EmbeddingResp, bool)
}

func NewEmbeddingResp

func NewEmbeddingResp(raw string) IEmbeddingResp

type ILLMReq

type ILLMReq interface {
	Stream() bool
	Data() map[string]interface{}
	OpenAIReq() (*LLMReq, bool)
}

func NewLLMReq

func NewLLMReq(data map[string]interface{}) ILLMReq

type ILLMResp

type ILLMResp interface {
	String() string
	Data() (map[string]interface{}, bool)
	ConvertResp() (*LLMResp, bool)
}

func ChatCompletions added in v0.1.5

func ChatCompletions(ctx context.Context, provider, apiKey, url string, req ILLMReq, respConverter func(bool, string) ILLMResp, headers ...Header) (ILLMResp, <-chan ILLMResp, error)

func NewLLMResp

func NewLLMResp(stream bool, raw string) ILLMResp

type IOcrReq added in v0.1.5

type IOcrReq interface {
	Data() *OcrReq
}

func NewOcrReq added in v0.1.5

func NewOcrReq(data *OcrReq) IOcrReq

type IOcrResp added in v0.1.5

type IOcrResp interface {
	String() string
	Data() (interface{}, bool)
	ConvertResp() (*OcrResp, bool)
}

func NewOcrResp added in v0.1.5

func NewOcrResp(raw string) IOcrResp

type IRerankReq

type IRerankReq interface {
	Data() map[string]interface{}
}

func NewRerankReq

func NewRerankReq(data map[string]interface{}) IRerankReq

type IRerankResp

type IRerankResp interface {
	String() string
	Data() (interface{}, bool)
	ConvertResp() (*RerankResp, bool)
}

func NewRerankResp

func NewRerankResp(raw string) IRerankResp

type LLMReq added in v0.1.4

type LLMReq struct {
	// general
	Model          string                `json:"model" validate:"required"`
	Messages       []OpenAIMsg           `json:"messages" validate:"required"`
	Stream         *bool                 `json:"stream,omitempty"`
	MaxTokens      *int                  `json:"max_tokens,omitempty"`
	Stop           *string               `json:"stop,omitempty"`
	ResponseFormat *OpenAIResponseFormat `json:"response_format,omitempty"`
	Temperature    *float64              `json:"temperature,omitempty"`
	Tools          []OpenAITool          `json:"tools,omitempty"`

	// custom
	Thinking            *Thinking      `json:"thinking,omitempty"` // 控制模型是否开启深度思考模式。
	EnableThinking      *bool          `json:"enable_thinking,omitempty"`
	MaxCompletionTokens *int           `json:"max_completion_tokens,omitempty"` // 控制模型输出的最大长度[0,64k]
	LogitBias           map[string]int `json:"logit_bias,omitempty"`            // 调整指定 token 在模型输出内容中出现的概率
	ToolChoice          interface{}    `json:"tool_choice,omitempty"`           // 强制指定工具调用的策略
	TopP                *float64       `json:"top_p,omitempty"`
	TopK                *int           `json:"top_k,omitempty"`
	MinP                *float64       `json:"min_p,omitempty"`
	ParallelToolCalls   *bool          `json:"parallel_tool_calls,omitempty"` // 是否开启并行工具调用
	StreamOptions       *StreamOptions `json:"stream_options,omitempty"`      //当启用流式输出时,可通过将本参数设置为{"include_usage": true},在输出的最后一行显示所使用的Token数。

	PresencePenalty   *float64 `json:"presence_penalty,omitempty"`  // 控制模型生成文本时的内容重复度
	FrequencyPenalty  *float64 `json:"frequency_penalty,omitempty"` // 频率惩罚系数
	RepetitionPenalty *float64 `json:"repetition_penalty,omitempty"`

	Seed           *int  `json:"seed,omitempty"`
	Logprobs       *bool `json:"logprobs,omitempty"`     // 是否返回输出 Token 的对数概率
	TopLogprobs    *int  `json:"top_logprobs,omitempty"` // 指定在每一步生成时,返回模型最大概率的候选 Token 个数
	N              *int  `json:"n,omitempty"`
	ThinkingBudget *int  `json:"thinking_budget,omitempty"` // 思考过程的最大长度,只在enable_thinking为true时生效

	WebSearch *WebSearch `json:"web_search,omitempty"` //搜索增强
	User      *string    `json:"user,omitempty"`
	// Yuanjing
	DoSample *bool `json:"do_sample,omitempty"`
}

func (*LLMReq) Check added in v0.1.4

func (req *LLMReq) Check() error

func (*LLMReq) Data added in v0.1.5

func (req *LLMReq) Data() (map[string]interface{}, error)

type LLMResp added in v0.1.4

type LLMResp struct {
	ID                string             `json:"id"`                               // 唯一标识
	Object            string             `json:"object"`                           // 固定为 "chat.completion"
	Created           int                `json:"created"`                          // 时间戳(秒)
	Model             string             `json:"model" validate:"required"`        // 使用的模型
	Choices           []OpenAIRespChoice `json:"choices" validate:"required,dive"` // 生成结果列表
	Usage             OpenAIRespUsage    `json:"usage"`                            // token 使用统计
	ServiceTier       *string            `json:"service_tier"`                     // (火山)指定是否使用TPM保障包。生效对象为购买了保障包推理接入点
	SystemFingerprint *string            `json:"system_fingerprint"`
}

type MsgRole added in v0.1.4

type MsgRole string
const (
	MsgRoleSystem    MsgRole = "system"
	MsgRoleUser      MsgRole = "user"
	MsgRoleAssistant MsgRole = "assistant"
	MsgRoleFunction  MsgRole = "tool"
)

type OcrData added in v0.1.5

type OcrData struct {
	PageNum []int  `json:"page_num" validate:"required,min=1"`
	Type    string `json:"type" validate:"required"`
	Text    string `json:"text" validate:"required"`
	Length  int    `json:"length"`
}

type OcrReq added in v0.1.5

type OcrReq struct {
	Files *multipart.FileHeader `form:"file" json:"file" validate:"required"`
}

func (*OcrReq) Check added in v0.1.5

func (req *OcrReq) Check() error

func (*OcrReq) Data added in v0.1.5

func (req *OcrReq) Data() (map[string]interface{}, error)

type OcrResp added in v0.1.5

type OcrResp struct {
	Code      int       `json:"code"`
	Message   string    `json:"message" validate:"required"`
	Version   string    `json:"version"`
	TimeStamp string    `json:"timestamp"`
	Id        string    `json:"id"`
	TimeCost  float64   `json:"time_cost"`
	OcrData   []OcrData `json:"data" validate:"required,dive"`
}

type OpenAIFunction added in v0.1.4

type OpenAIFunction struct {
	Name        string                    `json:"name" validate:"required"`
	Description string                    `json:"description,omitempty"`
	Parameters  *OpenAIFunctionParameters `json:"parameters,omitempty"`
}

type OpenAIFunctionParameters added in v0.1.4

type OpenAIFunctionParameters struct {
	Type       string                                      `json:"type"`
	Properties map[string]OpenAIFunctionParametersProperty `json:"properties"`
	Required   []string                                    `json:"required"`
}

type OpenAIFunctionParametersProperty added in v0.1.4

type OpenAIFunctionParametersProperty struct {
	Description string `json:"description"`
	Type        string `json:"type"`
}

type OpenAIMsg

type OpenAIMsg struct {
	Role             MsgRole       `json:"role"` // "system" | "user" | "assistant" | "function(已弃用)"
	Content          string        `json:"content"`
	ToolCallId       *string       `json:"tool_call_id,omitempty"`
	ReasoningContent *string       `json:"reasoning_content,omitempty"`
	Name             *string       `json:"name,omitempty"`
	FunctionCall     *FunctionCall `json:"function_call,omitempty"`
	ToolCalls        []*ToolCall   `json:"tool_calls,omitempty"`
}

type OpenAIRespChoice

type OpenAIRespChoice struct {
	Index        int         `json:"index"`             // 选项索引
	Message      *OpenAIMsg  `json:"message,omitempty"` // 非流式生成的消息
	Delta        *OpenAIMsg  `json:"delta,omitempty"`   // 流式生成的消息
	FinishReason string      `json:"finish_reason"`     // 停止原因
	Logprobs     interface{} `json:"logprobs"`
}

OpenAIRespChoice 结构体表示单个生成选项

type OpenAIRespChoiceMsg

type OpenAIRespChoiceMsg struct {
	Content string `json:"content"`
	Role    string `json:"role"`
}

type OpenAIRespUsage

type OpenAIRespUsage struct {
	CompletionTokens int `json:"completion_tokens"` // 输出 token 数
	PromptTokens     int `json:"prompt_tokens"`     // 输入 token 数
	TotalTokens      int `json:"total_tokens"`      // 总 token 数
}

OpenAIRespUsage 结构体表示 token 消耗

type OpenAIResponseFormat

type OpenAIResponseFormat struct {
	Type string `json:"type"` // "text" | "json"
}

type OpenAITool added in v0.1.4

type OpenAITool struct {
	Type     ToolType        `json:"type" validate:"required"`
	Function *OpenAIFunction `json:"function" validate:"required"`
}

type RerankReq added in v0.1.3

type RerankReq struct {
	Documents       []string `json:"documents" validate:"required"`
	Model           string   `json:"model" validate:"required"`
	Query           string   `json:"query" validate:"required"`
	ReturnDocuments *bool    `json:"return_documents,omitempty"`
	TopN            *int     `json:"top_n,omitempty"`
}

func (*RerankReq) Check added in v0.1.3

func (req *RerankReq) Check() error

func (*RerankReq) Data added in v0.1.5

func (req *RerankReq) Data() (map[string]interface{}, error)

type RerankResp added in v0.1.3

type RerankResp struct {
	Results   []Result `json:"results" validate:"required,dive"`
	Model     string   `json:"model"`
	Object    *string  `json:"object,omitempty"`
	Usage     Usage    `json:"usage"`
	RequestId *string  `json:"request_id,omitempty"`
}

type Result added in v0.1.3

type Result struct {
	Index          int       `json:"index"`
	Document       *Document `json:"document,omitempty"`
	RelevanceScore float64   `json:"relevance_score" validate:"required"`
}

type StreamOptions added in v0.1.4

type StreamOptions struct {
	IncludeUsage      *bool `json:"include_usage,omitempty"`
	ChunkIncludeUsage *bool `json:"chunk_include_usage,omitempty"`
}

type Thinking added in v0.1.4

type Thinking struct {
	Type string `json:"type" default:"enabled"`
}

type ToolCall added in v0.1.4

type ToolCall struct {
	ID       string       `json:"id"`
	Type     ToolType     `json:"type"`
	Function FunctionCall `json:"function"`
	Index    *int         `json:"index,omitempty"`
}

type ToolType added in v0.1.4

type ToolType string
const (
	ToolTypeFunction ToolType = "function"
)

type Usage added in v0.1.3

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	TotalTokens      int `json:"total_tokens"`
	CompletionTokens int `json:"completion_tokens"`
}

type WebSearch added in v0.1.4

type WebSearch struct {
	Enable         *bool `json:"enable,omitempty"`
	EnableCitation *bool `json:"enable_citation,omitempty"`
	EnableTrace    *bool `json:"enable_trace,omitempty"`
	EnableStatus   *bool `json:"enable_status,omitempty"`
}

Jump to

Keyboard shortcuts

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