Skip to content

Commit e4185bc

Browse files
RAG using Gemini and Elasticsearch (#162)
* Adding Question and answer example using Gemini, Elasticsearch and LangChain * Adding Question and answer example using Gemini, Elasticsearch and LangChain * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Update notebooks/integrations/gemini/qa-langchain-gemini-elasticsearch.ipynb Co-authored-by: Max Jakob <[email protected]> * Suggested changes --------- Co-authored-by: Max Jakob <[email protected]>
1 parent 7c890ec commit e4185bc

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2a3143e8-3949-4ecc-905c-8333a43c9c87",
6+
"metadata": {},
7+
"source": [
8+
"# Question Answering using Gemini, Langchain & Elasticsearch\n",
9+
"\n",
10+
"This tutorial demonstrates how to use the [Gemini API](https://ai.google.dev/docs) to create [embeddings](https://ai.google.dev/docs/embeddings_guide) and store them in Elasticsearch. We will learn how to connect Gemini to private data stored in Elasticsearch and build question/answer capabilities over it using [LangChian](https://python.langchain.com/docs/get_started/introduction)."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "68c5e34d-28f9-4195-9f9c-2a8aec1effe6",
16+
"metadata": {},
17+
"source": [
18+
"## setup\n",
19+
"\n",
20+
"* Elastic Credentials - Create an [Elastic Cloud deployment](https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud) to get all Elastic credentials (`ELASTIC_CLOUD_ID`, `ELASTIC_API_KEY`).\n",
21+
"\n",
22+
"* `GOOGLE_API_KEY` - To use the Gemini API, you need to [create an API key in Google AI Studio](https://ai.google.dev/tutorials/setup)."
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"id": "b8e9a58a-942f-4039-96c0-b276d5b8a97f",
28+
"metadata": {},
29+
"source": [
30+
"## Install packages"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "5c4781ec-06a5-48dd-963e-fb832b3f7ca2",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"pip install -q -U google-generativeai elasticsearch langchain langchain_google_genai"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"id": "851db243-ca7d-4a7c-a93b-d22ab149a1bb",
46+
"metadata": {},
47+
"source": [
48+
"## Import packages and credentials"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"id": "26e7f569-c680-447b-9246-b5140ff47b6b",
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"import json\n",
59+
"import os\n",
60+
"from getpass import getpass\n",
61+
"from urllib.request import urlopen\n",
62+
"\n",
63+
"from elasticsearch import Elasticsearch, helpers\n",
64+
"from langchain.vectorstores import ElasticsearchStore\n",
65+
"from langchain.text_splitter import CharacterTextSplitter\n",
66+
"from langchain_google_genai import GoogleGenerativeAIEmbeddings\n",
67+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
68+
"from langchain.prompts import ChatPromptTemplate\n",
69+
"from langchain.schema.output_parser import StrOutputParser\n",
70+
"from langchain.schema.runnable import RunnablePassthrough"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"id": "b2f68db5-21ac-47b0-941b-1d816b586e18",
76+
"metadata": {},
77+
"source": [
78+
"## Get Credentials"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"id": "543d27f4-2c53-4726-a324-716900d72338",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"os.environ[\"GOOGLE_API_KEY\"] = getpass(\"Google API Key :\")\n",
89+
"ELASTIC_API_KEY = getpass(\"Elastic API Key :\")\n",
90+
"ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID :\")\n",
91+
"elastic_index_name = \"gemini-qa\""
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "e8bd47b9-b946-46d1-ba02-adbda118415a",
97+
"metadata": {},
98+
"source": [
99+
"## Add documents"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"id": "bd04e921-206e-4c8b-937a-277d2c5a02e6",
105+
"metadata": {},
106+
"source": [
107+
"### Let's download the sample dataset and deserialize the document."
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"id": "44a5b79d-326e-4317-82a3-7918a11ff7b7",
114+
"metadata": {},
115+
"outputs": [],
116+
"source": [
117+
"url = \"https://raw.githubusercontent.com/ashishtiwari1993/langchain-elasticsearch-RAG/main/data.json\"\n",
118+
"\n",
119+
"response = urlopen(url)\n",
120+
"\n",
121+
"workplace_docs = json.loads(response.read())"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "8591dce2-3fe6-4c87-b268-4694bb86e803",
127+
"metadata": {},
128+
"source": [
129+
"### Split Documents into Passages"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"id": "3963b0db-80d5-4908-897c-bec6357adc0a",
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"metadata = []\n",
140+
"content = []\n",
141+
"\n",
142+
"for doc in workplace_docs:\n",
143+
" content.append(doc[\"content\"])\n",
144+
" metadata.append({\n",
145+
" \"name\": doc[\"name\"],\n",
146+
" \"summary\": doc[\"summary\"],\n",
147+
" \"rolePermissions\":doc[\"rolePermissions\"]\n",
148+
" })\n",
149+
"\n",
150+
"text_splitter = CharacterTextSplitter(chunk_size=50, chunk_overlap=0)\n",
151+
"docs = text_splitter.create_documents(content, metadatas=metadata)"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"id": "a066d5dc-dbc9-495f-934f-bfe96e0fdeec",
157+
"metadata": {},
158+
"source": [
159+
"## Index Documents into Elasticsearch using Gemini Embeddings"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"id": "42ba370a-e4b4-4375-b71a-2aee7c40a330",
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"query_embedding = GoogleGenerativeAIEmbeddings(\n",
170+
" model=\"models/embedding-001\", task_type=\"retrieval_document\"\n",
171+
")\n",
172+
"\n",
173+
"es = ElasticsearchStore.from_documents(\n",
174+
" docs,\n",
175+
" es_cloud_id=ELASTIC_CLOUD_ID,\n",
176+
" es_api_key=ELASTIC_API_KEY,\n",
177+
" index_name=elastic_index_name,\n",
178+
" embedding=query_embedding\n",
179+
")"
180+
]
181+
},
182+
{
183+
"cell_type": "markdown",
184+
"id": "dbdb2d55-3349-4e95-8087-68f927f0d864",
185+
"metadata": {},
186+
"source": [
187+
"## Create a retriever using Elasticsearch"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"id": "17920c1e-9228-42f5-893d-29b666d6f7b2",
194+
"metadata": {},
195+
"outputs": [],
196+
"source": [
197+
"query_embedding = GoogleGenerativeAIEmbeddings(\n",
198+
" model=\"models/embedding-001\", task_type=\"retrieval_query\"\n",
199+
")\n",
200+
"\n",
201+
"es = ElasticsearchStore(\n",
202+
" es_cloud_id=ELASTIC_CLOUD_ID,\n",
203+
" es_api_key=ELASTIC_API_KEY,\n",
204+
" embedding=query_embedding,\n",
205+
" index_name=elastic_index_name\n",
206+
")\n",
207+
"\n",
208+
"retriever = es.as_retriever(search_kwargs={\"k\": 3})"
209+
]
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"id": "3647d005-d70e-4c3a-b784-052b21e9f143",
214+
"metadata": {},
215+
"source": [
216+
"## Fromat Docs"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"id": "04ee4d3d-09fb-4a35-bc66-8a2951c402a8",
223+
"metadata": {},
224+
"outputs": [],
225+
"source": [
226+
"def format_docs(docs):\n",
227+
" return \"\\n\\n\".join(doc.page_content for doc in docs)"
228+
]
229+
},
230+
{
231+
"cell_type": "markdown",
232+
"id": "864afb6a-a671-434a-bd30-006c79ccda24",
233+
"metadata": {},
234+
"source": [
235+
"## Create a Chain using Prompt Template + `gemini-pro` model"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"id": "4818aef7-3535-494d-a5d4-16ef6d0581af",
242+
"metadata": {},
243+
"outputs": [],
244+
"source": [
245+
"template = \"\"\"Answer the question based only on the following context:\\n\n",
246+
"\n",
247+
"{context}\n",
248+
"\n",
249+
"Question: {question}\n",
250+
"\"\"\"\n",
251+
"prompt = ChatPromptTemplate.from_template(template)\n",
252+
"\n",
253+
"\n",
254+
"chain = (\n",
255+
" {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()} \n",
256+
" | prompt \n",
257+
" | ChatGoogleGenerativeAI(model=\"gemini-pro\", temperature=0.7) \n",
258+
" | StrOutputParser()\n",
259+
")\n",
260+
"\n",
261+
"chain.invoke(\"what is our sales goals?\")"
262+
]
263+
}
264+
],
265+
"metadata": {
266+
"kernelspec": {
267+
"display_name": "Python 3 (ipykernel)",
268+
"language": "python",
269+
"name": "python3"
270+
},
271+
"language_info": {
272+
"codemirror_mode": {
273+
"name": "ipython",
274+
"version": 3
275+
},
276+
"file_extension": ".py",
277+
"mimetype": "text/x-python",
278+
"name": "python",
279+
"nbconvert_exporter": "python",
280+
"pygments_lexer": "ipython3",
281+
"version": "3.11.6"
282+
}
283+
},
284+
"nbformat": 4,
285+
"nbformat_minor": 5
286+
}

0 commit comments

Comments
 (0)