Skip to content

附件 ​

向 sendMessage 传入 File、FileList 或 FilePart 对象。文件会被读取为 data URL,并作为 file part 存储在用户消息中。

vue
<script setup lang="ts">
const { messages, input, sendMessage } = useChat({ provider: ai, model: 'gpt-4o' })
const files = ref<FileList>()

async function submit() {
  await sendMessage({ text: input.value, files: files.value })
  input.value = ''
  files.value = undefined
}
</script>

<template>
  <input type="file" multiple @change="files = ($event.target as HTMLInputElement).files ?? undefined">
  <input v-model="input" @keydown.enter="submit">
</template>

文本是可选的:sendMessage({ files }) 只发送附件。

远程 URL 会跳过上传步骤:

ts
sendMessage({ text: 'Describe this', files: { type: 'file', mediaType: 'image/png', url: 'https://example.com/cat.png' } })

提供方支持 ​

提供方图片PDF音频文本远程 URL
OpenAI✓✓wav、mp3内联为文本仅图片
Anthropic✓✓—✓(document)图片、PDF
Google✓✓✓✓fileData URI
Ollama✓——内联为文本—

不支持的组合会在发送请求之前抛出描述清晰的错误。

辅助函数 ​

ts
import { fileToDataURL, convertFilesToParts } from 'vue-llm'

const url = await fileToDataURL(blob)
const parts = await convertFilesToParts(input.files) // FilePart[]

基于 MIT 许可发布。