|
5 | 5 | from langchain.docstore.document import Document |
6 | 6 | import os |
7 | 7 |
|
| 8 | + |
8 | 9 | # --- Environment Configuration (Set these variables) --- |
9 | 10 | os.environ["AZURE_OPENAI_API_KEY"] = "" # Replace with your actual key |
10 | 11 | os.environ["AZURE_ENDPOINT"] = "" # Replace with your endpoint |
11 | 12 | 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 | +) |
13 | 16 |
|
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 |
16 | 19 | 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 |
18 | 21 |
|
19 | 22 | # --- Initialize LLM and Embeddings --- |
20 | 23 | llm = AzureChatOpenAI( |
21 | 24 | azure_endpoint=os.environ["AZURE_ENDPOINT"], |
22 | 25 | deployment_name=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"], |
23 | 26 | model_name="gpt-4", |
24 | | - api_version="2024-02-15-preview" |
| 27 | + api_version="2024-02-15-preview", |
25 | 28 | ) |
26 | 29 |
|
27 | 30 | 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" |
30 | 32 | ) |
31 | 33 |
|
32 | 34 | # --- Define Metadata Attributes --- |
|
55 | 57 | name="title", |
56 | 58 | description="The title of the movie", |
57 | 59 | type="string", |
58 | | - ) |
| 60 | + ), |
59 | 61 | ] |
60 | 62 |
|
61 | 63 | # --- Ingest the Documents --- |
62 | 64 | docs = [ |
63 | | - Document( |
| 65 | + Document( |
64 | 66 | 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 | + }, |
66 | 73 | ), |
67 | 74 | Document( |
68 | 75 | 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 | + }, |
70 | 82 | ), |
71 | 83 | Document( |
72 | 84 | 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 | + }, |
74 | 91 | ), |
75 | 92 | Document( |
76 | 93 | 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 | + }, |
78 | 100 | ), |
79 | 101 | Document( |
80 | 102 | 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 | + }, |
82 | 109 | ), |
83 | 110 | Document( |
84 | 111 | 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 | + }, |
86 | 118 | ), |
87 | 119 | Document( |
88 | 120 | 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 | + }, |
90 | 127 | ), |
91 | 128 | Document( |
92 | 129 | 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 | + }, |
94 | 136 | ), |
95 | 137 | Document( |
96 | 138 | page_content="A former Roman General seeks revenge against the corrupt emperor who murdered his family and sent him into slavery.", |
97 | 139 | metadata={"year": 2000, "rating": 8.5, "genre": "action", "title": "Gladiator"}, |
98 | 140 | ), |
99 | 141 | Document( |
100 | 142 | 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 | + }, |
102 | 149 | ), |
103 | 150 | ] |
104 | 151 |
|
|
112 | 159 | es_user=ELASTIC_USERNAME, |
113 | 160 | es_password=ELASTIC_PASSWORD, |
114 | 161 | index_name=ELASTIC_INDEX_NAME, |
115 | | - embedding=embeddings, |
| 162 | + embedding=embeddings, |
116 | 163 | ) |
117 | 164 |
|
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 | +) |
120 | 168 |
|
121 | 169 |
|
122 | 170 | # --- Create the Self-Query Retriever (Using LLM) --- |
|
131 | 179 | while True: |
132 | 180 | # Prompt the user for a query |
133 | 181 | query = input("\nEnter your search query (or type 'exit' to quit): ") |
134 | | - |
| 182 | + |
135 | 183 | # Exit the loop if the user types 'exit' |
136 | | - if query.lower() == 'exit': |
| 184 | + if query.lower() == "exit": |
137 | 185 | break |
138 | | - |
| 186 | + |
139 | 187 | # Execute the query and print the results |
140 | 188 | print(f"\nQuery: {query}") |
141 | 189 | docs = retriever.invoke(query) |
|
0 commit comments