scrimba
Learn Langchain
Fetching the answer: the template
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Fetching the answer: the template
AboutCommentsNotes
Fetching the answer: the template
Expand for more info
index.js
run
preview
console
import { ChatOpenAI } from 'langchain/chat_models/openai'
import { PromptTemplate } from 'langchain/prompts'
import { StringOutputParser } from 'langchain/schema/output_parser'
import { retriever } from '/utils/retriever'

document.addEventListener('submit', (e) => {
e.preventDefault()
progressConversation()
})

const openAIApiKey = process.env.OPENAI_API_KEY
const llm = new ChatOpenAI({ openAIApiKey })

const standaloneQuestionTemplate = 'Given a question, convert it to a standalone question. question: {question} standalone question:'

const standaloneQuestionPrompt = PromptTemplate.fromTemplate(standaloneQuestionTemplate)

/**
* Challenge:
* 1. Create a template and prompt to get an answer to
* the user's original question. Remember to include
* the original question and the text chunks we got
* back from the vector store as input variables. Call
* these input variables 'original_question' and 'context'.
* ⚠️ Feel free to add this to the chain, but you will get
* an error.
*
* We want this chatbot to:
* - be friendly
* - only answer from the context provided and never make up
* answers
* - apologise if it doesn't know the answer and advise the
* user to email help@scrimba.com
*/

const chain = standaloneQuestionPrompt.pipe(llm).pipe(new StringOutputParser()).pipe(retriever)

const response = await chain.invoke({
question: 'What are the technical requirements for running Scrimba? I only have a very old laptop which is not that powerful.'
})

console.log(response)

async function progressConversation() {
const userInput = document.getElementById('user-input')
const chatbotConversation = document.getElementById('chatbot-conversation-container')
const question = userInput.value
userInput.value = ''

// add human message
const newHumanSpeechBubble = document.createElement('div')
newHumanSpeechBubble.classList.add('speech', 'speech-human')
chatbotConversation.appendChild(newHumanSpeechBubble)
newHumanSpeechBubble.textContent = question
chatbotConversation.scrollTop = chatbotConversation.scrollHeight

// add AI message
const newAiSpeechBubble = document.createElement('div')
newAiSpeechBubble.classList.add('speech', 'speech-ai')
chatbotConversation.appendChild(newAiSpeechBubble)
newAiSpeechBubble.textContent = result
chatbotConversation.scrollTop = chatbotConversation.scrollHeight
}
Console
[
Document {pageContent:
"What are the technical requir..."
, metadata:
{loc:
{lines:
{to:
31
, from:
30
}
}
}
}
,
Document {pageContent:
"What is the cost of the cours..."
, metadata:
{loc:
{lines:
{to:
28
, from:
24
}
}
}
}
,
Document {pageContent:
"I have a question which isn’t..."
, metadata:
{loc:
{lines:
{to:
251
, from:
240
}
}
}
}
,
Document {pageContent:
"We frequently invite Scrimba ..."
, metadata:
{loc:
{lines:
{to:
66
, from:
66
}
}
}
}
]
,
/index.html
-5:21