cohere is a Go client for the Cohere API. It implements the
github.com/goloop/ai interface, so it looks and works like every other goloop
AI provider, and exposes Cohere's native v2 endpoints on top.
- Chat:
Generatefor a single response,Streamfor token-by-token output throughiter.Seq2. - Tool use (function calling), multimodal image input and system prompts.
- Native
ChatCompletionandChatStreamover the v2 chat API. - Embeddings, reranking and model listing.
- Retries on 429 and 5xx with backoff; normalized, typed API errors.
- Depends only on
github.com/goloop/aiand the standard library. - Structured output:
ai.Formatmaps onto the provider'sresponse_format(JSON mode or a schema); read the reply withresp.JSON(&v). - Hosted capabilities:
ai.Request.Hostedis refused withai.ErrNoHosted, because nothing here runs on the provider's own side.
go get github.com/goloop/coherepackage main
import (
"context"
"fmt"
"os"
"github.com/goloop/ai"
"github.com/goloop/cohere"
)
func main() {
c := cohere.New(os.Getenv("COHERE_API_KEY"))
resp, err := c.Generate(context.Background(), &ai.Request{
Model: cohere.ModelCommandA,
Messages: []ai.Message{ai.UserText("Say hello in one word.")},
})
if err != nil {
panic(err)
}
fmt.Println(resp.Text())
}for chunk, err := range c.Stream(ctx, req) {
if err != nil {
break
}
fmt.Print(chunk.Text)
}docs, err := c.Embed(ctx, cohere.ModelEmbedV3, "hello", "world")
// for a search query, pick the input type explicitly:
q, err := c.Embeddings(ctx, &cohere.EmbedRequest{
Model: cohere.ModelEmbedV3, Texts: []string{"hello"}, InputType: "search_query",
})Rerank scores each document against the query and returns them ordered most
relevant first. Each result's Index points back into the request documents.
res, err := c.Rerank(ctx, &cohere.RerankRequest{
Model: cohere.ModelRerankV35,
Query: "What is the capital of Ukraine?",
Documents: []string{"Kyiv is the capital.", "Bananas are yellow."},
TopN: 1,
})
// res[0].Index == 0, res[0].RelevanceScore near 1.0Full reference: DOC.md (Ukrainian: DOC.UK.md).
See CONTRIBUTING.md.
MIT - see LICENSE.
Keep embedding requests unchanged, reject nil requests, and report malformed stream events.