Spaces:
Running
Running
File size: 4,170 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import {
generateCompletions,
GenerateCompletionsOptions,
} from "../../../scraper/scrapeURL/transformers/llmExtract";
import { buildDocument } from "../build-document";
import { ExtractResponse, TokenUsage } from "../../../controllers/v1/types";
import { Document } from "../../../controllers/v1/types";
import {
buildBatchExtractPrompt,
buildBatchExtractSystemPrompt,
} from "../build-prompts";
import { getModel } from "../../generic-ai";
import { CostTracking, CostLimitExceededError } from "../../cost-tracking";
import fs from "fs/promises";
import { extractData } from "../../../scraper/scrapeURL/lib/extractSmartScrape";
import type { Logger } from "winston";
type BatchExtractOptions = {
multiEntitySchema: any;
links: string[];
prompt: string;
systemPrompt: string;
doc: Document;
useAgent: boolean;
extractId?: string;
sessionId?: string;
costTracking: CostTracking;
metadata: {
teamId: string;
functionId?: string;
extractId?: string;
scrapeId?: string;
};
};
/**
* Batch extract information from a list of URLs using a multi-entity schema.
* @param multiEntitySchema - The schema for the multi-entity extraction
* @param links - The URLs to extract information from
* @param prompt - The prompt for the extraction
* @param systemPrompt - The system prompt for the extraction
* @param doc - The document to extract information from
* @returns The completion promise
*/
export async function batchExtractPromise(
options: BatchExtractOptions,
logger: Logger,
): Promise<{
extract: any; // array of extracted data
numTokens: number;
totalUsage: TokenUsage;
warning?: string;
sources: string[];
smartScrapeCost: number;
otherCost: number;
smartScrapeCallCount: number;
otherCallCount: number;
sessionId?: string;
}> {
const {
multiEntitySchema,
links,
prompt,
systemPrompt,
doc,
useAgent,
extractId,
sessionId,
metadata,
} = options;
const generationOptions: GenerateCompletionsOptions = {
logger: logger.child({
method: "extractService/generateCompletions",
}),
options: {
systemPrompt: buildBatchExtractSystemPrompt(
systemPrompt,
multiEntitySchema,
links,
),
prompt: buildBatchExtractPrompt(prompt),
schema: multiEntitySchema,
},
markdown: buildDocument(doc),
isExtractEndpoint: true,
model: getModel("gpt-4o-mini", "openai"),
retryModel: getModel("gpt-4.1", "openai"),
costTrackingOptions: {
costTracking: options.costTracking,
metadata: {
module: "extract",
method: "batchExtractPromise",
},
},
metadata: {
...metadata,
functionId: metadata.functionId
? metadata.functionId + "/batchExtractPromise"
: "batchExtractPromise",
},
};
let extractedDataArray: any[] = [];
let warning: string | undefined;
let smCost = 0,
oCost = 0,
smCallCount = 0,
oCallCount = 0;
try {
const { extractedDataArray: e, warning: w } = await extractData({
extractOptions: generationOptions,
urls: [doc.metadata.sourceURL || doc.metadata.url || ""],
useAgent,
extractId,
sessionId,
metadata: {
...metadata,
functionId: metadata.functionId
? metadata.functionId + "/batchExtractPromise"
: "batchExtractPromise",
},
});
extractedDataArray = e;
warning = w;
} catch (error) {
if (error instanceof CostLimitExceededError) {
throw error;
}
logger.error("extractData failed", { error });
}
// await fs.writeFile(
// `logs/extractedDataArray-${crypto.randomUUID()}.json`,
// JSON.stringify(extractedDataArray, null, 2),
// );
// TODO: fix this
return {
extract: extractedDataArray,
numTokens: 0,
totalUsage: {
promptTokens: 0,
completionTokens: 0,
totalTokens: 0,
model: "gpt-4o-mini",
},
warning: warning,
sources: [doc.metadata.url || doc.metadata.sourceURL || ""],
smartScrapeCost: smCost,
otherCost: oCost,
smartScrapeCallCount: smCallCount,
otherCallCount: oCallCount,
};
}
|