parseToolImageDescription (chat-llm/tool-images.ts)

airi · packages/stage-ui/src/stores/ai/chat-llm/tool-images.ts · typescript · MIT
ai, tag:embed-classified

Utility functions to parse and replace image parts with text descriptions for AI vision models.

// Source: https://github.com/moeru-ai/airi | License: MIT | Copyright 2024-PRESENT Neko Ayaka
// Provided via justclaw.it.com; keep this notice with the code.
import type { Tool, ToolExecuteResult } from '@xsai/shared-chat'

import * as v from 'valibot'

/** Reads one image with the vision model and returns its text description for the chat model. */
export type DescribeToolImage = (imageUrl: string) => Promise<string>

const imageUrlPartSchema = v.object({
  type: v.literal('image_url'),
  image_url: v.object({ url: v.string() }),
})

const mcpImageSchema = v.object({
  type: v.literal('image'),
  data: v.string(),
  mimeType: v.string(),
})

const mcpResultSchema = v.looseObject({
  content: v.array(v.unknown()),
})

const DESCRIPTION_START = '[Image description from the vision model]'


... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key