`addToolInputExamplesMiddleware`

addToolInputExamplesMiddleware

addToolInputExamplesMiddleware는 도구 설명에 입력 예제(input examples)를 추가해주는 미들웨어 함수예요. 특히 inputExamples 속성을 기본적으로 지원하지 않는 언어 모델 프로바이더에서 유용해요 — 미들웨어가 예제를 직렬화해 도구의 description에 주입하므로 모델이 이를 학습할 수 있게 해줍니다.

출처: 문서

본문

addToolInputExamplesMiddleware는 도구 설명에 입력 예제를 추가하는 미들웨어 함수입니다. 이것은 inputExamples 속성을 기본적으로 지원하지 않는 언어 모델 프로바이더에게 특히 유용합니다 — 미들웨어가 예제를 직렬화해 도구의 description에 주입하므로 모델이 이를 학습할 수 있습니다.

Import

import { addToolInputExamplesMiddleware } from "ai"

API

시그니처 (Signature)

function addToolInputExamplesMiddleware(options?: {
  prefix?: string;
  format?: (example: { input: JSONObject }, index: number) => string;
  remove?: boolean;
}): LanguageModelMiddleware;

Parameters (매개변수)

<PropertiesTable content={[ { name: 'prefix', type: 'string', isOptional: true, description: "입력 예제 섹션 앞에 붙는 접두사. 기본값: 'Input Examples:'.", }, { name: 'format', type: '(example: { input: JSONObject }, index: number) => string', isOptional: true, description: '각 예제의 선택적 커스텀 포매터. 예제 객체와 그 인덱스를 받습니다. 기본값: JSON.stringify(example.input).', }, { name: 'remove', type: 'boolean', isOptional: true, description: '예제를 설명에 추가한 후 도구에서 inputExamples 속성을 제거할지 여부. 기본값: true.', }, ]} />

Returns (반환값)

다음과 같은 동작을 하는 LanguageModelMiddleware:

  • inputExamples 속성이 있는 함수 도구를 찾습니다.
  • 각 입력 예제를 직렬화합니다(기본적으로 JSON으로, 또는 커스텀 포매터를 사용해).
  • 모든 포맷된 예제를 포함한 섹션을 도구 설명 끝에, prefix로 접두사를 붙여 추가합니다.
  • 도구에서 inputExamples 속성을 제거합니다(remove: false가 아니면).
  • 다른 모든 도구(예제가 없는 도구 포함)는 변경되지 않고 그대로 통과시킵니다.

사용 예제 (Usage Example)

import {
  generateText,
  tool,
  wrapLanguageModel,
  addToolInputExamplesMiddleware,
} from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const model = wrapLanguageModel({
  model: __MODEL__,
  middleware: addToolInputExamplesMiddleware({
    prefix: 'Input Examples:',
    format: (example, index) =>
      `${index + 1}. ${JSON.stringify(example.input)}`,
  }),
});

const result = await generateText({
  model,
  tools: {
    weather: tool({
      description: 'Get the weather in a location',
      inputSchema: z.object({ location: z.string() }),
      inputExamples: [
        { input: { location: 'San Francisco' } },
        { input: { location: 'London' } },
      ],
    }),
  },
  prompt: 'What is the weather in Tokyo?',
});

작동 방식 (How It Works)

  1. inputExamples를 정의하는 모든 함수 도구에 대해, 미들웨어는:

    • 각 예제를 format 함수로 포맷합니다 (기본: JSON.stringify).

    • 다음과 같은 섹션을 만듭니다:

      Input Examples:
      {"location":"San Francisco"}
      {"location":"London"}
      
    • 이 섹션을 도구의 description 끝에 추가합니다.

  2. 기본적으로, 중복을 방지하기 위해 추가한 후 inputExamples 속성을 제거합니다(remove: false로 비활성화 가능).

  3. 입력 예제가 없는 도구나 비함수 도구는 수정되지 않습니다.

팁: 이 미들웨어는 OpenAI나 Anthropic처럼 inputExamples에 대한 네이티브 지원이 없는 프로바이더에서 특히 유용합니다.

예제 효과 (Example effect)

원래 도구 정의가 다음과 같다면:

{
  type: 'function',
  name: 'weather',
  description: 'Get the weather in a location',
  inputSchema: { ... },
  inputExamples: [
    { input: { location: 'San Francisco' } },
    { input: { location: 'London' } }
  ]
}

미들웨어를 적용하면(기본 설정으로), 모델에 전달되는 도구는 다음과 같아집니다:

{
  type: 'function',
  name: 'weather',
  description: `Get the weather in a location

Input Examples:
{"location":"San Francisco"}
{"location":"London"}`,
  inputSchema: { ... }
  // inputExamples is removed by default
}

더 알아보기 (Learn more)

전체 사이트맵