client

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSetAuth      = errors.New("API key not set")
	ErrEmptyResponse   = errors.New("empty response")
	ErrInvalidResponse = errors.New("invalid response")
)

Functions

This section is empty.

Types

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	Delta        Delta   `json:"delta"`
	FinishReason string  `json:"finish_reason"`
}

Choice represents a choice in the completion response.

type Client

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

Client is a client for Baidu Qianfan API using API Key authentication.

func New

func New(opts ...Option) (*Client, error)

New creates a new Client with the given options.

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, model string, req *CompletionRequest) (*CompletionResponse, error)

CreateCompletion sends a chat completion request.

func (*Client) CreateEmbedding

func (c *Client) CreateEmbedding(ctx context.Context, model string, texts []string) (*EmbeddingResponse, error)

CreateEmbedding sends an embedding request.

type CompletionRequest

type CompletionRequest struct {
	Model         string                                        `json:"model"`
	Messages      []Message                                     `json:"messages"`
	Temperature   float64                                       `json:"temperature,omitempty"`
	TopP          float64                                       `json:"top_p,omitempty"`
	PenaltyScore  float64                                       `json:"penalty_score,omitempty"`
	MaxTokens     int                                           `json:"max_tokens,omitempty"`
	Stream        bool                                          `json:"stream,omitempty"`
	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}

CompletionRequest represents a request to the chat completion API.

type CompletionResponse

type CompletionResponse struct {
	ID        string   `json:"id"`
	Object    string   `json:"object"`
	Created   int64    `json:"created"`
	Result    string   `json:"result,omitempty"`
	Choices   []Choice `json:"choices"`
	Usage     Usage    `json:"usage"`
	ErrorCode int      `json:"error_code,omitempty"`
	ErrorMsg  string   `json:"error_msg,omitempty"`
}

CompletionResponse represents a response from the chat completion API.

type Delta

type Delta struct {
	Role    string `json:"role,omitempty"`
	Content string `json:"content,omitempty"`
}

Delta represents the delta content in streaming responses.

type EmbedData

type EmbedData struct {
	Object    string    `json:"object"`
	Embedding []float32 `json:"embedding"`
	Index     int       `json:"index"`
}

EmbedData represents embedding data in the response.

type EmbeddingRequest

type EmbeddingRequest struct {
	Model string   `json:"model"`
	Input []string `json:"input"`
}

EmbeddingRequest represents a request to the embedding API.

type EmbeddingResponse

type EmbeddingResponse struct {
	ID        string      `json:"id"`
	Object    string      `json:"object"`
	Created   int64       `json:"created"`
	Data      []EmbedData `json:"data"`
	Model     string      `json:"model"`
	Usage     Usage       `json:"usage"`
	ErrorCode int         `json:"error_code,omitempty"`
	ErrorMsg  string      `json:"error_msg,omitempty"`
}

EmbeddingResponse represents a response from the embedding API.

type Message

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

Message represents a chat message.

type Option

type Option func(*clientOptions)

Option is a function that configures a Client.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey sets the API key for the client.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the base URL for the API.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets the HTTP client for the API.

type Usage

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

Usage represents token usage information.

Jump to

Keyboard shortcuts

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