-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script for contextual retrieval with document chunking in genaisrc
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
script({ | ||
system: [], | ||
files: ["src/azure-lza/azure-azure-resource-manager-bicep.pdf"], | ||
}) | ||
|
||
const doc = env.files[0] | ||
const chunks = splitTextIntoChunks(doc.content, 100) | ||
|
||
console.log( | ||
`Document: ${doc.filename}, ${doc.content.length} characters, ${chunks.length} chunks` | ||
) | ||
for (const chunk of chunks) { | ||
console.log(`chunk: ${chunk.slice(0, 25) + "..."}`) | ||
const res = await runPrompt( | ||
(_) => { | ||
_.def("DOCUMENT", doc, { maxTokens: 10000 }) | ||
$`Here is the chunk we want to situate within the whole document` | ||
_.def("CHUNK", chunk) | ||
_.$`Please give a short succinct context to situate this chunk | ||
within the overall document for the purposes of improving search retrieval of the chunk. | ||
Answer only with the succinct context and nothing else. ` | ||
}, | ||
{ cache: "cr" } | ||
) | ||
} | ||
|
||
function splitTextIntoChunks(text: string, chunkSize: number): string[] { | ||
const tokens = text.split(/\s+/) // Split text into tokens based on whitespace | ||
const chunks = [] | ||
for (let i = 0; i < tokens.length; i += chunkSize) { | ||
chunks.push(tokens.slice(i, i + chunkSize).join(" ")) | ||
} | ||
return chunks | ||
} |