Documentation
¶
Overview ¶
Package cohere implements Core embedding and reranking with Cohere's v2 API.
Embedding callers select the official input_type explicitly because query, document, classification, and clustering embeddings have different task semantics. EmbeddingRequestOptions carries these controls; SDK outbound requests are not extension input types. Only float vectors are represented. Reranking returns indices into the caller-owned document batch.
Embedding limits. "Maximum number of texts per call is 96", which MaxTextsPerEmbedRequest names and the model refuses above rather than sending a request certain to be rejected.
Over-long input is truncated by default: Cohere's truncate parameter is "One of NONE|START|END" with END the default, so an input past the model's token limit is embedded from a prefix and nothing reports it. Set truncate to NONE through the request extension to get an error instead.
Rerank limits are advisory, not enforced here. Cohere writes "we recommend against sending more than 1,000 documents in a single request", which is a recommendation rather than a ceiling, so refusing above it would reject work Cohere would have done. Long documents are a different matter: they "will automatically be truncated to the value of max_tokens_per_doc", which "defaults to 4096", so a document past that is reranked from a prefix and nothing reports it. RerankRequestOptions carries the parameter under Cohere's own name.
Rerank scores reach rerank.Score unchanged because Cohere documents the range they arrive in: "Relevance scores are normalized to be in the range [0, 1]". Cohere also warns against reading them as ratios — a 0.9 is not twice as relevant as a 0.45 — which is why Core calls a score query-relative and not comparable across providers.
Core usage records meta.tokens.input_tokens only. Billed input tokens can differ from physical input tokens, so they remain separately addressable via BilledInputTokensMetadataKey; billed search units use RerankSearchUnitsMetadataKey. Missing token reports stay absent. Both kinds of counts retain exact integer values without passing through the SDK's float64 representation.
See https://docs.cohere.com/ for the full API reference. Embedding and rerank response bodies, including error envelopes, are bounded by Core's httpclient.DefaultMaxJSONResponseBytes before either native presence validation or SDK decoding can buffer them.
Index ¶
Constants ¶
const ( EmbeddingRequestExtensionKey = Provider + "/embedding_request" RerankRequestExtensionKey = Provider + "/rerank_request" RerankRequestIDMetadataKey = "cohere/request_id" RerankSearchUnitsMetadataKey = "cohere/search_units" BilledInputTokensMetadataKey = "cohere/billed_input_tokens" )
Namespacing preserves provider-specific data without promoting it into the shared Core protocol or colliding with another provider.
const MaxTextsPerEmbedRequest = 96
MaxTextsPerEmbedRequest is the documented ceiling for one embed call: "Maximum number of texts per call is 96". A larger request is refused here rather than sent to be rejected, and it cannot be split without turning one caller-visible call into several with their own partial-failure and usage accounting.
const ModelRerankV35 = "rerank-v3.5"
ModelRerankV35 names the rerank model this adapter is verified against, so a caller can pin it without copying a version string that moves.
const (
Provider = "cohere"
)
Provider is the stable backend name for host-side attribution.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EmbeddingModel ¶
type EmbeddingModel struct {
// contains filtered or unexported fields
}
func NewEmbeddingModel ¶
func NewEmbeddingModel(ctx context.Context, config EmbeddingModelConfig) (*EmbeddingModel, error)
type EmbeddingModelConfig ¶
type EmbeddingModelConfig struct {
APIKey string
DefaultOptions embedding.Options
BaseURL string
HTTPClient *http.Client
}
func (EmbeddingModelConfig) Validate ¶
func (e EmbeddingModelConfig) Validate() error
type EmbeddingRequestOptions ¶ added in v0.38.0
type EmbeddingRequestOptions struct {
InputType string `json:"input_type"`
MaxTokens *int `json:"max_tokens,omitzero"`
EmbeddingTypes []string `json:"embedding_types,omitzero"`
Truncate *string `json:"truncate,omitzero"`
Priority *int `json:"priority,omitzero"`
}
EmbeddingRequestOptions supplies Cohere controls under EmbeddingRequestExtensionKey. InputType is required. EmbeddingTypes may be omitted or contain only float, which is the Core result representation. Core owns the input texts, model, and output dimensions.
func (*EmbeddingRequestOptions) UnmarshalJSON ¶ added in v0.58.0
func (e *EmbeddingRequestOptions) UnmarshalJSON(data []byte) error
func (EmbeddingRequestOptions) Validate ¶ added in v0.60.0
func (e EmbeddingRequestOptions) Validate() error
type RerankModel ¶ added in v0.12.0
type RerankModel struct {
// contains filtered or unexported fields
}
func NewRerankModel ¶ added in v0.12.0
func NewRerankModel(ctx context.Context, config RerankModelConfig) (*RerankModel, error)
type RerankModelConfig ¶ added in v0.12.0
type RerankModelConfig struct {
APIKey string
DefaultOptions rerank.Options
BaseURL string
HTTPClient *http.Client
}
func (RerankModelConfig) Validate ¶ added in v0.12.0
func (r RerankModelConfig) Validate() error
type RerankRequestOptions ¶ added in v0.12.0
type RerankRequestOptions struct {
MaxTokensPerDoc *int `json:"max_tokens_per_doc,omitzero"`
Priority *int `json:"priority,omitzero"`
}
RerankRequestOptions contains Cohere controls that do not alter Core's reranking result semantics. Each field is keyed by the name Cohere's own reference uses, so a caller reading those docs sets the key they read.
func (*RerankRequestOptions) UnmarshalJSON ¶ added in v0.58.0
func (r *RerankRequestOptions) UnmarshalJSON(data []byte) error
func (RerankRequestOptions) Validate ¶ added in v0.60.0
func (r RerankRequestOptions) Validate() error