Skip to content

Commit cd9de4a

Browse files
authored
Merge pull request #1074 from narengogi/chore/anthropic-pdf
anthropic pdf
2 parents 269c0f0 + f80c462 commit cd9de4a

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

src/providers/anthropic/chatComplete.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,40 @@ interface AnthropicTextContentItem {
6363
text: string;
6464
}
6565

66+
interface AnthropicUrlPdfContentItem {
67+
type: string;
68+
source: {
69+
type: string;
70+
url: string;
71+
};
72+
}
73+
74+
interface AnthropicBase64PdfContentItem {
75+
type: string;
76+
source: {
77+
type: string;
78+
data: string;
79+
media_type: string;
80+
};
81+
}
82+
83+
interface AnthropicPlainTextContentItem {
84+
type: string;
85+
source: {
86+
type: string;
87+
data: string;
88+
media_type: string;
89+
};
90+
}
91+
6692
type AnthropicMessageContentItem =
6793
| AnthropicToolResultContentItem
6894
| AnthropicBase64ImageContentItem
6995
| AnthropicUrlImageContentItem
70-
| AnthropicTextContentItem;
96+
| AnthropicTextContentItem
97+
| AnthropicUrlPdfContentItem
98+
| AnthropicBase64PdfContentItem
99+
| AnthropicPlainTextContentItem;
71100

72101
interface AnthropicMessage extends Message, PromptCache {
73102
content: AnthropicMessageContentItem[];
@@ -166,6 +195,35 @@ const transformAndAppendImageContentItem = (
166195
}
167196
};
168197

198+
const transformAndAppendFileContentItem = (
199+
item: ContentType,
200+
transformedMessage: AnthropicMessage
201+
) => {
202+
const mimeType =
203+
(item.file?.mime_type as keyof typeof fileExtensionMimeTypeMap) ||
204+
fileExtensionMimeTypeMap.pdf;
205+
if (item.file?.file_url) {
206+
transformedMessage.content.push({
207+
type: 'document',
208+
source: {
209+
type: 'url',
210+
url: item.file.file_url,
211+
},
212+
});
213+
} else if (item.file?.file_data) {
214+
const contentType =
215+
mimeType === fileExtensionMimeTypeMap.txt ? 'text' : 'base64';
216+
transformedMessage.content.push({
217+
type: 'document',
218+
source: {
219+
type: contentType,
220+
data: item.file.file_data,
221+
media_type: mimeType,
222+
},
223+
});
224+
}
225+
};
226+
169227
export const AnthropicChatCompleteConfig: ProviderConfig = {
170228
model: {
171229
param: 'model',
@@ -205,6 +263,8 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
205263
});
206264
} else if (item.type === 'image_url') {
207265
transformAndAppendImageContentItem(item, transformedMessage);
266+
} else if (item.type === 'file') {
267+
transformAndAppendFileContentItem(item, transformedMessage);
208268
}
209269
});
210270
messages.push(transformedMessage as AnthropicMessage);

src/providers/bedrock/chatComplete.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { BEDROCK, documentMimeTypes, imagesMimeTypes } from '../../globals';
1+
import {
2+
BEDROCK,
3+
documentMimeTypes,
4+
fileExtensionMimeTypeMap,
5+
imagesMimeTypes,
6+
} from '../../globals';
27
import {
38
Message,
49
Params,
@@ -183,6 +188,32 @@ const getMessageContent = (message: Message) => {
183188
},
184189
});
185190
}
191+
} else if (item.type === 'file') {
192+
const mimeType = item.file?.mime_type || fileExtensionMimeTypeMap.pdf;
193+
const fileFormat = mimeType.split('/')[1];
194+
if (item.file?.file_url) {
195+
out.push({
196+
document: {
197+
format: fileFormat,
198+
name: item.file.file_name || crypto.randomUUID(),
199+
source: {
200+
s3Location: {
201+
uri: item.file.file_url,
202+
},
203+
},
204+
},
205+
});
206+
} else if (item.file?.file_data) {
207+
out.push({
208+
document: {
209+
format: fileFormat,
210+
name: item.file.file_name || crypto.randomUUID(),
211+
source: {
212+
bytes: item.file.file_data,
213+
},
214+
},
215+
});
216+
}
186217
}
187218

188219
if (item.cache_control) {
@@ -402,7 +433,10 @@ type BedrockContentItem = {
402433
format: string;
403434
name: string;
404435
source: {
405-
bytes: string;
436+
bytes?: string;
437+
s3Location?: {
438+
uri: string;
439+
};
406440
};
407441
};
408442
cachePoint?: {

src/types/requestBody.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export interface Config {
216216
}
217217

218218
/**
219+
* TODO: make this a union type
219220
* A message content type.
220221
* @interface
221222
*/
@@ -230,6 +231,17 @@ export interface ContentType extends PromptCache {
230231
mime_type?: string;
231232
};
232233
data?: string;
234+
file?: {
235+
file_data?: string;
236+
file_id?: string;
237+
file_name?: string;
238+
file_url?: string;
239+
mime_type?: string;
240+
};
241+
input_audio?: {
242+
data: string;
243+
format: string; //defaults to auto
244+
};
233245
}
234246

235247
export interface ToolCall {

0 commit comments

Comments
 (0)