Skip to content

Commit 4ab6c99

Browse files
committed
Release v2.5.0
1 parent 9e0426b commit 4ab6c99

File tree

11 files changed

+2042
-487
lines changed

11 files changed

+2042
-487
lines changed

client/client.go

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
v2 "github.com/cohere-ai/cohere-go/v2"
1111
connectors "github.com/cohere-ai/cohere-go/v2/connectors"
1212
core "github.com/cohere-ai/cohere-go/v2/core"
13+
datasets "github.com/cohere-ai/cohere-go/v2/datasets"
14+
embedjobs "github.com/cohere-ai/cohere-go/v2/embedjobs"
1315
io "io"
1416
http "net/http"
1517
)
@@ -19,7 +21,9 @@ type Client struct {
1921
caller *core.Caller
2022
header http.Header
2123

24+
Datasets *datasets.Client
2225
Connectors *connectors.Client
26+
EmbedJobs *embedjobs.Client
2327
}
2428

2529
func NewClient(opts ...core.ClientOption) *Client {
@@ -31,14 +35,17 @@ func NewClient(opts ...core.ClientOption) *Client {
3135
baseURL: options.BaseURL,
3236
caller: core.NewCaller(options.HTTPClient),
3337
header: options.ToHeader(),
38+
Datasets: datasets.NewClient(opts...),
3439
Connectors: connectors.NewClient(opts...),
40+
EmbedJobs: embedjobs.NewClient(opts...),
3541
}
3642
}
3743

3844
// The `chat` endpoint allows users to have conversations with a Large Language Model (LLM) from Cohere. Users can send messages as part of a persisted conversation using the `conversation_id` parameter, or they can pass in their own conversation history using the `chat_history` parameter.
39-
// The endpoint features additional parameters such as [connectors](https://docs.cohere.com/docs/connectors) and `documents` that enable conversations enriched by external knowledge. We call this "Retrieval Augmented Generation", or "RAG".
45+
//
46+
// The endpoint features additional parameters such as [connectors](https://docs.cohere.com/docs/connectors) and `documents` that enable conversations enriched by external knowledge. We call this ["Retrieval Augmented Generation"](https://docs.cohere.com/docs/retrieval-augmented-generation-rag), or "RAG". For a full breakdown of the Chat API endpoint, document and connector modes, and streaming (with code samples), see [this guide](https://docs.cohere.com/docs/cochat-beta).
4047
func (c *Client) ChatStream(ctx context.Context, request *v2.ChatStreamRequest) (*core.Stream[v2.StreamedChatResponse], error) {
41-
baseURL := "https://api.cohere.ai"
48+
baseURL := "https://api.cohere.ai/v1"
4249
if c.baseURL != "" {
4350
baseURL = c.baseURL
4451
}
@@ -57,9 +64,10 @@ func (c *Client) ChatStream(ctx context.Context, request *v2.ChatStreamRequest)
5764
}
5865

