Skip to content

Commit aa210d5

Browse files
overall improvement(token count and db update)
1 parent 91975ce commit aa210d5

File tree

3 files changed

+98
-164
lines changed

3 files changed

+98
-164
lines changed

lib/rag/embedding-generator.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface EmbeddingResult {
1212
embeddings: Array<{
1313
chunkId: string;
1414
embedding: number[];
15+
tokens?: number;
1516
}>;
1617
error?: string;
1718
processingTime: number;
@@ -63,7 +64,7 @@ export class EmbeddingGenerator {
6364

6465
async generateEmbeddings(chunks: EmbeddingBatch[]): Promise<EmbeddingResult> {
6566
const startTime = Date.now();
66-
const results: Array<{ chunkId: string; embedding: number[] }> = [];
67+
const results: Array<{ chunkId: string; embedding: number[]; tokens?: number }> = [];
6768
let totalTokens = 0;
6869
let cachedCount = 0;
6970
let newCount = 0;
@@ -89,14 +90,14 @@ export class EmbeddingGenerator {
8990
else groups.set(key, { content: chunk.content, chunkIds: [chunk.chunkId] });
9091
}
9192

92-
const cachedEmbeds: Array<{ chunkId: string; embedding: number[] }> = [];
93+
const cachedEmbeds: Array<{ chunkId: string; embedding: number[]; tokens?: number }> = [];
9394
const newGroups: Array<{ content: string; chunkIds: string[] }> = [];
9495

9596
for (const [, g] of groups) {
9697
const e = await this.getCachedEmbedding(g.content);
9798
if (e) {
9899
for (const id of g.chunkIds) {
99-
cachedEmbeds.push({ chunkId: id, embedding: e });
100+
cachedEmbeds.push({ chunkId: id, embedding: e, tokens: undefined });
100101
cachedCount++;
101102
}
102103
} else {
@@ -141,7 +142,11 @@ export class EmbeddingGenerator {
141142
const r = batchResult.results[i];
142143
const g = batchGroups[i];
143144
for (const id of g.chunkIds) {
144-
results.push({ chunkId: id, embedding: r.embedding });
145+
results.push({
146+
chunkId: id,
147+
embedding: r.embedding,
148+
tokens: r.tokens
149+
});
145150
}
146151
}
147152
totalTokens += batchResult.totalTokens;
@@ -228,7 +233,7 @@ export class EmbeddingGenerator {
228233
}
229234

230235
private async processBatch(chunks: EmbeddingBatch[]): Promise<{
231-
results: Array<{ chunkId: string; embedding: number[] }>;
236+
results: Array<{ chunkId: string; embedding: number[]; tokens?: number }>;
232237
totalTokens: number;
233238
}> {
234239
const inputs = chunks.map((c) => c.content);
@@ -251,14 +256,19 @@ export class EmbeddingGenerator {
251256

252257
const totalTokens = response.usage?.total_tokens ?? 0;
253258

259+
254260
const results = response.data.map((item, idx) => {
255261
const chunk = chunks[idx];
256262
if (!chunk) {
257263
throw RAGError.create('validation', `No chunk found for index ${idx}`, { field: 'chunk' });
258264
}
265+
266+
const chunkTokens = this.calculateChunkTokens(chunk.content, totalTokens, inputs);
267+
259268
return {
260269
chunkId: chunk.chunkId,
261270
embedding: item.embedding,
271+
tokens: chunkTokens,
262272
};
263273
});
264274

@@ -273,6 +283,16 @@ export class EmbeddingGenerator {
273283
}
274284
}
275285

286+
private calculateChunkTokens(chunkContent: string, totalTokens: number, allInputs: string[]): number {
287+
if (totalTokens === 0 || allInputs.length === 0) return 0;
288+
let totalChars = 0;
289+
for (const input of allInputs) {
290+
totalChars += input.length;
291+
}
292+
if (totalChars === 0) return 0;
293+
return Math.round((chunkContent.length / totalChars) * totalTokens);
294+
}
295+
276296
private async enforceRateLimit(): Promise<void> {
277297
const now = Date.now();
278298

lib/rag/queue-manager.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,6 @@ export class RAGQueueManager {
171171
}
172172
}
173173

174-
175-
static async hasPendingRequests(dataroomId: string): Promise<boolean> {
176-
this.validateInput(dataroomId);
177-
178-
try {
179-
const count = await this.getQueueLength(dataroomId);
180-
return count > 0;
181-
} catch (error) {
182-
logger.error("Failed to check pending requests", {
183-
dataroomId,
184-
error: getErrorMessage(error)
185-
});
186-
return false;
187-
}
188-
}
189-
190174
static async getQueueLength(dataroomId: string): Promise<number> {
191175
this.validateInput(dataroomId);
192176

0 commit comments

Comments
 (0)