cohere

package module
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

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. 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.

See https://docs.cohere.com/ for the full API reference.

Index

Constants

View Source
const (
	EmbeddingRequestExtensionKey = "cohere/embedding_request"
	RerankRequestExtensionKey    = "cohere/rerank_request"

	RerankRequestIDMetadataKey   = "cohere/request_id"
	RerankSearchUnitsMetadataKey = "cohere/search_units"
)

Namespacing preserves provider-specific data without promoting it into the shared Core protocol or colliding with another provider.

View Source
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.

View Source
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.

View Source
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
}

EmbeddingModel wraps Cohere's v2 embed endpoint.

Supported models: embed-english-v3.0, embed-multilingual-v3.0, embed-english-light-v3.0, embed-multilingual-light-v3.0, embed-v4.0. v4 is the only family that supports OutputDimension; older v3 models have a fixed 1024-dim output.

func NewEmbeddingModel

func NewEmbeddingModel(_ context.Context, config EmbeddingModelConfig) (*EmbeddingModel, error)

NewEmbeddingModel rejects an invalid provider binding before the first embedding call.

func (*EmbeddingModel) Call

func (e *EmbeddingModel) Call(ctx context.Context, req *embedding.Request) (response *embedding.Response, err error)

type EmbeddingModelConfig

type EmbeddingModelConfig struct {
	APIKey         string
	DefaultOptions embedding.Options
	BaseURL        string
	HTTPClient     *http.Client
}

EmbeddingModelConfig binds provider access and defaults shared by every embedding call.

func (EmbeddingModelConfig) Validate

func (e EmbeddingModelConfig) Validate() error

type RerankModel added in v0.12.0

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

RerankModel implements the Core reranking contract with Cohere.

func NewRerankModel added in v0.12.0

func NewRerankModel(_ context.Context, config RerankModelConfig) (*RerankModel, error)

NewRerankModel rejects an invalid provider binding before the first reranking call.

func (*RerankModel) Call added in v0.12.0

func (r *RerankModel) Call(ctx context.Context, request *rerank.Request) (*rerank.Response, error)

type RerankModelConfig added in v0.12.0

type RerankModelConfig struct {
	APIKey         string
	DefaultOptions rerank.Options
	BaseURL        string
	HTTPClient     *http.Client
}

RerankModelConfig binds provider access and defaults shared by every reranking call.

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,omitempty"`
	Priority        *int `json:"priority,omitempty"`
}

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.

Jump to

Keyboard shortcuts

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