chub-dev 0.1.0 → 0.1.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,1335 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: llm
|
|
3
|
+
description: "Cohere API JavaScript/TypeScript SDK coding guide for LLM, embeddings, and rerank"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "javascript"
|
|
6
|
+
versions: "7.19.0"
|
|
7
|
+
updated-on: "2026-03-02"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "cohere,llm,ai,embeddings,rerank"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Cohere API JavaScript/TypeScript SDK Coding Guide
|
|
13
|
+
|
|
14
|
+
## 1. Golden Rule
|
|
15
|
+
|
|
16
|
+
**Always use the official Cohere JavaScript SDK package:** `cohere-ai` (GitHub: https://github.com/cohere-ai/cohere-typescript)
|
|
17
|
+
|
|
18
|
+
**Never use deprecated or unofficial libraries.** The `cohere-ai` npm package is the only officially supported Cohere SDK for JavaScript/TypeScript maintained by Cohere. Avoid `cohere-js` (unofficial, different product), `cohere-api-web` (legacy/unofficial), and any other variants.
|
|
19
|
+
|
|
20
|
+
**Always use `CohereClientV2` for accessing the latest v2 API with current models like `command-a-03-2025`.**
|
|
21
|
+
|
|
22
|
+
## 2. Installation
|
|
23
|
+
|
|
24
|
+
### npm
|
|
25
|
+
```bash
|
|
26
|
+
npm install cohere-ai
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### yarn
|
|
30
|
+
```bash
|
|
31
|
+
yarn add cohere-ai
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### pnpm
|
|
35
|
+
```bash
|
|
36
|
+
pnpm add cohere-ai
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Environment Variables:**
|
|
40
|
+
```bash
|
|
41
|
+
COHERE_API_KEY=your_api_key_here
|
|
42
|
+
# Alternative:
|
|
43
|
+
CO_API_KEY=your_api_key_here
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The SDK automatically reads from either `COHERE_API_KEY` or `CO_API_KEY` environment variables.
|
|
47
|
+
|
|
48
|
+
## 3. Initialization
|
|
49
|
+
|
|
50
|
+
### Basic Client Initialization
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
54
|
+
|
|
55
|
+
// Uses COHERE_API_KEY or CO_API_KEY environment variable
|
|
56
|
+
const cohere = new CohereClientV2({});
|
|
57
|
+
|
|
58
|
+
// Or pass API key directly
|
|
59
|
+
const cohere = new CohereClientV2({
|
|
60
|
+
token: "your-api-key"
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Authentication Methods
|
|
65
|
+
|
|
66
|
+
The Cohere API uses Bearer token authentication. The SDK handles this automatically when you provide the API key via the constructor or environment variables.
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
70
|
+
|
|
71
|
+
// Best practice: use environment variables
|
|
72
|
+
const cohere = new CohereClientV2({
|
|
73
|
+
token: process.env.COHERE_API_KEY
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Important:** Never hardcode API keys in your source code. Always use environment variables or secure configuration management.
|
|
78
|
+
|
|
79
|
+
## 4. Core API Surfaces
|
|
80
|
+
|
|
81
|
+
### Chat API
|
|
82
|
+
|
|
83
|
+
The Chat API enables conversational AI, text generation, and summarization with Cohere's Command models.
|
|
84
|
+
|
|
85
|
+
**Minimal Example:**
|
|
86
|
+
```javascript
|
|
87
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
88
|
+
|
|
89
|
+
const cohere = new CohereClientV2({});
|
|
90
|
+
|
|
91
|
+
const response = await cohere.chat({
|
|
92
|
+
model: "command-a-03-2025",
|
|
93
|
+
messages: [
|
|
94
|
+
{ role: "user", content: "Explain quantum computing in simple terms" }
|
|
95
|
+
]
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
console.log(response.message.content[0].text);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Advanced Example with Configuration:**
|
|
102
|
+
```javascript
|
|
103
|
+
const response = await cohere.chat({
|
|
104
|
+
model: "command-a-03-2025",
|
|
105
|
+
messages: [
|
|
106
|
+
{
|
|
107
|
+
role: "system",
|
|
108
|
+
content: "You are a helpful AI assistant specialized in science education."
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
role: "user",
|
|
112
|
+
content: "Explain quantum computing"
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
temperature: 0.3,
|
|
116
|
+
maxTokens: 500,
|
|
117
|
+
k: 0,
|
|
118
|
+
p: 0.75,
|
|
119
|
+
stopSequences: ["--END--"],
|
|
120
|
+
frequencyPenalty: 0.0,
|
|
121
|
+
presencePenalty: 0.0,
|
|
122
|
+
safetyMode: "CONTEXTUAL"
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(response.message.content[0].text);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Multi-turn Conversation:**
|
|
129
|
+
```javascript
|
|
130
|
+
const messages = [
|
|
131
|
+
{ role: "user", content: "What is machine learning?" },
|
|
132
|
+
{ role: "assistant", content: "Machine learning is a subset of AI..." },
|
|
133
|
+
{ role: "user", content: "Can you give me an example?" }
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
const response = await cohere.chat({
|
|
137
|
+
model: "command-a-03-2025",
|
|
138
|
+
messages: messages
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Chat Streaming API
|
|
143
|
+
|
|
144
|
+
Stream responses in real-time to reduce perceived latency.
|
|
145
|
+
|
|
146
|
+
**Minimal Example:**
|
|
147
|
+
```javascript
|
|
148
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
149
|
+
|
|
150
|
+
const cohere = new CohereClientV2({});
|
|
151
|
+
|
|
152
|
+
const stream = await cohere.chatStream({
|
|
153
|
+
model: "command-a-03-2025",
|
|
154
|
+
messages: [
|
|
155
|
+
{ role: "user", content: "Write a short story about a robot" }
|
|
156
|
+
]
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
for await (const chatEvent of stream) {
|
|
160
|
+
if (chatEvent.type === "content-delta") {
|
|
161
|
+
process.stdout.write(chatEvent.delta?.message?.content?.text || "");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
console.log(); // final newline
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Advanced Streaming with Event Handling:**
|
|
168
|
+
```javascript
|
|
169
|
+
const stream = await cohere.chatStream({
|
|
170
|
+
model: "command-a-03-2025",
|
|
171
|
+
messages: [{ role: "user", content: "Explain photosynthesis" }],
|
|
172
|
+
temperature: 0.5
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
for await (const event of stream) {
|
|
176
|
+
switch (event.type) {
|
|
177
|
+
case "message-start":
|
|
178
|
+
console.log("Stream started");
|
|
179
|
+
break;
|
|
180
|
+
case "content-start":
|
|
181
|
+
console.log("Content generation began");
|
|
182
|
+
break;
|
|
183
|
+
case "content-delta":
|
|
184
|
+
process.stdout.write(event.delta?.message?.content?.text || "");
|
|
185
|
+
break;
|
|
186
|
+
case "content-end":
|
|
187
|
+
console.log("\nContent generation complete");
|
|
188
|
+
break;
|
|
189
|
+
case "message-end":
|
|
190
|
+
console.log("Stream ended");
|
|
191
|
+
console.log(`Tokens used: ${event.delta?.usage?.tokens}`);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Embed API
|
|
198
|
+
|
|
199
|
+
Generate vector embeddings for text data to enable semantic search, clustering, and classification.
|
|
200
|
+
|
|
201
|
+
**Minimal Example:**
|
|
202
|
+
```javascript
|
|
203
|
+
const embedResponse = await cohere.embed({
|
|
204
|
+
model: "embed-english-v3.0",
|
|
205
|
+
texts: ["Hello world", "Cohere embeddings are powerful"],
|
|
206
|
+
inputType: "search_document"
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
console.log(embedResponse.embeddings);
|
|
210
|
+
// Returns array of embedding vectors
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**Advanced Example with Classification Input Type:**
|
|
214
|
+
```javascript
|
|
215
|
+
const embedResponse = await cohere.embed({
|
|
216
|
+
model: "embed-multilingual-v3.0",
|
|
217
|
+
texts: [
|
|
218
|
+
"This product is amazing!",
|
|
219
|
+
"Terrible customer service",
|
|
220
|
+
"Neutral statement"
|
|
221
|
+
],
|
|
222
|
+
inputType: "classification",
|
|
223
|
+
embeddingTypes: ["float"]
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
console.log(`Generated ${embedResponse.embeddings.length} embeddings`);
|
|
227
|
+
console.log(`Embedding dimension: ${embedResponse.embeddings[0].length}`);
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
**Available Input Types:** `"search_document"` (for embeddings stored in vector databases), `"search_query"` (for search queries run against vector databases), `"classification"` (for embeddings passed through text classifiers), and `"clustering"` (for embeddings used in clustering algorithms). The `inputType` parameter is **required** for embed-v3 models and later.
|
|
231
|
+
|
|
232
|
+
**Multilingual Embeddings:**
|
|
233
|
+
```javascript
|
|
234
|
+
const multilingualEmbed = await cohere.embed({
|
|
235
|
+
model: "embed-multilingual-v3.0",
|
|
236
|
+
texts: [
|
|
237
|
+
"Hello, how are you?",
|
|
238
|
+
"Bonjour, comment allez-vous?",
|
|
239
|
+
"Hola, ¿cómo estás?",
|
|
240
|
+
"こんにちは、お元気ですか?"
|
|
241
|
+
],
|
|
242
|
+
inputType: "search_document"
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
The `embed-multilingual-v3.0` model supports 100+ languages.
|
|
247
|
+
|
|
248
|
+
### Rerank API
|
|
249
|
+
|
|
250
|
+
Rerank search results by semantic relevance to improve retrieval quality.
|
|
251
|
+
|
|
252
|
+
**Minimal Example:**
|
|
253
|
+
```javascript
|
|
254
|
+
const rerankResponse = await cohere.rerank({
|
|
255
|
+
model: "rerank-english-v3.0",
|
|
256
|
+
query: "What is machine learning?",
|
|
257
|
+
documents: [
|
|
258
|
+
"Machine learning is a subset of artificial intelligence.",
|
|
259
|
+
"Python is a programming language.",
|
|
260
|
+
"Neural networks are computational models inspired by the brain.",
|
|
261
|
+
"JavaScript is used for web development."
|
|
262
|
+
]
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// Results ordered by relevance score
|
|
266
|
+
rerankResponse.results.forEach((result, idx) => {
|
|
267
|
+
console.log(`${idx + 1}. Score: ${result.relevanceScore}`);
|
|
268
|
+
console.log(` Document: ${result.document.text}\n`);
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**Advanced Example with Top-N and Metadata:**
|
|
273
|
+
```javascript
|
|
274
|
+
const documents = [
|
|
275
|
+
{ text: "Machine learning algorithms learn from data", id: "doc1" },
|
|
276
|
+
{ text: "Supervised learning uses labeled data", id: "doc2" },
|
|
277
|
+
{ text: "Deep learning uses neural networks", id: "doc3" },
|
|
278
|
+
{ text: "Cooking recipes for dinner", id: "doc4" }
|
|
279
|
+
];
|
|
280
|
+
|
|
281
|
+
const rerankResponse = await cohere.rerank({
|
|
282
|
+
model: "rerank-english-v3.0",
|
|
283
|
+
query: "What are machine learning techniques?",
|
|
284
|
+
documents: documents,
|
|
285
|
+
topN: 3,
|
|
286
|
+
returnDocuments: true
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
console.log("Top ranked documents:");
|
|
290
|
+
rerankResponse.results.forEach((result) => {
|
|
291
|
+
console.log(`Document ID: ${result.document.id}`);
|
|
292
|
+
console.log(`Relevance: ${result.relevanceScore}`);
|
|
293
|
+
console.log(`Text: ${result.document.text}\n`);
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**Rerank with Raw Strings:**
|
|
298
|
+
```javascript
|
|
299
|
+
const query = "artificial intelligence applications";
|
|
300
|
+
const docs = [
|
|
301
|
+
"AI is used in healthcare for diagnosis",
|
|
302
|
+
"Machine learning powers recommendation systems",
|
|
303
|
+
"The weather is sunny today",
|
|
304
|
+
"Natural language processing enables chatbots"
|
|
305
|
+
];
|
|
306
|
+
|
|
307
|
+
const result = await cohere.rerank({
|
|
308
|
+
model: "rerank-english-v3.0",
|
|
309
|
+
query: query,
|
|
310
|
+
documents: docs,
|
|
311
|
+
topN: 2
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Classify API
|
|
316
|
+
|
|
317
|
+
Classify text into predefined categories using few-shot learning.
|
|
318
|
+
|
|
319
|
+
**Minimal Example:**
|
|
320
|
+
```javascript
|
|
321
|
+
const classifyResponse = await cohere.classify({
|
|
322
|
+
model: "embed-english-v3.0",
|
|
323
|
+
inputs: [
|
|
324
|
+
"This movie was fantastic!",
|
|
325
|
+
"Worst experience ever.",
|
|
326
|
+
"It was okay, nothing special."
|
|
327
|
+
],
|
|
328
|
+
examples: [
|
|
329
|
+
{ text: "I loved this!", label: "positive" },
|
|
330
|
+
{ text: "Amazing product", label: "positive" },
|
|
331
|
+
{ text: "Terrible quality", label: "negative" },
|
|
332
|
+
{ text: "Very disappointed", label: "negative" },
|
|
333
|
+
{ text: "It's fine", label: "neutral" },
|
|
334
|
+
{ text: "Average performance", label: "neutral" }
|
|
335
|
+
]
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
classifyResponse.classifications.forEach((classification) => {
|
|
339
|
+
console.log(`Text: "${classification.input}"`);
|
|
340
|
+
console.log(`Prediction: ${classification.prediction}`);
|
|
341
|
+
console.log(`Confidence: ${classification.confidence}\n`);
|
|
342
|
+
});
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Advanced Classification with Multiple Categories:**
|
|
346
|
+
```javascript
|
|
347
|
+
const trainingExamples = [
|
|
348
|
+
{ text: "Fix login bug", label: "bug" },
|
|
349
|
+
{ text: "Login page not working", label: "bug" },
|
|
350
|
+
{ text: "Add dark mode feature", label: "feature" },
|
|
351
|
+
{ text: "Implement user profiles", label: "feature" },
|
|
352
|
+
{ text: "Update documentation", label: "documentation" },
|
|
353
|
+
{ text: "Improve README", label: "documentation" },
|
|
354
|
+
{ text: "How do I install this?", label: "question" },
|
|
355
|
+
{ text: "Where is the config file?", label: "question" }
|
|
356
|
+
];
|
|
357
|
+
|
|
358
|
+
const classifyResponse = await cohere.classify({
|
|
359
|
+
model: "embed-english-v3.0",
|
|
360
|
+
inputs: [
|
|
361
|
+
"Cannot reset my password",
|
|
362
|
+
"Need help with setup",
|
|
363
|
+
"Add support for webhooks"
|
|
364
|
+
],
|
|
365
|
+
examples: trainingExamples
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
classifyResponse.classifications.forEach((item) => {
|
|
369
|
+
console.log(`"${item.input}" → ${item.prediction}`);
|
|
370
|
+
|
|
371
|
+
// Show confidence for all labels
|
|
372
|
+
item.labels.forEach((labelConf) => {
|
|
373
|
+
console.log(` ${labelConf.labelName}: ${labelConf.confidence}`);
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
**Requirements:** Minimum 2 examples per label, maximum 2500 examples total, and can classify up to 96 texts in a single request.
|
|
379
|
+
|
|
380
|
+
## 5. Advanced Features
|
|
381
|
+
|
|
382
|
+
### RAG (Retrieval Augmented Generation)
|
|
383
|
+
|
|
384
|
+
Combine Chat, Embed, and Rerank for production-grade RAG systems.
|
|
385
|
+
|
|
386
|
+
**Complete RAG Example:**
|
|
387
|
+
```javascript
|
|
388
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
389
|
+
|
|
390
|
+
const cohere = new CohereClientV2({});
|
|
391
|
+
|
|
392
|
+
async function ragPipeline(query, documents) {
|
|
393
|
+
// Step 1: Embed documents
|
|
394
|
+
const embedResponse = await cohere.embed({
|
|
395
|
+
model: "embed-english-v3.0",
|
|
396
|
+
texts: documents,
|
|
397
|
+
inputType: "search_document"
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// Step 2: Embed query
|
|
401
|
+
const queryEmbedResponse = await cohere.embed({
|
|
402
|
+
model: "embed-english-v3.0",
|
|
403
|
+
texts: [query],
|
|
404
|
+
inputType: "search_query"
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Step 3: Find top candidates using similarity (simplified)
|
|
408
|
+
const topDocs = documents.slice(0, 10); // In production, use vector DB
|
|
409
|
+
|
|
410
|
+
// Step 4: Rerank for precision
|
|
411
|
+
const rerankResponse = await cohere.rerank({
|
|
412
|
+
model: "rerank-english-v3.0",
|
|
413
|
+
query: query,
|
|
414
|
+
documents: topDocs,
|
|
415
|
+
topN: 3
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// Step 5: Generate answer with context
|
|
419
|
+
const context = rerankResponse.results
|
|
420
|
+
.map(r => r.document.text)
|
|
421
|
+
.join("\n\n");
|
|
422
|
+
|
|
423
|
+
const chatResponse = await cohere.chat({
|
|
424
|
+
model: "command-a-03-2025",
|
|
425
|
+
messages: [
|
|
426
|
+
{
|
|
427
|
+
role: "system",
|
|
428
|
+
content: `Answer the question based on this context:\n\n${context}`
|
|
429
|
+
},
|
|
430
|
+
{ role: "user", content: query }
|
|
431
|
+
]
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
return {
|
|
435
|
+
answer: chatResponse.message.content[0].text,
|
|
436
|
+
sources: rerankResponse.results
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Usage
|
|
441
|
+
const docs = [
|
|
442
|
+
"Machine learning is a subset of AI that enables systems to learn from data.",
|
|
443
|
+
"Deep learning uses neural networks with multiple layers.",
|
|
444
|
+
"Supervised learning requires labeled training data.",
|
|
445
|
+
// ... more documents
|
|
446
|
+
];
|
|
447
|
+
|
|
448
|
+
const result = await ragPipeline("What is machine learning?", docs);
|
|
449
|
+
console.log(result.answer);
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### Tool Use (Function Calling)
|
|
453
|
+
|
|
454
|
+
Enable the model to call external functions and APIs.
|
|
455
|
+
|
|
456
|
+
**Minimal Example:**
|
|
457
|
+
```javascript
|
|
458
|
+
const tools = [
|
|
459
|
+
{
|
|
460
|
+
type: "function",
|
|
461
|
+
function: {
|
|
462
|
+
name: "get_weather",
|
|
463
|
+
description: "Get current weather for a location",
|
|
464
|
+
parameters: {
|
|
465
|
+
type: "object",
|
|
466
|
+
properties: {
|
|
467
|
+
location: {
|
|
468
|
+
type: "string",
|
|
469
|
+
description: "City name"
|
|
470
|
+
},
|
|
471
|
+
unit: {
|
|
472
|
+
type: "string",
|
|
473
|
+
enum: ["celsius", "fahrenheit"]
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
required: ["location"]
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
];
|
|
481
|
+
|
|
482
|
+
const response = await cohere.chat({
|
|
483
|
+
model: "command-a-03-2025",
|
|
484
|
+
messages: [
|
|
485
|
+
{ role: "user", content: "What's the weather in San Francisco?" }
|
|
486
|
+
],
|
|
487
|
+
tools: tools
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
if (response.message.toolCalls) {
|
|
491
|
+
console.log("Function to call:", response.message.toolCalls[0].function.name);
|
|
492
|
+
console.log("Arguments:", response.message.toolCalls[0].function.arguments);
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
**Advanced Multi-Step Tool Use:**
|
|
497
|
+
```javascript
|
|
498
|
+
const tools = [
|
|
499
|
+
{
|
|
500
|
+
type: "function",
|
|
501
|
+
function: {
|
|
502
|
+
name: "search_database",
|
|
503
|
+
description: "Search internal knowledge base",
|
|
504
|
+
parameters: {
|
|
505
|
+
type: "object",
|
|
506
|
+
properties: {
|
|
507
|
+
query: { type: "string" },
|
|
508
|
+
limit: { type: "integer", default: 5 }
|
|
509
|
+
},
|
|
510
|
+
required: ["query"]
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
type: "function",
|
|
516
|
+
function: {
|
|
517
|
+
name: "calculate",
|
|
518
|
+
description: "Perform mathematical calculations",
|
|
519
|
+
parameters: {
|
|
520
|
+
type: "object",
|
|
521
|
+
properties: {
|
|
522
|
+
expression: { type: "string" }
|
|
523
|
+
},
|
|
524
|
+
required: ["expression"]
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
];
|
|
529
|
+
|
|
530
|
+
// Initial request
|
|
531
|
+
let messages = [
|
|
532
|
+
{ role: "user", content: "Search for quantum computing papers and calculate citations" }
|
|
533
|
+
];
|
|
534
|
+
|
|
535
|
+
const firstResponse = await cohere.chat({
|
|
536
|
+
model: "command-a-03-2025",
|
|
537
|
+
messages: messages,
|
|
538
|
+
tools: tools
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// Execute tool calls
|
|
542
|
+
if (firstResponse.message.toolCalls) {
|
|
543
|
+
for (const toolCall of firstResponse.message.toolCalls) {
|
|
544
|
+
const functionName = toolCall.function.name;
|
|
545
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
546
|
+
|
|
547
|
+
// Execute function (mock example)
|
|
548
|
+
let result;
|
|
549
|
+
if (functionName === "search_database") {
|
|
550
|
+
result = { papers: ["Paper A", "Paper B"], citations: [42, 38] };
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// Add tool result to conversation
|
|
554
|
+
messages.push({
|
|
555
|
+
role: "tool",
|
|
556
|
+
content: JSON.stringify(result),
|
|
557
|
+
toolCallId: toolCall.id
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// Get final response
|
|
562
|
+
const finalResponse = await cohere.chat({
|
|
563
|
+
model: "command-a-03-2025",
|
|
564
|
+
messages: messages,
|
|
565
|
+
tools: tools
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
console.log(finalResponse.message.content[0].text);
|
|
569
|
+
}
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
### Web Search Connector
|
|
573
|
+
|
|
574
|
+
Use Cohere's built-in web search to ground responses in current information.
|
|
575
|
+
|
|
576
|
+
**Example:**
|
|
577
|
+
```javascript
|
|
578
|
+
const response = await cohere.chat({
|
|
579
|
+
model: "command-a-03-2025",
|
|
580
|
+
messages: [
|
|
581
|
+
{ role: "user", content: "What are the latest developments in fusion energy?" }
|
|
582
|
+
],
|
|
583
|
+
connectors: [{ id: "web-search" }]
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
console.log(response.message.content[0].text);
|
|
587
|
+
|
|
588
|
+
// Access citations
|
|
589
|
+
if (response.message.citations) {
|
|
590
|
+
console.log("\nSources:");
|
|
591
|
+
response.message.citations.forEach((citation) => {
|
|
592
|
+
console.log(`- ${citation.sources[0]}`);
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
### Citations
|
|
598
|
+
|
|
599
|
+
Enable inline citations for transparency and verification.
|
|
600
|
+
|
|
601
|
+
**Example:**
|
|
602
|
+
```javascript
|
|
603
|
+
const documents = [
|
|
604
|
+
{ id: "doc1", text: "AI was founded as an academic discipline in 1956." },
|
|
605
|
+
{ id: "doc2", text: "Machine learning is a subset of AI." },
|
|
606
|
+
{ id: "doc3", text: "Deep learning breakthrough occurred in 2012." }
|
|
607
|
+
];
|
|
608
|
+
|
|
609
|
+
const response = await cohere.chat({
|
|
610
|
+
model: "command-a-03-2025",
|
|
611
|
+
messages: [
|
|
612
|
+
{ role: "user", content: "When was AI founded?" }
|
|
613
|
+
],
|
|
614
|
+
documents: documents
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
console.log(response.message.content[0].text);
|
|
618
|
+
|
|
619
|
+
// Access citations
|
|
620
|
+
if (response.message.citations) {
|
|
621
|
+
response.message.citations.forEach((citation) => {
|
|
622
|
+
console.log(`\nCitation: "${citation.text}"`);
|
|
623
|
+
console.log(`Source: Document ${citation.documentIds.join(", ")}`);
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
## 6. TypeScript Support
|
|
629
|
+
|
|
630
|
+
The SDK is written in TypeScript and provides full type definitions.
|
|
631
|
+
|
|
632
|
+
### Type Imports
|
|
633
|
+
|
|
634
|
+
```typescript
|
|
635
|
+
import {
|
|
636
|
+
CohereClientV2,
|
|
637
|
+
ChatRequest,
|
|
638
|
+
ChatResponse,
|
|
639
|
+
ChatStreamRequest,
|
|
640
|
+
EmbedRequest,
|
|
641
|
+
EmbedResponse,
|
|
642
|
+
RerankRequest,
|
|
643
|
+
RerankResponse,
|
|
644
|
+
CohereError,
|
|
645
|
+
CohereTimeoutError
|
|
646
|
+
} from "cohere-ai";
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
### Type-Safe Chat
|
|
650
|
+
|
|
651
|
+
```typescript
|
|
652
|
+
import { CohereClientV2, ChatRequest } from "cohere-ai";
|
|
653
|
+
|
|
654
|
+
const cohere = new CohereClientV2({ token: process.env.COHERE_API_KEY });
|
|
655
|
+
|
|
656
|
+
const request: ChatRequest = {
|
|
657
|
+
model: "command-a-03-2025",
|
|
658
|
+
messages: [
|
|
659
|
+
{ role: "user", content: "Explain TypeScript benefits" }
|
|
660
|
+
],
|
|
661
|
+
temperature: 0.3,
|
|
662
|
+
maxTokens: 1000
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const response = await cohere.chat(request);
|
|
666
|
+
const text: string = response.message.content[0].text;
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
### Type-Safe Embeddings
|
|
670
|
+
|
|
671
|
+
```typescript
|
|
672
|
+
import { EmbedRequest, EmbedResponse } from "cohere-ai";
|
|
673
|
+
|
|
674
|
+
const embedRequest: EmbedRequest = {
|
|
675
|
+
model: "embed-english-v3.0",
|
|
676
|
+
texts: ["Sample text"],
|
|
677
|
+
inputType: "classification"
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
const embedResponse: EmbedResponse = await cohere.embed(embedRequest);
|
|
681
|
+
const embeddings: number[][] = embedResponse.embeddings;
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
### Custom Types
|
|
685
|
+
|
|
686
|
+
```typescript
|
|
687
|
+
interface DocumentWithMetadata {
|
|
688
|
+
id: string;
|
|
689
|
+
text: string;
|
|
690
|
+
category: string;
|
|
691
|
+
timestamp: Date;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
async function embedDocuments(docs: DocumentWithMetadata[]) {
|
|
695
|
+
const texts = docs.map(d => d.text);
|
|
696
|
+
|
|
697
|
+
const response = await cohere.embed({
|
|
698
|
+
model: "embed-english-v3.0",
|
|
699
|
+
texts: texts,
|
|
700
|
+
inputType: "search_document"
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
return docs.map((doc, idx) => ({
|
|
704
|
+
...doc,
|
|
705
|
+
embedding: response.embeddings[idx]
|
|
706
|
+
}));
|
|
707
|
+
}
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
## 7. Best Practices
|
|
711
|
+
|
|
712
|
+
### Error Handling
|
|
713
|
+
|
|
714
|
+
**Always wrap API calls in try-catch blocks:**
|
|
715
|
+
|
|
716
|
+
```javascript
|
|
717
|
+
import { CohereClientV2, CohereError, CohereTimeoutError } from "cohere-ai";
|
|
718
|
+
|
|
719
|
+
const cohere = new CohereClientV2({});
|
|
720
|
+
|
|
721
|
+
async function generateText(prompt) {
|
|
722
|
+
try {
|
|
723
|
+
const response = await cohere.chat({
|
|
724
|
+
model: "command-a-03-2025",
|
|
725
|
+
messages: [{ role: "user", content: prompt }]
|
|
726
|
+
});
|
|
727
|
+
return response.message.content[0].text;
|
|
728
|
+
} catch (err) {
|
|
729
|
+
if (err instanceof CohereTimeoutError) {
|
|
730
|
+
console.error("Request timed out:", err.message);
|
|
731
|
+
// Retry logic here
|
|
732
|
+
} else if (err instanceof CohereError) {
|
|
733
|
+
console.error(`API Error ${err.statusCode}:`, err.message);
|
|
734
|
+
console.error("Error body:", err.body);
|
|
735
|
+
|
|
736
|
+
// Handle specific error codes
|
|
737
|
+
if (err.statusCode === 429) {
|
|
738
|
+
console.error("Rate limit exceeded");
|
|
739
|
+
// Implement backoff strategy
|
|
740
|
+
} else if (err.statusCode === 401) {
|
|
741
|
+
console.error("Invalid API key");
|
|
742
|
+
} else if (err.statusCode === 402) {
|
|
743
|
+
console.error("Billing limit reached");
|
|
744
|
+
}
|
|
745
|
+
} else {
|
|
746
|
+
console.error("Unexpected error:", err);
|
|
747
|
+
}
|
|
748
|
+
throw err;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
### Timeout Configuration
|
|
754
|
+
|
|
755
|
+
```javascript
|
|
756
|
+
// Set custom timeout (default is 60 seconds)
|
|
757
|
+
const response = await cohere.chat({
|
|
758
|
+
model: "command-a-03-2025",
|
|
759
|
+
messages: [{ role: "user", content: "Hello" }]
|
|
760
|
+
}, {
|
|
761
|
+
timeoutInSeconds: 10
|
|
762
|
+
});
|
|
763
|
+
```
|
|
764
|
+
|
|
765
|
+
### Retry Configuration
|
|
766
|
+
|
|
767
|
+
```javascript
|
|
768
|
+
// Configure retries (default: 2 retries with exponential backoff)
|
|
769
|
+
// 409 Conflict, 429 Rate Limit, and >=500 errors are automatically retried
|
|
770
|
+
|
|
771
|
+
const response = await cohere.chat({
|
|
772
|
+
model: "command-a-03-2025",
|
|
773
|
+
messages: [{ role: "user", content: "Hello" }]
|
|
774
|
+
}, {
|
|
775
|
+
maxRetries: 3
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
// Disable retries
|
|
779
|
+
const responseNoRetry = await cohere.chat({
|
|
780
|
+
model: "command-a-03-2025",
|
|
781
|
+
messages: [{ role: "user", content: "Hello" }]
|
|
782
|
+
}, {
|
|
783
|
+
maxRetries: 0
|
|
784
|
+
});
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
### Rate Limiting
|
|
788
|
+
|
|
789
|
+
**Trial Keys:** Chat 20 calls/minute, Embed 5 calls/minute, all endpoints 1,000 calls/month total.
|
|
790
|
+
|
|
791
|
+
**Production Keys:** Chat 500 requests/minute, Embed (Text) 2,000 requests/minute, Embed (Images) 400 requests/minute, Rerank 1,000 requests/minute, unlimited monthly usage.
|
|
792
|
+
|
|
793
|
+
**Handle rate limits gracefully:**
|
|
794
|
+
|
|
795
|
+
```javascript
|
|
796
|
+
async function chatWithBackoff(messages, retries = 3, delay = 1000) {
|
|
797
|
+
for (let i = 0; i < retries; i++) {
|
|
798
|
+
try {
|
|
799
|
+
return await cohere.chat({
|
|
800
|
+
model: "command-a-03-2025",
|
|
801
|
+
messages: messages
|
|
802
|
+
});
|
|
803
|
+
} catch (err) {
|
|
804
|
+
if (err instanceof CohereError && err.statusCode === 429) {
|
|
805
|
+
if (i < retries - 1) {
|
|
806
|
+
console.log(`Rate limited, retrying in ${delay}ms...`);
|
|
807
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
808
|
+
delay *= 2; // Exponential backoff
|
|
809
|
+
} else {
|
|
810
|
+
throw err;
|
|
811
|
+
}
|
|
812
|
+
} else {
|
|
813
|
+
throw err;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
```
|
|
819
|
+
|
|
820
|
+
### Model Selection
|
|
821
|
+
|
|
822
|
+
**Current Production Models (as of 2025):** `command-a-03-2025` (latest Command A model with 256K context, 111B parameters, highest throughput - 150% faster than Command R+), `command-r-plus-08-2024` (Command R+ with strong reasoning capabilities), `embed-english-v3.0` (English embeddings with 1024 dimensions), `embed-multilingual-v3.0` (multilingual embeddings supporting 100+ languages), `rerank-english-v3.0` (English reranking model), and `rerank-multilingual-v3.0` (multilingual reranking).
|
|
823
|
+
|
|
824
|
+
**Choosing the right model:**
|
|
825
|
+
|
|
826
|
+
```javascript
|
|
827
|
+
// For general chat and high throughput
|
|
828
|
+
const model = "command-a-03-2025";
|
|
829
|
+
|
|
830
|
+
// For embeddings - English only
|
|
831
|
+
const embedModel = "embed-english-v3.0";
|
|
832
|
+
|
|
833
|
+
// For embeddings - Multilingual
|
|
834
|
+
const multilingualEmbedModel = "embed-multilingual-v3.0";
|
|
835
|
+
|
|
836
|
+
// For reranking search results
|
|
837
|
+
const rerankModel = "rerank-english-v3.0";
|
|
838
|
+
```
|
|
839
|
+
|
|
840
|
+
### Temperature Guidelines
|
|
841
|
+
|
|
842
|
+
**Temperature** controls randomness (0.0 to 1.0, default 0.3):
|
|
843
|
+
|
|
844
|
+
```javascript
|
|
845
|
+
// Deterministic, factual responses
|
|
846
|
+
const factualResponse = await cohere.chat({
|
|
847
|
+
model: "command-a-03-2025",
|
|
848
|
+
messages: [{ role: "user", content: "What is 2+2?" }],
|
|
849
|
+
temperature: 0
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
// Balanced (default)
|
|
853
|
+
const balancedResponse = await cohere.chat({
|
|
854
|
+
model: "command-a-03-2025",
|
|
855
|
+
messages: [{ role: "user", content: "Explain photosynthesis" }],
|
|
856
|
+
temperature: 0.3
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
// Creative, varied responses
|
|
860
|
+
const creativeResponse = await cohere.chat({
|
|
861
|
+
model: "command-a-03-2025",
|
|
862
|
+
messages: [{ role: "user", content: "Write a poem" }],
|
|
863
|
+
temperature: 0.8
|
|
864
|
+
});
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
**Warning:** High temperatures (>0.9) can introduce hallucinations and factually incorrect information.
|
|
868
|
+
|
|
869
|
+
### Token Management
|
|
870
|
+
|
|
871
|
+
```javascript
|
|
872
|
+
// Control max output length
|
|
873
|
+
const response = await cohere.chat({
|
|
874
|
+
model: "command-a-03-2025",
|
|
875
|
+
messages: [{ role: "user", content: "Summarize this in 50 words" }],
|
|
876
|
+
maxTokens: 100
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
// Monitor token usage (available in streaming)
|
|
880
|
+
const stream = await cohere.chatStream({
|
|
881
|
+
model: "command-a-03-2025",
|
|
882
|
+
messages: [{ role: "user", content: "Hello" }]
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
for await (const event of stream) {
|
|
886
|
+
if (event.type === "message-end" && event.delta?.usage) {
|
|
887
|
+
console.log(`Input tokens: ${event.delta.usage.inputTokens}`);
|
|
888
|
+
console.log(`Output tokens: ${event.delta.usage.outputTokens}`);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
### Security Best Practices
|
|
894
|
+
|
|
895
|
+
**API Key Management:**
|
|
896
|
+
|
|
897
|
+
```javascript
|
|
898
|
+
// ✅ CORRECT: Use environment variables
|
|
899
|
+
const cohere = new CohereClientV2({
|
|
900
|
+
token: process.env.COHERE_API_KEY
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
// ❌ WRONG: Never hardcode API keys
|
|
904
|
+
const cohere = new CohereClientV2({
|
|
905
|
+
token: "sk-xxxxxx" // DON'T DO THIS
|
|
906
|
+
});
|
|
907
|
+
```
|
|
908
|
+
|
|
909
|
+
**Input Validation:**
|
|
910
|
+
|
|
911
|
+
```javascript
|
|
912
|
+
function validateInput(text) {
|
|
913
|
+
if (!text || typeof text !== "string") {
|
|
914
|
+
throw new Error("Invalid input: text must be a non-empty string");
|
|
915
|
+
}
|
|
916
|
+
if (text.length > 100000) {
|
|
917
|
+
throw new Error("Input too long");
|
|
918
|
+
}
|
|
919
|
+
return text.trim();
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async function safeChat(userInput) {
|
|
923
|
+
const validatedInput = validateInput(userInput);
|
|
924
|
+
|
|
925
|
+
return await cohere.chat({
|
|
926
|
+
model: "command-a-03-2025",
|
|
927
|
+
messages: [{ role: "user", content: validatedInput }]
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
**Safety Mode:**
|
|
933
|
+
|
|
934
|
+
```javascript
|
|
935
|
+
// Enable contextual safety filtering
|
|
936
|
+
const response = await cohere.chat({
|
|
937
|
+
model: "command-a-03-2025",
|
|
938
|
+
messages: [{ role: "user", content: "User message" }],
|
|
939
|
+
safetyMode: "CONTEXTUAL" // or "STRICT" or "NONE"
|
|
940
|
+
});
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
### Performance Optimization
|
|
944
|
+
|
|
945
|
+
**Batch Embeddings:**
|
|
946
|
+
|
|
947
|
+
```javascript
|
|
948
|
+
// ✅ GOOD: Batch multiple texts in one request
|
|
949
|
+
const texts = ["text1", "text2", "text3", /* ... up to 96 texts */];
|
|
950
|
+
const response = await cohere.embed({
|
|
951
|
+
model: "embed-english-v3.0",
|
|
952
|
+
texts: texts,
|
|
953
|
+
inputType: "search_document"
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
// ❌ INEFFICIENT: Multiple individual requests
|
|
957
|
+
for (const text of texts) {
|
|
958
|
+
await cohere.embed({
|
|
959
|
+
model: "embed-english-v3.0",
|
|
960
|
+
texts: [text],
|
|
961
|
+
inputType: "search_document"
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
```
|
|
965
|
+
|
|
966
|
+
**Streaming for Long Responses:**
|
|
967
|
+
|
|
968
|
+
```javascript
|
|
969
|
+
// Use streaming for better user experience
|
|
970
|
+
async function streamResponse(prompt) {
|
|
971
|
+
const stream = await cohere.chatStream({
|
|
972
|
+
model: "command-a-03-2025",
|
|
973
|
+
messages: [{ role: "user", content: prompt }]
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
for await (const event of stream) {
|
|
977
|
+
if (event.type === "content-delta") {
|
|
978
|
+
// Display partial results immediately
|
|
979
|
+
process.stdout.write(event.delta?.message?.content?.text || "");
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
**Caching Strategy:**
|
|
986
|
+
|
|
987
|
+
```javascript
|
|
988
|
+
// Cache embeddings to avoid redundant API calls
|
|
989
|
+
class EmbeddingCache {
|
|
990
|
+
constructor() {
|
|
991
|
+
this.cache = new Map();
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
async getEmbedding(text, cohere) {
|
|
995
|
+
if (this.cache.has(text)) {
|
|
996
|
+
return this.cache.get(text);
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
const response = await cohere.embed({
|
|
1000
|
+
model: "embed-english-v3.0",
|
|
1001
|
+
texts: [text],
|
|
1002
|
+
inputType: "search_document"
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
const embedding = response.embeddings[0];
|
|
1006
|
+
this.cache.set(text, embedding);
|
|
1007
|
+
return embedding;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
```
|
|
1011
|
+
|
|
1012
|
+
## 8. Production Checklist
|
|
1013
|
+
|
|
1014
|
+
### Version Management
|
|
1015
|
+
|
|
1016
|
+
```json
|
|
1017
|
+
// package.json - Pin exact SDK version
|
|
1018
|
+
{
|
|
1019
|
+
"dependencies": {
|
|
1020
|
+
"cohere-ai": "7.14.0"
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
```
|
|
1024
|
+
|
|
1025
|
+
- Pin exact SDK version in `package.json`
|
|
1026
|
+
- Review changelog before upgrading
|
|
1027
|
+
- Test thoroughly in staging before production deployment
|
|
1028
|
+
- Monitor Cohere's release notes for breaking changes
|
|
1029
|
+
|
|
1030
|
+
### Environment Configuration
|
|
1031
|
+
|
|
1032
|
+
```javascript
|
|
1033
|
+
// config.js
|
|
1034
|
+
const config = {
|
|
1035
|
+
development: {
|
|
1036
|
+
cohereApiKey: process.env.COHERE_API_KEY_DEV,
|
|
1037
|
+
model: "command-a-03-2025",
|
|
1038
|
+
timeout: 30,
|
|
1039
|
+
maxRetries: 2
|
|
1040
|
+
},
|
|
1041
|
+
production: {
|
|
1042
|
+
cohereApiKey: process.env.COHERE_API_KEY_PROD,
|
|
1043
|
+
model: "command-a-03-2025",
|
|
1044
|
+
timeout: 60,
|
|
1045
|
+
maxRetries: 3
|
|
1046
|
+
}
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
const env = process.env.NODE_ENV || "development";
|
|
1050
|
+
export default config[env];
|
|
1051
|
+
```
|
|
1052
|
+
|
|
1053
|
+
**Environment variables:** Set `COHERE_API_KEY` or `CO_API_KEY`, `NODE_ENV` (development/staging/production), use separate API keys for each environment, and use secret management systems (AWS Secrets Manager, Azure Key Vault, etc.).
|
|
1054
|
+
|
|
1055
|
+
### Error Handling & Monitoring
|
|
1056
|
+
|
|
1057
|
+
```javascript
|
|
1058
|
+
import { CohereClientV2, CohereError, CohereTimeoutError } from "cohere-ai";
|
|
1059
|
+
|
|
1060
|
+
class CohereService {
|
|
1061
|
+
constructor(logger, metrics) {
|
|
1062
|
+
this.cohere = new CohereClientV2({
|
|
1063
|
+
token: process.env.COHERE_API_KEY
|
|
1064
|
+
});
|
|
1065
|
+
this.logger = logger;
|
|
1066
|
+
this.metrics = metrics;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
async chat(messages, options = {}) {
|
|
1070
|
+
const startTime = Date.now();
|
|
1071
|
+
|
|
1072
|
+
try {
|
|
1073
|
+
const response = await this.cohere.chat({
|
|
1074
|
+
model: "command-a-03-2025",
|
|
1075
|
+
messages: messages,
|
|
1076
|
+
...options
|
|
1077
|
+
}, {
|
|
1078
|
+
timeoutInSeconds: 60,
|
|
1079
|
+
maxRetries: 3
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
// Log success metrics
|
|
1083
|
+
const duration = Date.now() - startTime;
|
|
1084
|
+
this.metrics.recordSuccess("chat", duration);
|
|
1085
|
+
this.logger.info("Chat request succeeded", {
|
|
1086
|
+
duration,
|
|
1087
|
+
model: "command-a-03-2025"
|
|
1088
|
+
});
|
|
1089
|
+
|
|
1090
|
+
return response;
|
|
1091
|
+
|
|
1092
|
+
} catch (err) {
|
|
1093
|
+
const duration = Date.now() - startTime;
|
|
1094
|
+
|
|
1095
|
+
if (err instanceof CohereTimeoutError) {
|
|
1096
|
+
this.metrics.recordError("chat", "timeout");
|
|
1097
|
+
this.logger.error("Chat request timed out", { duration });
|
|
1098
|
+
} else if (err instanceof CohereError) {
|
|
1099
|
+
this.metrics.recordError("chat", `http_${err.statusCode}`);
|
|
1100
|
+
this.logger.error("Chat request failed", {
|
|
1101
|
+
statusCode: err.statusCode,
|
|
1102
|
+
message: err.message,
|
|
1103
|
+
duration
|
|
1104
|
+
});
|
|
1105
|
+
|
|
1106
|
+
// Alert on specific errors
|
|
1107
|
+
if (err.statusCode === 429) {
|
|
1108
|
+
this.logger.warn("Rate limit exceeded");
|
|
1109
|
+
} else if (err.statusCode >= 500) {
|
|
1110
|
+
this.logger.error("Cohere service error", { statusCode: err.statusCode });
|
|
1111
|
+
}
|
|
1112
|
+
} else {
|
|
1113
|
+
this.metrics.recordError("chat", "unknown");
|
|
1114
|
+
this.logger.error("Unexpected error", { error: err });
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
throw err;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
```
|
|
1122
|
+
|
|
1123
|
+
### Validate Responses
|
|
1124
|
+
|
|
1125
|
+
```javascript
|
|
1126
|
+
function validateChatResponse(response) {
|
|
1127
|
+
if (!response || !response.message) {
|
|
1128
|
+
throw new Error("Invalid response: missing message");
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
if (!response.message.content || response.message.content.length === 0) {
|
|
1132
|
+
throw new Error("Invalid response: empty content");
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
if (!response.message.content[0].text) {
|
|
1136
|
+
throw new Error("Invalid response: missing text");
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
return true;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
async function safeChat(messages) {
|
|
1143
|
+
const response = await cohere.chat({
|
|
1144
|
+
model: "command-a-03-2025",
|
|
1145
|
+
messages: messages
|
|
1146
|
+
});
|
|
1147
|
+
|
|
1148
|
+
validateChatResponse(response);
|
|
1149
|
+
return response.message.content[0].text;
|
|
1150
|
+
}
|
|
1151
|
+
```
|
|
1152
|
+
|
|
1153
|
+
### Testing Strategy
|
|
1154
|
+
|
|
1155
|
+
```javascript
|
|
1156
|
+
// Mock Cohere client for testing
|
|
1157
|
+
class MockCohereClient {
|
|
1158
|
+
async chat({ messages }) {
|
|
1159
|
+
return {
|
|
1160
|
+
message: {
|
|
1161
|
+
content: [{ text: "Mocked response" }],
|
|
1162
|
+
role: "assistant"
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
async embed({ texts }) {
|
|
1168
|
+
return {
|
|
1169
|
+
embeddings: texts.map(() => Array(1024).fill(0))
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// Test example
|
|
1175
|
+
describe("CohereService", () => {
|
|
1176
|
+
it("should handle chat requests", async () => {
|
|
1177
|
+
const mockClient = new MockCohereClient();
|
|
1178
|
+
const service = new CohereService(mockClient);
|
|
1179
|
+
|
|
1180
|
+
const response = await service.chat([
|
|
1181
|
+
{ role: "user", content: "test" }
|
|
1182
|
+
]);
|
|
1183
|
+
|
|
1184
|
+
expect(response.message.content[0].text).toBe("Mocked response");
|
|
1185
|
+
});
|
|
1186
|
+
});
|
|
1187
|
+
```
|
|
1188
|
+
|
|
1189
|
+
### Monitoring Checklist
|
|
1190
|
+
|
|
1191
|
+
**Metrics to track:** Request count (by endpoint, model), success rate, error rate (by error type, status code), latency (p50, p95, p99), token usage, rate limit hits, and timeout frequency.
|
|
1192
|
+
|
|
1193
|
+
**Logging requirements:** All API errors with context, rate limit warnings, unusual latency spikes, and token usage patterns.
|
|
1194
|
+
|
|
1195
|
+
**Alerting:** Error rate > 5%, rate limit exceeded, average latency > 10 seconds, service availability < 99%.
|
|
1196
|
+
|
|
1197
|
+
### API Key Rotation
|
|
1198
|
+
|
|
1199
|
+
```javascript
|
|
1200
|
+
class CohereClientManager {
|
|
1201
|
+
constructor() {
|
|
1202
|
+
this.client = null;
|
|
1203
|
+
this.apiKey = null;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
initialize(apiKey) {
|
|
1207
|
+
this.apiKey = apiKey;
|
|
1208
|
+
this.client = new CohereClientV2({ token: apiKey });
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
rotateApiKey(newApiKey) {
|
|
1212
|
+
console.log("Rotating API key...");
|
|
1213
|
+
this.apiKey = newApiKey;
|
|
1214
|
+
this.client = new CohereClientV2({ token: newApiKey });
|
|
1215
|
+
console.log("API key rotated successfully");
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
getClient() {
|
|
1219
|
+
if (!this.client) {
|
|
1220
|
+
throw new Error("Client not initialized");
|
|
1221
|
+
}
|
|
1222
|
+
return this.client;
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
// Usage
|
|
1227
|
+
const manager = new CohereClientManager();
|
|
1228
|
+
manager.initialize(process.env.COHERE_API_KEY);
|
|
1229
|
+
|
|
1230
|
+
// When rotating keys
|
|
1231
|
+
manager.rotateApiKey(process.env.NEW_COHERE_API_KEY);
|
|
1232
|
+
```
|
|
1233
|
+
|
|
1234
|
+
### Cost Optimization
|
|
1235
|
+
|
|
1236
|
+
**Monitor usage:**
|
|
1237
|
+
|
|
1238
|
+
```javascript
|
|
1239
|
+
class UsageTracker {
|
|
1240
|
+
constructor() {
|
|
1241
|
+
this.usage = {
|
|
1242
|
+
requests: 0,
|
|
1243
|
+
inputTokens: 0,
|
|
1244
|
+
outputTokens: 0
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
async trackChat(messages) {
|
|
1249
|
+
this.usage.requests++;
|
|
1250
|
+
|
|
1251
|
+
const stream = await cohere.chatStream({
|
|
1252
|
+
model: "command-a-03-2025",
|
|
1253
|
+
messages: messages
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
for await (const event of stream) {
|
|
1257
|
+
if (event.type === "message-end" && event.delta?.usage) {
|
|
1258
|
+
this.usage.inputTokens += event.delta.usage.inputTokens || 0;
|
|
1259
|
+
this.usage.outputTokens += event.delta.usage.outputTokens || 0;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
return this.getUsage();
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
getUsage() {
|
|
1267
|
+
return { ...this.usage };
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
```
|
|
1271
|
+
|
|
1272
|
+
**Optimize costs:** Use appropriate models (don't use command-a-03-2025 for simple tasks), set reasonable `maxTokens` limits, cache embeddings and rerank results, batch requests when possible, and use streaming to cancel long-running requests early if needed.
|
|
1273
|
+
|
|
1274
|
+
### Upgrade to Production API Key
|
|
1275
|
+
|
|
1276
|
+
**Trial key limitations:** 20 chat calls/minute, 5 embed calls/minute, 1,000 calls/month total.
|
|
1277
|
+
|
|
1278
|
+
**To upgrade:** Add payment method in Cohere dashboard, get production API key, update `COHERE_API_KEY` environment variable, and verify rate limits increased.
|
|
1279
|
+
|
|
1280
|
+
**Production benefits:** 500+ requests/minute (varies by endpoint), unlimited monthly usage, priority support, and higher rate limits available on request.
|
|
1281
|
+
|
|
1282
|
+
### Compliance & Safety
|
|
1283
|
+
|
|
1284
|
+
```javascript
|
|
1285
|
+
// Content moderation example
|
|
1286
|
+
async function moderatedChat(userMessage) {
|
|
1287
|
+
const response = await cohere.chat({
|
|
1288
|
+
model: "command-a-03-2025",
|
|
1289
|
+
messages: [{ role: "user", content: userMessage }],
|
|
1290
|
+
safetyMode: "STRICT"
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
// Log for compliance audit
|
|
1294
|
+
auditLogger.log({
|
|
1295
|
+
timestamp: new Date().toISOString(),
|
|
1296
|
+
userId: getCurrentUserId(),
|
|
1297
|
+
input: userMessage,
|
|
1298
|
+
output: response.message.content[0].text,
|
|
1299
|
+
model: "command-a-03-2025"
|
|
1300
|
+
});
|
|
1301
|
+
|
|
1302
|
+
return response;
|
|
1303
|
+
}
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
**Compliance considerations:** Log all interactions for audit trails, implement content filtering, handle PII appropriately, follow data retention policies, and comply with regional regulations (GDPR, CCPA, etc.)
|
|
1307
|
+
|
|
1308
|
+
### Deployment Checklist
|
|
1309
|
+
|
|
1310
|
+
Ensure API key is stored in secure secret management system, exact SDK version is pinned in `package.json`, comprehensive error handling is implemented, timeout and retry configuration is set, response validation is in place, logging and monitoring is configured, rate limiting is handled gracefully, tests cover error scenarios, load testing is completed, alerting rules are configured, documentation is updated, staging environment is tested, rollback plan is prepared, and team is trained on debugging procedures.
|
|
1311
|
+
|
|
1312
|
+
---
|
|
1313
|
+
|
|
1314
|
+
## Additional Resources
|
|
1315
|
+
|
|
1316
|
+
Access comprehensive documentation and resources using the npm CLI:
|
|
1317
|
+
|
|
1318
|
+
```bash
|
|
1319
|
+
# View package information and links
|
|
1320
|
+
npm info cohere-ai
|
|
1321
|
+
|
|
1322
|
+
# Open package homepage in browser
|
|
1323
|
+
npm home cohere-ai
|
|
1324
|
+
|
|
1325
|
+
# Open package repository in browser
|
|
1326
|
+
npm repo cohere-ai
|
|
1327
|
+
|
|
1328
|
+
# Open package issues page
|
|
1329
|
+
npm bugs cohere-ai
|
|
1330
|
+
|
|
1331
|
+
# View package documentation
|
|
1332
|
+
npm docs cohere-ai
|
|
1333
|
+
```
|
|
1334
|
+
|
|
1335
|
+
Official documentation available at: https://docs.cohere.com/reference/about
|