Created
July 20, 2023 20:23
-
-
Save iceener/7a77dce7b9b1b109b169628a89178983 to your computer and use it in GitHub Desktop.
LangChain Playwright
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
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