|
| 1 | +from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI |
| 2 | +from langchain_elasticsearch import ElasticsearchStore |
| 3 | +from langchain.chains.query_constructor.base import AttributeInfo |
| 4 | +from langchain.retrievers.self_query.base import SelfQueryRetriever |
| 5 | +from langchain.docstore.document import Document |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +# --- Environment Configuration (Set these variables) --- |
| 10 | +os.environ["AZURE_OPENAI_API_KEY"] = "" # Replace with your actual key |
| 11 | +os.environ["AZURE_ENDPOINT"] = "" # Replace with your endpoint |
| 12 | +os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "gpt-4" # For LLM |
| 13 | +os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"] = ( |
| 14 | + "text-embedding-ada-002" # For embeddings |
| 15 | +) |
| 16 | + |
| 17 | +ELASTIC_CLOUD_ID = "" # if using Elastic Cloud, your Cloud ID |
| 18 | +ELASTIC_USERNAME = "" # ES user, alternatively can be API key |
| 19 | +ELASTIC_PASSWORD = "" |
| 20 | +ELASTIC_INDEX_NAME = "yourElasticIndex" # replace with your index name, if no matching index is present one will be created |
| 21 | + |
| 22 | +# --- Initialize LLM and Embeddings --- |
| 23 | +llm = AzureChatOpenAI( |
| 24 | + azure_endpoint=os.environ["AZURE_ENDPOINT"], |
| 25 | + deployment_name=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"], |
| 26 | + model_name="gpt-4", |
| 27 | + api_version="2024-02-15-preview", |
| 28 | +) |
| 29 | + |
| 30 | +embeddings = AzureOpenAIEmbeddings( |
| 31 | + azure_endpoint=os.environ["AZURE_ENDPOINT"], model="text-embedding-ada-002" |
| 32 | +) |
| 33 | + |
| 34 | +# --- Define Metadata Attributes --- |
| 35 | +metadata_field_info = [ |
| 36 | + AttributeInfo( |
| 37 | + name="year", |
| 38 | + description="The year the movie was released", |
| 39 | + type="integer", |
| 40 | + ), |
| 41 | + AttributeInfo( |
| 42 | + name="rating", |
| 43 | + description="The rating of the movie (out of 10)", |
| 44 | + type="float", |
| 45 | + ), |
| 46 | + AttributeInfo( |
| 47 | + name="genre", |
| 48 | + description="The genre of the movie", |
| 49 | + type="string", |
| 50 | + ), |
| 51 | + AttributeInfo( |
| 52 | + name="director", |
| 53 | + description="The director of the movie", |
| 54 | + type="string", |
| 55 | + ), |
| 56 | + AttributeInfo( |
| 57 | + name="title", |
| 58 | + description="The title of the movie", |
| 59 | + type="string", |
| 60 | + ), |
| 61 | +] |
| 62 | + |
| 63 | +# --- Ingest the Documents --- |
| 64 | +docs = [ |
| 65 | + Document( |
| 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.", |
| 67 | + metadata={ |
| 68 | + "year": 2010, |
| 69 | + "rating": 8.8, |
| 70 | + "genre": "science fiction", |
| 71 | + "title": "Inception", |
| 72 | + }, |
| 73 | + ), |
| 74 | + Document( |
| 75 | + page_content="When the menace known as the Joker emerges from the shadows, it causes Batman to question everything he stands for.", |
| 76 | + metadata={ |
| 77 | + "year": 2008, |
| 78 | + "rating": 9.0, |
| 79 | + "genre": "action", |
| 80 | + "title": "The Dark Knight", |
| 81 | + }, |
| 82 | + ), |
| 83 | + Document( |
| 84 | + page_content="The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.", |
| 85 | + metadata={ |
| 86 | + "year": 1972, |
| 87 | + "rating": 9.2, |
| 88 | + "genre": "crime", |
| 89 | + "title": "The Godfather", |
| 90 | + }, |
| 91 | + ), |
| 92 | + Document( |
| 93 | + page_content="A young hobbit, Frodo, is tasked with destroying an ancient ring that holds the power to enslave the world.", |
| 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 | + }, |
| 100 | + ), |
| 101 | + Document( |
| 102 | + page_content="A cyborg assassin travels back in time to kill the mother of the future leader of the human resistance.", |
| 103 | + metadata={ |
| 104 | + "year": 1984, |
| 105 | + "rating": 8.0, |
| 106 | + "genre": "science fiction", |
| 107 | + "title": "The Terminator", |
| 108 | + }, |
| 109 | + ), |
| 110 | + Document( |
| 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.", |
| 112 | + metadata={ |
| 113 | + "year": 1995, |
| 114 | + "rating": 8.3, |
| 115 | + "genre": "animation", |
| 116 | + "title": "Toy Story", |
| 117 | + }, |
| 118 | + ), |
| 119 | + Document( |
| 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.", |
| 121 | + metadata={ |
| 122 | + "year": 2001, |
| 123 | + "rating": 7.6, |
| 124 | + "genre": "fantasy", |
| 125 | + "title": "Harry Potter and the Sorcerer's Stone", |
| 126 | + }, |
| 127 | + ), |
| 128 | + Document( |
| 129 | + page_content="A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.", |
| 130 | + metadata={ |
| 131 | + "year": 2014, |
| 132 | + "rating": 8.6, |
| 133 | + "genre": "science fiction", |
| 134 | + "title": "Interstellar", |
| 135 | + }, |
| 136 | + ), |
| 137 | + Document( |
| 138 | + page_content="A former Roman General seeks revenge against the corrupt emperor who murdered his family and sent him into slavery.", |
| 139 | + metadata={"year": 2000, "rating": 8.5, "genre": "action", "title": "Gladiator"}, |
| 140 | + ), |
| 141 | + Document( |
| 142 | + page_content="A young lion prince is exiled from his kingdom and must learn the true meaning of responsibility and bravery.", |
| 143 | + metadata={ |
| 144 | + "year": 1994, |
| 145 | + "rating": 8.5, |
| 146 | + "genre": "animation", |
| 147 | + "title": "The Lion King", |
| 148 | + }, |
| 149 | + ), |
| 150 | +] |
| 151 | + |
| 152 | +# Generate embeddings *before* creating the ElasticsearchStore |
| 153 | +texts = [doc.page_content for doc in docs] |
| 154 | +metadatas = [doc.metadata for doc in docs] |
| 155 | +doc_embeddings = embeddings.embed_documents(texts) |
| 156 | + |
| 157 | +es_store = ElasticsearchStore( |
| 158 | + es_cloud_id=ELASTIC_CLOUD_ID, |
| 159 | + es_user=ELASTIC_USERNAME, |
| 160 | + es_password=ELASTIC_PASSWORD, |
| 161 | + index_name=ELASTIC_INDEX_NAME, |
| 162 | + embedding=embeddings, |
| 163 | +) |
| 164 | + |
| 165 | +es_store.add_embeddings( |
| 166 | + text_embeddings=list(zip(texts, doc_embeddings)), metadatas=metadatas |
| 167 | +) |
| 168 | + |
| 169 | + |
| 170 | +# --- Create the Self-Query Retriever (Using LLM) --- |
| 171 | +retriever = SelfQueryRetriever.from_llm( |
| 172 | + llm, |
| 173 | + es_store, |
| 174 | + "Search for movies", |
| 175 | + metadata_field_info, |
| 176 | + verbose=True, |
| 177 | +) |
| 178 | + |
| 179 | +while True: |
| 180 | + # Prompt the user for a query |
| 181 | + query = input("\nEnter your search query (or type 'exit' to quit): ") |
| 182 | + |
| 183 | + # Exit the loop if the user types 'exit' |
| 184 | + if query.lower() == "exit": |
| 185 | + break |
| 186 | + |
| 187 | + # Execute the query and print the results |
| 188 | + print(f"\nQuery: {query}") |
| 189 | + docs = retriever.invoke(query) |
| 190 | + print(f"Found {len(docs)} documents:") |
| 191 | + for doc in docs: |
| 192 | + print(doc.page_content) |
| 193 | + print(doc.metadata) |
| 194 | + print("-" * 20) |
0 commit comments