Skip to content

Instantly share code, notes, and snippets.

@iceener
Created July 20, 2023 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iceener/7a77dce7b9b1b109b169628a89178983 to your computer and use it in GitHub Desktop.
Save iceener/7a77dce7b9b1b109b169628a89178983 to your computer and use it in GitHub Desktop.
LangChain Playwright
const embeddings = new OpenAIEmbeddings();
const loader = new PlaywrightWebBaseLoader(url, {
launchOptions: { headless: true },
gotoOptions: { waitUntil: 'networkidle' },
});
const [html] = await loader.load();
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 2500,
chunkOverlap: 1,
});
const docs = await splitter.createDocuments([
html.pageContent.replace(/<[^>]+>/g, ''),
]);
const vectorStore = await MemoryVectorStore.fromDocuments(docs, embeddings);
// Search for the most similar document
const context = await vectorStore.similaritySearch(
'Simple clone function\n',
3,
);
console.log(context);
const chat = new ChatOpenAI({
modelName: 'gpt-4',
});
const response = await chat.call([
new SystemMessage(
`
Answer questions as truthfully as possible using the context below and nothing more. Return properly formatted markdown response grammatically correct and ultra-readable.
Pay attention that context may be not formatted properly, so you may need to fix it. Wrap code with triple backticks.
context###
${context.map((doc) => doc.pageContent).join('\n\n')}
###
`,
),
new HumanMessage('how to move a node 10 pixels to the right:'),
]);
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment