@ai-sdk/cohere 4.0.0-beta.2 → 4.0.0-beta.21
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/CHANGELOG.md +155 -4
- package/README.md +2 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +266 -225
- package/dist/index.js.map +1 -1
- package/package.json +7 -9
- package/src/cohere-chat-language-model.ts +72 -25
- package/src/cohere-embedding-model.ts +5 -5
- package/src/cohere-prepare-tools.ts +6 -6
- package/src/cohere-provider.ts +14 -14
- package/src/convert-cohere-usage.ts +2 -2
- package/src/convert-to-cohere-chat-prompt.ts +12 -5
- package/src/map-cohere-finish-reason.ts +2 -2
- package/src/reranking/cohere-reranking-model.ts +6 -6
- package/dist/index.d.mts +0 -117
- package/dist/index.mjs +0 -1067
- package/dist/index.mjs.map +0 -1
package/src/cohere-provider.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
EmbeddingModelV4,
|
|
3
|
+
LanguageModelV4,
|
|
4
4
|
NoSuchModelError,
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
RerankingModelV4,
|
|
6
|
+
ProviderV4,
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
|
|
9
9
|
import {
|
|
@@ -21,43 +21,43 @@ import { CohereRerankingModel } from './reranking/cohere-reranking-model';
|
|
|
21
21
|
import { CohereEmbeddingModelId } from './cohere-embedding-options';
|
|
22
22
|
import { VERSION } from './version';
|
|
23
23
|
|
|
24
|
-
export interface CohereProvider extends
|
|
25
|
-
(modelId: CohereChatModelId):
|
|
24
|
+
export interface CohereProvider extends ProviderV4 {
|
|
25
|
+
(modelId: CohereChatModelId): LanguageModelV4;
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Creates a model for text generation.
|
|
29
29
|
*/
|
|
30
|
-
languageModel(modelId: CohereChatModelId):
|
|
30
|
+
languageModel(modelId: CohereChatModelId): LanguageModelV4;
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Creates a model for text embeddings.
|
|
34
34
|
*/
|
|
35
|
-
embedding(modelId: CohereEmbeddingModelId):
|
|
35
|
+
embedding(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Creates a model for text embeddings.
|
|
39
39
|
*/
|
|
40
|
-
embeddingModel(modelId: CohereEmbeddingModelId):
|
|
40
|
+
embeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* @deprecated Use `embedding` instead.
|
|
44
44
|
*/
|
|
45
|
-
textEmbedding(modelId: CohereEmbeddingModelId):
|
|
45
|
+
textEmbedding(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* @deprecated Use `embeddingModel` instead.
|
|
49
49
|
*/
|
|
50
|
-
textEmbeddingModel(modelId: CohereEmbeddingModelId):
|
|
50
|
+
textEmbeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Creates a model for reranking.
|
|
54
54
|
*/
|
|
55
|
-
reranking(modelId: CohereRerankingModelId):
|
|
55
|
+
reranking(modelId: CohereRerankingModelId): RerankingModelV4;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Creates a model for reranking.
|
|
59
59
|
*/
|
|
60
|
-
rerankingModel(modelId: CohereRerankingModelId):
|
|
60
|
+
rerankingModel(modelId: CohereRerankingModelId): RerankingModelV4;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export interface CohereProviderSettings {
|
|
@@ -147,7 +147,7 @@ export function createCohere(
|
|
|
147
147
|
return createChatModel(modelId);
|
|
148
148
|
};
|
|
149
149
|
|
|
150
|
-
provider.specificationVersion = '
|
|
150
|
+
provider.specificationVersion = 'v4' as const;
|
|
151
151
|
provider.languageModel = createChatModel;
|
|
152
152
|
provider.embedding = createEmbeddingModel;
|
|
153
153
|
provider.embeddingModel = createEmbeddingModel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export type CohereUsageTokens = {
|
|
4
4
|
input_tokens: number;
|
|
@@ -7,7 +7,7 @@ export type CohereUsageTokens = {
|
|
|
7
7
|
|
|
8
8
|
export function convertCohereUsage(
|
|
9
9
|
tokens: CohereUsageTokens | undefined | null,
|
|
10
|
-
):
|
|
10
|
+
): LanguageModelV4Usage {
|
|
11
11
|
if (tokens == null) {
|
|
12
12
|
return {
|
|
13
13
|
inputTokens: {
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
SharedV4Warning,
|
|
3
|
+
LanguageModelV4Prompt,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
+
import { isProviderReference } from '@ai-sdk/provider-utils';
|
|
6
7
|
import { CohereAssistantMessage, CohereChatPrompt } from './cohere-chat-prompt';
|
|
7
8
|
|
|
8
|
-
export function convertToCohereChatPrompt(prompt:
|
|
9
|
+
export function convertToCohereChatPrompt(prompt: LanguageModelV4Prompt): {
|
|
9
10
|
messages: CohereChatPrompt;
|
|
10
11
|
documents: Array<{
|
|
11
12
|
data: { text: string; title?: string };
|
|
12
13
|
}>;
|
|
13
|
-
warnings:
|
|
14
|
+
warnings: SharedV4Warning[];
|
|
14
15
|
} {
|
|
15
16
|
const messages: CohereChatPrompt = [];
|
|
16
17
|
const documents: Array<{ data: { text: string; title?: string } }> = [];
|
|
17
|
-
const warnings:
|
|
18
|
+
const warnings: SharedV4Warning[] = [];
|
|
18
19
|
|
|
19
20
|
for (const { role, content } of prompt) {
|
|
20
21
|
switch (role) {
|
|
@@ -33,6 +34,12 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
|
|
|
33
34
|
return part.text;
|
|
34
35
|
}
|
|
35
36
|
case 'file': {
|
|
37
|
+
if (isProviderReference(part.data)) {
|
|
38
|
+
throw new UnsupportedFunctionalityError({
|
|
39
|
+
functionality: 'file parts with provider references',
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
36
43
|
// Extract documents for RAG
|
|
37
44
|
let textContent: string;
|
|
38
45
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export function mapCohereFinishReason(
|
|
4
4
|
finishReason: string | null | undefined,
|
|
5
|
-
):
|
|
5
|
+
): LanguageModelV4FinishReason['unified'] {
|
|
6
6
|
switch (finishReason) {
|
|
7
7
|
case 'COMPLETE':
|
|
8
8
|
case 'STOP_SEQUENCE':
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RerankingModelV4, SharedV4Warning } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
3
|
combineHeaders,
|
|
4
4
|
createJsonResponseHandler,
|
|
@@ -23,8 +23,8 @@ type CohereRerankingConfig = {
|
|
|
23
23
|
fetch?: FetchFunction;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
export class CohereRerankingModel implements
|
|
27
|
-
readonly specificationVersion = '
|
|
26
|
+
export class CohereRerankingModel implements RerankingModelV4 {
|
|
27
|
+
readonly specificationVersion = 'v4';
|
|
28
28
|
readonly modelId: CohereRerankingModelId;
|
|
29
29
|
|
|
30
30
|
private readonly config: CohereRerankingConfig;
|
|
@@ -46,8 +46,8 @@ export class CohereRerankingModel implements RerankingModelV3 {
|
|
|
46
46
|
topN,
|
|
47
47
|
abortSignal,
|
|
48
48
|
providerOptions,
|
|
49
|
-
}: Parameters<
|
|
50
|
-
Awaited<ReturnType<
|
|
49
|
+
}: Parameters<RerankingModelV4['doRerank']>[0]): Promise<
|
|
50
|
+
Awaited<ReturnType<RerankingModelV4['doRerank']>>
|
|
51
51
|
> {
|
|
52
52
|
const rerankingOptions = await parseProviderOptions({
|
|
53
53
|
provider: 'cohere',
|
|
@@ -55,7 +55,7 @@ export class CohereRerankingModel implements RerankingModelV3 {
|
|
|
55
55
|
schema: cohereRerankingModelOptionsSchema,
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
-
const warnings:
|
|
58
|
+
const warnings: SharedV4Warning[] = [];
|
|
59
59
|
|
|
60
60
|
if (documents.type === 'object') {
|
|
61
61
|
warnings.push({
|
package/dist/index.d.mts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod/v4';
|
|
2
|
-
import { ProviderV3, LanguageModelV3, EmbeddingModelV3, RerankingModelV3 } from '@ai-sdk/provider';
|
|
3
|
-
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
4
|
-
|
|
5
|
-
type CohereChatModelId = 'command-a-03-2025' | 'command-a-reasoning-08-2025' | 'command-r7b-12-2024' | 'command-r-plus-04-2024' | 'command-r-plus' | 'command-r-08-2024' | 'command-r-03-2024' | 'command-r' | 'command' | 'command-nightly' | 'command-light' | 'command-light-nightly' | (string & {});
|
|
6
|
-
declare const cohereLanguageModelOptions: z.ZodObject<{
|
|
7
|
-
thinking: z.ZodOptional<z.ZodObject<{
|
|
8
|
-
type: z.ZodOptional<z.ZodEnum<{
|
|
9
|
-
enabled: "enabled";
|
|
10
|
-
disabled: "disabled";
|
|
11
|
-
}>>;
|
|
12
|
-
tokenBudget: z.ZodOptional<z.ZodNumber>;
|
|
13
|
-
}, z.core.$strip>>;
|
|
14
|
-
}, z.core.$strip>;
|
|
15
|
-
type CohereLanguageModelOptions = z.infer<typeof cohereLanguageModelOptions>;
|
|
16
|
-
|
|
17
|
-
type CohereRerankingModelId = 'rerank-v3.5' | 'rerank-english-v3.0' | 'rerank-multilingual-v3.0' | (string & {});
|
|
18
|
-
type CohereRerankingModelOptions = {
|
|
19
|
-
/**
|
|
20
|
-
* Long documents will be automatically truncated to the specified number of tokens.
|
|
21
|
-
*
|
|
22
|
-
* @default 4096
|
|
23
|
-
*/
|
|
24
|
-
maxTokensPerDoc?: number;
|
|
25
|
-
/**
|
|
26
|
-
* The priority of the request.
|
|
27
|
-
*
|
|
28
|
-
* @default 0
|
|
29
|
-
*/
|
|
30
|
-
priority?: number;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type CohereEmbeddingModelId = 'embed-english-v3.0' | 'embed-multilingual-v3.0' | 'embed-english-light-v3.0' | 'embed-multilingual-light-v3.0' | 'embed-english-v2.0' | 'embed-english-light-v2.0' | 'embed-multilingual-v2.0' | (string & {});
|
|
34
|
-
declare const cohereEmbeddingModelOptions: z.ZodObject<{
|
|
35
|
-
inputType: z.ZodOptional<z.ZodEnum<{
|
|
36
|
-
search_document: "search_document";
|
|
37
|
-
search_query: "search_query";
|
|
38
|
-
classification: "classification";
|
|
39
|
-
clustering: "clustering";
|
|
40
|
-
}>>;
|
|
41
|
-
truncate: z.ZodOptional<z.ZodEnum<{
|
|
42
|
-
NONE: "NONE";
|
|
43
|
-
START: "START";
|
|
44
|
-
END: "END";
|
|
45
|
-
}>>;
|
|
46
|
-
outputDimension: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<256>, z.ZodLiteral<512>, z.ZodLiteral<1024>, z.ZodLiteral<1536>]>>;
|
|
47
|
-
}, z.core.$strip>;
|
|
48
|
-
type CohereEmbeddingModelOptions = z.infer<typeof cohereEmbeddingModelOptions>;
|
|
49
|
-
|
|
50
|
-
interface CohereProvider extends ProviderV3 {
|
|
51
|
-
(modelId: CohereChatModelId): LanguageModelV3;
|
|
52
|
-
/**
|
|
53
|
-
* Creates a model for text generation.
|
|
54
|
-
*/
|
|
55
|
-
languageModel(modelId: CohereChatModelId): LanguageModelV3;
|
|
56
|
-
/**
|
|
57
|
-
* Creates a model for text embeddings.
|
|
58
|
-
*/
|
|
59
|
-
embedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
|
|
60
|
-
/**
|
|
61
|
-
* Creates a model for text embeddings.
|
|
62
|
-
*/
|
|
63
|
-
embeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
|
|
64
|
-
/**
|
|
65
|
-
* @deprecated Use `embedding` instead.
|
|
66
|
-
*/
|
|
67
|
-
textEmbedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
|
|
68
|
-
/**
|
|
69
|
-
* @deprecated Use `embeddingModel` instead.
|
|
70
|
-
*/
|
|
71
|
-
textEmbeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
|
|
72
|
-
/**
|
|
73
|
-
* Creates a model for reranking.
|
|
74
|
-
*/
|
|
75
|
-
reranking(modelId: CohereRerankingModelId): RerankingModelV3;
|
|
76
|
-
/**
|
|
77
|
-
* Creates a model for reranking.
|
|
78
|
-
*/
|
|
79
|
-
rerankingModel(modelId: CohereRerankingModelId): RerankingModelV3;
|
|
80
|
-
}
|
|
81
|
-
interface CohereProviderSettings {
|
|
82
|
-
/**
|
|
83
|
-
* Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
84
|
-
* The default prefix is `https://api.cohere.com/v2`.
|
|
85
|
-
*/
|
|
86
|
-
baseURL?: string;
|
|
87
|
-
/**
|
|
88
|
-
* API key that is being send using the `Authorization` header.
|
|
89
|
-
* It defaults to the `COHERE_API_KEY` environment variable.
|
|
90
|
-
*/
|
|
91
|
-
apiKey?: string;
|
|
92
|
-
/**
|
|
93
|
-
* Custom headers to include in the requests.
|
|
94
|
-
*/
|
|
95
|
-
headers?: Record<string, string>;
|
|
96
|
-
/**
|
|
97
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
98
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
99
|
-
*/
|
|
100
|
-
fetch?: FetchFunction;
|
|
101
|
-
/**
|
|
102
|
-
* Optional function to generate a unique ID for each request.
|
|
103
|
-
*/
|
|
104
|
-
generateId?: () => string;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Create a Cohere AI provider instance.
|
|
108
|
-
*/
|
|
109
|
-
declare function createCohere(options?: CohereProviderSettings): CohereProvider;
|
|
110
|
-
/**
|
|
111
|
-
* Default Cohere provider instance.
|
|
112
|
-
*/
|
|
113
|
-
declare const cohere: CohereProvider;
|
|
114
|
-
|
|
115
|
-
declare const VERSION: string;
|
|
116
|
-
|
|
117
|
-
export { type CohereLanguageModelOptions as CohereChatModelOptions, type CohereEmbeddingModelOptions, type CohereLanguageModelOptions, type CohereProvider, type CohereProviderSettings, type CohereRerankingModelOptions, type CohereRerankingModelOptions as CohereRerankingOptions, VERSION, cohere, createCohere };
|