Skip to content

Commit

Permalink
[Spelunker] Add References to the end of the main answer (if any) (#740)
Browse files Browse the repository at this point in the history
Also lint chunker.py, since apparently the lint rules have changed?
  • Loading branch information
gvanrossum-ms authored Feb 20, 2025
1 parent d5b4ab3 commit e7ab730
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion ts/packages/agents/spelunker/src/chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ def main():
items.append(result)
else:
# Only keep non-empty chunks, and the root node (empty parentId).
chunks = [chunk for chunk in result if chunk.blobs or not chunk.parentId]
chunks = [
chunk for chunk in result if chunk.blobs or not chunk.parentId
]
items.append(ChunkedFile(filename, chunks))

print(json.dumps(items, default=custom_json, indent=2))
Expand Down
22 changes: 20 additions & 2 deletions ts/packages/agents/spelunker/src/searchCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import {
} from "@typeagent/agent-sdk/helpers/action";

import { keepBestChunks, makeBatches } from "./batching.js";
import { Blob, Chunk, ChunkedFile, ChunkerErrorItem } from "./chunkSchema.js";
import {
Blob,
Chunk,
ChunkId,
ChunkedFile,
ChunkerErrorItem,
} from "./chunkSchema.js";
import { createDatabase, purgeFile } from "./databaseUtils.js";
import { loadEmbeddings, preSelectChunks } from "./embeddings.js";
import { console_log, resetEpoch } from "./logging.js";
Expand Down Expand Up @@ -71,7 +77,8 @@ export async function searchCode(

// 6. Extract answer from result.
const result = wrappedResult.data;
const answer = result.answer;
const answer =
result.answer.trimEnd() + formatReferences(result.references);

// 7. Produce entities and an action result from the result.
const outputEntities = produceEntitiesFromResult(result, allChunks, db);
Expand All @@ -84,6 +91,17 @@ export async function searchCode(
);
}

function formatReferences(references: ChunkId[]): string {
if (!references.length) return "";
const answer: string[] = ["\n\nReferences: "];
let prefix: string = " ";
for (const ref of references) {
answer.push(`${prefix}${ref}`);
prefix = ", ";
}
return answer.join("");
}

async function readAllChunksFromDatabase(
db: sqlite.Database,
): Promise<Chunk[]> {
Expand Down

0 comments on commit e7ab730

Please sign in to comment.