@ai-sdk/cohere 4.0.0-beta.2 → 4.0.0-beta.20
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 +143 -4
- package/README.md +2 -0
- package/dist/index.d.mts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +82 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +55 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -5
- 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/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({
|