feat: add support for Claude LLM integration across multiple modules
- Introduced `useClaude` option in `AnalysisPipelineOptions` to toggle Claude LLM usage. - Updated `processProductChunk` and `analyzeProducts` functions to accept and handle `useClaude` parameter. - Modified argument parsing in various scripts (`bestsellers-by-category`, `mid-range-sellers-by-category`, `top-monthly-sold-by-category`, etc.) to include `--claude` flag. - Enhanced `analyzeProductsInternal` to differentiate between LLM providers and handle requests to Claude API. - Added error handling for Claude API responses and ensured proper configuration for using Claude. - Updated documentation and usage messages to reflect the new `--claude` flag.
This commit is contained in:
@@ -26,6 +26,7 @@ type ParsedArgs = {
|
||||
categoryLimit: number;
|
||||
perCategoryTop: number;
|
||||
blacklistFile: string;
|
||||
useClaude: boolean;
|
||||
};
|
||||
|
||||
type CategoryRunSummary = {
|
||||
@@ -72,6 +73,7 @@ function log(
|
||||
|
||||
function parseArgs(): ParsedArgs {
|
||||
const args = process.argv.slice(2);
|
||||
const useClaude = hasFlag(args, "--claude");
|
||||
const outputDir =
|
||||
readFlagValue(args, "--out-dir") ?? path.join(process.cwd(), "output");
|
||||
const blacklistFile =
|
||||
@@ -100,9 +102,14 @@ function parseArgs(): ParsedArgs {
|
||||
categoryLimit,
|
||||
perCategoryTop,
|
||||
blacklistFile,
|
||||
useClaude,
|
||||
};
|
||||
}
|
||||
|
||||
function hasFlag(args: string[], flag: string): boolean {
|
||||
return args.includes(flag);
|
||||
}
|
||||
|
||||
function readFlagValue(args: string[], flag: string): string | undefined {
|
||||
const idx = args.indexOf(flag);
|
||||
if (idx === -1) return undefined;
|
||||
@@ -118,7 +125,7 @@ function printUsageAndExit(message: string): never {
|
||||
"error",
|
||||
[
|
||||
"Usage:",
|
||||
" bun run src/bestsellers-by-category.ts [--category-limit 32] [--per-category-top 100] [--out-dir output] [--blacklist-file category-blacklist.csv]",
|
||||
" bun run src/bestsellers-by-category.ts [--category-limit 32] [--per-category-top 100] [--out-dir output] [--blacklist-file category-blacklist.csv] [--claude]",
|
||||
"",
|
||||
"Flow:",
|
||||
" 1) Discover categories and round-robin selection.",
|
||||
@@ -1011,6 +1018,7 @@ export async function processCategory(
|
||||
runId: number,
|
||||
category: CategoryInfo,
|
||||
perCategoryTop: number,
|
||||
useClaude = false,
|
||||
): Promise<CategoryRunSummary> {
|
||||
log("info", `\nCategory ${category.label} (${category.id})`);
|
||||
|
||||
@@ -1106,7 +1114,7 @@ export async function processCategory(
|
||||
|
||||
let batchVerdicts: LlmVerdict[];
|
||||
try {
|
||||
batchVerdicts = await analyzeProducts(batch);
|
||||
batchVerdicts = await analyzeProducts(batch, { useClaude });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
log("warn", ` LLM batch failed: ${message}`);
|
||||
@@ -1192,7 +1200,7 @@ export async function main(): Promise<void> {
|
||||
|
||||
mkdirSync(args.outputDir, { recursive: true });
|
||||
const DB_PATH =
|
||||
process.env.RESULTS_DB_PATH || path.join(process.cwd(), "db", "results.db");
|
||||
process.env.RESULTS_DB_PATH || path.join(process.cwd(), "db", "results.db");
|
||||
initDb(DB_PATH);
|
||||
const db = getDb(DB_PATH);
|
||||
|
||||
@@ -1249,6 +1257,7 @@ export async function main(): Promise<void> {
|
||||
runId,
|
||||
category,
|
||||
args.perCategoryTop,
|
||||
args.useClaude,
|
||||
);
|
||||
|
||||
totalInsertedAsins += categorySummary.results?.length ?? 0;
|
||||
|
||||
Reference in New Issue
Block a user