scrimba
Learn Langchain
Adding the first chain
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

AboutCommentsNotes
Adding the first chain
Expand for more info
index.js
run
preview
console
import { ChatOpenAI } from "langchain/chat_models/openai"
import { PromptTemplate } from "langchain/prompts"

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

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

/**
* Challenge:
* 1. Create a prompt to turn a user's question into a
* standalone question. (Hint: the AI understands
* the concept of a standalone question. You don't
* need to explain it, just ask for it.)
* 2. Create a chain with the prompt and the model.
* 3. Invoke the chain remembering to pass in a question.
* 4. Log out the response.
* **/

// A string holding the phrasing of the prompt
const standaloneQuestionTemplate

// A prompt created using PromptTemplate and the fromTemplate method
const standaloneQuestionPrompt

// Take the standaloneQuestionPrompt and PIPE the model
const standaloneQuestionChain

// Await the response when you INVOKE the chain.
// Remember to pass in a question.
const response

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
!
SyntaxError: Missing initializer in const declaration
,
!
SyntaxError: Missing initializer in const declaration
,
!
SyntaxError: Missing initializer in const declaration
,
/index.html
-3:59