2
0

🐛 (ai) Enable multi urls for vision

Closes #1764
This commit is contained in:
Baptiste Arnaud
2024-09-04 14:07:32 +02:00
parent fa14029fed
commit 3b87801c92
3 changed files with 66 additions and 82 deletions

View File

@ -9,48 +9,45 @@ export const splitUserTextMessageIntoBlocks = async ({
input,
shouldDownloadImages,
}: Props): Promise<UserContent> => {
const urlRegex = /(^|\n\n)(https?:\/\/.+)(\n\n|$)/g
const match = input.match(urlRegex)
if (!match) return input
const splittedInput = input.split('\n\n')
let parts: (TextPart | ImagePart)[] = []
let processedInput = input
for (const url of match) {
const textBeforeUrl = processedInput.slice(0, processedInput.indexOf(url))
if (textBeforeUrl.trim().length > 0) {
parts.push({ type: 'text', text: textBeforeUrl })
}
const cleanUrl = url.trim()
try {
const response = await ky.get(cleanUrl)
if (
!response.ok ||
!response.headers.get('content-type')?.startsWith('image/')
) {
parts.push({ type: 'text', text: cleanUrl })
} else {
parts.push({
type: 'image',
image: shouldDownloadImages
? await response.arrayBuffer()
: url.trim(),
})
for (const part of splittedInput) {
if (part.startsWith('http') || part.startsWith('["http')) {
const urls = part.startsWith('[') ? JSON.parse(part) : [part]
for (const url of urls) {
const cleanUrl = url.trim()
try {
const response = await ky.get(cleanUrl)
if (
!response.ok ||
!response.headers.get('content-type')?.startsWith('image/')
) {
parts.push({ type: 'text', text: cleanUrl })
} else {
parts.push({
type: 'image',
image: shouldDownloadImages
? await response.arrayBuffer()
: url.trim(),
})
}
} catch (err) {
if (err instanceof HTTPError) {
console.log(err.response.status, await err.response.text())
} else {
console.error(err)
}
}
}
} catch (err) {
if (err instanceof HTTPError) {
console.log(err.response.status, await err.response.text())
} else {
if (parts.at(-1)?.type === 'text') {
const lastText = parts.at(-1) as TextPart
parts = parts.slice(0, -1)
parts.push({ type: 'text', text: lastText.text + '\n\n' + part })
} else {
console.error(err)
parts.push({ type: 'text', text: part })
}
}
processedInput = processedInput.slice(
processedInput.indexOf(url) + url.length
)
}
if (processedInput.trim().length > 0) {
parts.push({ type: 'text', text: processedInput })
}
return parts