Documentation
¶
Index ¶
- Variables
- type LLM
- func (o *LLM) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error)
- func (o *LLM) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error)
- func (o *LLM) GenerateContent(ctx context.Context, messages []llms.MessageContent, ...) (*llms.ContentResponse, error)
- type ModelName
- type Option
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyResponse = errors.New("no response") ErrCodeResponse = errors.New("has error code") )
Functions ¶
This section is empty.
Types ¶
type LLM ¶
LLM is a client for Baidu Qianfan (Ernie) LLM. It uses OpenAI-compatible API for chat completions and custom client for embeddings.
func New ¶
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) CreateEmbedding ¶
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 WithBaseURL ¶
WithBaseURL sets the base URL for the LLM API. Default is "https://qianfan.baidubce.com".
func WithCallbacks ¶
WithCallbacks sets the callbacks handler for the LLM.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client for the LLM.
func WithModel ¶
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