Skip to content

Commit a51f816

Browse files
committed
black changes
1 parent 0bfc7c1 commit a51f816

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

supporting-blog-content/self-querying-retrieval/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pip install elasticsearch
3939
```bash
4040
pip install langchain-core langchain-community
4141
```
42+
4243
```bash
4344
pip install lark
4445
```

supporting-blog-content/self-querying-retrieval/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ typing-extensions>=4.5.0
88
pydantic>=2.0.0
99
tenacity>=8.2.0
1010
lark>=1.1.5
11+

supporting-blog-content/self-querying-retrieval/selfquery.py

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
from langchain.docstore.document import Document
66
import os
77

8+
89
# --- Environment Configuration (Set these variables) ---
910
os.environ["AZURE_OPENAI_API_KEY"] = "" # Replace with your actual key
1011
os.environ["AZURE_ENDPOINT"] = "" # Replace with your endpoint
1112
os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "gpt-4" # For LLM
12-
os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"] = "text-embedding-ada-002" # For embeddings
13+
os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"] = (
14+
"text-embedding-ada-002" # For embeddings
15+
)
1316

14-
ELASTIC_CLOUD_ID = "" #if using Elastic Cloud, your Cloud ID
15-
ELASTIC_USERNAME = "" # ES user, alternatively can be API key
17+
ELASTIC_CLOUD_ID = "" # if using Elastic Cloud, your Cloud ID
18+
ELASTIC_USERNAME = "" # ES user, alternatively can be API key
1619
ELASTIC_PASSWORD = ""
17-
ELASTIC_INDEX_NAME = "yourElasticIndex" #replace with your index name, if no matching index is present one will be created
20+
ELASTIC_INDEX_NAME = "yourElasticIndex" # replace with your index name, if no matching index is present one will be created
1821

1922
# --- Initialize LLM and Embeddings ---
2023
llm = AzureChatOpenAI(
2124
azure_endpoint=os.environ["AZURE_ENDPOINT"],
2225
deployment_name=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
2326
model_name="gpt-4",
24-
api_version="2024-02-15-preview"
27+
api_version="2024-02-15-preview",
2528
)
2629

2730
embeddings = AzureOpenAIEmbeddings(
28-
azure_endpoint=os.environ["AZURE_ENDPOINT"],
29-
model="text-embedding-ada-002"
31+
azure_endpoint=os.environ["AZURE_ENDPOINT"], model="text-embedding-ada-002"
3032
)
3133

3234
# --- Define Metadata Attributes ---
@@ -55,50 +57,95 @@
5557
name="title",
5658
description="The title of the movie",
5759
type="string",
58-
)
60+
),
5961
]
6062

