@ai-sdk/cohere 3.0.10 → 3.0.11

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.
@@ -1,143 +0,0 @@
1
- import { EmbeddingModelV3Embedding } from '@ai-sdk/provider';
2
- import { createTestServer } from '@ai-sdk/test-server/with-vitest';
3
- import { createCohere } from './cohere-provider';
4
- import { describe, it, expect, vi } from 'vitest';
5
-
6
- vi.mock('./version', () => ({
7
- VERSION: '0.0.0-test',
8
- }));
9
-
10
- const dummyEmbeddings = [
11
- [0.1, 0.2, 0.3, 0.4, 0.5],
12
- [0.6, 0.7, 0.8, 0.9, 1.0],
13
- ];
14
- const testValues = ['sunny day at the beach', 'rainy day in the city'];
15
-
16
- const provider = createCohere({ apiKey: 'test-api-key' });
17
- const model = provider.embeddingModel('embed-english-v3.0');
18
-
19
- const server = createTestServer({
20
- 'https://api.cohere.com/v2/embed': {},
21
- });
22
-
23
- describe('doEmbed', () => {
24
- function prepareJsonResponse({
25
- embeddings = dummyEmbeddings,
26
- meta = { billed_units: { input_tokens: 8 } },
27
- headers,
28
- }: {
29
- embeddings?: EmbeddingModelV3Embedding[];
30
- meta?: { billed_units: { input_tokens: number } };
31
- headers?: Record<string, string>;
32
- } = {}) {
33
- server.urls['https://api.cohere.com/v2/embed'].response = {
34
- type: 'json-value',
35
- headers,
36
- body: {
37
- id: 'test-id',
38
- texts: testValues,
39
- embeddings: { float: embeddings },
40
- meta,
41
- },
42
- };
43
- }
44
-
45
- it('should extract embedding', async () => {
46
- prepareJsonResponse();
47
-
48
- const { embeddings } = await model.doEmbed({ values: testValues });
49
-
50
- expect(embeddings).toStrictEqual(dummyEmbeddings);
51
- });
52
-
53
- it('should expose the raw response', async () => {
54
- prepareJsonResponse({
55
- headers: { 'test-header': 'test-value' },
56
- });
57
-
58
- const { response } = await model.doEmbed({ values: testValues });
59
-
60
- expect(response?.headers).toStrictEqual({
61
- // default headers:
62
- 'content-length': '185',
63
- 'content-type': 'application/json',
64
-
65
- // custom header
66
- 'test-header': 'test-value',
67
- });
68
- expect(response).toMatchSnapshot();
69
- });
70
-
71
- it('should extract usage', async () => {
72
- prepareJsonResponse({
73
- meta: { billed_units: { input_tokens: 20 } },
74
- });
75
-
76
- const { usage } = await model.doEmbed({ values: testValues });
77
-
78
- expect(usage).toStrictEqual({ tokens: 20 });
79
- });
80
-
81
- it('should pass the model and the values', async () => {
82
- prepareJsonResponse();
83
-
84
- await model.doEmbed({ values: testValues });
85
-
86
- expect(await server.calls[0].requestBodyJson).toStrictEqual({
87
- model: 'embed-english-v3.0',
88
- embedding_types: ['float'],
89
- texts: testValues,
90
- input_type: 'search_query',
91
- });
92
- });
93
-
94
- it('should pass the input_type setting', async () => {
95
- prepareJsonResponse();
96
-
97
- await provider.embeddingModel('embed-english-v3.0').doEmbed({
98
- values: testValues,
99
- providerOptions: {
100
- cohere: {
101
- inputType: 'search_document',
102
- },
103
- },
104
- });
105
-
106
- expect(await server.calls[0].requestBodyJson).toStrictEqual({
107
- model: 'embed-english-v3.0',
108
- embedding_types: ['float'],
109
- texts: testValues,
110
- input_type: 'search_document',
111
- });
112
- });
113
-
114
- it('should pass headers', async () => {
115
- prepareJsonResponse();
116
-
117
- const provider = createCohere({
118
- apiKey: 'test-api-key',
119
- headers: {
120
- 'Custom-Provider-Header': 'provider-header-value',
121
- },
122
- });
123
-
124
- await provider.embeddingModel('embed-english-v3.0').doEmbed({
125
- values: testValues,
126
- headers: {
127
- 'Custom-Request-Header': 'request-header-value',
128
- },
129
- });
130
-
131
- const requestHeaders = server.calls[0].requestHeaders;
132
-
133
- expect(requestHeaders).toStrictEqual({
134
- authorization: 'Bearer test-api-key',
135
- 'content-type': 'application/json',
136
- 'custom-provider-header': 'provider-header-value',
137
- 'custom-request-header': 'request-header-value',
138
- });
139
- expect(server.calls[0].requestUserAgent).toContain(
140
- `ai-sdk/cohere/0.0.0-test`,
141
- );
142
- });
143
- });
@@ -1,152 +0,0 @@
1
- import { prepareTools } from './cohere-prepare-tools';
2
- import { describe, it, expect } from 'vitest';
3
-
4
- it('should return undefined tools when no tools are provided', () => {
5
- const result = prepareTools({
6
- tools: [],
7
- });
8
-
9
- expect(result).toStrictEqual({
10
- tools: undefined,
11
- toolChoice: undefined,
12
- toolWarnings: [],
13
- });
14
- });
15
-
16
- it('should process function tools correctly', () => {
17
- const functionTool = {
18
- type: 'function' as const,
19
- name: 'testFunction',
20
- description: 'test description',
21
- inputSchema: { type: 'object' as const, properties: {} },
22
- };
23
-
24
- const result = prepareTools({
25
- tools: [functionTool],
26
- });
27
-
28
- expect(result).toStrictEqual({
29
- tools: [
30
- {
31
- type: 'function',
32
- function: {
33
- name: 'testFunction',
34
- description: 'test description',
35
- parameters: { type: 'object' as const, properties: {} },
36
- },
37
- },
38
- ],
39
- toolChoice: undefined,
40
- toolWarnings: [],
41
- });
42
- });
43
-
44
- it('should add warnings for provider-defined tools', () => {
45
- const result = prepareTools({
46
- tools: [
47
- {
48
- type: 'provider' as const,
49
- id: 'provider.tool',
50
- name: 'tool',
51
- args: {},
52
- },
53
- ],
54
- });
55
-
56
- expect(result).toMatchInlineSnapshot(`
57
- {
58
- "toolChoice": undefined,
59
- "toolWarnings": [
60
- {
61
- "feature": "provider-defined tool provider.tool",
62
- "type": "unsupported",
63
- },
64
- ],
65
- "tools": [],
66
- }
67
- `);
68
- });
69
-
70
- describe('tool choice handling', () => {
71
- const basicTool = {
72
- type: 'function' as const,
73
- name: 'testFunction',
74
- description: 'test description',
75
- inputSchema: { type: 'object' as const, properties: {} },
76
- };
77
-
78
- it('should handle auto tool choice', () => {
79
- const result = prepareTools({
80
- tools: [basicTool],
81
- toolChoice: { type: 'auto' },
82
- });
83
-
84
- expect(result.toolChoice).toBe(undefined);
85
- });
86
-
87
- it('should handle none tool choice', () => {
88
- const result = prepareTools({
89
- tools: [basicTool],
90
- toolChoice: { type: 'none' },
91
- });
92
-
93
- expect(result).toStrictEqual({
94
- tools: [
95
- {
96
- type: 'function',
97
- function: {
98
- name: 'testFunction',
99
- description: 'test description',
100
- parameters: { type: 'object', properties: {} },
101
- },
102
- },
103
- ],
104
- toolChoice: 'NONE',
105
- toolWarnings: [],
106
- });
107
- });
108
-
109
- it('should handle required tool choice', () => {
110
- const result = prepareTools({
111
- tools: [basicTool],
112
- toolChoice: { type: 'required' },
113
- });
114
-
115
- expect(result).toStrictEqual({
116
- tools: [
117
- {
118
- type: 'function',
119
- function: {
120
- name: 'testFunction',
121
- description: 'test description',
122
- parameters: { type: 'object', properties: {} },
123
- },
124
- },
125
- ],
126
- toolChoice: 'REQUIRED',
127
- toolWarnings: [],
128
- });
129
- });
130
-
131
- it('should handle tool type tool choice by filtering tools', () => {
132
- const result = prepareTools({
133
- tools: [basicTool],
134
- toolChoice: { type: 'tool', toolName: 'testFunction' },
135
- });
136
-
137
- expect(result).toStrictEqual({
138
- tools: [
139
- {
140
- type: 'function',
141
- function: {
142
- name: 'testFunction',
143
- description: 'test description',
144
- parameters: { type: 'object', properties: {} },
145
- },
146
- },
147
- ],
148
- toolChoice: 'REQUIRED',
149
- toolWarnings: [],
150
- });
151
- });
152
- });
@@ -1,175 +0,0 @@
1
- import { convertToCohereChatPrompt } from './convert-to-cohere-chat-prompt';
2
- import { describe, it, expect } from 'vitest';
3
-
4
- describe('convert to cohere chat prompt', () => {
5
- describe('file processing', () => {
6
- it('should extract documents from file parts', () => {
7
- const result = convertToCohereChatPrompt([
8
- {
9
- role: 'user',
10
- content: [
11
- { type: 'text', text: 'Analyze this file: ' },
12
- {
13
- type: 'file',
14
- data: Buffer.from('This is file content'),
15
- mediaType: 'text/plain',
16
- filename: 'test.txt',
17
- },
18
- ],
19
- },
20
- ]);
21
-
22
- expect(result).toEqual({
23
- messages: [
24
- {
25
- role: 'user',
26
- content: 'Analyze this file: ',
27
- },
28
- ],
29
- documents: [
30
- {
31
- data: {
32
- text: 'This is file content',
33
- title: 'test.txt',
34
- },
35
- },
36
- ],
37
- warnings: [],
38
- });
39
- });
40
-
41
- it('should throw error for unsupported media types', () => {
42
- expect(() => {
43
- convertToCohereChatPrompt([
44
- {
45
- role: 'user',
46
- content: [
47
- {
48
- type: 'file',
49
- data: Buffer.from('PDF content'),
50
- mediaType: 'application/pdf',
51
- filename: 'test.pdf',
52
- },
53
- ],
54
- },
55
- ]);
56
- }).toThrow("Media type 'application/pdf' is not supported");
57
- });
58
- });
59
-
60
- describe('tool messages', () => {
61
- it('should convert a tool call into a cohere chatbot message', async () => {
62
- const result = convertToCohereChatPrompt([
63
- {
64
- role: 'assistant',
65
- content: [
66
- {
67
- type: 'text',
68
- text: 'Calling a tool',
69
- },
70
- {
71
- type: 'tool-call',
72
- toolName: 'tool-1',
73
- toolCallId: 'tool-call-1',
74
- input: { test: 'This is a tool message' },
75
- },
76
- ],
77
- },
78
- ]);
79
-
80
- expect(result).toEqual({
81
- messages: [
82
- {
83
- content: undefined,
84
- role: 'assistant',
85
- tool_calls: [
86
- {
87
- id: 'tool-call-1',
88
- type: 'function',
89
- function: {
90
- name: 'tool-1',
91
- arguments: JSON.stringify({ test: 'This is a tool message' }),
92
- },
93
- },
94
- ],
95
- },
96
- ],
97
- documents: [],
98
- warnings: [],
99
- });
100
- });
101
-
102
- it('should convert a single tool result into a cohere tool message', async () => {
103
- const result = convertToCohereChatPrompt([
104
- {
105
- role: 'tool',
106
- content: [
107
- {
108
- type: 'tool-result',
109
- toolName: 'tool-1',
110
- toolCallId: 'tool-call-1',
111
- output: {
112
- type: 'json',
113
- value: { test: 'This is a tool message' },
114
- },
115
- },
116
- ],
117
- },
118
- ]);
119
-
120
- expect(result).toEqual({
121
- messages: [
122
- {
123
- role: 'tool',
124
- content: JSON.stringify({ test: 'This is a tool message' }),
125
- tool_call_id: 'tool-call-1',
126
- },
127
- ],
128
- documents: [],
129
- warnings: [],
130
- });
131
- });
132
-
133
- it('should convert multiple tool results into a cohere tool message', async () => {
134
- const result = convertToCohereChatPrompt([
135
- {
136
- role: 'tool',
137
- content: [
138
- {
139
- type: 'tool-result',
140
- toolName: 'tool-1',
141
- toolCallId: 'tool-call-1',
142
- output: {
143
- type: 'json',
144
- value: { test: 'This is a tool message' },
145
- },
146
- },
147
- {
148
- type: 'tool-result',
149
- toolName: 'tool-2',
150
- toolCallId: 'tool-call-2',
151
- output: { type: 'json', value: { something: 'else' } },
152
- },
153
- ],
154
- },
155
- ]);
156
-
157
- expect(result).toEqual({
158
- messages: [
159
- {
160
- role: 'tool',
161
- content: JSON.stringify({ test: 'This is a tool message' }),
162
- tool_call_id: 'tool-call-1',
163
- },
164
- {
165
- role: 'tool',
166
- content: JSON.stringify({ something: 'else' }),
167
- tool_call_id: 'tool-call-2',
168
- },
169
- ],
170
- documents: [],
171
- warnings: [],
172
- });
173
- });
174
- });
175
- });
@@ -1,21 +0,0 @@
1
- {
2
- "id": "b44fe75b-e3d3-489a-b61e-1a1aede3ef72",
3
- "results": [
4
- {
5
- "index": 1,
6
- "relevance_score": 0.10183054
7
- },
8
- {
9
- "index": 0,
10
- "relevance_score": 0.03762639
11
- }
12
- ],
13
- "meta": {
14
- "api_version": {
15
- "version": "2"
16
- },
17
- "billed_units": {
18
- "search_units": 1
19
- }
20
- }
21
- }