(responseFormat: any)
| 380 | } |
| 381 | |
| 382 | export function generateStructuredOutputInstructions(responseFormat: any): string { |
| 383 | if (!responseFormat) return '' |
| 384 | |
| 385 | if (responseFormat.schema || (responseFormat.type === 'object' && responseFormat.properties)) { |
| 386 | return '' |
| 387 | } |
| 388 | |
| 389 | if (!responseFormat.fields) return '' |
| 390 | |
| 391 | function generateFieldStructure(field: any): string { |
| 392 | if (field.type === 'object' && field.properties) { |
| 393 | return `{ |
| 394 | ${Object.entries(field.properties) |
| 395 | .map(([key, prop]: [string, any]) => `"${key}": ${prop.type === 'number' ? '0' : '"value"'}`) |
| 396 | .join(',\n ')} |
| 397 | }` |
| 398 | } |
| 399 | return field.type === 'string' |
| 400 | ? '"value"' |
| 401 | : field.type === 'number' |
| 402 | ? '0' |
| 403 | : field.type === 'boolean' |
| 404 | ? 'true/false' |
| 405 | : '[]' |
| 406 | } |
| 407 | |
| 408 | const exampleFormat = responseFormat.fields |
| 409 | .map((field: any) => ` "${field.name}": ${generateFieldStructure(field)}`) |
| 410 | .join(',\n') |
| 411 | |
| 412 | const fieldDescriptions = responseFormat.fields |
| 413 | .map((field: any) => { |
| 414 | let desc = `${field.name} (${field.type})` |
| 415 | if (field.description) desc += `: ${field.description}` |
| 416 | if (field.type === 'object' && field.properties) { |
| 417 | desc += '\nProperties:' |
| 418 | Object.entries(field.properties).forEach(([key, prop]: [string, any]) => { |
| 419 | desc += `\n - ${key} (${(prop as any).type}): ${(prop as any).description || ''}` |
| 420 | }) |
| 421 | } |
| 422 | return desc |
| 423 | }) |
| 424 | .join('\n') |
| 425 | |
| 426 | return ` |
| 427 | Please provide your response in the following JSON format: |
| 428 | { |
| 429 | ${exampleFormat} |
| 430 | } |
| 431 | |
| 432 | Field descriptions: |
| 433 | ${fieldDescriptions} |
| 434 | |
| 435 | Your response MUST be valid JSON and include all the specified fields with their correct types. |
| 436 | Each metric should be an object containing 'score' (number) and 'reasoning' (string).` |
| 437 | } |
| 438 | |
| 439 | export function extractAndParseJSON(content: string): any { |
no test coverage detected