|
69 | 69 | },
|
70 | 70 | {
|
71 | 71 | "cell_type": "code",
|
72 |
| - "execution_count": 3, |
| 72 | + "execution_count": null, |
73 | 73 | "metadata": {},
|
74 | 74 | "outputs": [],
|
75 | 75 | "source": [
|
76 | 76 | "import \"cheerio\";\n",
|
77 |
| - "import { CheerioWebBaseLoader } from \"langchain/document_loaders/web/cheerio\";\n", |
| 77 | + "import { CheerioWebBaseLoader } from \"@langchain/community/document_loaders/web/cheerio\";\n", |
78 | 78 | "import { RecursiveCharacterTextSplitter } from \"langchain/text_splitter\";\n",
|
79 | 79 | "import { MemoryVectorStore } from \"langchain/vectorstores/memory\"\n",
|
80 | 80 | "import { OpenAIEmbeddings, ChatOpenAI } from \"@langchain/openai\";\n",
|
|
119 | 119 | },
|
120 | 120 | {
|
121 | 121 | "cell_type": "code",
|
122 |
| - "execution_count": 4, |
| 122 | + "execution_count": 2, |
123 | 123 | "metadata": {},
|
124 | 124 | "outputs": [
|
125 | 125 | {
|
|
139 | 139 | },
|
140 | 140 | {
|
141 | 141 | "cell_type": "code",
|
142 |
| - "execution_count": 11, |
| 142 | + "execution_count": 3, |
143 | 143 | "metadata": {},
|
144 | 144 | "outputs": [
|
145 | 145 | {
|
146 | 146 | "data": {
|
147 | 147 | "text/plain": [
|
148 |
| - "\u001b[32m\"Task decomposition is a technique used to break down complex tasks into smaller and simpler steps. I\"\u001b[39m... 208 more characters" |
| 148 | + "\u001b[32m\"Task decomposition is a technique used to break down complex tasks into smaller and simpler steps. T\"\u001b[39m... 287 more characters" |
149 | 149 | ]
|
150 | 150 | },
|
151 |
| - "execution_count": 11, |
| 151 | + "execution_count": 3, |
152 | 152 | "metadata": {},
|
153 | 153 | "output_type": "execute_result"
|
154 | 154 | }
|
|
213 | 213 | " }\n",
|
214 | 214 | " }\n",
|
215 | 215 | " ],\n",
|
216 |
| - " answer: \u001b[32m\"Task decomposition is a technique used to break down complex tasks into smaller and simpler steps. I\"\u001b[39m... 256 more characters\n", |
| 216 | + " answer: \u001b[32m\"Task decomposition is a technique used to break down complex tasks into smaller and simpler steps fo\"\u001b[39m... 232 more characters\n", |
217 | 217 | "}"
|
218 | 218 | ]
|
219 | 219 | },
|
|
223 | 223 | }
|
224 | 224 | ],
|
225 | 225 | "source": [
|
226 |
| - "import { RunnableMap, RunnablePassthrough, RunnableSequence } from \"@langchain/core/runnables\";\n", |
| 226 | + "import {\n", |
| 227 | + " RunnableMap,\n", |
| 228 | + " RunnablePassthrough,\n", |
| 229 | + " RunnableSequence\n", |
| 230 | + "} from \"@langchain/core/runnables\";\n", |
227 | 231 | "import { formatDocumentsAsString } from \"langchain/util/document\";\n",
|
228 | 232 | "\n",
|
229 |
| - "const ragChainFromDocs = RunnableSequence.from([\n", |
230 |
| - " RunnablePassthrough.assign({ context: (input) => formatDocumentsAsString(input.context) }),\n", |
231 |
| - " prompt,\n", |
232 |
| - " llm,\n", |
233 |
| - " new StringOutputParser()\n", |
234 |
| - "]);\n", |
235 |
| - "\n", |
236 |
| - "let ragChainWithSource = new RunnableMap({ steps: { context: retriever, question: new RunnablePassthrough() }})\n", |
237 |
| - "ragChainWithSource = ragChainWithSource.assign({ answer: ragChainFromDocs });\n", |
| 233 | + "const ragChainWithSources = RunnableMap.from({\n", |
| 234 | + " // Return raw documents here for now since we want to return them at\n", |
| 235 | + " // the end - we'll format in the next step of the chain\n", |
| 236 | + " context: retriever,\n", |
| 237 | + " question: new RunnablePassthrough(),\n", |
| 238 | + "}).assign({\n", |
| 239 | + " answer: RunnableSequence.from([\n", |
| 240 | + " (input) => {\n", |
| 241 | + " return {\n", |
| 242 | + " // Now we format the documents as strings for the prompt\n", |
| 243 | + " context: formatDocumentsAsString(input.context),\n", |
| 244 | + " question: input.question\n", |
| 245 | + " };\n", |
| 246 | + " },\n", |
| 247 | + " prompt,\n", |
| 248 | + " llm,\n", |
| 249 | + " new StringOutputParser()\n", |
| 250 | + " ]),\n", |
| 251 | + "})\n", |
238 | 252 | "\n",
|
239 |
| - "await ragChainWithSource.invoke(\"What is Task Decomposition\")" |
| 253 | + "await ragChainWithSources.invoke(\"What is Task Decomposition\")" |
240 | 254 | ]
|
241 | 255 | },
|
242 | 256 | {
|
|
0 commit comments