kbot iterator format
This commit is contained in:
parent
dcae735c74
commit
9391478181
File diff suppressed because one or more lines are too long
@ -1,9 +1,9 @@
|
||||
{
|
||||
"model": "openrouter/quasar-alpha",
|
||||
"model": "openai/chatgpt-4o-latest",
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Make this description more engaging and detailed, around 10 words\n\nText to transform: \"A yellow tropical fruit\""
|
||||
"content": "Analyze this product review and extract key information using EXACTLY the schema specified below.\n\nThe review: \"Great selection of fruits with good prices and quality. Some items were out of stock.\"\n\nYour response MUST be a valid JSON object following this exact schema:\n{\n \"sentiment\": \"positive\" | \"neutral\" | \"negative\",\n \"pros\": [\"string\", \"string\"...], // 1-3 items\n \"cons\": [\"string\"...] // 0-3 items\n}\n\nDo not add any extra fields not in the schema, and make sure to use the exact field names as specified.\n\nText to transform: \"Great selection of fruits with good prices and quality. Some items were out of stock.\""
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
|
||||
@ -4,16 +4,21 @@ import * as fs from 'fs';
|
||||
import type { IKBotTask } from '@polymech/ai-tools';
|
||||
import { E_OPENROUTER_MODEL } from '../../models/cache/openrouter-models.js';
|
||||
import { E_Mode } from '../../zod_schema.js';
|
||||
import { FieldMapping, createIterator, createLLMTransformer, CacheConfig, INetworkOptions } from '../../iterator.js';
|
||||
import { FieldMapping, createIterator, createLLMTransformer, CacheConfig, INetworkOptions, transform } from '../../iterator.js';
|
||||
|
||||
/**
|
||||
* Notes for LLM modifications
|
||||
*
|
||||
* - to test it, use `npm run examples:iterator-factory`
|
||||
* - demonstrates how to use the schema format option for structured output
|
||||
* - shows transformation of string fields in complex objects
|
||||
* - shows transformation of string arrays and number arrays
|
||||
* - tests caching system with multiple runs
|
||||
* - supports the --no-cache option to force fresh responses
|
||||
*/
|
||||
|
||||
|
||||
const MODEL = E_OPENROUTER_MODEL.MODEL_OPENROUTER_QUASAR_ALPHA;
|
||||
const MODEL = E_OPENROUTER_MODEL.MODEL_OPENAI_CHATGPT_4O_LATEST;
|
||||
const ROUTER = 'openrouter';
|
||||
|
||||
// Example using the simplified transform function
|
||||
@ -22,9 +27,6 @@ export async function simpleTransformExample() {
|
||||
// Use the same example data and field mappings
|
||||
const data = JSON.parse(JSON.stringify(exampleData));
|
||||
|
||||
// Import the transform function directly
|
||||
const { transform } = await import('../../iterator.js');
|
||||
|
||||
// Minimal configuration, just pass the data and mappings
|
||||
await transform(data, fieldMappings, {
|
||||
model: MODEL,
|
||||
@ -34,6 +36,32 @@ export async function simpleTransformExample() {
|
||||
|
||||
console.log("\nSimplified Transform Result:");
|
||||
console.log(JSON.stringify(data.products.fruits[0], null, 2));
|
||||
console.log(JSON.stringify(data.numberArrays, null, 2));
|
||||
if (data.productReview && data.productReview.analysis) {
|
||||
console.log("\nSimplified Transform - Formatted Review Analysis:");
|
||||
try {
|
||||
// Parse the JSON string if needed
|
||||
const analysisJson = typeof data.productReview.analysis === 'string'
|
||||
? JSON.parse(data.productReview.analysis)
|
||||
: data.productReview.analysis;
|
||||
console.log(JSON.stringify(analysisJson, null, 2));
|
||||
} catch (e) {
|
||||
console.log(data.productReview.analysis);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\nSimplified Transform - Before/After Comparisons:");
|
||||
console.log(`Original description: ${exampleData.products.fruits[0].description}`);
|
||||
console.log(`Transformed description: ${data.products.fruits[0].description}`);
|
||||
|
||||
console.log(`\nOriginal prices: ${exampleData.numberArrays.prices}`);
|
||||
console.log(`Pricing Analysis: ${data.numberArrays.pricingAnalysis}`);
|
||||
|
||||
console.log(`\nOriginal ratings: ${exampleData.numberArrays.ratings}`);
|
||||
console.log(`Ratings Summary: ${data.numberArrays.ratingsSummary}`);
|
||||
|
||||
console.log(`\nOriginal quantities: ${exampleData.numberArrays.quantities}`);
|
||||
console.log(`Inventory Status: ${data.numberArrays.inventoryStatus}`);
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
@ -77,6 +105,14 @@ const exampleData = {
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
numberArrays: {
|
||||
prices: JSON.stringify([5, 3, 8, 12, 7]),
|
||||
ratings: JSON.stringify([4.5, 3.8, 4.2, 5.0, 4.1]),
|
||||
quantities: JSON.stringify([100, 50, 75, 30, 60])
|
||||
},
|
||||
productReview: {
|
||||
reviewText: 'Great selection of fruits with good prices and quality. Some items were out of stock.'
|
||||
}
|
||||
};
|
||||
|
||||
@ -102,6 +138,76 @@ const fieldMappings: FieldMapping[] = [
|
||||
options: {
|
||||
prompt: 'Generate a more appealing marketing name for this product'
|
||||
}
|
||||
},
|
||||
{
|
||||
jsonPath: '$.numberArrays.prices',
|
||||
targetPath: 'pricingAnalysis',
|
||||
options: {
|
||||
prompt: 'Analyze these prices and provide a brief 20-30 word market assessment'
|
||||
}
|
||||
},
|
||||
{
|
||||
jsonPath: '$.numberArrays.ratings',
|
||||
targetPath: 'ratingsSummary',
|
||||
options: {
|
||||
prompt: 'Summarize these product ratings (out of 5) in a brief 15-20 word statement'
|
||||
}
|
||||
},
|
||||
{
|
||||
jsonPath: '$.numberArrays.quantities',
|
||||
targetPath: 'inventoryStatus',
|
||||
options: {
|
||||
prompt: 'Based on these inventory quantities, provide a 15-20 word stock assessment'
|
||||
}
|
||||
},
|
||||
{
|
||||
jsonPath: '$.productReview.reviewText',
|
||||
targetPath: 'analysis',
|
||||
options: {
|
||||
// Clear and explicit prompt that includes the schema format details
|
||||
prompt: `Analyze this product review and extract key information using EXACTLY the schema specified below.
|
||||
|
||||
The review: "Great selection of fruits with good prices and quality. Some items were out of stock."
|
||||
|
||||
Your response MUST be a valid JSON object following this exact schema:
|
||||
{
|
||||
"sentiment": "positive" | "neutral" | "negative",
|
||||
"pros": ["string", "string"...], // 1-3 items
|
||||
"cons": ["string"...] // 0-3 items
|
||||
}
|
||||
|
||||
Do not add any extra fields not in the schema, and make sure to use the exact field names as specified.`,
|
||||
// Schema validation ensures structured output format
|
||||
format: {
|
||||
type: "object",
|
||||
properties: {
|
||||
sentiment: {
|
||||
type: "string",
|
||||
enum: ["positive", "neutral", "negative"],
|
||||
description: "The overall sentiment of the review"
|
||||
},
|
||||
pros: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
},
|
||||
description: "Positive aspects mentioned in the review",
|
||||
minItems: 1,
|
||||
maxItems: 3
|
||||
},
|
||||
cons: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
},
|
||||
description: "Negative aspects mentioned in the review",
|
||||
minItems: 0,
|
||||
maxItems: 3
|
||||
}
|
||||
},
|
||||
required: ["sentiment", "pros", "cons"]
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
@ -151,7 +257,11 @@ export async function factoryExample() {
|
||||
filterCallback: async () => true,
|
||||
transformerFactory: (options) => createLLMTransformer(options, logger, cacheConfig),
|
||||
logger,
|
||||
cacheConfig
|
||||
cacheConfig: {
|
||||
...cacheConfig,
|
||||
// Force a new response for format testing
|
||||
enabled: process.argv.includes('--no-cache') ? false : cacheConfig.enabled
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@ -175,6 +285,19 @@ export async function factoryExample() {
|
||||
// Print the transformed data structure
|
||||
console.log("\nTransformed data structure:");
|
||||
console.log(JSON.stringify(data.products.fruits[0], null, 2));
|
||||
console.log(JSON.stringify(data.numberArrays, null, 2));
|
||||
if (data.productReview && data.productReview.analysis) {
|
||||
console.log("\nFormatted Review Analysis:");
|
||||
try {
|
||||
// Parse the JSON string if needed
|
||||
const analysisJson = typeof data.productReview.analysis === 'string'
|
||||
? JSON.parse(data.productReview.analysis)
|
||||
: data.productReview.analysis;
|
||||
console.log(JSON.stringify(analysisJson, null, 2));
|
||||
} catch (e) {
|
||||
console.log(data.productReview.analysis);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a second instance with the same data to test cache
|
||||
console.log("\n========================================");
|
||||
@ -204,6 +327,26 @@ export async function factoryExample() {
|
||||
console.log(`Transformed description: ${data2.products.fruits[0].description}`);
|
||||
console.log(`Original name: ${exampleData.products.fruits[0].name}`);
|
||||
console.log(`Marketing name: ${data2.products.fruits[0].marketingName || 'Not available'}`);
|
||||
|
||||
console.log("\nNumber Arrays Transformation Examples:");
|
||||
console.log(`Original prices: ${exampleData.numberArrays.prices}`);
|
||||
console.log(`Pricing analysis: ${data2.numberArrays.pricingAnalysis || 'Not available'}`);
|
||||
console.log(`Original ratings: ${exampleData.numberArrays.ratings}`);
|
||||
console.log(`Ratings summary: ${data2.numberArrays.ratingsSummary || 'Not available'}`);
|
||||
|
||||
if (data2.productReview && data2.productReview.analysis) {
|
||||
console.log("\nReview Analysis Example:");
|
||||
console.log(`Original review: ${exampleData.productReview.reviewText}`);
|
||||
try {
|
||||
// Parse the JSON string if needed
|
||||
const analysisJson = typeof data2.productReview.analysis === 'string'
|
||||
? JSON.parse(data2.productReview.analysis)
|
||||
: data2.productReview.analysis;
|
||||
console.log(`Analyzed review: ${JSON.stringify(analysisJson, null, 2)}`);
|
||||
} catch (e) {
|
||||
console.log(`Analyzed review: ${data2.productReview.analysis}`);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
|
||||
@ -4,25 +4,37 @@
|
||||
{
|
||||
"id": "f1",
|
||||
"name": "apple",
|
||||
"description": "A deliciously juicy fruit bursting with sweet flavor and crisp crunch.",
|
||||
"description": "A delightfully crisp and juicy fruit bursting with natural sweetness.",
|
||||
"details": {
|
||||
"color": "red",
|
||||
"origin": "Worldwide",
|
||||
"nutrition": "Rich in fiber and vitamin D, this promotes digestive health, supports gut microbiome balance, and helps maintain strong bones and immune function through enhanced calcium absorption and regulation of immune responses."
|
||||
"nutrition": "Rich in fiber and vitamin D, this food supports healthy digestion, helps regulate blood sugar levels, and promotes strong bones by enhancing calcium absorption and supporting the immune system."
|
||||
},
|
||||
"marketingName": "Crimson Orchard Delight"
|
||||
"marketingName": "Sure! Here are a few appealing marketing name options for \"apple\":\n\n1. Crimson Bliss\n2. Orchard Jewel\n3. Scarlet Crunch\n4. Nature’s Candy\n5. Ruby Crisp\n6. SweetHarvest\n7. Eden Bite\n8. Golden Orchard (if it’s a yellow variety)\n9. PurePom\n10. FreshMuse\n\nLet me know if you'd like names tailored to a specific apple variety or target audience!"
|
||||
},
|
||||
{
|
||||
"id": "f2",
|
||||
"name": "banana",
|
||||
"description": "A vibrant, sweet, sun-ripened yellow tropical fruit bursting with flavor",
|
||||
"description": "A sweet, sun-ripened yellow fruit bursting with tropical flavor.",
|
||||
"details": {
|
||||
"color": "yellow",
|
||||
"origin": "Southeast Asia",
|
||||
"nutrition": "High in potassium, which helps regulate blood pressure, supports proper muscle and nerve function, and maintains fluid balance in the body, contributing to overall heart health and reduced risk of stroke."
|
||||
"nutrition": "High in potassium, which helps regulate blood pressure, supports proper muscle function, and maintains fluid balance in the body, contributing to overall cardiovascular and muscular health."
|
||||
},
|
||||
"marketingName": "Golden Tropic Delight"
|
||||
"marketingName": "Golden Delight"
|
||||
}
|
||||
]
|
||||
},
|
||||
"numberArrays": {
|
||||
"prices": "[5,3,8,12,7]",
|
||||
"ratings": "[4.5,3.8,4.2,5,4.1]",
|
||||
"quantities": "[100,50,75,30,60]",
|
||||
"pricingAnalysis": "Prices show moderate volatility with a peak at 12 and a low at 3, indicating fluctuating demand. Overall trend is slightly upward with intermittent dips, suggesting cautious market optimism.",
|
||||
"ratingsSummary": "Average rating is 4.3, indicating generally positive user feedback with minor variations in satisfaction.",
|
||||
"inventoryStatus": "Stock levels are moderately balanced, though the lowest item at 30 units may need restocking soon."
|
||||
},
|
||||
"productReview": {
|
||||
"reviewText": "Great selection of fruits with good prices and quality. Some items were out of stock.",
|
||||
"analysis": "{\n \"sentiment\": \"positive\",\n \"pros\": [\"great selection of fruits\", \"good prices\", \"good quality\"],\n \"cons\": [\"some items were out of stock\"]\n}"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user