Skip to content

Commit

Permalink
handle fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishi Raj Jain committed Aug 20, 2023
1 parent 40ba893 commit b1a22f1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/generateTutorials.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'node:path'
import { appendFileSync, existsSync } from 'node:fs'
import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'node:fs'
import { z } from 'zod'
import * as dotenv from 'dotenv'
import { Document } from 'langchain/document'
Expand All @@ -21,14 +21,16 @@ async function loadVectorStore() {
}

async function chat(input, pluginPath) {
const outputFile = join(process.cwd(), 'src', 'content', 'plugins-tutorials', `${pluginPath}.md`)
const currentContent = readFileSync(outputFile, 'utf8')
try {
const vectorStore = await loadVectorStore()
writeFileSync(outputFile, '', 'utf8')
const model = new ChatOpenAI({
streaming: true,
callbacks: [
{
handleLLMNewToken(token) {
const outputFile = join(process.cwd(), 'src', 'content', 'plugins-tutorials', `${pluginPath}.md`)
appendFileSync(outputFile, token)
},
},
Expand All @@ -50,6 +52,7 @@ async function chat(input, pluginPath) {
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever(), prompt)
await chain.call({ query: input })
} catch (error) {
writeFileSync(outputFile, currentContent, 'utf8')
console.error(error.message || error.toString())
}
}
Expand Down

0 comments on commit b1a22f1

Please sign in to comment.