ernie

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyResponse = errors.New("no response")
	ErrCodeResponse  = errors.New("has error code")
)

Functions

This section is empty.

Types

type LLM

type LLM struct {
	CallbacksHandler callbacks.Handler
	// contains filtered or unexported fields
}

LLM is a client for Baidu Qianfan (Ernie) LLM. It uses OpenAI-compatible API for chat completions and custom client for embeddings.

func New

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

New returns a new Ernie LLM client using API Key authentication.

Authentication options: 1. WithAPIKey(apiKey) - pass API key directly 2. Set ERNIE_API_KEY environment variable

Model options: - WithModel(modelName) - set model name (default: ernie-speed-8k)

Common models: ernie-4.5-turbo-128k, ernie-speed-128k, ernie-speed-8k, deepseek-r1 For the complete model list, visit: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu

Example:

llm, err := ernie.New(
	ernie.WithAPIKey("your-api-key"),
	ernie.WithModel("ernie-4.5-turbo-128k"),
)

func (*LLM) Call

func (o *LLM) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error)

Call generates a response from the LLM for the given prompt.

func (*LLM) CreateEmbedding

func (o *LLM) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error)

CreateEmbedding generates embeddings for the given texts using Ernie embedding models.

The embedding model has the following limitations:

  • Embedding-V1: token count <= 384, text length <= 1000 characters
  • bge-large-zh or bge-large-en: token count <= 512, text length <= 2000 characters
  • tao-8k: token count <= 8192, text length <= 28000 characters
  • Qwen3-Embedding-0.6B or Qwen3-Embedding-4B: max 8k tokens per text

Text count limits:

  • tao-8k: only 1 text
  • Others: max 16 texts

API documentation: https://cloud.baidu.com/doc/qianfan-api/s/Fm7u3ropn

func (*LLM) GenerateContent

func (o *LLM) GenerateContent(ctx context.Context, messages []llms.MessageContent, options ...llms.CallOption) (*llms.ContentResponse, error)

GenerateContent implements the Model interface. Uses OpenAI-compatible API for chat completions.

type ModelName

type ModelName string

ModelName represents the model identifier for Baidu Qianfan (Ernie) API.

IMPORTANT: You should use your model name as a string directly. Common model names include:

  • ernie-4.5-turbo-128k (推荐)
  • ernie-speed-128k
  • ernie-speed-8k
  • deepseek-r1

For the complete model list, visit: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu

Example usage:

llm, err := ernie.New(
	ernie.WithAPIKey("your-api-key"),
	ernie.WithModel("ernie-4.5-turbo-128k"), // Use model name directly as string
)

type Option

type Option func(*options)

Option is a function that configures an LLM.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey sets the API key for the LLM.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the base URL for the LLM API. Default is "https://qianfan.baidubce.com".

func WithCallbacks

func WithCallbacks(handler callbacks.Handler) Option

WithCallbacks sets the callbacks handler for the LLM.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets the HTTP client for the LLM.

func WithModel

func WithModel(model ModelName) Option

WithModel sets the model name for the LLM. You should use the model name directly as a string. Common models: ernie-4.5-turbo-128k, ernie-speed-128k, deepseek-r1, etc. For the complete model list, visit: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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