5966
// The `chat` endpoint allows users to have conversations with a Large Language Model (LLM) from Cohere. Users can send messages as part of a persisted conversation using the `conversation_id` parameter, or they can pass in their own conversation history using the `chat_history` parameter.
60-
// The endpoint features additional parameters such as [connectors](https://docs.cohere.com/docs/connectors) and `documents` that enable conversations enriched by external knowledge. We call this "Retrieval Augmented Generation", or "RAG".
67+
//
68+
// The endpoint features additional parameters such as [connectors](https://docs.cohere.com/docs/connectors) and `documents` that enable conversations enriched by external knowledge. We call this ["Retrieval Augmented Generation"](https://docs.cohere.com/docs/retrieval-augmented-generation-rag), or "RAG". For a full breakdown of the Chat API endpoint, document and connector modes, and streaming (with code samples), see [this guide](https://docs.cohere.com/docs/cochat-beta).
6169
func (c *Client) Chat(ctx context.Context, request *v2.ChatRequest) (*v2.NonStreamedChatResponse, error) {
62-
baseURL := "https://api.cohere.ai"
70+
baseURL := "https://api.cohere.ai/v1"
6371
if c.baseURL != "" {
6472
baseURL = c.baseURL
6573
}
@@ -81,9 +89,56 @@ func (c *Client) Chat(ctx context.Context, request *v2.ChatRequest) (*v2.NonStre
8189
return response, nil
8290
}
8391

92+
// This endpoint generates realistic text conditioned on a given input.
93+
func (c *Client) GenerateStream(ctx context.Context, request *v2.GenerateStreamRequest) (*core.Stream[v2.GenerateStreamedResponse], error) {
94+
baseURL := "https://api.cohere.ai/v1"
95+
if c.baseURL != "" {
96+
baseURL = c.baseURL
97+
}
98+
endpointURL := baseURL + "/" + "v1/generate"
99+
100+
errorDecoder := func(statusCode int, body io.Reader) error {
101+
raw, err := io.ReadAll(body)
102+
if err != nil {
103+
return err
104+
}
105+
apiError := core.NewAPIError(statusCode, errors.New(string(raw)))
106+
decoder := json.NewDecoder(bytes.NewReader(raw))
107+
switch statusCode {
108+
case 400:
109+
value := new(v2.BadRequestError)
110+
value.APIError = apiError
111+
if err := decoder.Decode(value); err != nil {
112+
return apiError
113+
}
114+
return value
115+
case 500:
116+
value := new(v2.InternalServerError)
117+
value.APIError = apiError
118+
if err := decoder.Decode(value); err != nil {
119+
return apiError
120+
}
121+
return value
122+
}
123+
return apiError
124+
}
125+
126+
streamer := core.NewStreamer[v2.GenerateStreamedResponse](c.caller)
127+
return streamer.Stream(
128+
ctx,
129+
&core.StreamParams{
130+
URL: endpointURL,
131+
Method: http.MethodPost,
132+
Headers: c.header,
133+
Request: request,
134+
ErrorDecoder: errorDecoder,
135+
},
136+
)
137+
}
138+
84139
// This endpoint generates realistic text conditioned on a given input.
85140
func (c *Client) Generate(ctx context.Context, request *v2.GenerateRequest) (*v2.Generation, error) {
86-
baseURL := "https://api.cohere.ai"
141+
baseURL := "https://api.cohere.ai/v1"
87142
if c.baseURL != "" {
88143
baseURL = c.baseURL
89144
}
@@ -138,7 +193,7 @@ func (c *Client) Generate(ctx context.Context, request *v2.GenerateRequest) (*v2
138193
//
139194
// If you want to learn more how to use the embedding model, have a look at the [Semantic Search Guide](/docs/semantic-search).
140195
func (c *Client) Embed(ctx context.Context, request *v2.EmbedRequest) (*v2.EmbedResponse, error) {
141-
baseURL := "https://api.cohere.ai"
196+
baseURL := "https://api.cohere.ai/v1"
142197
if c.baseURL != "" {
143198
baseURL = c.baseURL
144199
}
@@ -189,7 +244,7 @@ func (c *Client) Embed(ctx context.Context, request *v2.EmbedRequest) (*v2.Embed
189244

190245
// This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
191246
func (c *Client) Rerank(ctx context.Context, request *v2.RerankRequest) (*v2.RerankResponse, error) {
192-
baseURL := "https://api.cohere.ai"
247+
baseURL := "https://api.cohere.ai/v1"
193248
if c.baseURL != "" {
194249
baseURL = c.baseURL
195250
}
@@ -212,9 +267,9 @@ func (c *Client) Rerank(ctx context.Context, request *v2.RerankRequest) (*v2.Rer
212267
}
213268

214269
// This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided `examples` of text + label pairs as a reference.
215-
// Note: [Custom Models](/training-representation-models) trained on classification examples don't require the `examples` parameter to be passed in explicitly.
270+
// Note: [Fine-tuned models](https://docs.cohere.com/docs/classify-fine-tuning) trained on classification examples don't require the `examples` parameter to be passed in explicitly.
216271
func (c *Client) Classify(ctx context.Context, request *v2.ClassifyRequest) (*v2.ClassifyResponse, error) {
217-
baseURL := "https://api.cohere.ai"
272+
baseURL := "https://api.cohere.ai/v1"
218273
if c.baseURL != "" {
219274
baseURL = c.baseURL
220275
}
@@ -265,7 +320,7 @@ func (c *Client) Classify(ctx context.Context, request *v2.ClassifyRequest) (*v2
265320

266321
// This endpoint identifies which language each of the provided texts is written in.
267322
func (c *Client) DetectLanguage(ctx context.Context, request *v2.DetectLanguageRequest) (*v2.DetectLanguageResponse, error) {
268-
baseURL := "https://api.cohere.ai"
323+
baseURL := "https://api.cohere.ai/v1"
269324
if c.baseURL != "" {
270325
baseURL = c.baseURL
271326
}
@@ -289,7 +344,7 @@ func (c *Client) DetectLanguage(ctx context.Context, request *v2.DetectLanguageR
289344

290345
// This endpoint generates a summary in English for a given text.
291346
func (c *Client) Summarize(ctx context.Context, request *v2.SummarizeRequest) (*v2.SummarizeResponse, error) {
292-
baseURL := "https://api.cohere.ai"
347+
baseURL := "https://api.cohere.ai/v1"
293348
if c.baseURL != "" {
294349
baseURL = c.baseURL
295350
}
@@ -313,7 +368,7 @@ func (c *Client) Summarize(ctx context.Context, request *v2.SummarizeRequest) (*
313368

314369
// This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
315370
func (c *Client) Tokenize(ctx context.Context, request *v2.TokenizeRequest) (*v2.TokenizeResponse, error) {
316-
baseURL := "https://api.cohere.ai"
371+
baseURL := "https://api.cohere.ai/v1"
317372
if c.baseURL != "" {
318373
baseURL = c.baseURL
319374
}
@@ -364,7 +419,7 @@ func (c *Client) Tokenize(ctx context.Context, request *v2.TokenizeRequest) (*v2
364419

365420
// This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
366421
func (c *Client) Detokenize(ctx context.Context, request *v2.DetokenizeRequest) (*v2.DetokenizeResponse, error) {
367-
baseURL := "https://api.cohere.ai"
422+
baseURL := "https://api.cohere.ai/v1"
368423
if c.baseURL != "" {
369424
baseURL = c.baseURL
370425
}

client/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ func WithToken(token string) core.ClientOption {
3737
opts.Token = token
3838
}
3939
}
40+
41+
// WithClientName sets the clientName header on every request.
42+
//
43+
// The name of the project that is making the request.
44+
func WithClientName(clientName *string) core.ClientOption {
45+
return func(opts *core.ClientOptions) {
46+
opts.ClientName = clientName
47+
}
48+
}

0 commit comments

Comments
 (0)