Professional Documents
Culture Documents
Advanced RAG Part - 1
Advanced RAG Part - 1
ipynb - Colaboratory
import os
os.environ['GOOGLE_API_KEY']=""
Name: langchain
Version: 0.1.5
Summary: Building applications with LLMs through composability
Home-page: https://github.com/langchain-ai/langchain
Author:
Author-email:
License: MIT
Location: /usr/local/lib/python3.10/dist-packages
Requires: aiohttp, async-timeout, dataclasses-json, jsonpatch, langchain-community, langchain-core, langsmith, numpy, pydantic, PyYA
Required-by: langchain-experimental
---
Name: langchain-core
Version: 0.1.18
Summary: Building applications with LLMs through composability
Home-page: https://github.com/langchain-ai/langchain
Author:
Author-email:
License: MIT
Location: /usr/local/lib/python3.10/dist-packages
Requires: anyio, jsonpatch, langsmith, packaging, pydantic, PyYAML, requests, tenacity
Required-by: langchain, langchain-community, langchain-experimental, langchain-google-genai
https://colab.research.google.com/drive/1vZDBWt2ju9nn3IBrBVy52RQdb_cGrhZP?authuser=0#scrollTo=7kb_Bl4soqAc&printMode=true 1/4
2/3/24, 11:16 PM Advances-RAG-Part-1.ipynb - Colaboratory
import os
import google.generativeai as genai
genai.configure(api_key="AIzaSyAEobksWFeT3riDxlp5kBPbpr9JClzuW54")
docs = [
Document(
page_content="Complex, layered, rich red with dark fruit flavors",
metadata={"name":"Opus One", "year": 2018, "rating": 96, "grape": "Cabernet Sauvignon", "color":"red", "country":"USA"},
),
Document(
page_content="Luxurious, sweet wine with flavors of honey, apricot, and peach",
metadata={"name":"Château d'Yquem", "year": 2015, "rating": 98, "grape": "Sémillon", "color":"white", "country":"France"},
),
Document(
page_content="Full-bodied red with notes of black fruit and spice",
metadata={"name":"Penfolds Grange", "year": 2017, "rating": 97, "grape": "Shiraz", "color":"red", "country":"Australia"},
),
Document(
page_content="Elegant, balanced red with herbal and berry nuances",
metadata={"name":"Sassicaia", "year": 2016, "rating": 95, "grape": "Cabernet Franc", "color":"red", "country":"Italy"},
),
Document(
page_content="Highly sought-after Pinot Noir with red fruit and earthy notes",
metadata={"name":"Domaine de la Romanée-Conti", "year": 2018, "rating": 100, "grape": "Pinot Noir", "color":"red", "country":"Fr
),
Document(
page_content="Crisp white with tropical fruit and citrus flavors",
metadata={"name":"Cloudy Bay", "year": 2021, "rating": 92, "grape": "Sauvignon Blanc", "color":"white", "country":"New Zealand"}
),
Document(
page_content="Rich, complex Champagne with notes of brioche and citrus",
metadata={"name":"Krug Grande Cuvée", "year": 2010, "rating": 93, "grape": "Chardonnay blend", "color":"sparkling", "country":"N
),
Document(
page_content="Intense, dark fruit flavors with hints of chocolate",
metadata={"name":"Caymus Special Selection", "year": 2018, "rating": 96, "grape": "Cabernet Sauvignon", "color":"red", "country"
),
Document(
page_content="Exotic, aromatic white with stone fruit and floral notes",
metadata={"name":"Jermann Vintage Tunina", "year": 2020, "rating": 91, "grape": "Sauvignon Blanc blend", "color":"white", "count
),
]
vectorstore = Chroma.from_documents(docs, embeddings)
https://colab.research.google.com/drive/1vZDBWt2ju9nn3IBrBVy52RQdb_cGrhZP?authuser=0#scrollTo=7kb_Bl4soqAc&printMode=true 2/4
2/3/24, 11:16 PM Advances-RAG-Part-1.ipynb - Colaboratory
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.chains.query_constructor.base import AttributeInfo
metadata_field_info = [
AttributeInfo(
name="grape",
description="The grape used to make the wine",
type="string or list[string]",
),
AttributeInfo(
name="name",
description="The name of the wine",
type="string or list[string]",
),
AttributeInfo(
name="color",
description="The color of the wine",
type="string or list[string]",
),
AttributeInfo(
name="year",
description="The year the wine was released",
type="integer",
),
AttributeInfo(
name="country",
description="The name of the country the wine comes from",
type="string",
),
AttributeInfo(
name="rating", description="The Robert Parker rating for the wine 0-100", type="integer" #float
),
]
document_content_description = "Brief description of the wine"
llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.8)
retriever = SelfQueryRetriever.from_llm(
llm,
vectorstore,
document_content_description,
metadata_field_info,
verbose=True
)
[Document(page_content='Complex, layered, rich red with dark fruit flavors', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Opus One', 'rating': 96, 'year': 2018}),
Document(page_content='Full-bodied red with notes of black fruit and spice', metadata={'color': 'red', 'country': 'Australia',
'grape': 'Shiraz', 'name': 'Penfolds Grange', 'rating': 97, 'year': 2017}),
Document(page_content='Elegant, balanced red with herbal and berry nuances', metadata={'color': 'red', 'country': 'Italy',
'grape': 'Cabernet Franc', 'name': 'Sassicaia', 'rating': 95, 'year': 2016}),
Document(page_content='Intense, dark fruit flavors with hints of chocolate', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Caymus Special Selection', 'rating': 96, 'year': 2018})]
[Document(page_content='Intense, dark fruit flavors with hints of chocolate', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Caymus Special Selection', 'rating': 96, 'year': 2018}),
Document(page_content='Complex, layered, rich red with dark fruit flavors', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Opus One', 'rating': 96, 'year': 2018}),
Document(page_content='Exotic, aromatic white with stone fruit and floral notes', metadata={'color': 'white', 'country': 'Italy',
'grape': 'Sauvignon Blanc blend', 'name': 'Jermann Vintage Tunina', 'rating': 91, 'year': 2020}),
Document(page_content='Full-bodied red with notes of black fruit and spice', metadata={'color': 'red', 'country': 'Australia',
'grape': 'Shiraz', 'name': 'Penfolds Grange', 'rating': 97, 'year': 2017})]
retriever.get_relevant_documents("I want a wine that has fruity nodes and has a rating above 97")
[Document(page_content='Luxurious, sweet wine with flavors of honey, apricot, and peach', metadata={'color': 'white', 'country':
'France', 'grape': 'Sémillon', 'name': "Château d'Yquem", 'rating': 98, 'year': 2015}),
Document(page_content='Highly sought-after Pinot Noir with red fruit and earthy notes', metadata={'color': 'red', 'country':
'France', 'grape': 'Pinot Noir', 'name': 'Domaine de la Romanée-Conti', 'rating': 100, 'year': 2018})]
[Document(page_content='Elegant, balanced red with herbal and berry nuances', metadata={'color': 'red', 'country': 'Italy',
'grape': 'Cabernet Franc', 'name': 'Sassicaia', 'rating': 95, 'year': 2016}),
Document(page_content='Exotic, aromatic white with stone fruit and floral notes', metadata={'color': 'white', 'country': 'Italy',
'grape': 'Sauvignon Blanc blend', 'name': 'Jermann Vintage Tunina', 'rating': 91, 'year': 2020})]
https://colab.research.google.com/drive/1vZDBWt2ju9nn3IBrBVy52RQdb_cGrhZP?authuser=0#scrollTo=7kb_Bl4soqAc&printMode=true 3/4
2/3/24, 11:16 PM Advances-RAG-Part-1.ipynb - Colaboratory
retriever.get_relevant_documents("What's a wine after 2015 but before 2020 that's all earthy")
[Document(page_content='Intense, dark fruit flavors with hints of chocolate', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Caymus Special Selection', 'rating': 96, 'year': 2018}),
Document(page_content='Complex, layered, rich red with dark fruit flavors', metadata={'color': 'red', 'country': 'USA', 'grape':
'Cabernet Sauvignon', 'name': 'Opus One', 'rating': 96, 'year': 2018}),
Document(page_content='Elegant, balanced red with herbal and berry nuances', metadata={'color': 'red', 'country': 'Italy',
'grape': 'Cabernet Franc', 'name': 'Sassicaia', 'rating': 95, 'year': 2016}),
Document(page_content='Full-bodied red with notes of black fruit and spice', metadata={'color': 'red', 'country': 'Australia',
'grape': 'Shiraz', 'name': 'Penfolds Grange', 'rating': 97, 'year': 2017})]
[Document(page_content='Highly sought-after Pinot Noir with red fruit and earthy notes', metadata={'color': 'red', 'country':
'France', 'grape': 'Pinot Noir', 'name': 'Domaine de la Romanée-Conti', 'rating': 100, 'year': 2018}),
Document(page_content='Luxurious, sweet wine with flavors of honey, apricot, and peach', metadata={'color': 'white', 'country':
'France', 'grape': 'Sémillon', 'name': "Château d'Yquem", 'rating': 98, 'year': 2015})]
https://colab.research.google.com/drive/1vZDBWt2ju9nn3IBrBVy52RQdb_cGrhZP?authuser=0#scrollTo=7kb_Bl4soqAc&printMode=true 4/4