6163
# --- Ingest the Documents ---
6264
docs = [
63-
Document(
65+
Document(
6466
page_content="A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.",
65-
metadata={"year": 2010, "rating": 8.8, "genre": "science fiction", "title": "Inception"},
67+
metadata={
68+
"year": 2010,
69+
"rating": 8.8,
70+
"genre": "science fiction",
71+
"title": "Inception",
72+
},
6673
),
6774
Document(
6875
page_content="When the menace known as the Joker emerges from the shadows, it causes Batman to question everything he stands for.",
69-
metadata={"year": 2008, "rating": 9.0, "genre": "action", "title": "The Dark Knight"},
76+
metadata={
77+
"year": 2008,
78+
"rating": 9.0,
79+
"genre": "action",
80+
"title": "The Dark Knight",
81+
},
7082
),
7183
Document(
7284
page_content="The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.",
73-
metadata={"year": 1972, "rating": 9.2, "genre": "crime", "title": "The Godfather"},
85+
metadata={
86+
"year": 1972,
87+
"rating": 9.2,
88+
"genre": "crime",
89+
"title": "The Godfather",
90+
},
7491
),
7592
Document(
7693
page_content="A young hobbit, Frodo, is tasked with destroying an ancient ring that holds the power to enslave the world.",
77-
metadata={"year": 2001, "rating": 8.8, "genre": "fantasy", "title": "The Lord of the Rings: The Fellowship of the Ring"},
94+
metadata={
95+
"year": 2001,
96+
"rating": 8.8,
97+
"genre": "fantasy",
98+
"title": "The Lord of the Rings: The Fellowship of the Ring",
99+
},
78100
),
79101
Document(
80102
page_content="A cyborg assassin travels back in time to kill the mother of the future leader of the human resistance.",
81-
metadata={"year": 1984, "rating": 8.0, "genre": "science fiction", "title": "The Terminator"},
103+
metadata={
104+
"year": 1984,
105+
"rating": 8.0,
106+
"genre": "science fiction",
107+
"title": "The Terminator",
108+
},
82109
),
83110
Document(
84111
page_content="A cowboy doll is profoundly threatened when a new spaceman action figure replaces him as the top toy in a boy's room.",
85-
metadata={"year": 1995, "rating": 8.3, "genre": "animation", "title": "Toy Story"},
112+
metadata={
113+
"year": 1995,
114+
"rating": 8.3,
115+
"genre": "animation",
116+
"title": "Toy Story",
117+
},
86118
),
87119
Document(
88120
page_content="A young wizard, Harry Potter, begins his journey at Hogwarts School of Witchcraft and Wizardry, where he learns of his magical heritage.",
89-
metadata={"year": 2001, "rating": 7.6, "genre": "fantasy", "title": "Harry Potter and the Sorcerer's Stone"},
121+
metadata={
122+
"year": 2001,
123+
"rating": 7.6,
124+
"genre": "fantasy",
125+
"title": "Harry Potter and the Sorcerer's Stone",
126+
},
90127
),
91128
Document(
92129
page_content="A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
93-
metadata={"year": 2014, "rating": 8.6, "genre": "science fiction", "title": "Interstellar"},
130+
metadata={
131+
"year": 2014,
132+
"rating": 8.6,
133+
"genre": "science fiction",
134+
"title": "Interstellar",
135+
},
94136
),
95137
Document(
96138
page_content="A former Roman General seeks revenge against the corrupt emperor who murdered his family and sent him into slavery.",
97139
metadata={"year": 2000, "rating": 8.5, "genre": "action", "title": "Gladiator"},
98140
),
99141
Document(
100142
page_content="A young lion prince is exiled from his kingdom and must learn the true meaning of responsibility and bravery.",
101-
metadata={"year": 1994, "rating": 8.5, "genre": "animation", "title": "The Lion King"},
143+
metadata={
144+
"year": 1994,
145+
"rating": 8.5,
146+
"genre": "animation",
147+
"title": "The Lion King",
148+
},
102149
),
103150
]
104151

@@ -112,11 +159,12 @@
112159
es_user=ELASTIC_USERNAME,
113160
es_password=ELASTIC_PASSWORD,
114161
index_name=ELASTIC_INDEX_NAME,
115-
embedding=embeddings,
162+
embedding=embeddings,
116163
)
117164

118-
es_store.add_embeddings(text_embeddings=list(zip(texts, doc_embeddings)), metadatas=metadatas)
119-
165+
es_store.add_embeddings(
166+
text_embeddings=list(zip(texts, doc_embeddings)), metadatas=metadatas
167+
)
120168

121169

122170
# --- Create the Self-Query Retriever (Using LLM) ---
@@ -131,11 +179,11 @@
131179
while True:
132180
# Prompt the user for a query
133181
query = input("\nEnter your search query (or type 'exit' to quit): ")
134-
182+
135183
# Exit the loop if the user types 'exit'
136-
if query.lower() == 'exit':
184+
if query.lower() == "exit":
137185
break
138-
186+
139187
# Execute the query and print the results
140188
print(f"\nQuery: {query}")
141189
docs = retriever.invoke(query)

0 commit comments

Comments
 (0)