|
26 | 26 | },
|
27 | 27 | {
|
28 | 28 | "cell_type": "code",
|
29 |
| - "execution_count": 3, |
| 29 | + "execution_count": 2, |
30 | 30 | "id": "17d1306e",
|
31 | 31 | "metadata": {},
|
32 | 32 | "outputs": [],
|
|
41 | 41 | },
|
42 | 42 | {
|
43 | 43 | "cell_type": "code",
|
44 |
| - "execution_count": 4, |
| 44 | + "execution_count": 3, |
45 | 45 | "id": "0e745d99",
|
46 | 46 | "metadata": {},
|
47 | 47 | "outputs": [],
|
|
51 | 51 | },
|
52 | 52 | {
|
53 | 53 | "cell_type": "code",
|
54 |
| - "execution_count": 5, |
| 54 | + "execution_count": 4, |
55 | 55 | "id": "f42d79dc",
|
56 | 56 | "metadata": {},
|
57 | 57 | "outputs": [],
|
|
63 | 63 | },
|
64 | 64 | {
|
65 | 65 | "cell_type": "code",
|
66 |
| - "execution_count": 6, |
| 66 | + "execution_count": 5, |
67 | 67 | "id": "8aa571ae",
|
68 | 68 | "metadata": {},
|
69 | 69 | "outputs": [],
|
|
73 | 73 | },
|
74 | 74 | {
|
75 | 75 | "cell_type": "code",
|
76 |
| - "execution_count": 8, |
| 76 | + "execution_count": 6, |
77 | 77 | "id": "aa859d4c",
|
78 | 78 | "metadata": {},
|
79 | 79 | "outputs": [],
|
80 | 80 | "source": [
|
81 | 81 | "from langchain import OpenAI\n",
|
82 | 82 | "\n",
|
83 |
| - "chain = VectorDBQAWithSourcesChain.from_llm(OpenAI(temperature=0), vectorstore=docsearch)" |
| 83 | + "chain = VectorDBQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type=\"stuff\", vectorstore=docsearch)" |
84 | 84 | ]
|
85 | 85 | },
|
86 | 86 | {
|
87 | 87 | "cell_type": "code",
|
88 |
| - "execution_count": 9, |
| 88 | + "execution_count": 7, |
89 | 89 | "id": "8ba36fa7",
|
90 | 90 | "metadata": {},
|
91 | 91 | "outputs": [
|
92 | 92 | {
|
93 | 93 | "data": {
|
94 | 94 | "text/plain": [
|
95 |
| - "{'answer': ' The president thanked Justice Breyer for his service.',\n", |
| 95 | + "{'answer': ' The president thanked Justice Breyer for his service.\\n',\n", |
| 96 | + " 'sources': '30-pl'}" |
| 97 | + ] |
| 98 | + }, |
| 99 | + "execution_count": 7, |
| 100 | + "metadata": {}, |
| 101 | + "output_type": "execute_result" |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "chain({\"question\": \"What did the president say about Justice Breyer\"}, return_only_outputs=True)" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "id": "718ecbda", |
| 111 | + "metadata": {}, |
| 112 | + "source": [ |
| 113 | + "## Chain Type\n", |
| 114 | + "You can easily specify different chain types to load and use in the VectorDBQAWithSourcesChain chain. For a more detailed walkthrough of these types, please see [this notebook](qa_with_sources.ipynb).\n", |
| 115 | + "\n", |
| 116 | + "There are two ways to load different chain types. First, you can specify the chain type argument in the `from_chain_type` method. This allows you to pass in the name of the chain type you want to use. For example, in the below we change the chain type to `map_reduce`." |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 8, |
| 122 | + "id": "8b35b30a", |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [], |
| 125 | + "source": [ |
| 126 | + "chain = VectorDBQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type=\"map_reduce\", vectorstore=docsearch)" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": 9, |
| 132 | + "id": "58bd424f", |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/plain": [ |
| 138 | + "{'answer': ' The president honored Justice Stephen Breyer for his service.\\n',\n", |
96 | 139 | " 'sources': '30-pl'}"
|
97 | 140 | ]
|
98 | 141 | },
|
|
104 | 147 | "source": [
|
105 | 148 | "chain({\"question\": \"What did the president say about Justice Breyer\"}, return_only_outputs=True)"
|
106 | 149 | ]
|
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "markdown", |
| 153 | + "id": "21e14eed", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "The above way allows you to really simply change the chain_type, but it does provide a ton of flexibility over parameters to that chain type. If you want to control those parameters, you can load the chain directly (as you did in [this notebook](qa_with_sources.ipynb)) and then pass that directly to the the VectorDBQA chain with the `combine_documents_chain` parameter. For example:" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 12, |
| 162 | + "id": "af35f0c6", |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n", |
| 167 | + "qa_chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type=\"stuff\")\n", |
| 168 | + "qa = VectorDBQAWithSourcesChain(combine_document_chain=qa_chain, vectorstore=docsearch)" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 11, |
| 174 | + "id": "c91fdc8a", |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [ |
| 177 | + { |
| 178 | + "data": { |
| 179 | + "text/plain": [ |
| 180 | + "{'answer': ' The president honored Justice Stephen Breyer for his service.\\n',\n", |
| 181 | + " 'sources': '30-pl'}" |
| 182 | + ] |
| 183 | + }, |
| 184 | + "execution_count": 11, |
| 185 | + "metadata": {}, |
| 186 | + "output_type": "execute_result" |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "chain({\"question\": \"What did the president say about Justice Breyer\"}, return_only_outputs=True)" |
| 191 | + ] |
107 | 192 | }
|
108 | 193 | ],
|
109 | 194 | "metadata": {
|
110 | 195 | "kernelspec": {
|
111 |
| - "display_name": "Python 3.9.0 64-bit ('llm-env')", |
| 196 | + "display_name": "Python 3 (ipykernel)", |
112 | 197 | "language": "python",
|
113 | 198 | "name": "python3"
|
114 | 199 | },
|
|
122 | 207 | "name": "python",
|
123 | 208 | "nbconvert_exporter": "python",
|
124 | 209 | "pygments_lexer": "ipython3",
|
125 |
| - "version": "3.9.0" |
| 210 | + "version": "3.10.9" |
126 | 211 | },
|
127 | 212 | "vscode": {
|
128 | 213 | "interpreter": {
|
|
0 commit comments