cohere

package
v0.1.7 Latest Latest
Warning

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

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

Documentation

Overview

Package cohere implements ai.RerankModel against the Cohere Rerank API and compatible endpoints without the vendor SDK.

In addition to Cohere's official API (POST /v1/rerank), this package works with compatible rerank services such as SiliconFlow, Jina AI, Together AI, and self-hosted inference servers (vLLM, Hugging Face TEI) by supplying WithBaseURL, WithAPIKey, and WithProvider, or by using preset constructors like SiliconFlowRerank, JinaRerank, and TogetherRerank.

Index

Constants

View Source
const (
	BaseURLSiliconFlow = "https://api.siliconflow.cn/v1"
	BaseURLJina        = "https://api.jina.ai/v1"
	BaseURLTogether    = "https://api.together.ai/v1"
)

Base URLs for popular Cohere-compatible reranking endpoints.

View Source
const DefaultBaseURL = "https://api.cohere.com/v1"

DefaultBaseURL is the official Cohere v1 API endpoint used by NewRerankModel.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option configures a RerankModel.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets the provider API key. Defaults to the COHERE_API_KEY environment variable.

func WithAllowHTTP

func WithAllowHTTP() Option

WithAllowHTTP permits a plain-HTTP base URL (local inference servers).

func WithAllowPrivateIPs

func WithAllowPrivateIPs() Option

WithAllowPrivateIPs disables the SSRF guard for private/loopback endpoints (local inference servers).

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL points the model at a different endpoint, including any path prefix: DefaultBaseURL (the default), "https://api.cohere.com/v2", a compatible cloud service, or a local server. Non-HTTPS or private endpoints additionally need WithAllowHTTP / WithAllowPrivateIPs.

func WithCapabilities

func WithCapabilities(capabilities ai.Capabilities) Option

WithCapabilities overrides the static capability report.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient supplies a custom *http.Client. The client is used as-is: transport tuning and the SSRF dial guard become the caller's responsibility.

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a header to every request. It overrides same-named adapter headers.

func WithProvider

func WithProvider(provider ai.Provider) Option

WithProvider overrides the provider identity reported by RerankModel.Provider. Defaults to ai.ProviderCohere.

type RerankModel

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

RerankModel is an ai.RerankModel backed by the Cohere Rerank API or compatible endpoints. Create one with NewRerankModel; it is immutable and safe for concurrent use.

func JinaRerank

func JinaRerank(model string, opts ...Option) *RerankModel

JinaRerank returns a rerank model configured for Jina AI (https://api.jina.ai/v1). API key defaults to the JINA_API_KEY environment variable.

func NewRerankModel

func NewRerankModel(model string, opts ...Option) *RerankModel

NewRerankModel returns a rerank model bound to the given model ID (for example "rerank-v3.5" or "rerank-multilingual-v3.0"). Configuration problems (such as an invalid base URL) surface on the first call, not from NewRerankModel.

func SiliconFlowRerank

func SiliconFlowRerank(model string, opts ...Option) *RerankModel

SiliconFlowRerank returns a rerank model configured for SiliconFlow (https://api.siliconflow.cn/v1). API key defaults to the SILICONFLOW_API_KEY environment variable.

func TogetherRerank

func TogetherRerank(model string, opts ...Option) *RerankModel

TogetherRerank returns a rerank model configured for Together AI (https://api.together.ai/v1). API key defaults to the TOGETHER_API_KEY environment variable.

func (*RerankModel) Capabilities

func (m *RerankModel) Capabilities() ai.Capabilities

Capabilities reports, best effort, what the bound model supports. It is a static hint, never a call gate.

func (*RerankModel) ModelID

func (m *RerankModel) ModelID() string

ModelID implements ai.RerankModel.

func (*RerankModel) Provider

func (m *RerankModel) Provider() ai.Provider

Provider implements ai.RerankModel.

func (*RerankModel) Rerank

Rerank implements ai.RerankModel against the Cohere Rerank API or compatible endpoint.

type RerankOptions

type RerankOptions struct {
	// MaxTokensPerDoc limits the maximum number of tokens to consider per document.
	// Long documents are automatically truncated.
	MaxTokensPerDoc *int
	// RankFields specifies the keys in structured documents to consider for ranking.
	RankFields []string
	// Priority controls request handling priority during system load (0-999).
	Priority *int

	// ExtraFields is merged into the top level of the outgoing request body
	// using bounded, add-only semantics. It is the escape hatch for vendor
	// parameters this adapter does not model.
	ExtraFields map[string]any
}

RerankOptions is the Cohere entry for ai.RerankRequest.ProviderOptions:

req.ProviderOptions = map[ai.Provider]any{
    ai.ProviderCohere: cohere.RerankOptions{
        MaxTokensPerDoc: pips.Int(4096),
    },
}

Jump to

Keyboard shortcuts

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