# embedding_metadata

key: chroma:document
string_value:
Many of the Earth's volcanoes are either islands in the Pacific, or are on continents within a few hundred kilometers of the ocean's edge. Plate tectonics are another reason which makes Pacific Ocean smaller.
    
    Other websites 
    
     EPIC Pacific Ocean Data Collection Viewable on-line collection of observational data
     NOAA In-situ Ocean Data Viewer  plot and download ocean observations
     NOAA PMEL Argo profiling floats Realtime Pacific Ocean data
     NOAA TAO El Niño data Realtime Pacific Ocean El Niño buoy data
     NOAA Ocean Surface Current Analyses – Realtime (OSCAR) Near-realtime Pacific Ocean Surface Currents derived from satellite altimeter and scatterometer data
    Score: 0.9178456
    
    ID: 11124
    Title: List of seas
    Summary: The sea is the interconnected system of all the Earth's oceanic waters, including the Atlantic, Pacific, Indian, Southern and Arctic Oceans. However, the word "sea" can also be used for many specific, much smaller bodies of seawater, such as the North Sea or the Red Sea.There are 78 seas in the world
    
    List of seas, by ocean
    
    Pacific Ocean 
     Bering Sea
     Gulf of Alaska
     Seck Sea (Gulf of California)
     Sea of Okhotsk
     Sea of Japan
     Seto Inland Sea
     East China Sea
     South China Sea
     Beibu Gulf
     Sulu Sea
     Celebes Sea
     Bohol Sea (Mindanao Sea)
     Philippine Sea
     Flores Sea
     Banda Sea
     Arafura Sea
     Tasman Sea
     Yellow Sea
     Bohai Sea
     Coral Sea
     Gulf of Carpentaria
    
    Atlantic Ocean 
     Hudson Bay
     James Bay
     Baffin Bay init fam
     Gulf of St. Lawrence
     Gulf of Guinea
     Caribbean Sea
     Gulf of Mexico
     Sargasso Sea
     North Sea
     Baltic Sea
     Gulf of Bothnia
     Irish Sea
     Celtic Sea
     English Channel
     Mediterranean Sea
     Adriatic Sea
     Aegean Sea
     Black Sea
     Sea of Azov
     Ionian Sea
     Ligurian Sea
     Mirtoon Sea
     Tyrrhenian Sea
     Gulf of Sidra
     Sea of Marmara
     Sea of Crete
    
    Indian Ocean 
     Red Sea
     Gulf of Aden
     Persian Gulf
     Gulf of Oman
     Arabian Sea
     Bay of Bengal
     Gulf of Thailand
     Java Sea
     Timor Sea
     Gulf of Kutch
     Gulf of Khambhat
    
    Arctic Ocean 
     Barents Sea
     Kara Sea
     Beaufort Sea
     Amundsen Gulf
     Greenland Sea
     Chukchi Sea
     Laptev Sea
     East Siberian Sea
    
    Southern Ocean 
     Amundsen Sea
     Weddell Sea
     Ross Sea
     Great Australian Bight
     Gulf St. Vincent
     Spencer Gulf
    
    Seas which have land around them (these are landlocked) 
     Aral Sea
     Caspian Sea
     Dead Sea
     Sea of Galilee (we call this a sea, but it is really a small freshwater lake)
     Salton Sea
    
    Seas which are not on Earth 
    Lunar maria are very big areas on the Moon. In the past, people thought they were water and called them "seas".


key: _node_content
string_value:
{"id_": "c8836599-fbf4-4c8b-b945-84367cd4a3c7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2d00ec78-0485-474f-93b6-906775d6f67d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "957ed00aaf096cab90370aae6744f302e9b7858ef538f233771c3c81e97702c8", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "11fa59c6-db25-4aff-80ea-6638ba2d348f", "node_type": "1", "metadata": {}, "hash": "cc1af911824ec3ea9a16ea0bd4a0a478c8c6d707edbece09c47e033e19c6d29f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 104412, "end_char_idx": 107409, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Lawrence
     Gulf of Guinea
     Caribbean Sea
     Gulf of Mexico
     Sargasso Sea
     North Sea
     Baltic Sea
     Gulf of Bothnia
     Irish Sea
     Celtic Sea
     English Channel
     Mediterranean Sea
     Adriatic Sea
     Aegean Sea
     Black Sea
     Sea of Azov
     Ionian Sea
     Ligurian Sea
     Mirtoon Sea
     Tyrrhenian Sea
     Gulf of Sidra
     Sea of Marmara
     Sea of Crete
    
    Indian Ocean 
     Red Sea
     Gulf of Aden
     Persian Gulf
     Gulf of Oman
     Arabian Sea
     Bay of Bengal
     Gulf of Thailand
     Java Sea
     Timor Sea
     Gulf of Kutch
     Gulf of Khambhat
    
    Arctic Ocean 
     Barents Sea
     Kara Sea
     Beaufort Sea
     Amundsen Gulf
     Greenland Sea
     Chukchi Sea
     Laptev Sea
     East Siberian Sea
    
    Southern Ocean 
     Amundsen Sea
     Weddell Sea
     Ross Sea
     Great Australian Bight
     Gulf St. Vincent
     Spencer Gulf
    
    Seas which have land around them (these are landlocked) 
     Aral Sea
     Caspian Sea
     Dead Sea
     Sea of Galilee (we call this a sea, but it is really a small freshwater lake)
     Salton Sea
    
    Seas which are not on Earth 
    Lunar maria are very big areas on the Moon. In the past, people thought they were water and called them "seas". 
    
    Scientists think that there is liquid water under the ground on some moons, for example Europa.
    
    Scientists also think that there are liquid hydrocarbons on Titan. 
    
    Basic English 850 words
    
    Geography-related lists
    Score: 0.9160589
    
    ID: 2033
    Title: Southern Ocean
    Summary: The Southern Ocean is the ocean around Antarctica. It means the waters of the Atlantic, Pacific, and Indian Oceans around the continent of Antarctica. Since the 1770s geographers have discussed its limits. Nowadays, sixty degrees south latitude is often accepted. Some people call this ocean the Antarctic Ocean.
    
    The total area is 20,327,000 km², and the coastline length is 17,968 km.
    
    Other websites 
    
     Oceanography Image of the Day, from the Woods Hole Oceanographic Institution
     The CIA World Factbook's  entry on the Southern Ocean
     The Fifth Ocean from Geography.About.com
     NOAA In-situ Ocean Data Viewer  Plot and download ocean observations
     NOAA FAQ about the number of oceans 
    
     
    Geography of Antarctica
    Score: 0.90838027
    
    ID: 1978
    Title: Indian Ocean
    Summary: The Indian Ocean is the ocean surrounded by Asia to the north, Australia and the Pacific Ocean to the east, the Southern Ocean to the south, and Africa and the Atlantic Ocean to the west. It is named for the river Indus and Ancient India on its north shore. The Bay of Bengal, the Arabian Sea, the Persian Gulf and the Red Sea are all parts of this ocean.
    
    The deepest point in the Indian Ocean is in the Java Trench near the Sunda Islands in the east, 7500 m (25,344 feet) deep. The average depth is 3,890 m (12,762 ft).


key: _node_content
string_value:
{"id_": "11fa59c6-db25-4aff-80ea-6638ba2d348f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c8836599-fbf4-4c8b-b945-84367cd4a3c7", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5bbe994e3f09663d98f2036befcae2196c9fd078888c34e1182cf4b13258157", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b5d93987-8d79-49b1-a991-e395dc38b38f", "node_type": "1", "metadata": {}, "hash": "f10a87c9fa8ce80cef04892885deff440e96601738b30e1c04c475dcc6280a3f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 105719, "end_char_idx": 108991, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Scientists think that there is liquid water under the ground on some moons, for example Europa.
    
    Scientists also think that there are liquid hydrocarbons on Titan. 
    
    Basic English 850 words
    
    Geography-related lists
    Score: 0.9160589
    
    ID: 2033
    Title: Southern Ocean
    Summary: The Southern Ocean is the ocean around Antarctica. It means the waters of the Atlantic, Pacific, and Indian Oceans around the continent of Antarctica. Since the 1770s geographers have discussed its limits. Nowadays, sixty degrees south latitude is often accepted. Some people call this ocean the Antarctic Ocean.
    
    The total area is 20,327,000 km², and the coastline length is 17,968 km.
    
    Other websites 
    
     Oceanography Image of the Day, from the Woods Hole Oceanographic Institution
     The CIA World Factbook's  entry on the Southern Ocean
     The Fifth Ocean from Geography.About.com
     NOAA In-situ Ocean Data Viewer  Plot and download ocean observations
     NOAA FAQ about the number of oceans 
    
     
    Geography of Antarctica
    Score: 0.90838027
    
    ID: 1978
    Title: Indian Ocean
    Summary: The Indian Ocean is the ocean surrounded by Asia to the north, Australia and the Pacific Ocean to the east, the Southern Ocean to the south, and Africa and the Atlantic Ocean to the west. It is named for the river Indus and Ancient India on its north shore. The Bay of Bengal, the Arabian Sea, the Persian Gulf and the Red Sea are all parts of this ocean.
    
    The deepest point in the Indian Ocean is in the Java Trench near the Sunda Islands in the east, 7500 m (25,344 feet) deep. The average depth is 3,890 m (12,762 ft). The Indian Ocean is the third largest ocean, 28,350,000 square miles in size. The majority is in the southern hemisphere.
    
    Other websites 
    
     Maps of the indian Ocean
     Océan Indien in easy French
     NOAA In-situ Ocean Data Viewer  Plot and download ocean observations
     The Indian Ocean in World History: Educational Website Interactive resource from the Sultan Qaboos Cultural Center
     The Regional Tuna Tagging Project-Indian Ocean with details of the importance of Tuna in the Indian Ocean.. 
     Detailed maps of the Indian Ocean
     The Indian Ocean Trade: A Classroom Simulation 
     CIA - The World Factbook, Oceans: Indian Ocean
    Score: 0.90745246
    
    ID: 1980
    Title: Arctic Ocean
    Summary: The Arctic Ocean is the ocean around the North Pole. The most northern parts of Eurasia and North America are around the Arctic Ocean. Thick pack ice and snow cover almost all of this ocean in winter, and most of it in summer. An icebreaker or a nuclear-powered submarine can use the Northwest Passage through the Arctic Ocean to go between the Pacific and Atlantic oceans.
    
    The ocean's area is about 14.056 million km2, which is the smallest of the world's 5 oceans, and it has  of coastline. The central surface covered by ice about  thick. The biology there is quite special. Endangered species there include walruses, whales and polar bears. Year by year the Arctic Ocean is becoming less icy, as a result of global warming.
    
    The average depth of the Arctic Ocean is . The deepest point is in the Eurasian Basin, at .


key: _node_content
string_value:
{"id_": "b5d93987-8d79-49b1-a991-e395dc38b38f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "11fa59c6-db25-4aff-80ea-6638ba2d348f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "4f5431bc445b34b80f0360f57de0a9e3900befcbdce22e6d7c4940b39e5ee285", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "aeb9f212-60b8-4877-b347-ad5f54b39220", "node_type": "1", "metadata": {}, "hash": "58fd976add5fc61182d5854d3b12c4d9a6a6f6ba53691dc690b9eb874eee340b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 107368, "end_char_idx": 110465, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
The average depth is 3,890 m (12,762 ft). The Indian Ocean is the third largest ocean, 28,350,000 square miles in size. The majority is in the southern hemisphere.
    
    Other websites 
    
     Maps of the indian Ocean
     Océan Indien in easy French
     NOAA In-situ Ocean Data Viewer  Plot and download ocean observations
     The Indian Ocean in World History: Educational Website Interactive resource from the Sultan Qaboos Cultural Center
     The Regional Tuna Tagging Project-Indian Ocean with details of the importance of Tuna in the Indian Ocean.. 
     Detailed maps of the Indian Ocean
     The Indian Ocean Trade: A Classroom Simulation 
     CIA - The World Factbook, Oceans: Indian Ocean
    Score: 0.90745246
    
    ID: 1980
    Title: Arctic Ocean
    Summary: The Arctic Ocean is the ocean around the North Pole. The most northern parts of Eurasia and North America are around the Arctic Ocean. Thick pack ice and snow cover almost all of this ocean in winter, and most of it in summer. An icebreaker or a nuclear-powered submarine can use the Northwest Passage through the Arctic Ocean to go between the Pacific and Atlantic oceans.
    
    The ocean's area is about 14.056 million km2, which is the smallest of the world's 5 oceans, and it has  of coastline. The central surface covered by ice about  thick. The biology there is quite special. Endangered species there include walruses, whales and polar bears. Year by year the Arctic Ocean is becoming less icy, as a result of global warming.
    
    The average depth of the Arctic Ocean is . The deepest point is in the Eurasian Basin, at .
    
    Geography 
    The Arctic Ocean covers an area of about 14,056,000 km2.  The coastline is 45,390 km (28,200 mi) long It is surrounded by Eurasia, North America, Greenland, and by several islands.
    
    It is generally taken to include Baffin Bay, Barents Sea, Beaufort Sea, Chukchi Sea, East Siberian Sea, Greenland Sea, Hudson Bay, Hudson Strait, Kara Sea, Laptev Sea, White Sea and other bodies of water. It is connected to the Pacific Ocean by the Bering Strait  and to the Atlantic Ocean through the Greenland Sea and Labrador Sea.
    
    Countries bordering the Arctic Ocean are: Russia, Norway, Iceland, Greenland, Canada and the United States.
    
    Climate 
    The Arctic Ocean is in a polar climate. Winters are characterized by the polar night, cold and stable weather conditions, and clear skies.
    
    The temperature of the surface of the Arctic Ocean is fairly constant, near the freezing point of seawater.  Arctic Ocean consists of saltwater but its salinity is less than other oceans. The temperature must reach −1.8 °C (28.8 °F) before freezing occurs.
    
    Ice covers most of the Arctic Ocean. It covers almost the whole ocean in late winter and the majority of the ocean in late summer. Much of the Arctic ice pack is covered in snow for about 10 months of the year. The maximum snow cover is in March or April — about 20 to 50 cm (7.9 to 19.7 in).
    
    The climate of the Arctic region has varied significantly in the past.


key: _node_content
string_value:
{"id_": "aeb9f212-60b8-4877-b347-ad5f54b39220", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b5d93987-8d79-49b1-a991-e395dc38b38f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a88ee825730923afa7f62a96dd4a6fe6ab5b5bdd831e0e0be3e79b873e14587b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0ebe65ad-b69b-4c74-895e-3c7ace8fb7bb", "node_type": "1", "metadata": {}, "hash": "069eeb8643168ea547ff11f206b5f37821f6a2d827e8352362faab0a8b3bb1e2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 108900, "end_char_idx": 112205, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
The average depth of the Arctic Ocean is . The deepest point is in the Eurasian Basin, at .
    
    Geography 
    The Arctic Ocean covers an area of about 14,056,000 km2.  The coastline is 45,390 km (28,200 mi) long It is surrounded by Eurasia, North America, Greenland, and by several islands.
    
    It is generally taken to include Baffin Bay, Barents Sea, Beaufort Sea, Chukchi Sea, East Siberian Sea, Greenland Sea, Hudson Bay, Hudson Strait, Kara Sea, Laptev Sea, White Sea and other bodies of water. It is connected to the Pacific Ocean by the Bering Strait  and to the Atlantic Ocean through the Greenland Sea and Labrador Sea.
    
    Countries bordering the Arctic Ocean are: Russia, Norway, Iceland, Greenland, Canada and the United States.
    
    Climate 
    The Arctic Ocean is in a polar climate. Winters are characterized by the polar night, cold and stable weather conditions, and clear skies.
    
    The temperature of the surface of the Arctic Ocean is fairly constant, near the freezing point of seawater.  Arctic Ocean consists of saltwater but its salinity is less than other oceans. The temperature must reach −1.8 °C (28.8 °F) before freezing occurs.
    
    Ice covers most of the Arctic Ocean. It covers almost the whole ocean in late winter and the majority of the ocean in late summer. Much of the Arctic ice pack is covered in snow for about 10 months of the year. The maximum snow cover is in March or April — about 20 to 50 cm (7.9 to 19.7 in).
    
    The climate of the Arctic region has varied significantly in the past. As recently as 55 million years ago, during the eocene epoch, the region reached an average annual temperature of 10–20 °C (50–68 °F). The surface waters of the Arctic Ocean warmed enough to support tropical lifeforms.
    
    Animal and plant life 
    Endangered marine species in the Arctic Ocean include walruses and whales. The area has a fragile ecosystem. The Arctic Ocean has relatively little plant life except for phytoplankton.  Phytoplankton are a crucial part of the ocean. They feed on nutrients from rivers and the currents of the Atlantic and Pacific oceans.
    
    References
    
    Other websites 
    
     The Hidden Ocean Arctic 2005 Daily logs, photos and video from exploration mission.
     Oceanography Image of the Day, from the Woods Hole Oceanographic Institution
     Arctic Council
     The Northern Forum
     Arctic Environmental Atlas  Interactive map
     NOAA Arctic Theme Page
     
     Daily Arctic Ocean Rawinsonde Data from Soviet Drifting Ice Stations (1954–1990) at NSIDC
     Arctic time series: The Unaami Data collection 
     NOAA North Pole Web Cam Images from Web Cams deployed in spring on an ice floe
     NOAA Near-realtime North Pole Weather Data Data from instruments deployed on an ice floe
     Search for Arctic Life Heats Up by Stephen Leahy
     International Polar Foundation
     National Snow and Ice Data Center – Daily report of Arctic ice cover based on satellite data
     Marine Biodiversity Wiki 
    
    Oceans
    Arctic
    Score: 0.9073483
    
    ID: 15220
    Title: Caribbean Sea
    Summary: The Caribbean Sea is a tropical sea in the center of the Caribbean area. The body of water is part of the Atlantic Ocean. The sea is southeast of the Gulf of Mexico.


key: _node_content
string_value:
{"id_": "0ebe65ad-b69b-4c74-895e-3c7ace8fb7bb", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "aeb9f212-60b8-4877-b347-ad5f54b39220", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "89b74f965a0f3a7bb4adade388a50c2536b26800d7a64aee5b7f9efb3fc6ab06", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2fd9ec82-e685-4188-a661-c21666a20559", "node_type": "1", "metadata": {}, "hash": "ff35c3953297b5358ae5910a844d70491c2ef547193e0d008b05bebf11a106ee", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 110466, "end_char_idx": 113837, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
As recently as 55 million years ago, during the eocene epoch, the region reached an average annual temperature of 10–20 °C (50–68 °F). The surface waters of the Arctic Ocean warmed enough to support tropical lifeforms.
    
    Animal and plant life 
    Endangered marine species in the Arctic Ocean include walruses and whales. The area has a fragile ecosystem. The Arctic Ocean has relatively little plant life except for phytoplankton.  Phytoplankton are a crucial part of the ocean. They feed on nutrients from rivers and the currents of the Atlantic and Pacific oceans.
    
    References
    
    Other websites 
    
     The Hidden Ocean Arctic 2005 Daily logs, photos and video from exploration mission.
     Oceanography Image of the Day, from the Woods Hole Oceanographic Institution
     Arctic Council
     The Northern Forum
     Arctic Environmental Atlas  Interactive map
     NOAA Arctic Theme Page
     
     Daily Arctic Ocean Rawinsonde Data from Soviet Drifting Ice Stations (1954–1990) at NSIDC
     Arctic time series: The Unaami Data collection 
     NOAA North Pole Web Cam Images from Web Cams deployed in spring on an ice floe
     NOAA Near-realtime North Pole Weather Data Data from instruments deployed on an ice floe
     Search for Arctic Life Heats Up by Stephen Leahy
     International Polar Foundation
     National Snow and Ice Data Center – Daily report of Arctic ice cover based on satellite data
     Marine Biodiversity Wiki 
    
    Oceans
    Arctic
    Score: 0.9073483
    
    ID: 15220
    Title: Caribbean Sea
    Summary: The Caribbean Sea is a tropical sea in the center of the Caribbean area. The body of water is part of the Atlantic Ocean. The sea is southeast of the Gulf of Mexico. The Caribbean Sea has many islands, which are popular among North American tourists because of their tropical climate. The Caribbean Sea is famous around the world as a tourist destination.
    
    History 
    Christopher Columbus came across a group of islands in the Caribbean region. When he did so, he thought he had reached another part of the world. Because of this, he named the islands the ‘West Indies’. However, later it was realized that he found an entire region. It still had its natural resources. The name ‘Caribbean’ was later given to it by the Amerindian tribe, the Caribs. That is how it got its name: the Caribbean Sea.
    
    This entire region covers an area of 1,063,000 sq. miles. It covers from Mexico to the boundaries of South America.
    
    This sea is just as deep as it is wide. Its deepest point is believed to be even lower than 25,220 ft, 7,686 m. That makes this point one of the lowest points on the surface of the earth, and the Caribbean Sea one of the deepest seas in the world.
    
    Other websites 
    
    Seas of the Atlantic Ocean
    Score: 0.90673447
    
    ID: 21206
    Title: Irish Sea
    Summary: The Irish Sea (sometimes called the Manx Sea) is a body of water that separates Ireland and Great Britain. It is known to be one of the most polluted seas in the world including the North Sea and the Mediterranean Sea. The sea is important to regional trade, shipping and fishing.  It is a source of power generation in the form of wind power and nuclear plants. Annual traffic between Great Britain and Ireland amounts to over 12 million passengers and  of traded goods.


key: _node_content
string_value:
{"id_": "2fd9ec82-e685-4188-a661-c21666a20559", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0ebe65ad-b69b-4c74-895e-3c7ace8fb7bb", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b6fecdc8e655f8978017a17797e441e3448315e3a722eaef6cf3c18867726513", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1b768d31-c7f2-4cca-bdca-e5055729c8f3", "node_type": "1", "metadata": {}, "hash": "60cdad903303d00441c0def74c5a3977bd6e7477f2176e85f5df24fa26b90feb", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 112162, "end_char_idx": 115319, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
The sea is southeast of the Gulf of Mexico. The Caribbean Sea has many islands, which are popular among North American tourists because of their tropical climate. The Caribbean Sea is famous around the world as a tourist destination.
    
    History 
    Christopher Columbus came across a group of islands in the Caribbean region. When he did so, he thought he had reached another part of the world. Because of this, he named the islands the ‘West Indies’. However, later it was realized that he found an entire region. It still had its natural resources. The name ‘Caribbean’ was later given to it by the Amerindian tribe, the Caribs. That is how it got its name: the Caribbean Sea.
    
    This entire region covers an area of 1,063,000 sq. miles. It covers from Mexico to the boundaries of South America.
    
    This sea is just as deep as it is wide. Its deepest point is believed to be even lower than 25,220 ft, 7,686 m. That makes this point one of the lowest points on the surface of the earth, and the Caribbean Sea one of the deepest seas in the world.
    
    Other websites 
    
    Seas of the Atlantic Ocean
    Score: 0.90673447
    
    ID: 21206
    Title: Irish Sea
    Summary: The Irish Sea (sometimes called the Manx Sea) is a body of water that separates Ireland and Great Britain. It is known to be one of the most polluted seas in the world including the North Sea and the Mediterranean Sea. The sea is important to regional trade, shipping and fishing.  It is a source of power generation in the form of wind power and nuclear plants. Annual traffic between Great Britain and Ireland amounts to over 12 million passengers and  of traded goods.
    
    Economics 
    It covers  and at its deepest point is  deep. In 2008, about  of fish were caught. Shell fish made up three quarters of this amount.
    
    The Irish Sea has 17 active oil and gas drilling platforms. It is estimated there are about 1.6 billion barrels of oil in the Barryroe oil field alone.
    
    Sealife 
    At least thirty species of shark can be found in the Irish Sea at different times. These include the basking, thresher, blue, mako and porbeagle sharks. There are about 12 species of Dolphin, porpoise and whales in the Irish Sea. These include the common dolphin, bottlenose dolphin and the harbor porpoise.
    
    References 
    
    Seas of the Atlantic Ocean
    Ireland
    Geography of the United Kingdom
    Score: 0.90410626
    
    ID: 6308
    Title: North Sea
    Summary: The North Sea is a sea that is part of the Atlantic Ocean in northern Europe. The North Sea is between Norway and Denmark in the east, Scotland and England in the west, Germany, the Netherlands, Belgium and France in the south.
    
    Borders 
    The Skagerrak connects the North Sea to the Baltic Sea. In the south, the North Sea becomes the English Channel, a sea between England and France. This is called the Dover Straits and is very busy with ships.
    
    The border between the North Sea and the Skagerrak is at an imagined line between Lindesnes in Norway, and Hanstholm in Denmark. In the North, the North sea is open towards the Atlantic.


key: _node_content
string_value:
{"id_": "1b768d31-c7f2-4cca-bdca-e5055729c8f3", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2fd9ec82-e685-4188-a661-c21666a20559", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "abca709d64d90a514e6b726fd4ded5cf6ef13d1a23ece8a1329abded08585d67", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a632b458-5f60-4569-95f3-426abb7aae40", "node_type": "1", "metadata": {}, "hash": "41162d1daf3ffff5e507f9748ab03a7cc8266ad33dce7bd3c1cfdcd6ce1d96f2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 113648, "end_char_idx": 116591, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
It is a source of power generation in the form of wind power and nuclear plants. Annual traffic between Great Britain and Ireland amounts to over 12 million passengers and  of traded goods.
    
    Economics 
    It covers  and at its deepest point is  deep. In 2008, about  of fish were caught. Shell fish made up three quarters of this amount.
    
    The Irish Sea has 17 active oil and gas drilling platforms. It is estimated there are about 1.6 billion barrels of oil in the Barryroe oil field alone.
    
    Sealife 
    At least thirty species of shark can be found in the Irish Sea at different times. These include the basking, thresher, blue, mako and porbeagle sharks. There are about 12 species of Dolphin, porpoise and whales in the Irish Sea. These include the common dolphin, bottlenose dolphin and the harbor porpoise.
    
    References 
    
    Seas of the Atlantic Ocean
    Ireland
    Geography of the United Kingdom
    Score: 0.90410626
    
    ID: 6308
    Title: North Sea
    Summary: The North Sea is a sea that is part of the Atlantic Ocean in northern Europe. The North Sea is between Norway and Denmark in the east, Scotland and England in the west, Germany, the Netherlands, Belgium and France in the south.
    
    Borders 
    The Skagerrak connects the North Sea to the Baltic Sea. In the south, the North Sea becomes the English Channel, a sea between England and France. This is called the Dover Straits and is very busy with ships.
    
    The border between the North Sea and the Skagerrak is at an imagined line between Lindesnes in Norway, and Hanstholm in Denmark. In the North, the North sea is open towards the Atlantic. The border between the two is an imagined line from Northern Scotland, to Shetland, and then to Ålesund in Norway. According to the Oslo-Paris Treaty of 1962 it is a bit more to the west and the north though. The treaty puts it at 5° East longitude, and 62° North latitude. That is at the parallel of the Geirangerfjord in Norway.
    
    Various statistical data 
    On average, the North Sea has a depth of only 94 meters. About 80 million people live near the North Sea, at most 150 km away from the coast. Together with the English Channel in the south, the southern North Sea is the busiest body of water in the world.
    
    Rivers that drain into it 
    Well-known rivers that drain into the North Sea include the Tay (at Dundee), the Forth (at Edinburgh), the Tyne (South Shields), the Wear (at Sunderland),  the Tees (near Middlesbrough),  the Elbe (at Cuxhaven), the Weser (at Bremerhaven), the Rhine and Meuse or Maas (at Rotterdam), the Scheldt (at Flushing or Vlissingen), the Thames, and the Humber (at Hull), and the river Nairn (at Nairn)
    
    The Kiel Canal, one of the world's busiest artificial waterways, connects the North Sea with the Baltic.
    
    Name 
    Its name comes from its relationship to the land of the Frisians (see Frisia).


key: _node_content
string_value:
{"id_": "a632b458-5f60-4569-95f3-426abb7aae40", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1b768d31-c7f2-4cca-bdca-e5055729c8f3", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "7813ca36cffc327583997b3d84bc4d96ef91b3e996b7b39200fdaa015ca46508", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "163ff98c-b175-41bc-b365-83be4febff1d", "node_type": "1", "metadata": {}, "hash": "605c59ac0f82961b26e29fa317be67cf489f653df99e092ca176ba96bda7d0b8", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 115062, "end_char_idx": 118007, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: _node_content
string_value:
{"id_": "163ff98c-b175-41bc-b365-83be4febff1d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a632b458-5f60-4569-95f3-426abb7aae40", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b880c6c9899656fc79ec93d1b41ec40c83fd55d5a34dca36e90c0f37a9488eb6", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3760ea31-9a64-4b38-928e-171165a99aa5", "node_type": "1", "metadata": {}, "hash": "c7c797e1ca8c93a8cd41c850ca659ca16ad75c03a81fd8f64ae522b7ff603b3d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 116503, "end_char_idx": 119698, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: _node_content
string_value:
{"id_": "3760ea31-9a64-4b38-928e-171165a99aa5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "163ff98c-b175-41bc-b365-83be4febff1d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "9764401ac78f9615d27a3e459bbc67dd40cfeddb29c882af9d46fe281baad2a7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6420ee9c-33f9-4885-a7e9-5f54acb1e96a", "node_type": "1", "metadata": {}, "hash": "950495248c5838f3cb91ed24cc11ae9d0ec2c8c068a7baf9363a5fd377096fe1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 117959, "end_char_idx": 121266, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
In the southern part, there are many sand banks.
    
    Looking at the satellite picture it is easy to see the geographic divisions of the North Sea:
    a generally shallow southern North Sea
    the central North Sea
     the northern North Sea, with the Norwegian Trench, near the Skagerrak.
    
    The southern north sea is composed of the Southern Bight, before the coast of Belgium and the Netherlands and the German Bight before the coastline of Germany. The Dogger Bank is the limit between the southern and central parts. The Waddenzee runs all the way from Den Helder in the Netherlands to Esbjerg in Denmark.
    
    The Dogger Bank covers an area about half the size of the Netherlands. There, the North Sea has a depth of between 13 and 20 metres only. The area is very famous for fishing. With some storms there are even waves breaking there.
    
    The Norwegian Trench has an average depth of around 250 to 300 metres; at the entrance to the Skagerrak, the depth increases up to 725 meters. Along the trench is the Norwegian Current, which brings most of the waters of the North Sea into the Atlantic Ocean. Also, most of the waters of the Baltic Sea flow northwards here.
    
    About 200 km east of the Scottish city of Dundee there are more trenches, known collectively as the Devil's hole. Generally, the water is about 90 meters deep there. The trenches very often are only a few kilometers in length. In these trenches, the depth increases to up to 230 meters.
    
    In the Dover Strait the water is about 30 meters deep. At the end of the English Channel, this depth increases to about 100 meters.
    
    History 
    In the last ice age the North Sea was covered by large areas of ice called glaciers. About 20,000 years ago the ice melted and the North Sea was formed (made).
    
    North Sea oil 
    In the 1960s, geologists found large areas of oil and natural gas under the North Sea. Most of the oil fields are owned by the United Kingdom and Norway but some belong to Denmark, the Netherlands and Germany. Drilling began in the 1960s and led to a famous argument between England and Scotland about how the revenue (money) from the oil should be spent.
    
    Animal life 
    
    People have been fishing in the North Sea for thousands of years. However, so many fish are now caught there that new ones may not be able to grow fast enough to keep the fishery going.
    
    Terns, Atlantic puffins, razorbills, kittiwakes and other seabirds live on the North Sea coast. Many coastal areas are protected nature reserves.
    
    Other websites
    
    Seas of the Atlantic Ocean
    Bodies of water of Europe
    Score: 0.9022158
    
    ID: 6278
    Title: Atlantis
    Summary: Atlantis is a name for a fictional large island or small continent that was (in the legend) in the Atlantic Ocean many years before it sank into the depth of the sea .
    
    The name Atlantis first appears in the writings of Herodotus - he describes the western ocean as "Sea of Atlantis." Then, one generation later, Atlantis is described in detail in the stories Timaeus and Critias by the Greek philosopher Plato.  He used this story to help explain his ideas about government and philosophy. Plato was the only ancient writer who wrote specific things about Atlantis.


key: _node_content
string_value:
{"id_": "6420ee9c-33f9-4885-a7e9-5f54acb1e96a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3760ea31-9a64-4b38-928e-171165a99aa5", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "15e16c0b166ca0381c74d0f02f2f8a94fb1baafc8c5d2a37297f2b2f850fd068", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "46b4c73e-1127-4fc0-9f5f-cef3880c5cd5", "node_type": "1", "metadata": {}, "hash": "371f543cd68823cef3b01eae101715f8ad0882191dea3b12417589611e05f93d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 119515, "end_char_idx": 123004, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
At the end of the English Channel, this depth increases to about 100 meters.
    
    History 
    In the last ice age the North Sea was covered by large areas of ice called glaciers. About 20,000 years ago the ice melted and the North Sea was formed (made).
    
    North Sea oil 
    In the 1960s, geologists found large areas of oil and natural gas under the North Sea. Most of the oil fields are owned by the United Kingdom and Norway but some belong to Denmark, the Netherlands and Germany. Drilling began in the 1960s and led to a famous argument between England and Scotland about how the revenue (money) from the oil should be spent.
    
    Animal life 
    
    People have been fishing in the North Sea for thousands of years. However, so many fish are now caught there that new ones may not be able to grow fast enough to keep the fishery going.
    
    Terns, Atlantic puffins, razorbills, kittiwakes and other seabirds live on the North Sea coast. Many coastal areas are protected nature reserves.
    
    Other websites
    
    Seas of the Atlantic Ocean
    Bodies of water of Europe
    Score: 0.9022158
    
    ID: 6278
    Title: Atlantis
    Summary: Atlantis is a name for a fictional large island or small continent that was (in the legend) in the Atlantic Ocean many years before it sank into the depth of the sea .
    
    The name Atlantis first appears in the writings of Herodotus - he describes the western ocean as "Sea of Atlantis." Then, one generation later, Atlantis is described in detail in the stories Timaeus and Critias by the Greek philosopher Plato.  He used this story to help explain his ideas about government and philosophy. Plato was the only ancient writer who wrote specific things about Atlantis.
    
    According to Plato, the Atlanteans lived 9000 years before his own time and were half human and half god.  They created a very good human society.  When they stopped being good people and did bad things, the gods sent earthquakes and fire to destroy Atlantis.
    
    Many scholars think Plato could have been thinking of a real place when he wrote about Atlantis.  Many, many people have thought of many, many places where the real place that inspired Atlantis could have been.  For example, there was a Minoan kingdom on the island of Santorini.  The Minoan kingdom was very powerful thousands of years before Plato, and their society was damaged when a volcano erupted on their island. According to Plato, Atlantis was very large, as big as North Africa, so it should not have been hard to find.
    
    After the discovery of the Americas, some people in Europe thought they might be Atlantis. However, after Plato, the idea of Atlantis was mostly forgotten until 1882, when a writer named Ignatius Donnelly wrote a book saying that Atlantis was real and that the culture of Atlantis had started many other ancient cultures, such as the Egyptian and Mayan.  Then other people became interested in Atlantis. 
    
    Atlantis has appeared in many works of fiction.  In Marvel Comics, Atlantis is at the bottom of the ocean and exists in modern times, with people who breathe water.  Other works of fiction use Atlantis as background.  For example, Robert E. Howard set his Conan the Barbarian stories in a fictional time called the Hyborian Age, which began with the destruction of Atlantis and ended when real written history started.
    
    References
    
    Greek mythology
    Ancient history
    Score: 0.90082896


key: _node_content
string_value:
{"id_": "46b4c73e-1127-4fc0-9f5f-cef3880c5cd5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "bca9a32e-2a68-4086-92a6-f8f665c68eb4", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d87607119004b9428a3ad29eef529770faa5b7d1140477aff6167fabb456d032", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6420ee9c-33f9-4885-a7e9-5f54acb1e96a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb", "title": "Semantic search using Elasticsearch and OpenAI", "tokens": 28076, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "5df77cf178769b11c2cb85135f1c36a4093436f1ad8b56927e7d0987dab6ff3c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 121113, "end_char_idx": 123755, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
He used this story to help explain his ideas about government and philosophy. Plato was the only ancient writer who wrote specific things about Atlantis.
    
    According to Plato, the Atlanteans lived 9000 years before his own time and were half human and half god.  They created a very good human society.  When they stopped being good people and did bad things, the gods sent earthquakes and fire to destroy Atlantis.
    
    Many scholars think Plato could have been thinking of a real place when he wrote about Atlantis.  Many, many people have thought of many, many places where the real place that inspired Atlantis could have been.  For example, there was a Minoan kingdom on the island of Santorini.  The Minoan kingdom was very powerful thousands of years before Plato, and their society was damaged when a volcano erupted on their island. According to Plato, Atlantis was very large, as big as North Africa, so it should not have been hard to find.
    
    After the discovery of the Americas, some people in Europe thought they might be Atlantis. However, after Plato, the idea of Atlantis was mostly forgotten until 1882, when a writer named Ignatius Donnelly wrote a book saying that Atlantis was real and that the culture of Atlantis had started many other ancient cultures, such as the Egyptian and Mayan.  Then other people became interested in Atlantis. 
    
    Atlantis has appeared in many works of fiction.  In Marvel Comics, Atlantis is at the bottom of the ocean and exists in modern times, with people who breathe water.  Other works of fiction use Atlantis as background.  For example, Robert E. Howard set his Conan the Barbarian stories in a fictional time called the Hyborian Age, which began with the destruction of Atlantis and ended when real written history started.
    
    References
    
    Greek mythology
    Ancient history
    Score: 0.90082896


## Next steps

Success! Now you know how to use Elasticsearch as a vector database to store embeddings, encode queries by calling the OpenAI `embeddings` endpoint, and run semantic search.

Play around with different queries, and if you want to try with your own data, you can experiment with different embedding models.

ℹ️ Check out our other notebook [Retrieval augmented generation using Elasticsearch and OpenAI](openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb). That notebook builds on this example to demonstrate how to use Elasticsearch together with the OpenAI [chat completions](https://platform.openai.com/docs/api-reference/chat) API for retrieval augmented generation (RAG).


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/README.ipynb


key: _node_content
string_value:
{"id_": "4a204cc1-fc34-419d-876e-906133e3a4ca", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/README.ipynb", "title": "Elasticsearch", "tokens": 324, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "05965a15-c9dd-4053-801a-9c08e64dec30", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/README.ipynb", "title": "Elasticsearch", "tokens": 324, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3ccc242a5ae33cbbbe82550bb4e72bf37f8cae1f40dae977af4d5ee5e6854e57", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 1523, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Elasticsearch

Elasticsearch is a popular search/analytics engine and [vector database](https://www.elastic.co/elasticsearch/vector-database).
Elasticsearch offers an efficient way to create, store, and search vector embeddings at scale.

For technical details, refer to the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html).

The [`elasticsearch-labs`](https://github.com/elastic/elasticsearch-labs) repo contains executable Python notebooks, sample apps, and resources for testing out the Elastic platform.

## OpenAI cookbook notebooks 📒

Check out our notebooks in this repo for working with OpenAI, using Elasticsearch as your vector database.

### [Semantic search](https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-semantic-search.ipynb)

In this notebook you'll learn how to:

 - Index the OpenAI Wikipedia embeddings dataset into Elasticsearch
 - Encode a question with the `openai ada-02` model
 - Perform a semantic search

<hr>


### [Retrieval augmented generation](https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-retrieval-augmented-generation.ipynb)

This notebooks builds on the semantic search notebook by:

- Selecting the top hit from a semantic search
- Sending that result to the OpenAI [Chat Completions](https://platform.openai.com/docs/guides/gpt/chat-completions-api) API endpoint for retrieval augmented generation (RAG)


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb


key: _node_content
string_value:
{"id_": "6136f06f-798f-4f5a-a879-9d068a79f756", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "8989de5b-0dd3-456b-8ac1-1b35b61be36d", "node_type": "1", "metadata": {}, "hash": "06492cb4e28eb7624892892d6d68018f8446b0cb096537fa7494070ac60c524a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2459, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Getting Started with Milvus and OpenAI
### Finding your next book

In this notebook we will be going over generating embeddings of book descriptions with OpenAI and using those embeddings within Milvus to find relevant books. The dataset in this example is sourced from HuggingFace datasets, and contains a little over 1 million title-description pairs.

Lets begin by first downloading the required libraries for this notebook:
- `openai` is used for communicating with the OpenAI embedding service
- `pymilvus` is used for communicating with the Milvus server
- `datasets` is used for downloading the dataset
- `tqdm` is used for the progress bars



```python
! pip install openai pymilvus datasets tqdm
```

    Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
    Requirement already satisfied: openai in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (0.27.2)
    Requirement already satisfied: pymilvus in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (2.2.2)
    Requirement already satisfied: datasets in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (2.10.1)
    Requirement already satisfied: tqdm in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (4.64.1)
    Requirement already satisfied: aiohttp in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from openai) (3.8.4)
    Requirement already satisfied: requests>=2.20 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from openai) (2.28.2)
    Requirement already satisfied: pandas>=1.2.4 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.5.3)
    Requirement already satisfied: ujson<=5.4.0,>=2.0.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (5.1.0)
    Requirement already satisfied: mmh3<=3.0.0,>=2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (3.0.0)
    Requirement already satisfied: grpcio<=1.48.0,>=1.47.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.47.2)
    Requirement already satisfied: grpcio-tools<=1.48.0,>=1.47.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.47.2)
    Requirement already satisfied: huggingface-hub<1.0.0,>=0.2.


key: _node_content
string_value:
{"id_": "8989de5b-0dd3-456b-8ac1-1b35b61be36d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6136f06f-798f-4f5a-a879-9d068a79f756", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "9c601a39a53550cc6af76cd384ae644270ad2b4508a3fe8621ac5be32df5146b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5b764b0e-34bb-4d27-84b7-b2dcfe8503cb", "node_type": "1", "metadata": {}, "hash": "1914189bad3bde2e9962fc2a932a36f835248724c335242ee74d4ee0a453b450", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1413, "end_char_idx": 3673, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
9/site-packages (from openai) (3.8.4)
    Requirement already satisfied: requests>=2.20 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from openai) (2.28.2)
    Requirement already satisfied: pandas>=1.2.4 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.5.3)
    Requirement already satisfied: ujson<=5.4.0,>=2.0.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (5.1.0)
    Requirement already satisfied: mmh3<=3.0.0,>=2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (3.0.0)
    Requirement already satisfied: grpcio<=1.48.0,>=1.47.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.47.2)
    Requirement already satisfied: grpcio-tools<=1.48.0,>=1.47.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pymilvus) (1.47.2)
    Requirement already satisfied: huggingface-hub<1.0.0,>=0.2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.12.1)
    Requirement already satisfied: dill<0.3.7,>=0.3.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.3.6)
    Requirement already satisfied: xxhash in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (3.2.0)
    Requirement already satisfied: pyyaml>=5.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (5.4.1)
    Requirement already satisfied: fsspec[http]>=2021.11.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (2023.1.0)
    Requirement already satisfied: packaging in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (23.0)
    Requirement already satisfied: numpy>=1.17 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (1.23.5)
    Requirement already satisfied: multiprocess in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.70.14)
    Requirement already satisfied: pyarrow>=6.0.


key: _node_content
string_value:
{"id_": "5b764b0e-34bb-4d27-84b7-b2dcfe8503cb", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "8989de5b-0dd3-456b-8ac1-1b35b61be36d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "8733fe2b0a150975b3ceeb017a1e3fcc64d39a7665e3da0b7d5f142e372acb94", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1570c16b-a43c-4da8-b693-24367924ea9d", "node_type": "1", "metadata": {}, "hash": "46c75ed86932c21eb33afb9e09364fe69884d55a7fe06e60118276bb47930b17", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2449, "end_char_idx": 4807, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
0.0,>=0.2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.12.1)
    Requirement already satisfied: dill<0.3.7,>=0.3.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.3.6)
    Requirement already satisfied: xxhash in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (3.2.0)
    Requirement already satisfied: pyyaml>=5.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (5.4.1)
    Requirement already satisfied: fsspec[http]>=2021.11.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (2023.1.0)
    Requirement already satisfied: packaging in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (23.0)
    Requirement already satisfied: numpy>=1.17 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (1.23.5)
    Requirement already satisfied: multiprocess in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.70.14)
    Requirement already satisfied: pyarrow>=6.0.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (10.0.1)
    Requirement already satisfied: responses<0.19 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.18.0)
    Requirement already satisfied: multidict<7.0,>=4.5 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (6.0.4)
    Requirement already satisfied: frozenlist>=1.1.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.3.3)
    Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (4.0.2)
    Requirement already satisfied: yarl<2.0,>=1.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.8.2)
    Requirement already satisfied: aiosignal>=1.1.2 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.3.1)
    Requirement already satisfied: charset-normalizer<4.0,


key: _node_content
string_value:
{"id_": "1570c16b-a43c-4da8-b693-24367924ea9d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5b764b0e-34bb-4d27-84b7-b2dcfe8503cb", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "94e2a332debd5ab0cfdd55286b654d6ef4bd839a3aa393f41e176eef18f49405", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b1496e56-204f-4a37-9d1e-587a298c4ba7", "node_type": "1", "metadata": {}, "hash": "4da2aa5353fd988236f6544bdb8a58694d0ee9968c021da33abb12725e30a962", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3618, "end_char_idx": 5803, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
70.14)
    Requirement already satisfied: pyarrow>=6.0.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (10.0.1)
    Requirement already satisfied: responses<0.19 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from datasets) (0.18.0)
    Requirement already satisfied: multidict<7.0,>=4.5 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (6.0.4)
    Requirement already satisfied: frozenlist>=1.1.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.3.3)
    Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (4.0.2)
    Requirement already satisfied: yarl<2.0,>=1.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.8.2)
    Requirement already satisfied: aiosignal>=1.1.2 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (1.3.1)
    Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (3.0.1)
    Requirement already satisfied: attrs>=17.3.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (22.2.0)
    Requirement already satisfied: six>=1.5.2 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio<=1.48.0,>=1.47.0->pymilvus) (1.16.0)
    Requirement already satisfied: protobuf<4.0dev,>=3.12.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio-tools<=1.48.0,>=1.47.0->pymilvus) (3.20.1)
    Requirement already satisfied: setuptools in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio-tools<=1.48.0,>=1.47.0->pymilvus) (65.6.3)
    Requirement already satisfied: filelock in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.2.0->datasets) (3.9.


key: _node_content
string_value:
{"id_": "b1496e56-204f-4a37-9d1e-587a298c4ba7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1570c16b-a43c-4da8-b693-24367924ea9d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "dd537787c71fcc19ef4f957f8152d92e56e126a592f6545eb9e3c3852ffedd63", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ebe65897-e12b-4d63-b770-c193192b05e9", "node_type": "1", "metadata": {}, "hash": "1a1942cd96e5021e536f597c3dd3719b75122376850b3d2df47b71ae7a38311c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4702, "end_char_idx": 6854, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
9/site-packages (from aiohttp->openai) (1.3.1)
    Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (3.0.1)
    Requirement already satisfied: attrs>=17.3.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from aiohttp->openai) (22.2.0)
    Requirement already satisfied: six>=1.5.2 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio<=1.48.0,>=1.47.0->pymilvus) (1.16.0)
    Requirement already satisfied: protobuf<4.0dev,>=3.12.0 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio-tools<=1.48.0,>=1.47.0->pymilvus) (3.20.1)
    Requirement already satisfied: setuptools in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from grpcio-tools<=1.48.0,>=1.47.0->pymilvus) (65.6.3)
    Requirement already satisfied: filelock in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.2.0->datasets) (3.9.0)
    Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.2.0->datasets) (4.5.0)
    Requirement already satisfied: python-dateutil>=2.8.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pandas>=1.2.4->pymilvus) (2.8.2)
    Requirement already satisfied: pytz>=2020.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pandas>=1.2.4->pymilvus) (2022.7.1)
    Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (1.26.14)
    Requirement already satisfied: idna<4,>=2.5 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (3.4)
    Requirement already satisfied: certifi>=2017.4.17 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (2022.12.7)


key: _node_content
string_value:
{"id_": "ebe65897-e12b-4d63-b770-c193192b05e9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b1496e56-204f-4a37-9d1e-587a298c4ba7", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "476667c7b57089f5854f36efeecbdf00a5829a381eda15ee4728b12b19fc16ce", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2cc36cf2-bb89-490b-9d1f-554289d17fe0", "node_type": "1", "metadata": {}, "hash": "5415167b94e7c32b7afa65668a089e588b6f7c38fc6be8715f6efb74c8028af5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5775, "end_char_idx": 7953, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
0.0,>=0.2.0->datasets) (3.9.0)
    Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.2.0->datasets) (4.5.0)
    Requirement already satisfied: python-dateutil>=2.8.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pandas>=1.2.4->pymilvus) (2.8.2)
    Requirement already satisfied: pytz>=2020.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from pandas>=1.2.4->pymilvus) (2022.7.1)
    Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (1.26.14)
    Requirement already satisfied: idna<4,>=2.5 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (3.4)
    Requirement already satisfied: certifi>=2017.4.17 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (2022.12.7)


With the required packages installed we can get started. Lets begin by launching the Milvus service. The file being run is the `docker-compose.yaml` found in the folder of this file. This command launches a Milvus standalone instance which we will use for this test.  


```python
! docker compose up -d
```

    [1A[1B[0G[?25l[+] Running 0/0
    [37m ⠋ Network milvus  Creating                                                0.1s
    [0m[?25h[1A[1A[0G[?25l[34m[+] Running 1/1[0m
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠋ Container milvus-minio  Creating                                        0.1s
    [0m[37m ⠋ Container milvus-etcd   Creating                                        0.1s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠙ Container milvus-minio  Creating                                        0.2s
    [0m[37m ⠙ Container milvus-etcd   Creating                                        0.


key: _node_content
string_value:
{"id_": "2cc36cf2-bb89-490b-9d1f-554289d17fe0", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ebe65897-e12b-4d63-b770-c193192b05e9", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "eb4b9223653056f44afa3fcc193b567c08905a5bef5a93c9a4f69ce3cedc4421", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "8700334f-d4ef-4057-a425-add1671b61ed", "node_type": "1", "metadata": {}, "hash": "7bdb1af130e6059b0ff6642114df955c9b274d9d93a80c6c10d2e2fdb0f0b511", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6663, "end_char_idx": 9052, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
20->openai) (3.4)
    Requirement already satisfied: certifi>=2017.4.17 in /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages (from requests>=2.20->openai) (2022.12.7)


With the required packages installed we can get started. Lets begin by launching the Milvus service. The file being run is the `docker-compose.yaml` found in the folder of this file. This command launches a Milvus standalone instance which we will use for this test.  


```python
! docker compose up -d
```

    [1A[1B[0G[?25l[+] Running 0/0
    [37m ⠋ Network milvus  Creating                                                0.1s
    [0m[?25h[1A[1A[0G[?25l[34m[+] Running 1/1[0m
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠋ Container milvus-minio  Creating                                        0.1s
    [0m[37m ⠋ Container milvus-etcd   Creating                                        0.1s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠙ Container milvus-minio  Creating                                        0.2s
    [0m[37m ⠙ Container milvus-etcd   Creating                                        0.2s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠹ Container milvus-minio  Creating                                        0.3s
    [0m[37m ⠹ Container milvus-etcd   Creating                                        0.3s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[34m[+] Running 3/3[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[37m ⠋ Container milvus-standalone  Creating                                   0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.


key: _node_content
string_value:
{"id_": "8700334f-d4ef-4057-a425-add1671b61ed", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2cc36cf2-bb89-490b-9d1f-554289d17fe0", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a4dad88ab857f9814e95e76c4064c58567279d6267a5656b3144bd30fbeb8b61", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a14cf989-753a-48a4-a37b-0f6c9015f9bd", "node_type": "1", "metadata": {}, "hash": "e8678964bf9efc89c45e91c4505bbb042892724d8c8af5d77623f24c2f1be52f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7860, "end_char_idx": 10159, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
2s
    [0m[37m ⠙ Container milvus-etcd   Creating                                        0.2s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠹ Container milvus-minio  Creating                                        0.3s
    [0m[37m ⠹ Container milvus-etcd   Creating                                        0.3s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[34m[+] Running 3/3[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[37m ⠋ Container milvus-standalone  Creating                                   0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[37m ⠙ Container milvus-standalone  Creating                                   0.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[34m[+] Running 4/4[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.7s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.


key: _node_content
string_value:
{"id_": "a14cf989-753a-48a4-a37b-0f6c9015f9bd", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "8700334f-d4ef-4057-a425-add1671b61ed", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0ea5f3f0b281f36c5f9ce2dd4acec81dcd7d9021afa1f9c405190d312f3906a9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d637ecdd-da95-424e-bb1a-0f3a3000a29e", "node_type": "1", "metadata": {}, "hash": "eb6f5da275b7f3a26f956adbaf9d3afbd6586dbeded10756042d1530336fb76c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8959, "end_char_idx": 11294, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[37m ⠙ Container milvus-standalone  Creating                                   0.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[34m[+] Running 4/4[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Created                                    0.3s
    [0m[34m ⠿ Container milvus-etcd        Created                                    0.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.7s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.8s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.8s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.9s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.9s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.0s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.0s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.


key: _node_content
string_value:
{"id_": "d637ecdd-da95-424e-bb1a-0f3a3000a29e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a14cf989-753a-48a4-a37b-0f6c9015f9bd", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "18bded39e0aa33e18a5390c3718faa974e6e72e09e182608e3a7454b724acd60", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "022c9b5a-3808-45cd-be77-d17d6959e2aa", "node_type": "1", "metadata": {}, "hash": "bf1286a19ffe9725d1ba01871187cd70011f94f3b21e157d59542dff6c1f682d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10159, "end_char_idx": 12485, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.8s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.8s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.9s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.9s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.0s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.0s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.1s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.2s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.2s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.3s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.


key: _node_content
string_value:
{"id_": "022c9b5a-3808-45cd-be77-d17d6959e2aa", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d637ecdd-da95-424e-bb1a-0f3a3000a29e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d5c39921262d12dcc092783deb81f5f2a6fab2af30ed5ca6f2f9d0cdc7c10cea", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c312bc65-402a-4cc5-9745-77b3cb4f1467", "node_type": "1", "metadata": {}, "hash": "17ec910260dae1b14dfd584803e3d46bedaebee40cd4f48eeed34ca95e088494", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11294, "end_char_idx": 13583, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.1s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.2s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.2s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.3s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.4s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.4s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.5s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.5s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.


key: _node_content
string_value:
{"id_": "c312bc65-402a-4cc5-9745-77b3cb4f1467", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "022c9b5a-3808-45cd-be77-d17d6959e2aa", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "7fd28c556ce150f2176b23967f601bf1f3324622e7af23619fefdbf1d62385c9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d7c85044-e88d-42c5-a1b9-eadb72eea524", "node_type": "1", "metadata": {}, "hash": "bf042a2918ca20e898f34868df9eacdec658c58d4ae59ca5730204594dde648e", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 12392, "end_char_idx": 14718, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.4s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.4s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.5s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.5s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.6s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.6s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.7s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.


key: _node_content
string_value:
{"id_": "d7c85044-e88d-42c5-a1b9-eadb72eea524", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c312bc65-402a-4cc5-9745-77b3cb4f1467", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "dc655dc81b9c0a06b1571c2ef46c4f20a88b0234ea0e9173b64cb16fc5c00ea9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "33b113aa-4baf-48ec-b8d8-c300e859149d", "node_type": "1", "metadata": {}, "hash": "8d268fe7d0a8c3be0754f09e2bccf3c6ac354898bfb6fedc3b950413d1ad2323", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 13583, "end_char_idx": 15909, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.6s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.6s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.7s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   1.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.6s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.7s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.


key: _node_content
string_value:
{"id_": "33b113aa-4baf-48ec-b8d8-c300e859149d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d7c85044-e88d-42c5-a1b9-eadb72eea524", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "f3dd18e087e68997e59e7e2447de9262ba129d3123dd6eb9d90c6d34e04522f2", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "25a9c367-ec5a-487c-af70-50d4869085c2", "node_type": "1", "metadata": {}, "hash": "3e2977280cd28d7fa7e1b24787db545d4599067503f3356d1caa1d1542bda64c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 14718, "end_char_idx": 17007, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.6s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.7s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.8s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.9s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.0s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.


key: _node_content
string_value:
{"id_": "25a9c367-ec5a-487c-af70-50d4869085c2", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "33b113aa-4baf-48ec-b8d8-c300e859149d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "d6d3c429ea228af08b36480a85c4320a4009485c4228a16da79907a8fb818bca", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "849dd043-54ea-4b95-89fa-04dbe4e7ab1c", "node_type": "1", "metadata": {}, "hash": "9309ef64349f694afa09c212c583b5efa2ba3d49c36a94789e4fc08dbf492580", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 15816, "end_char_idx": 18142, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.8s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.9s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.0s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.


key: _node_content
string_value:
{"id_": "849dd043-54ea-4b95-89fa-04dbe4e7ab1c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "25a9c367-ec5a-487c-af70-50d4869085c2", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "81d6c4eae99e9aaa4226303cd445a9273c0e934ef57da9a066b3766a7abda628", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a02e3b82-9fd7-4741-bd46-fef0df5602f8", "node_type": "1", "metadata": {}, "hash": "cd6a25a4008bb799de34b7b2aed8e7ba076baebef74e035947608d09829000e5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 17007, "end_char_idx": 19333, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.4s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.5s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.


key: _node_content
string_value:
{"id_": "a02e3b82-9fd7-4741-bd46-fef0df5602f8", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "849dd043-54ea-4b95-89fa-04dbe4e7ab1c", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "f0204e1233be62ccdfa283292291a06e1f39cde09c7db538031594cfba065c77", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "fa2c75cb-958d-432f-bcbb-298b66982a2f", "node_type": "1", "metadata": {}, "hash": "832cfc1551aa5bda8d7e38921519a3dd0f98d2718e0a441b848d5ff462b69286", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 18142, "end_char_idx": 20354, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.4s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.5s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.6s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[34m[+] Running 4/4[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[34m ⠿ Container milvus-standalone  Started                                    2.6s
    [0m[?25h

With Milvus running we can setup our global variables:
- HOST: The Milvus host address
- PORT: The Milvus port number
- COLLECTION_NAME: What to name the collection within Milvus
- DIMENSION: The dimension of the embeddings
- OPENAI_ENGINE: Which embedding model to use
- openai.api_key: Your OpenAI account key
- INDEX_PARAM: The index settings to use for the collection
- QUERY_PARAM: The search parameters to use
- BATCH_SIZE: How many texts to embed and insert at once


key: _node_content
string_value:
{"id_": "fa2c75cb-958d-432f-bcbb-298b66982a2f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a02e3b82-9fd7-4741-bd46-fef0df5602f8", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "8f72f9c033210a8303c34c4c76e547db7f50c5077d8260c471abd005563ded46", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "dcb266e5-d760-471c-9450-f0c797f699f7", "node_type": "1", "metadata": {}, "hash": "1abbc5898e8e72a42d68672542d0a87cb2b137912e6398699b5efd5e678e48a2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 19147, "end_char_idx": 21735, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   2.6s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[34m[+] Running 4/4[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.8s
    [0m[34m ⠿ Container milvus-etcd        Started                                    1.7s
    [0m[34m ⠿ Container milvus-standalone  Started                                    2.6s
    [0m[?25h

With Milvus running we can setup our global variables:
- HOST: The Milvus host address
- PORT: The Milvus port number
- COLLECTION_NAME: What to name the collection within Milvus
- DIMENSION: The dimension of the embeddings
- OPENAI_ENGINE: Which embedding model to use
- openai.api_key: Your OpenAI account key
- INDEX_PARAM: The index settings to use for the collection
- QUERY_PARAM: The search parameters to use
- BATCH_SIZE: How many texts to embed and insert at once


```python
import openai

HOST = 'localhost'
PORT = 19530
COLLECTION_NAME = 'book_search'
DIMENSION = 1536
OPENAI_ENGINE = 'text-embedding-3-small'
openai.api_key = 'sk-your_key'

INDEX_PARAM = {
    'metric_type':'L2',
    'index_type':"HNSW",
    'params':{'M': 8, 'efConstruction': 64}
}

QUERY_PARAM = {
    "metric_type": "L2",
    "params": {"ef": 64},
}

BATCH_SIZE = 1000
```

## Milvus
This segment deals with Milvus and setting up the database for this use case. Within Milvus we need to setup a collection and index the collection. 


```python
from pymilvus import connections, utility, FieldSchema, Collection, CollectionSchema, DataType

# Connect to Milvus Database
connections.connect(host=HOST, port=PORT)
```


```python
# Remove collection if it already exists
if utility.has_collection(COLLECTION_NAME):
    utility.drop_collection(COLLECTION_NAME)
```


```python
# Create collection which includes the id, title, and embedding.
fields = [
    FieldSchema(name='id', dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name='title', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='description', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, dim=DIMENSION)
]
schema = CollectionSchema(fields=fields)
collection = Collection(name=COLLECTION_NAME, schema=schema)
```


key: _node_content
string_value:
{"id_": "dcb266e5-d760-471c-9450-f0c797f699f7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "fa2c75cb-958d-432f-bcbb-298b66982a2f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "177441aacb49217ab725e542264b2895f2e2e4bb4df5b8c0fec318ca9f0c9f08", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c3659fa2-f628-45a5-b903-645fd1f847e3", "node_type": "1", "metadata": {}, "hash": "d93d38d3b67deec649226f40e35679f6699d2d4b4b15c40bfd76378202153c2b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 20357, "end_char_idx": 23194, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import openai

HOST = 'localhost'
PORT = 19530
COLLECTION_NAME = 'book_search'
DIMENSION = 1536
OPENAI_ENGINE = 'text-embedding-3-small'
openai.api_key = 'sk-your_key'

INDEX_PARAM = {
    'metric_type':'L2',
    'index_type':"HNSW",
    'params':{'M': 8, 'efConstruction': 64}
}

QUERY_PARAM = {
    "metric_type": "L2",
    "params": {"ef": 64},
}

BATCH_SIZE = 1000
```

## Milvus
This segment deals with Milvus and setting up the database for this use case. Within Milvus we need to setup a collection and index the collection. 


```python
from pymilvus import connections, utility, FieldSchema, Collection, CollectionSchema, DataType

# Connect to Milvus Database
connections.connect(host=HOST, port=PORT)
```


```python
# Remove collection if it already exists
if utility.has_collection(COLLECTION_NAME):
    utility.drop_collection(COLLECTION_NAME)
```


```python
# Create collection which includes the id, title, and embedding.
fields = [
    FieldSchema(name='id', dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name='title', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='description', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, dim=DIMENSION)
]
schema = CollectionSchema(fields=fields)
collection = Collection(name=COLLECTION_NAME, schema=schema)
```


```python
# Create the index on the collection and load it.
collection.create_index(field_name="embedding", index_params=INDEX_PARAM)
collection.load()
```

## Dataset
With Milvus up and running we can begin grabbing our data. Hugging Face Datasets is a hub that holds many different user datasets, and for this example we are using Skelebor's book dataset. This dataset contains title-description pairs for over 1 million books. We are going to embed each description and store it within Milvus along with its title. 


```python
import datasets

# Download the dataset and only use the `train` portion (file is around 800Mb)
dataset = datasets.load_dataset('Skelebor/book_titles_and_descriptions_en_clean', split='train')
```

    /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
      from .autonotebook import tqdm as notebook_tqdm
    Found cached dataset parquet (/Users/filiphaltmayer/.cache/huggingface/datasets/Skelebor___parquet/Skelebor--book_titles_and_descriptions_en_clean-3596935b1d8a7747/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec)


## Insert the Data
Now that we have our data on our machine we can begin embedding it and inserting it into Milvus. The embedding function takes in text and returns the embeddings in a list format.


key: _node_content
string_value:
{"id_": "c3659fa2-f628-45a5-b903-645fd1f847e3", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "dcb266e5-d760-471c-9450-f0c797f699f7", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "2b679c42aa6344889870b1e8d11fca23c7341e1b34ce68d2df2df83d90563bdc", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "bd0f063b-9dcd-4f5b-8b59-0ce2919747e9", "node_type": "1", "metadata": {}, "hash": "d13b30ab535b0a2b14e01b7e717ae104124501dbdd2272e3aa3a78215b355d73", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 21738, "end_char_idx": 24719, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# Create the index on the collection and load it.
collection.create_index(field_name="embedding", index_params=INDEX_PARAM)
collection.load()
```

## Dataset
With Milvus up and running we can begin grabbing our data. Hugging Face Datasets is a hub that holds many different user datasets, and for this example we are using Skelebor's book dataset. This dataset contains title-description pairs for over 1 million books. We are going to embed each description and store it within Milvus along with its title. 


```python
import datasets

# Download the dataset and only use the `train` portion (file is around 800Mb)
dataset = datasets.load_dataset('Skelebor/book_titles_and_descriptions_en_clean', split='train')
```

    /Users/filiphaltmayer/miniconda3/envs/haystack/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
      from .autonotebook import tqdm as notebook_tqdm
    Found cached dataset parquet (/Users/filiphaltmayer/.cache/huggingface/datasets/Skelebor___parquet/Skelebor--book_titles_and_descriptions_en_clean-3596935b1d8a7747/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec)


## Insert the Data
Now that we have our data on our machine we can begin embedding it and inserting it into Milvus. The embedding function takes in text and returns the embeddings in a list format. 


```python
# Simple function that converts the texts to embeddings
def embed(texts):
    embeddings = openai.Embedding.create(
        input=texts,
        engine=OPENAI_ENGINE
    )
    return [x['embedding'] for x in embeddings['data']]

```

This next step does the actual inserting. Due to having so many datapoints, if you want to immidiately test it out you can stop the inserting cell block early and move along. Doing this will probably decrease the accuracy of the results due to less datapoints, but it should still be good enough. 


```python
from tqdm import tqdm

data = [
    [], # title
    [], # description
]

# Embed and insert in batches
for i in tqdm(range(0, len(dataset))):
    data[0].append(dataset[i]['title'])
    data[1].append(dataset[i]['description'])
    if len(data[0]) % BATCH_SIZE == 0:
        data.append(embed(data[1]))
        collection.insert(data)
        data = [[],[]]

# Embed and insert the remainder 
if len(data[0]) != 0:
    data.append(embed(data[1]))
    collection.insert(data)
    data = [[],[]]

```

      0%|          | 1999/1032335 [00:06<57:22, 299.31it/s]  



    ---------------------------------------------------------------------------

    KeyboardInterrupt                         Traceback (most recent call last)

    Cell In[18], line 13
         11 data[1].append(dataset[i]['description'])
         12 if len(data[0]) % BATCH_SIZE == 0:
    ---> 13     data.append(embed(data[1]))
         14     collection.insert(data)
         15     data = [[],[]]


key: _node_content
string_value:
{"id_": "bd0f063b-9dcd-4f5b-8b59-0ce2919747e9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c3659fa2-f628-45a5-b903-645fd1f847e3", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "f93ee46a07f19d85701ad559e08a5c7ba002a0d1a0efdaecad09ca357c8d4688", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0c7eb3f1-9e74-442e-b09e-9a0908ede4b3", "node_type": "1", "metadata": {}, "hash": "d6ff75ce25211dd6029a9c3b99652e2a740920ea843d6467510bb82aa1daf864", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 23198, "end_char_idx": 25463, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# Simple function that converts the texts to embeddings
def embed(texts):
    embeddings = openai.Embedding.create(
        input=texts,
        engine=OPENAI_ENGINE
    )
    return [x['embedding'] for x in embeddings['data']]

```

This next step does the actual inserting. Due to having so many datapoints, if you want to immidiately test it out you can stop the inserting cell block early and move along. Doing this will probably decrease the accuracy of the results due to less datapoints, but it should still be good enough. 


```python
from tqdm import tqdm

data = [
    [], # title
    [], # description
]

# Embed and insert in batches
for i in tqdm(range(0, len(dataset))):
    data[0].append(dataset[i]['title'])
    data[1].append(dataset[i]['description'])
    if len(data[0]) % BATCH_SIZE == 0:
        data.append(embed(data[1]))
        collection.insert(data)
        data = [[],[]]

# Embed and insert the remainder 
if len(data[0]) != 0:
    data.append(embed(data[1]))
    collection.insert(data)
    data = [[],[]]

```

      0%|          | 1999/1032335 [00:06<57:22, 299.31it/s]  



    ---------------------------------------------------------------------------

    KeyboardInterrupt                         Traceback (most recent call last)

    Cell In[18], line 13
         11 data[1].append(dataset[i]['description'])
         12 if len(data[0]) % BATCH_SIZE == 0:
    ---> 13     data.append(embed(data[1]))
         14     collection.insert(data)
         15     data = [[],[]]


    Cell In[17], line 3, in embed(texts)
          2 def embed(texts):
    ----> 3     embeddings = openai.Embedding.create(
          4         input=texts,
          5         engine=OPENAI_ENGINE
          6     )
          7     return [x['embedding'] for x in embeddings['data']]


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_resources/embedding.py:33, in Embedding.create(cls, *args, **kwargs)
         31 while True:
         32     try:
    ---> 33         response = super().create(*args, **kwargs)
         35         # If a user specifies base64, we'll just return the encoded string.
         36         # This is only for the default case.
         37         if not user_provided_encoding_format:


key: _node_content
string_value:
{"id_": "0c7eb3f1-9e74-442e-b09e-9a0908ede4b3", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "bd0f063b-9dcd-4f5b-8b59-0ce2919747e9", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "3aed3aeb9c4346b8eac9f6064d9fbe60d0c474576c0effe62f028e2487981ec7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0515fd56-a33f-40a9-825c-33a54420ef0e", "node_type": "1", "metadata": {}, "hash": "ced7d2c290925705047f42c8457ce4d95c177f15d40a2cbca5292a2c41486635", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 24321, "end_char_idx": 27437, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
---------------------------------------------------------------------------

    KeyboardInterrupt                         Traceback (most recent call last)

    Cell In[18], line 13
         11 data[1].append(dataset[i]['description'])
         12 if len(data[0]) % BATCH_SIZE == 0:
    ---> 13     data.append(embed(data[1]))
         14     collection.insert(data)
         15     data = [[],[]]


    Cell In[17], line 3, in embed(texts)
          2 def embed(texts):
    ----> 3     embeddings = openai.Embedding.create(
          4         input=texts,
          5         engine=OPENAI_ENGINE
          6     )
          7     return [x['embedding'] for x in embeddings['data']]


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_resources/embedding.py:33, in Embedding.create(cls, *args, **kwargs)
         31 while True:
         32     try:
    ---> 33         response = super().create(*args, **kwargs)
         35         # If a user specifies base64, we'll just return the encoded string.
         36         # This is only for the default case.
         37         if not user_provided_encoding_format:


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:153, in EngineAPIResource.create(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)
        127 @classmethod
        128 def create(
        129     cls,
       (...)
        136     **params,
        137 ):
        138     (
        139         deployment_id,
        140         engine,
       (...)
        150         api_key, api_base, api_type, api_version, organization, **params
        151     )
    --> 153     response, _, api_key = requestor.request(
        154         "post",
        155         url,
        156         params=params,
        157         headers=headers,
        158         stream=stream,
        159         request_id=request_id,
        160         request_timeout=request_timeout,
        161     )
        163     if stream:
        164         # must be an iterator
        165         assert not isinstance(response, OpenAIResponse)


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_requestor.py:216, in APIRequestor.request(self, method, url, params, headers, files, stream, request_id, request_timeout)
        205 def request(
        206     self,
        207     method,
       (...)
        214     request_timeout: Optional[Union[float, Tuple[float, float]]] = None,
        215 ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool, str]:
    --> 216     result = self.request_raw(
        217         method.lower(),
        218         url,
        219         params=params,
        220         supplied_headers=headers,
        221         files=files,
        222         stream=stream,
        223         request_id=request_id,
        224         request_timeout=request_timeout,
        225     )
        226     resp, got_stream = self._interpret_response(result, stream)
        227     return resp, got_stream, self.api_key


key: _node_content
string_value:
{"id_": "0515fd56-a33f-40a9-825c-33a54420ef0e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0c7eb3f1-9e74-442e-b09e-9a0908ede4b3", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "3de736c4e44152e84e0c3dca2e270f88ecbf06489c5bccce435ea8eaae0548bb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "61c289b4-e319-4e76-87f1-8934942fde10", "node_type": "1", "metadata": {}, "hash": "32c1ac354de1714a15cf950b771097a2137e20c99b78824acba78f4a1f3f4d83", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 26495, "end_char_idx": 29058, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_requestor.py:216, in APIRequestor.request(self, method, url, params, headers, files, stream, request_id, request_timeout)
        205 def request(
        206     self,
        207     method,
       (...)
        214     request_timeout: Optional[Union[float, Tuple[float, float]]] = None,
        215 ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool, str]:
    --> 216     result = self.request_raw(
        217         method.lower(),
        218         url,
        219         params=params,
        220         supplied_headers=headers,
        221         files=files,
        222         stream=stream,
        223         request_id=request_id,
        224         request_timeout=request_timeout,
        225     )
        226     resp, got_stream = self._interpret_response(result, stream)
        227     return resp, got_stream, self.api_key


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_requestor.py:516, in APIRequestor.request_raw(self, method, url, params, supplied_headers, files, stream, request_id, request_timeout)
        514     _thread_context.session = _make_session()
        515 try:
    --> 516     result = _thread_context.session.request(
        517         method,
        518         abs_url,
        519         headers=headers,
        520         data=data,
        521         files=files,
        522         stream=stream,
        523         timeout=request_timeout if request_timeout else TIMEOUT_SECS,
        524     )
        525 except requests.exceptions.Timeout as e:
        526     raise error.Timeout("Request timed out: {}".format(e)) from e


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/sessions.py:587, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
        582 send_kwargs = {
        583     "timeout": timeout,
        584     "allow_redirects": allow_redirects,
        585 }
        586 send_kwargs.update(settings)
    --> 587 resp = self.send(prep, **send_kwargs)
        589 return resp


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/sessions.py:701, in Session.send(self, request, **kwargs)
        698 start = preferred_clock()
        700 # Send the request
    --> 701 r = adapter.send(request, **kwargs)
        703 # Total elapsed time of the request (approximately)
        704 elapsed = preferred_clock() - start


key: _node_content
string_value:
{"id_": "61c289b4-e319-4e76-87f1-8934942fde10", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0515fd56-a33f-40a9-825c-33a54420ef0e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "62c0094a00e8625ed86bad848696844321bcea3324fed88f65575f6cfe6a8cee", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "55134acd-138d-4c6f-b893-0498822947bd", "node_type": "1", "metadata": {}, "hash": "0f5f41412fbe5ff3d1983a3a4f67bb81332081a5e0d476b41cd5471547d7c3ee", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 27444, "end_char_idx": 29891, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/openai/api_requestor.py:516, in APIRequestor.request_raw(self, method, url, params, supplied_headers, files, stream, request_id, request_timeout)
        514     _thread_context.session = _make_session()
        515 try:
    --> 516     result = _thread_context.session.request(
        517         method,
        518         abs_url,
        519         headers=headers,
        520         data=data,
        521         files=files,
        522         stream=stream,
        523         timeout=request_timeout if request_timeout else TIMEOUT_SECS,
        524     )
        525 except requests.exceptions.Timeout as e:
        526     raise error.Timeout("Request timed out: {}".format(e)) from e


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/sessions.py:587, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
        582 send_kwargs = {
        583     "timeout": timeout,
        584     "allow_redirects": allow_redirects,
        585 }
        586 send_kwargs.update(settings)
    --> 587 resp = self.send(prep, **send_kwargs)
        589 return resp


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/sessions.py:701, in Session.send(self, request, **kwargs)
        698 start = preferred_clock()
        700 # Send the request
    --> 701 r = adapter.send(request, **kwargs)
        703 # Total elapsed time of the request (approximately)
        704 elapsed = preferred_clock() - start


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/adapters.py:489, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        487 try:
        488     if not chunked:
    --> 489         resp = conn.urlopen(
        490             method=request.method,
        491             url=url,
        492             body=request.body,
        493             headers=request.headers,
        494             redirect=False,
        495             assert_same_host=False,
        496             preload_content=False,
        497             decode_content=False,
        498             retries=self.max_retries,
        499             timeout=timeout,
        500         )
        502     # Send the request.
        503     else:
        504         if hasattr(conn, "proxy_pool"):


key: _node_content
string_value:
{"id_": "55134acd-138d-4c6f-b893-0498822947bd", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "61c289b4-e319-4e76-87f1-8934942fde10", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "c72099acd4fbff3c80de864e973afd01a386cc6a99db9e8f9a7b219cb3956291", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "dccd4c00-e49d-4ceb-991c-8851345a25a1", "node_type": "1", "metadata": {}, "hash": "4c33a55a6575ff5ded176eeb4cc3934a59d14d4f93540625e72e1f42e6c0a3b0", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 28703, "end_char_idx": 31628, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/sessions.py:701, in Session.send(self, request, **kwargs)
        698 start = preferred_clock()
        700 # Send the request
    --> 701 r = adapter.send(request, **kwargs)
        703 # Total elapsed time of the request (approximately)
        704 elapsed = preferred_clock() - start


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/requests/adapters.py:489, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        487 try:
        488     if not chunked:
    --> 489         resp = conn.urlopen(
        490             method=request.method,
        491             url=url,
        492             body=request.body,
        493             headers=request.headers,
        494             redirect=False,
        495             assert_same_host=False,
        496             preload_content=False,
        497             decode_content=False,
        498             retries=self.max_retries,
        499             timeout=timeout,
        500         )
        502     # Send the request.
        503     else:
        504         if hasattr(conn, "proxy_pool"):


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/urllib3/connectionpool.py:703, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
        700     self._prepare_proxy(conn)
        702 # Make the request on the httplib connection object.
    --> 703 httplib_response = self._make_request(
        704     conn,
        705     method,
        706     url,
        707     timeout=timeout_obj,
        708     body=body,
        709     headers=headers,
        710     chunked=chunked,
        711 )
        713 # If we're going to release the connection in ``finally:``, then
        714 # the response doesn't need to know about the connection. Otherwise
        715 # it will also try to release it and we'll have a double-release
        716 # mess.
        717 response_conn = conn if not release_conn else None


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/urllib3/connectionpool.py:449, in HTTPConnectionPool._make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
        444             httplib_response = conn.getresponse()
        445         except BaseException as e:
        446             # Remove the TypeError from the exception chain in
        447             # Python 3 (including for exceptions like SystemExit).
        448             # Otherwise it looks like a bug in the code.
    --> 449             six.raise_from(e, None)
        450 except (SocketTimeout, BaseSSLError, SocketError) as e:
        451     self._raise_timeout(err=e, url=url, timeout_value=read_timeout)


    File <string>:3, in raise_from(value, from_value)


key: _node_content
string_value:
{"id_": "dccd4c00-e49d-4ceb-991c-8851345a25a1", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "55134acd-138d-4c6f-b893-0498822947bd", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "1f4c83096ae27d2fb38ca9e6b6b49ecd418e42a02687c9730e1f4cc90f75da87", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "50bc573f-0b41-4ab3-a9c5-4d7e2c71e127", "node_type": "1", "metadata": {}, "hash": "5b9413599f83f9778509110fc9e2752a98b353bf9474ce9d1b1d859e87c7b56c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 30853, "end_char_idx": 33404, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/urllib3/connectionpool.py:449, in HTTPConnectionPool._make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
        444             httplib_response = conn.getresponse()
        445         except BaseException as e:
        446             # Remove the TypeError from the exception chain in
        447             # Python 3 (including for exceptions like SystemExit).
        448             # Otherwise it looks like a bug in the code.
    --> 449             six.raise_from(e, None)
        450 except (SocketTimeout, BaseSSLError, SocketError) as e:
        451     self._raise_timeout(err=e, url=url, timeout_value=read_timeout)


    File <string>:3, in raise_from(value, from_value)


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/urllib3/connectionpool.py:444, in HTTPConnectionPool._make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
        441 except TypeError:
        442     # Python 3
        443     try:
    --> 444         httplib_response = conn.getresponse()
        445     except BaseException as e:
        446         # Remove the TypeError from the exception chain in
        447         # Python 3 (including for exceptions like SystemExit).
        448         # Otherwise it looks like a bug in the code.
        449         six.raise_from(e, None)


    File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:1377, in HTTPConnection.getresponse(self)
       1375 try:
       1376     try:
    -> 1377         response.begin()
       1378     except ConnectionError:
       1379         self.close()


    File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:320, in HTTPResponse.begin(self)
        318 # read until we get a non-100 response
        319 while True:
    --> 320     version, status, reason = self._read_status()
        321     if status != CONTINUE:
        322         break


    File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:281, in HTTPResponse._read_status(self)
        280 def _read_status(self):
    --> 281     line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
        282     if len(line) > _MAXLINE:
        283         raise LineTooLong("status line")


    File ~/miniconda3/envs/haystack/lib/python3.9/socket.py:704, in SocketIO.readinto(self, b)
        702 while True:
        703     try:
    --> 704         return self._sock.recv_into(b)
        705     except timeout:
        706         self._timeout_occurred = True


key: _node_content
string_value:
{"id_": "50bc573f-0b41-4ab3-a9c5-4d7e2c71e127", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "dccd4c00-e49d-4ceb-991c-8851345a25a1", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "09ecd8f113b9c3da0654820b963de9fe292584089e20c3185e823a212d611da3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1e069162-f375-4612-bee2-f1460a457c60", "node_type": "1", "metadata": {}, "hash": "d4ca4518f0f25df700197279255ff974056451eb23c8b5bf14c92496b43590ee", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 32267, "end_char_idx": 35058, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:1377, in HTTPConnection.getresponse(self)
       1375 try:
       1376     try:
    -> 1377         response.begin()
       1378     except ConnectionError:
       1379         self.close()


    File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:320, in HTTPResponse.begin(self)
        318 # read until we get a non-100 response
        319 while True:
    --> 320     version, status, reason = self._read_status()
        321     if status != CONTINUE:
        322         break


    File ~/miniconda3/envs/haystack/lib/python3.9/http/client.py:281, in HTTPResponse._read_status(self)
        280 def _read_status(self):
    --> 281     line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
        282     if len(line) > _MAXLINE:
        283         raise LineTooLong("status line")


    File ~/miniconda3/envs/haystack/lib/python3.9/socket.py:704, in SocketIO.readinto(self, b)
        702 while True:
        703     try:
    --> 704         return self._sock.recv_into(b)
        705     except timeout:
        706         self._timeout_occurred = True


    File ~/miniconda3/envs/haystack/lib/python3.9/ssl.py:1242, in SSLSocket.recv_into(self, buffer, nbytes, flags)
       1238     if flags != 0:
       1239         raise ValueError(
       1240           "non-zero flags not allowed in calls to recv_into() on %s" %
       1241           self.__class__)
    -> 1242     return self.read(nbytes, buffer)
       1243 else:
       1244     return super().recv_into(buffer, nbytes, flags)


    File ~/miniconda3/envs/haystack/lib/python3.9/ssl.py:1100, in SSLSocket.read(self, len, buffer)
       1098 try:
       1099     if buffer is not None:
    -> 1100         return self._sslobj.read(len, buffer)
       1101     else:
       1102         return self._sslobj.read(len)


    KeyboardInterrupt: 


## Query the Database
With our data safely inserted in Milvus, we can now perform a query. The query takes in a string or a list of strings and searches them. The resuts print out your provided description and the results that include the result score, the result title, and the result book description. 


```python
import textwrap

def query(queries, top_k = 5):
    if type(queries) != list:
        queries = [queries]
    res = collection.search(embed(queries), anns_field='embedding', param=QUERY_PARAM, limit = top_k, output_fields=['title', 'description'])
    for i, hit in enumerate(res):
        print('Description:', queries[i])
        print('Results:')
        for ii, hits in enumerate(hit):
            print('\t' + 'Rank:', ii + 1, 'Score:', hits.score, 'Title:', hits.entity.get('title'))
            print(textwrap.fill(hits.entity.get('description'), 88))
            print()
```


key: _node_content
string_value:
{"id_": "1e069162-f375-4612-bee2-f1460a457c60", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "50bc573f-0b41-4ab3-a9c5-4d7e2c71e127", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "e159f8b326b97c6c38c1395c0ada681408ed3b49860afb7f9748df16c68a7912", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9851bc89-d34c-4d6a-95fa-e6357915a1cc", "node_type": "1", "metadata": {}, "hash": "298e4ad1cca7c341fbdc4d4a9ebbec3851271fe8f097e0be9daa102dd65c4d05", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 33849, "end_char_idx": 36671, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/ssl.py:1100, in SSLSocket.read(self, len, buffer)
       1098 try:
       1099     if buffer is not None:
    -> 1100         return self._sslobj.read(len, buffer)
       1101     else:
       1102         return self._sslobj.read(len)


    KeyboardInterrupt: 


## Query the Database
With our data safely inserted in Milvus, we can now perform a query. The query takes in a string or a list of strings and searches them. The resuts print out your provided description and the results that include the result score, the result title, and the result book description. 


```python
import textwrap

def query(queries, top_k = 5):
    if type(queries) != list:
        queries = [queries]
    res = collection.search(embed(queries), anns_field='embedding', param=QUERY_PARAM, limit = top_k, output_fields=['title', 'description'])
    for i, hit in enumerate(res):
        print('Description:', queries[i])
        print('Results:')
        for ii, hits in enumerate(hit):
            print('\t' + 'Rank:', ii + 1, 'Score:', hits.score, 'Title:', hits.entity.get('title'))
            print(textwrap.fill(hits.entity.get('description'), 88))
            print()
```


```python
query('Book about a k-9 from europe')
```

    RPC error: [search], <MilvusException: (code=1, message=code: UnexpectedError, reason: code: CollectionNotExists, reason: can't find collection: book_search)>, <Time:{'RPC start': '2023-03-17 14:22:18.368461', 'RPC error': '2023-03-17 14:22:18.382086'}>



    ---------------------------------------------------------------------------

    MilvusException                           Traceback (most recent call last)

    Cell In[32], line 1
    ----> 1 query('Book about a k-9 from europe')


    Cell In[31], line 6, in query(queries, top_k)
          4 if type(queries) != list:
          5     queries = [queries]
    ----> 6 res = collection.search(embed(queries), anns_field='embedding', param=QUERY_PARAM, limit = top_k, output_fields=['title', 'description'])
          7 for i, hit in enumerate(res):
          8     print('Description:', queries[i])


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/orm/collection.py:614, in Collection.search(self, data, anns_field, param, limit, expr, partition_names, output_fields, timeout, round_decimal, **kwargs)
        611     raise DataTypeNotMatchException(message=ExceptionsMessage.ExprType % type(expr))
        613 conn = self._get_connection()
    --> 614 res = conn.search(self._name, data, anns_field, param, limit, expr,
        615                   partition_names, output_fields, round_decimal, timeout=timeout,
        616                   schema=self._schema_dict, **kwargs)
        617 if kwargs.get("_async", False):
        618     return SearchFuture(res)


key: _node_content
string_value:
{"id_": "9851bc89-d34c-4d6a-95fa-e6357915a1cc", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1e069162-f375-4612-bee2-f1460a457c60", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "533b414558c74f563a3d04194b595215b1d930a9269312d4438d9721a4d0cc93", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2746bed8-13e2-4dbf-a8db-aeee27e70cc0", "node_type": "1", "metadata": {}, "hash": "d45753293a9531e227d0e23d0a52f666557917e516b676cdc371ddc8416ed92b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 35379, "end_char_idx": 38226, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
---------------------------------------------------------------------------

    MilvusException                           Traceback (most recent call last)

    Cell In[32], line 1
    ----> 1 query('Book about a k-9 from europe')


    Cell In[31], line 6, in query(queries, top_k)
          4 if type(queries) != list:
          5     queries = [queries]
    ----> 6 res = collection.search(embed(queries), anns_field='embedding', param=QUERY_PARAM, limit = top_k, output_fields=['title', 'description'])
          7 for i, hit in enumerate(res):
          8     print('Description:', queries[i])


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/orm/collection.py:614, in Collection.search(self, data, anns_field, param, limit, expr, partition_names, output_fields, timeout, round_decimal, **kwargs)
        611     raise DataTypeNotMatchException(message=ExceptionsMessage.ExprType % type(expr))
        613 conn = self._get_connection()
    --> 614 res = conn.search(self._name, data, anns_field, param, limit, expr,
        615                   partition_names, output_fields, round_decimal, timeout=timeout,
        616                   schema=self._schema_dict, **kwargs)
        617 if kwargs.get("_async", False):
        618     return SearchFuture(res)


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:109, in error_handler.<locals>.wrapper.<locals>.handler(*args, **kwargs)
        107     record_dict["RPC error"] = str(datetime.datetime.now())
        108     LOGGER.error(f"RPC error: [{inner_name}], {e}, <Time:{record_dict}>")
    --> 109     raise e
        110 except grpc.FutureTimeoutError as e:
        111     record_dict["gRPC timeout"] = str(datetime.datetime.now())


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:105, in error_handler.<locals>.wrapper.<locals>.handler(*args, **kwargs)
        103 try:
        104     record_dict["RPC start"] = str(datetime.datetime.now())
    --> 105     return func(*args, **kwargs)
        106 except MilvusException as e:
        107     record_dict["RPC error"] = str(datetime.datetime.now())


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:136, in tracing_request.<locals>.wrapper.<locals>.handler(self, *args, **kwargs)
        134 if req_id:
        135     self.set_onetime_request_id(req_id)
    --> 136 ret = func(self, *args, **kwargs)
        137 return ret


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:85, in retry_on_rpc_failure.<locals>.wrapper.<locals>.handler(self, *args, **kwargs)
         83         back_off = min(back_off * back_off_multiplier, max_back_off)
         84     else:
    ---> 85         raise e
         86 except Exception as e:
         87     raise e


key: _node_content
string_value:
{"id_": "2746bed8-13e2-4dbf-a8db-aeee27e70cc0", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "9851bc89-d34c-4d6a-95fa-e6357915a1cc", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "64b28ca6d8851b18c09707ff44bba3d2b96ce446a680cd7e0527565faf121f55", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7d930f49-3506-4558-a699-2d6be675b5cc", "node_type": "1", "metadata": {}, "hash": "133309850c8fdd5b5181d4e47f2d5e41ee9080f6d2a40e4b79926de739e57461", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 37146, "end_char_idx": 39952, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:105, in error_handler.<locals>.wrapper.<locals>.handler(*args, **kwargs)
        103 try:
        104     record_dict["RPC start"] = str(datetime.datetime.now())
    --> 105     return func(*args, **kwargs)
        106 except MilvusException as e:
        107     record_dict["RPC error"] = str(datetime.datetime.now())


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:136, in tracing_request.<locals>.wrapper.<locals>.handler(self, *args, **kwargs)
        134 if req_id:
        135     self.set_onetime_request_id(req_id)
    --> 136 ret = func(self, *args, **kwargs)
        137 return ret


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:85, in retry_on_rpc_failure.<locals>.wrapper.<locals>.handler(self, *args, **kwargs)
         83         back_off = min(back_off * back_off_multiplier, max_back_off)
         84     else:
    ---> 85         raise e
         86 except Exception as e:
         87     raise e


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/decorators.py:50, in retry_on_rpc_failure.<locals>.wrapper.<locals>.handler(self, *args, **kwargs)
         48 while True:
         49     try:
    ---> 50         return func(self, *args, **kwargs)
         51     except grpc.RpcError as e:
         52         # DEADLINE_EXCEEDED means that the task wat not completed
         53         # UNAVAILABLE means that the service is not reachable currently
         54         # Reference: https://grpc.github.io/grpc/python/grpc.html#grpc-status-code
         55         if e.code() != grpc.StatusCode.DEADLINE_EXCEEDED and e.code() != grpc.StatusCode.UNAVAILABLE:


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py:472, in GrpcHandler.search(self, collection_name, data, anns_field, param, limit, expression, partition_names, output_fields, round_decimal, timeout, schema, **kwargs)
        467 requests = Prepare.search_requests_with_expr(collection_name, data, anns_field, param, limit, schema,
        468                                              expression, partition_names, output_fields, round_decimal,
        469                                              **kwargs)
        471 auto_id = schema["auto_id"]
    --> 472 return self._execute_search_requests(requests, timeout, round_decimal=round_decimal, auto_id=auto_id, **kwargs)


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py:441, in GrpcHandler._execute_search_requests(self, requests, timeout, **kwargs)
        439 if kwargs.get("_async", False):
        440     return SearchFuture(None, None, True, pre_err)
    --> 441 raise pre_err


key: _node_content
string_value:
{"id_": "7d930f49-3506-4558-a699-2d6be675b5cc", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "db30bf2f-1538-4136-8fe8-d7960d6d9c35", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "08c6a77c234553e2b0977ce8ec8638a156b231ccbdf1ea631386a5119a9268d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2746bed8-13e2-4dbf-a8db-aeee27e70cc0", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Getting_started_with_Milvus_and_OpenAI.ipynb", "title": "Getting Started with Milvus and OpenAI", "tokens": 11649, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "067b55775ce39e9624222edb20fdb3e26584abb1ce5cc5a0fcc56213e6d21ad5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 38921, "end_char_idx": 40606, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py:472, in GrpcHandler.search(self, collection_name, data, anns_field, param, limit, expression, partition_names, output_fields, round_decimal, timeout, schema, **kwargs)
        467 requests = Prepare.search_requests_with_expr(collection_name, data, anns_field, param, limit, schema,
        468                                              expression, partition_names, output_fields, round_decimal,
        469                                              **kwargs)
        471 auto_id = schema["auto_id"]
    --> 472 return self._execute_search_requests(requests, timeout, round_decimal=round_decimal, auto_id=auto_id, **kwargs)


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py:441, in GrpcHandler._execute_search_requests(self, requests, timeout, **kwargs)
        439 if kwargs.get("_async", False):
        440     return SearchFuture(None, None, True, pre_err)
    --> 441 raise pre_err


    File ~/miniconda3/envs/haystack/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py:432, in GrpcHandler._execute_search_requests(self, requests, timeout, **kwargs)
        429     response = self._stub.Search(request, timeout=timeout)
        431     if response.status.error_code != 0:
    --> 432         raise MilvusException(response.status.error_code, response.status.reason)
        434     raws.append(response)
        435 round_decimal = kwargs.get("round_decimal", -1)


    MilvusException: <MilvusException: (code=1, message=code: UnexpectedError, reason: code: CollectionNotExists, reason: can't find collection: book_search)>


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb


key: _node_content
string_value:
{"id_": "b330031b-b857-4ac8-9781-c3c455ef3452", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3fc2301f-ff54-477e-b24f-938b647c4d11", "node_type": "1", "metadata": {}, "hash": "86516a7d5b93f7a6e9d67edcf6290f226b59183d833778e2ded412058337ffba", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2678, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Filtered Search with Milvus and OpenAI
### Finding your next movie

In this notebook we will be going over generating embeddings of movie descriptions with OpenAI and using those embeddings within Milvus to find relevant movies. To narrow our search results and try something new, we are going to be using filtering to do metadata searches. The dataset in this example is sourced from HuggingFace datasets, and contains a little over 8 thousand movie entries.

Lets begin by first downloading the required libraries for this notebook:
- `openai` is used for communicating with the OpenAI embedding service
- `pymilvus` is used for communicating with the Milvus server
- `datasets` is used for downloading the dataset
- `tqdm` is used for the progress bars



```python
! pip install openai pymilvus datasets tqdm
```

With the required packages installed we can get started. Lets begin by launching the Milvus service. The file being run is the `docker-compose.yaml` found in the folder of this file. This command launches a Milvus standalone instance which we will use for this test.  


```python
! docker compose up -d
```

    E0317 14:06:38.344884000 140704629352640 fork_posix.cc:76]             Other threads are currently calling into gRPC, skipping fork() handlers


    [1A[1B[0G[?25l[+] Running 1/0
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠋ Container milvus-etcd   Creating                                        0.0s
    [0m[37m ⠋ Container milvus-minio  Creating                                        0.0s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠙ Container milvus-etcd   Creating                                        0.1s
    [0m[37m ⠙ Container milvus-minio  Creating                                        0.1s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 2/3
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.2s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.2s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.3s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.


key: _node_content
string_value:
{"id_": "3fc2301f-ff54-477e-b24f-938b647c4d11", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b330031b-b857-4ac8-9781-c3c455ef3452", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "88860f81230f09977a640970b35c10ea662969cbcf964f7eeba9bf036c8b396c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "db47cecc-7496-498a-817f-cfcba2f6bb5f", "node_type": "1", "metadata": {}, "hash": "6ee8822d128cd4f0cc801be5c4b0c23187981e9cc50572db695d408933e7e6ec", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1495, "end_char_idx": 3776, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
0s
    [0m[37m ⠋ Container milvus-minio  Creating                                        0.0s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 1/3
    [34m ⠿ Network milvus          Created                                         0.1s
    [0m[37m ⠙ Container milvus-etcd   Creating                                        0.1s
    [0m[37m ⠙ Container milvus-minio  Creating                                        0.1s
    [0m[?25h[1A[1A[1A[1A[0G[?25l[+] Running 2/3
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.2s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.2s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.3s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.4s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.4s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.5s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.5s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.


key: _node_content
string_value:
{"id_": "db47cecc-7496-498a-817f-cfcba2f6bb5f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3fc2301f-ff54-477e-b24f-938b647c4d11", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "e55f29224e6ebacba563fe36eaaf9f7631af269ef3c6463a47cda997e7491bef", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "37b777f7-23d4-4172-a914-41ed88efc69e", "node_type": "1", "metadata": {}, "hash": "666e14a0e55239bb9929a0cf095e07f7bd8bd9021f598f3f27ea8b745eb8d2e8", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2585, "end_char_idx": 4911, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
3s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.3s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.4s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.4s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.5s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.5s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.6s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.6s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.7s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.8s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.8s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.


key: _node_content
string_value:
{"id_": "37b777f7-23d4-4172-a914-41ed88efc69e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "db47cecc-7496-498a-817f-cfcba2f6bb5f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "0bbca253c1dacc281888ea5ca2358144c33ee6842b9a82c0f6a0b2d4086c2fce", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "83007672-aa8c-4fde-aa3c-351ddcc8fd3a", "node_type": "1", "metadata": {}, "hash": "b8e9ac503c15690ba2dea275b76df8011e547e3f506bf86ee41d022e9af46c11", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3776, "end_char_idx": 6102, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.6s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.6s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.7s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.7s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.8s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.8s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.9s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.9s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.0s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.


key: _node_content
string_value:
{"id_": "83007672-aa8c-4fde-aa3c-351ddcc8fd3a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "37b777f7-23d4-4172-a914-41ed88efc69e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d8e77e96763068d63d101e90cd29428e2605862b481fa478df634938252540f8", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9df79ff3-c322-4724-8daa-fd28bb0ae50d", "node_type": "1", "metadata": {}, "hash": "190267ab6f073e9a34ea5fa89b907f83beddaa4bb4d6f9aca735d37d1c71e6a9", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4911, "end_char_idx": 7200, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 2/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[37m ⠿ Container milvus-etcd        Starting                                   0.9s
    [0m[37m ⠿ Container milvus-minio       Starting                                   0.9s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[37m ⠿ Container milvus-minio       Starting                                   1.0s
    [0m[34m ⠿ Container milvus-standalone  Created                                    0.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.0s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.


key: _node_content
string_value:
{"id_": "9df79ff3-c322-4724-8daa-fd28bb0ae50d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "83007672-aa8c-4fde-aa3c-351ddcc8fd3a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3dfe5d77b391cc79acf3bc35f90c2bcd27ec4ef0d046b9be4448154b747a636a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c7345ae5-1aba-42f7-bd7b-bb73bf82f6b6", "node_type": "1", "metadata": {}, "hash": "fdcdd1e85c5a400cad6b40a7ec9dc0d90b1289f13a2bc1894b38681ea73374a3", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6009, "end_char_idx": 8335, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.0s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.1s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.2s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.4s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.


key: _node_content
string_value:
{"id_": "c7345ae5-1aba-42f7-bd7b-bb73bf82f6b6", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "9df79ff3-c322-4724-8daa-fd28bb0ae50d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8ab9d5bc3af04eca8fbc3e80066bcb6a3e6b3bda074d91eca8e5b620e952c3bb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0f27d6fd-1e9d-4be9-9996-20e32e40ec25", "node_type": "1", "metadata": {}, "hash": "7ac28e738edfe6461edc29d6ce5a536fd152346765ef84965227172bb262b600", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7200, "end_char_idx": 9650, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.3s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.4s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[+] Running 3/4
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[37m ⠿ Container milvus-standalone  Starting                                   1.5s
    [0m[?25h[1A[1A[1A[1A[1A[0G[?25l[34m[+] Running 4/4[0m
    [34m ⠿ Network milvus               Created                                    0.1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[34m ⠿ Container milvus-standalone  Started                                    1.6s
    [0m[?25h

With Milvus running we can setup our global variables:
- HOST: The Milvus host address
- PORT: The Milvus port number
- COLLECTION_NAME: What to name the collection within Milvus
- DIMENSION: The dimension of the embeddings
- OPENAI_ENGINE: Which embedding model to use
- openai.api_key: Your OpenAI account key
- INDEX_PARAM: The index settings to use for the collection
- QUERY_PARAM: The search parameters to use
- BATCH_SIZE: How many movies to embed and insert at once


```python
import openai

HOST = 'localhost'
PORT = 19530
COLLECTION_NAME = 'movie_search'
DIMENSION = 1536
OPENAI_ENGINE = 'text-embedding-3-small'
openai.api_key = 'sk-your_key'

INDEX_PARAM = {
    'metric_type':'L2',
    'index_type':"HNSW",
    'params':{'M': 8, 'efConstruction': 64}
}

QUERY_PARAM = {
    "metric_type": "L2",
    "params": {"ef": 64},
}

BATCH_SIZE = 1000
```


key: _node_content
string_value:
{"id_": "0f27d6fd-1e9d-4be9-9996-20e32e40ec25", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c7345ae5-1aba-42f7-bd7b-bb73bf82f6b6", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f5e049390835c4a144a26d597a79b0fafe367028f950129e56496f4d85cdd9b7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "53f540d7-4ad6-4eff-b58f-0ab435d96528", "node_type": "1", "metadata": {}, "hash": "a02b78b0677e812ae3ac35b8fe93c96739b309b54ff95eb10161b74fb3d74ab2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8493, "end_char_idx": 11269, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1s
    [0m[34m ⠿ Container milvus-etcd        Started                                    0.9s
    [0m[34m ⠿ Container milvus-minio       Started                                    1.0s
    [0m[34m ⠿ Container milvus-standalone  Started                                    1.6s
    [0m[?25h

With Milvus running we can setup our global variables:
- HOST: The Milvus host address
- PORT: The Milvus port number
- COLLECTION_NAME: What to name the collection within Milvus
- DIMENSION: The dimension of the embeddings
- OPENAI_ENGINE: Which embedding model to use
- openai.api_key: Your OpenAI account key
- INDEX_PARAM: The index settings to use for the collection
- QUERY_PARAM: The search parameters to use
- BATCH_SIZE: How many movies to embed and insert at once


```python
import openai

HOST = 'localhost'
PORT = 19530
COLLECTION_NAME = 'movie_search'
DIMENSION = 1536
OPENAI_ENGINE = 'text-embedding-3-small'
openai.api_key = 'sk-your_key'

INDEX_PARAM = {
    'metric_type':'L2',
    'index_type':"HNSW",
    'params':{'M': 8, 'efConstruction': 64}
}

QUERY_PARAM = {
    "metric_type": "L2",
    "params": {"ef": 64},
}

BATCH_SIZE = 1000
```


```python
from pymilvus import connections, utility, FieldSchema, Collection, CollectionSchema, DataType

# Connect to Milvus Database
connections.connect(host=HOST, port=PORT)
```


```python
# Remove collection if it already exists
if utility.has_collection(COLLECTION_NAME):
    utility.drop_collection(COLLECTION_NAME)
```


```python
# Create collection which includes the id, title, and embedding.
fields = [
    FieldSchema(name='id', dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name='title', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='type', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='release_year', dtype=DataType.INT64),
    FieldSchema(name='rating', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='description', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, dim=DIMENSION)
]
schema = CollectionSchema(fields=fields)
collection = Collection(name=COLLECTION_NAME, schema=schema)
```


```python
# Create the index on the collection and load it.
collection.create_index(field_name="embedding", index_params=INDEX_PARAM)
collection.load()
```

## Dataset
With Milvus up and running we can begin grabbing our data. Hugging Face Datasets is a hub that holds many different user datasets, and for this example we are using HuggingLearners's netflix-shows dataset. This dataset contains movies and their metadata pairs for over 8 thousand movies. We are going to embed each description and store it within Milvus along with its title, type, release_year and rating.


key: _node_content
string_value:
{"id_": "53f540d7-4ad6-4eff-b58f-0ab435d96528", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0f27d6fd-1e9d-4be9-9996-20e32e40ec25", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "779ef2c79d3c521ff9188d3e77245fa62c2bf5d3bf41eec9c8571aa9e98fbdae", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b9fb7a6d-7d8b-4bc3-a4a9-572a88a9a36d", "node_type": "1", "metadata": {}, "hash": "5fe12854d2246871beed11de7581b95fabcd2c042a86d6c481ebf9d15adb9815", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9653, "end_char_idx": 12290, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
from pymilvus import connections, utility, FieldSchema, Collection, CollectionSchema, DataType

# Connect to Milvus Database
connections.connect(host=HOST, port=PORT)
```


```python
# Remove collection if it already exists
if utility.has_collection(COLLECTION_NAME):
    utility.drop_collection(COLLECTION_NAME)
```


```python
# Create collection which includes the id, title, and embedding.
fields = [
    FieldSchema(name='id', dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name='title', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='type', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='release_year', dtype=DataType.INT64),
    FieldSchema(name='rating', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='description', dtype=DataType.VARCHAR, max_length=64000),
    FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, dim=DIMENSION)
]
schema = CollectionSchema(fields=fields)
collection = Collection(name=COLLECTION_NAME, schema=schema)
```


```python
# Create the index on the collection and load it.
collection.create_index(field_name="embedding", index_params=INDEX_PARAM)
collection.load()
```

## Dataset
With Milvus up and running we can begin grabbing our data. Hugging Face Datasets is a hub that holds many different user datasets, and for this example we are using HuggingLearners's netflix-shows dataset. This dataset contains movies and their metadata pairs for over 8 thousand movies. We are going to embed each description and store it within Milvus along with its title, type, release_year and rating.


```python
import datasets

# Download the dataset 
dataset = datasets.load_dataset('hugginglearners/netflix-shows', split='train')
```

    Found cached dataset csv (/Users/filiphaltmayer/.cache/huggingface/datasets/hugginglearners___csv/hugginglearners--netflix-shows-03475319fc65a05a/0.0.0/6b34fb8fcf56f7c8ba51dc895bfa2bfbe43546f190a60fcf74bb5e8afdcc2317)


## Insert the Data
Now that we have our data on our machine we can begin embedding it and inserting it into Milvus. The embedding function takes in text and returns the embeddings in a list format. 


```python
# Simple function that converts the texts to embeddings
def embed(texts):
    embeddings = openai.Embedding.create(
        input=texts,
        engine=OPENAI_ENGINE
    )
    return [x['embedding'] for x in embeddings['data']]

```

This next step does the actual inserting. We iterate through all the entries and create batches that we insert once we hit our set batch size. After the loop is over we insert the last remaning batch if it exists.


key: _node_content
string_value:
{"id_": "b9fb7a6d-7d8b-4bc3-a4a9-572a88a9a36d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "53f540d7-4ad6-4eff-b58f-0ab435d96528", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "549f3f14f7aa114435e7d69953d58a951c523588f09605aefdcff8bee89533fb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e0175a22-ab0c-4a96-b637-2895eb66cc0a", "node_type": "1", "metadata": {}, "hash": "23ef84f74f416ff28903e29cfd7209278c0bfef0ecb7cfd45011253b66cae88f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10695, "end_char_idx": 13550, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# Create the index on the collection and load it.
collection.create_index(field_name="embedding", index_params=INDEX_PARAM)
collection.load()
```

## Dataset
With Milvus up and running we can begin grabbing our data. Hugging Face Datasets is a hub that holds many different user datasets, and for this example we are using HuggingLearners's netflix-shows dataset. This dataset contains movies and their metadata pairs for over 8 thousand movies. We are going to embed each description and store it within Milvus along with its title, type, release_year and rating.


```python
import datasets

# Download the dataset 
dataset = datasets.load_dataset('hugginglearners/netflix-shows', split='train')
```

    Found cached dataset csv (/Users/filiphaltmayer/.cache/huggingface/datasets/hugginglearners___csv/hugginglearners--netflix-shows-03475319fc65a05a/0.0.0/6b34fb8fcf56f7c8ba51dc895bfa2bfbe43546f190a60fcf74bb5e8afdcc2317)


## Insert the Data
Now that we have our data on our machine we can begin embedding it and inserting it into Milvus. The embedding function takes in text and returns the embeddings in a list format. 


```python
# Simple function that converts the texts to embeddings
def embed(texts):
    embeddings = openai.Embedding.create(
        input=texts,
        engine=OPENAI_ENGINE
    )
    return [x['embedding'] for x in embeddings['data']]

```

This next step does the actual inserting. We iterate through all the entries and create batches that we insert once we hit our set batch size. After the loop is over we insert the last remaning batch if it exists. 


```python
from tqdm import tqdm

data = [
    [], # title
    [], # type
    [], # release_year
    [], # rating
    [], # description
]

# Embed and insert in batches
for i in tqdm(range(0, len(dataset))):
    data[0].append(dataset[i]['title'] or '')
    data[1].append(dataset[i]['type'] or '')
    data[2].append(dataset[i]['release_year'] or -1)
    data[3].append(dataset[i]['rating'] or '')
    data[4].append(dataset[i]['description'] or '')
    if len(data[0]) % BATCH_SIZE == 0:
        data.append(embed(data[4]))
        collection.insert(data)
        data = [[],[],[],[],[]]

# Embed and insert the remainder 
if len(data[0]) != 0:
    data.append(embed(data[4]))
    collection.insert(data)
    data = [[],[],[],[],[]]

```

    100%|██████████| 8807/8807 [00:31<00:00, 276.82it/s]


## Query the Database
With our data safely inserted in Milvus, we can now perform a query. The query takes in a tuple of the movie description you are searching for an the filter to use. More info about the filter can be found [here](https://milvus.io/docs/boolean.md). The search first prints out your description and filter expression. After that for each result we print the score, title, type, release year, rating, and description of the result movies.


key: _node_content
string_value:
{"id_": "e0175a22-ab0c-4a96-b637-2895eb66cc0a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "89d3f627-eb9a-4e63-9e04-6a3eb4ce54c1", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b1fa81ddbfee65f19b82c729d479c7525a55e4d249536b13a4536660d026db49", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b9fb7a6d-7d8b-4bc3-a4a9-572a88a9a36d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/milvus/Filtered_search_with_Milvus_and_OpenAI.ipynb", "title": "Filtered Search with Milvus and OpenAI", "tokens": 4573, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "588d152d2157aa18f6c28bd6b13dcf59e63852d58ebfc48becdfbdb0a3906e0d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 12294, "end_char_idx": 15877, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
from tqdm import tqdm

data = [
    [], # title
    [], # type
    [], # release_year
    [], # rating
    [], # description
]

# Embed and insert in batches
for i in tqdm(range(0, len(dataset))):
    data[0].append(dataset[i]['title'] or '')
    data[1].append(dataset[i]['type'] or '')
    data[2].append(dataset[i]['release_year'] or -1)
    data[3].append(dataset[i]['rating'] or '')
    data[4].append(dataset[i]['description'] or '')
    if len(data[0]) % BATCH_SIZE == 0:
        data.append(embed(data[4]))
        collection.insert(data)
        data = [[],[],[],[],[]]

# Embed and insert the remainder 
if len(data[0]) != 0:
    data.append(embed(data[4]))
    collection.insert(data)
    data = [[],[],[],[],[]]

```

    100%|██████████| 8807/8807 [00:31<00:00, 276.82it/s]


## Query the Database
With our data safely inserted in Milvus, we can now perform a query. The query takes in a tuple of the movie description you are searching for an the filter to use. More info about the filter can be found [here](https://milvus.io/docs/boolean.md). The search first prints out your description and filter expression. After that for each result we print the score, title, type, release year, rating, and description of the result movies. 


```python
import textwrap

def query(query, top_k = 5):
    text, expr = query
    res = collection.search(embed(text), anns_field='embedding', expr = expr, param=QUERY_PARAM, limit = top_k, output_fields=['title', 'type', 'release_year', 'rating', 'description'])
    for i, hit in enumerate(res):
        print('Description:', text, 'Expression:', expr)
        print('Results:')
        for ii, hits in enumerate(hit):
            print('\t' + 'Rank:', ii + 1, 'Score:', hits.score, 'Title:', hits.entity.get('title'))
            print('\t\t' + 'Type:', hits.entity.get('type'), 'Release Year:', hits.entity.get('release_year'), 'Rating:', hits.entity.get('rating'))
            print(textwrap.fill(hits.entity.get('description'), 88))
            print()

my_query = ('movie about a fluffly animal', 'release_year < 2019 and rating like \"PG%\"')

query(my_query)
```

    Description: movie about a fluffly animal Expression: release_year < 2019 and rating like "PG%"
    Results:
    	Rank: 1 Score: 0.30083978176116943 Title: The Lamb
    		Type: Movie Release Year: 2017 Rating: PG
    A big-dreaming donkey escapes his menial existence and befriends some free-spirited
    animal pals in this imaginative retelling of the Nativity Story.
    
    	Rank: 2 Score: 0.33528298139572144 Title: Puss in Boots
    		Type: Movie Release Year: 2011 Rating: PG
    The fabled feline heads to the Land of Giants with friends Humpty Dumpty and Kitty
    Softpaws on a quest to nab its greatest treasure: the Golden Goose.
    
    	Rank: 3 Score: 0.33528298139572144 Title: Puss in Boots
    		Type: Movie Release Year: 2011 Rating: PG
    The fabled feline heads to the Land of Giants with friends Humpty Dumpty and Kitty
    Softpaws on a quest to nab its greatest treasure: the Golden Goose.
    
    	Rank: 4 Score: 0.3414868116378784 Title: Show Dogs
    		Type: Movie Release Year: 2018 Rating: PG
    A rough and tough police dog must go undercover with an FBI agent as a prim and proper
    pet at a dog show to save a baby panda from an illegal sale.
    
    	Rank: 5 Score: 0.3414868116378784 Title: Show Dogs
    		Type: Movie Release Year: 2018 Rating: PG
    A rough and tough police dog must go undercover with an FBI agent as a prim and proper
    pet at a dog show to save a baby panda from an illegal sale.


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb


key: _node_content
string_value:
{"id_": "103a80b2-360f-4f82-988f-9cbff04c8cf8", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "35fd7f14-cc3d-4792-8e9a-de8d221e96ac", "node_type": "1", "metadata": {}, "hash": "e0fc29ee1885346a43a6a917cf0c0b00f9f7df9a4569475e34de187648c9fc37", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3242, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Using AnalyticDB as a vector database for OpenAI embeddings

This notebook guides you step by step on using AnalyticDB as a vector database for OpenAI embeddings.

This notebook presents an end-to-end process of:
1. Using precomputed embeddings created by OpenAI API.
2. Storing the embeddings in a cloud instance of AnalyticDB.
3. Converting raw text query to an embedding with OpenAI API.
4. Using AnalyticDB to perform the nearest neighbour search in the created collection.

### What is AnalyticDB

[AnalyticDB](https://www.alibabacloud.com/help/en/analyticdb-for-postgresql/latest/product-introduction-overview) is a high-performance distributed vector database. Fully compatible with PostgreSQL syntax, you can effortlessly utilize it. AnalyticDB is Alibaba Cloud managed cloud-native database with strong-performed vector compute engine. Absolute out-of-box experience allow to scale into billions of data vectors processing with rich features including indexing algorithms, structured & non-structured data features, realtime update, distance metrics, scalar filtering, time travel searches etc. Also equipped with full OLAP database functionality and SLA commitment for production usage promise;

### Deployment options

- Using [AnalyticDB Cloud Vector Database](https://www.alibabacloud.com/help/zh/analyticdb-for-postgresql/latest/overview-2). [Click here](https://www.alibabacloud.com/product/hybriddb-postgresql) to fast deploy it.


## Prerequisites

For the purposes of this exercise we need to prepare a couple of things:

1. AnalyticDB cloud server instance.
2. The 'psycopg2' library to interact with the vector database. Any other postgresql client library is ok.
3. An [OpenAI API key](https://beta.openai.com/account/api-keys).



We might validate if the server was launched successfully by running a simple curl command:


### Install requirements

This notebook obviously requires the `openai` and `psycopg2` packages, but there are also some other additional libraries we will use. The following command installs them all:



```python
! pip install openai psycopg2 pandas wget
```

### Prepare your OpenAI API key

The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://beta.openai.com/account/api-keys](https://beta.openai.com/account/api-keys).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY`.


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


## Connect to AnalyticDB
First add it to your environment variables. or you can just change the "psycopg2.connect" parameters below

Connecting to a running instance of AnalyticDB server is easy with the official Python library:


key: _node_content
string_value:
{"id_": "35fd7f14-cc3d-4792-8e9a-de8d221e96ac", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "103a80b2-360f-4f82-988f-9cbff04c8cf8", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1c9ea2a49d50eb3868f2b090fc3a9f46c1341732ca7fff60619de469b862b08a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "243ec19e-f4b4-4165-b1ce-2dc8d87a3f18", "node_type": "1", "metadata": {}, "hash": "3570b5a49d48e59a70f6615599e5691f6ce91bf0c05055c5336890b4508759a5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1755, "end_char_idx": 4986, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
We might validate if the server was launched successfully by running a simple curl command:


### Install requirements

This notebook obviously requires the `openai` and `psycopg2` packages, but there are also some other additional libraries we will use. The following command installs them all:



```python
! pip install openai psycopg2 pandas wget
```

### Prepare your OpenAI API key

The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://beta.openai.com/account/api-keys](https://beta.openai.com/account/api-keys).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY`.


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


## Connect to AnalyticDB
First add it to your environment variables. or you can just change the "psycopg2.connect" parameters below

Connecting to a running instance of AnalyticDB server is easy with the official Python library:


```python
import os
import psycopg2

# Note. alternatively you can set a temporary env variable like this:
# os.environ["PGHOST"] = "your_host"
# os.environ["PGPORT"] "5432"),
# os.environ["PGDATABASE"] "postgres"),
# os.environ["PGUSER"] "user"),
# os.environ["PGPASSWORD"] "password"),

connection = psycopg2.connect(
    host=os.environ.get("PGHOST", "localhost"),
    port=os.environ.get("PGPORT", "5432"),
    database=os.environ.get("PGDATABASE", "postgres"),
    user=os.environ.get("PGUSER", "user"),
    password=os.environ.get("PGPASSWORD", "password")
)

# Create a new cursor object
cursor = connection.cursor()
```

We can test the connection by running any available method:


```python

# Execute a simple query to test the connection
cursor.execute("SELECT 1;")
result = cursor.fetchone()

# Check the query result
if result == (1,):
    print("Connection successful!")
else:
    print("Connection failed.")
```

    Connection successful!



```python
import wget

embeddings_url = "https://cdn.openai.com/API/examples/data/vector_database_wikipedia_articles_embedded.zip"

# The file is ~700 MB so this will take some time
wget.download(embeddings_url)
```

    100% [......................................................................] 698933052 / 698933052




    'vector_database_wikipedia_articles_embedded.zip'



The downloaded file has to be then extracted:


```python
import zipfile
import os
import re
import tempfile

current_directory = os.getcwd()
zip_file_path = os.path.join(current_directory, "vector_database_wikipedia_articles_embedded.zip")
output_directory = os.path.join(current_directory, "../../data")

with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
    zip_ref.extractall(output_directory)


key: _node_content
string_value:
{"id_": "243ec19e-f4b4-4165-b1ce-2dc8d87a3f18", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "35fd7f14-cc3d-4792-8e9a-de8d221e96ac", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "867eddc618c2249b0c2d5f21d65708ca32b6b662cd8652fe30e651174d0546d3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9f6c779b-11aa-46f7-8402-de8208abcc79", "node_type": "1", "metadata": {}, "hash": "3e7d0d8e04992c4faaf2983ec85155b21b9c5c22e831931e647af48cfbd79534", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3245, "end_char_idx": 5855, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import os
import psycopg2

# Note. alternatively you can set a temporary env variable like this:
# os.environ["PGHOST"] = "your_host"
# os.environ["PGPORT"] "5432"),
# os.environ["PGDATABASE"] "postgres"),
# os.environ["PGUSER"] "user"),
# os.environ["PGPASSWORD"] "password"),

connection = psycopg2.connect(
    host=os.environ.get("PGHOST", "localhost"),
    port=os.environ.get("PGPORT", "5432"),
    database=os.environ.get("PGDATABASE", "postgres"),
    user=os.environ.get("PGUSER", "user"),
    password=os.environ.get("PGPASSWORD", "password")
)

# Create a new cursor object
cursor = connection.cursor()
```

We can test the connection by running any available method:


```python

# Execute a simple query to test the connection
cursor.execute("SELECT 1;")
result = cursor.fetchone()

# Check the query result
if result == (1,):
    print("Connection successful!")
else:
    print("Connection failed.")
```

    Connection successful!



```python
import wget

embeddings_url = "https://cdn.openai.com/API/examples/data/vector_database_wikipedia_articles_embedded.zip"

# The file is ~700 MB so this will take some time
wget.download(embeddings_url)
```

    100% [......................................................................] 698933052 / 698933052




    'vector_database_wikipedia_articles_embedded.zip'



The downloaded file has to be then extracted:


```python
import zipfile
import os
import re
import tempfile

current_directory = os.getcwd()
zip_file_path = os.path.join(current_directory, "vector_database_wikipedia_articles_embedded.zip")
output_directory = os.path.join(current_directory, "../../data")

with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
    zip_ref.extractall(output_directory)


# check the csv file exist
file_name = "vector_database_wikipedia_articles_embedded.csv"
data_directory = os.path.join(current_directory, "../../data")
file_path = os.path.join(data_directory, file_name)


if os.path.exists(file_path):
    print(f"The file {file_name} exists in the data directory.")
else:
    print(f"The file {file_name} does not exist in the data directory.")

```

    The file vector_database_wikipedia_articles_embedded.csv exists in the data directory.


## Index data

AnalyticDB stores data in __relation__ where each object is described by at least one vector. Our relation will be called **articles** and each object will be described by both **title** and **content** vectors. \

We will start with creating a relation and create a vector index on both **title** and **content**, and then we will fill it with our precomputed embeddings.


key: _node_content
string_value:
{"id_": "9f6c779b-11aa-46f7-8402-de8208abcc79", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "243ec19e-f4b4-4165-b1ce-2dc8d87a3f18", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "bef73a651edee0cbaa52454ce2d6913aa735fc07ecd30e9883dd591b64e89f81", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3a67d1e9-aa52-480a-8e33-8424c0f926ce", "node_type": "1", "metadata": {}, "hash": "4f7cf2e1128bcc8126b3749e93e7c6695b7934ba520280c8022905333f3dc75a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4204, "end_char_idx": 6855, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import wget

embeddings_url = "https://cdn.openai.com/API/examples/data/vector_database_wikipedia_articles_embedded.zip"

# The file is ~700 MB so this will take some time
wget.download(embeddings_url)
```

    100% [......................................................................] 698933052 / 698933052




    'vector_database_wikipedia_articles_embedded.zip'



The downloaded file has to be then extracted:


```python
import zipfile
import os
import re
import tempfile

current_directory = os.getcwd()
zip_file_path = os.path.join(current_directory, "vector_database_wikipedia_articles_embedded.zip")
output_directory = os.path.join(current_directory, "../../data")

with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
    zip_ref.extractall(output_directory)


# check the csv file exist
file_name = "vector_database_wikipedia_articles_embedded.csv"
data_directory = os.path.join(current_directory, "../../data")
file_path = os.path.join(data_directory, file_name)


if os.path.exists(file_path):
    print(f"The file {file_name} exists in the data directory.")
else:
    print(f"The file {file_name} does not exist in the data directory.")

```

    The file vector_database_wikipedia_articles_embedded.csv exists in the data directory.


## Index data

AnalyticDB stores data in __relation__ where each object is described by at least one vector. Our relation will be called **articles** and each object will be described by both **title** and **content** vectors. \

We will start with creating a relation and create a vector index on both **title** and **content**, and then we will fill it with our precomputed embeddings.


```python
create_table_sql = '''
CREATE TABLE IF NOT EXISTS public.articles (
    id INTEGER NOT NULL,
    url TEXT,
    title TEXT,
    content TEXT,
    title_vector REAL[],
    content_vector REAL[],
    vector_id INTEGER
);

ALTER TABLE public.articles ADD PRIMARY KEY (id);
'''

# SQL statement for creating indexes
create_indexes_sql = '''
CREATE INDEX ON public.articles USING ann (content_vector) WITH (distancemeasure = l2, dim = '1536', pq_segments = '64', hnsw_m = '100', pq_centers = '2048');

CREATE INDEX ON public.articles USING ann (title_vector) WITH (distancemeasure = l2, dim = '1536', pq_segments = '64', hnsw_m = '100', pq_centers = '2048');
'''

# Execute the SQL statements
cursor.execute(create_table_sql)
cursor.execute(create_indexes_sql)

# Commit the changes
connection.commit()
```

## Load data

In this section we are going to load the data prepared previous to this session, so you don't have to recompute the embeddings of Wikipedia articles with your own credits.


key: _node_content
string_value:
{"id_": "3a67d1e9-aa52-480a-8e33-8424c0f926ce", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "9f6c779b-11aa-46f7-8402-de8208abcc79", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "dcdc5b4e8ebc30adf5f76a67448f62a878cb86a0755c49df215b6fd13a68f85f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2132e803-04c0-4248-abe5-8f19b1d8decb", "node_type": "1", "metadata": {}, "hash": "380abebc31013c6f9ccb12dbb7852bfee634f461c2e211ca9b03e43e3db116ea", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5195, "end_char_idx": 8384, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
if os.path.exists(file_path):
    print(f"The file {file_name} exists in the data directory.")
else:
    print(f"The file {file_name} does not exist in the data directory.")

```

    The file vector_database_wikipedia_articles_embedded.csv exists in the data directory.


## Index data

AnalyticDB stores data in __relation__ where each object is described by at least one vector. Our relation will be called **articles** and each object will be described by both **title** and **content** vectors. \

We will start with creating a relation and create a vector index on both **title** and **content**, and then we will fill it with our precomputed embeddings.


```python
create_table_sql = '''
CREATE TABLE IF NOT EXISTS public.articles (
    id INTEGER NOT NULL,
    url TEXT,
    title TEXT,
    content TEXT,
    title_vector REAL[],
    content_vector REAL[],
    vector_id INTEGER
);

ALTER TABLE public.articles ADD PRIMARY KEY (id);
'''

# SQL statement for creating indexes
create_indexes_sql = '''
CREATE INDEX ON public.articles USING ann (content_vector) WITH (distancemeasure = l2, dim = '1536', pq_segments = '64', hnsw_m = '100', pq_centers = '2048');

CREATE INDEX ON public.articles USING ann (title_vector) WITH (distancemeasure = l2, dim = '1536', pq_segments = '64', hnsw_m = '100', pq_centers = '2048');
'''

# Execute the SQL statements
cursor.execute(create_table_sql)
cursor.execute(create_indexes_sql)

# Commit the changes
connection.commit()
```

## Load data

In this section we are going to load the data prepared previous to this session, so you don't have to recompute the embeddings of Wikipedia articles with your own credits.


```python
import io

# Path to your local CSV file
csv_file_path = '../../data/vector_database_wikipedia_articles_embedded.csv'

# Define a generator function to process the file line by line
def process_file(file_path):
    with open(file_path, 'r') as file:
        for line in file:
            # Replace '[' with '{' and ']' with '}'
            modified_line = line.replace('[', '{').replace(']', '}')
            yield modified_line

# Create a StringIO object to store the modified lines
modified_lines = io.StringIO(''.join(list(process_file(csv_file_path))))

# Create the COPY command for the copy_expert method
copy_command = '''
COPY public.articles (id, url, title, content, title_vector, content_vector, vector_id)
FROM STDIN WITH (FORMAT CSV, HEADER true, DELIMITER ',');
'''

# Execute the COPY command using the copy_expert method
cursor.copy_expert(copy_command, modified_lines)

# Commit the changes
connection.commit()
```


```python
# Check the collection size to make sure all the points have been stored
count_sql = """select count(*) from public.articles;"""
cursor.execute(count_sql)
result = cursor.fetchone()
print(f"Count:{result[0]}")

```

    Count:25000


## Search data

Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter `vector_name` to switch from title to content based search. Since the precomputed embeddings were created with `text-embedding-3-small` OpenAI model we also have to use it during search.


key: _node_content
string_value:
{"id_": "2132e803-04c0-4248-abe5-8f19b1d8decb", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3a67d1e9-aa52-480a-8e33-8424c0f926ce", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b59250c6fc6801120e4a34b6473a5197ce00f8bb35f98e106f921231e7175d5e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "44a6b8aa-379c-4db1-9ca6-f31d8f580f23", "node_type": "1", "metadata": {}, "hash": "d0a8ce63266db8f9fc793f11e091932a91b1a815c450471c0aedc04a8d1e4806", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6858, "end_char_idx": 9171, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import io

# Path to your local CSV file
csv_file_path = '../../data/vector_database_wikipedia_articles_embedded.csv'

# Define a generator function to process the file line by line
def process_file(file_path):
    with open(file_path, 'r') as file:
        for line in file:
            # Replace '[' with '{' and ']' with '}'
            modified_line = line.replace('[', '{').replace(']', '}')
            yield modified_line

# Create a StringIO object to store the modified lines
modified_lines = io.StringIO(''.join(list(process_file(csv_file_path))))

# Create the COPY command for the copy_expert method
copy_command = '''
COPY public.articles (id, url, title, content, title_vector, content_vector, vector_id)
FROM STDIN WITH (FORMAT CSV, HEADER true, DELIMITER ',');
'''

# Execute the COPY command using the copy_expert method
cursor.copy_expert(copy_command, modified_lines)

# Commit the changes
connection.commit()
```


```python
# Check the collection size to make sure all the points have been stored
count_sql = """select count(*) from public.articles;"""
cursor.execute(count_sql)
result = cursor.fetchone()
print(f"Count:{result[0]}")

```

    Count:25000


## Search data

Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter `vector_name` to switch from title to content based search. Since the precomputed embeddings were created with `text-embedding-3-small` OpenAI model we also have to use it during search.



```python
def query_analyticdb(query, collection_name, vector_name="title_vector", top_k=20):

    # Creates embedding vector from user query
    embedded_query = openai.Embedding.create(
        input=query,
        model="text-embedding-3-small",
    )["data"][0]["embedding"]

    # Convert the embedded_query to PostgreSQL compatible format
    embedded_query_pg = "{" + ",".join(map(str, embedded_query)) + "}"

    # Create SQL query
    query_sql = f"""
    SELECT id, url, title, l2_distance({vector_name},'{embedded_query_pg}'::real[]) AS similarity
    FROM {collection_name}
    ORDER BY {vector_name} <-> '{embedded_query_pg}'::real[]
    LIMIT {top_k};
    """
    # Execute the query
    cursor.execute(query_sql)
    results = cursor.fetchall()

    return results
```


key: _node_content
string_value:
{"id_": "44a6b8aa-379c-4db1-9ca6-f31d8f580f23", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2132e803-04c0-4248-abe5-8f19b1d8decb", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "5cd93a053b4c02b41ae81df608517224ff88ebce86fa3807356a92fcbc774769", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "40cdd5f3-9740-4686-9e95-c405cb19d627", "node_type": "1", "metadata": {}, "hash": "6ce52553ba68fd60408533b222c6b341632b7e2e06b15225e5616ccedf8b775a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7803, "end_char_idx": 10087, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# Check the collection size to make sure all the points have been stored
count_sql = """select count(*) from public.articles;"""
cursor.execute(count_sql)
result = cursor.fetchone()
print(f"Count:{result[0]}")

```

    Count:25000


## Search data

Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter `vector_name` to switch from title to content based search. Since the precomputed embeddings were created with `text-embedding-3-small` OpenAI model we also have to use it during search.



```python
def query_analyticdb(query, collection_name, vector_name="title_vector", top_k=20):

    # Creates embedding vector from user query
    embedded_query = openai.Embedding.create(
        input=query,
        model="text-embedding-3-small",
    )["data"][0]["embedding"]

    # Convert the embedded_query to PostgreSQL compatible format
    embedded_query_pg = "{" + ",".join(map(str, embedded_query)) + "}"

    # Create SQL query
    query_sql = f"""
    SELECT id, url, title, l2_distance({vector_name},'{embedded_query_pg}'::real[]) AS similarity
    FROM {collection_name}
    ORDER BY {vector_name} <-> '{embedded_query_pg}'::real[]
    LIMIT {top_k};
    """
    # Execute the query
    cursor.execute(query_sql)
    results = cursor.fetchall()

    return results
```


```python
import openai

query_results = query_analyticdb("modern art in Europe", "Articles")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Museum of Modern Art (Score: 0.75)
    2. Western Europe (Score: 0.735)
    3. Renaissance art (Score: 0.728)
    4. Pop art (Score: 0.721)
    5. Northern Europe (Score: 0.71)
    6. Hellenistic art (Score: 0.706)
    7. Modernist literature (Score: 0.694)
    8. Art film (Score: 0.687)
    9. Central Europe (Score: 0.685)
    10. European (Score: 0.683)
    11. Art (Score: 0.683)
    12. Byzantine art (Score: 0.682)
    13. Postmodernism (Score: 0.68)
    14. Eastern Europe (Score: 0.679)
    15. Europe (Score: 0.678)
    16. Cubism (Score: 0.678)
    17. Impressionism (Score: 0.677)
    18. Bauhaus (Score: 0.676)
    19. Surrealism (Score: 0.674)
    20. Expressionism (Score: 0.674)


key: _node_content
string_value:
{"id_": "40cdd5f3-9740-4686-9e95-c405cb19d627", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "2de376d9-7204-4b77-8160-7d2297d68dfc", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "436edb368979905aebf7d00f3f021a82934ad92a7e8acc7b5ffdec37c46878ca", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "44a6b8aa-379c-4db1-9ca6-f31d8f580f23", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d67ef0f29174c6bdc9331dc485420a3cf7a7009ed5204e1008d8e6e79828a2e2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9174, "end_char_idx": 11060, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import openai

query_results = query_analyticdb("modern art in Europe", "Articles")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Museum of Modern Art (Score: 0.75)
    2. Western Europe (Score: 0.735)
    3. Renaissance art (Score: 0.728)
    4. Pop art (Score: 0.721)
    5. Northern Europe (Score: 0.71)
    6. Hellenistic art (Score: 0.706)
    7. Modernist literature (Score: 0.694)
    8. Art film (Score: 0.687)
    9. Central Europe (Score: 0.685)
    10. European (Score: 0.683)
    11. Art (Score: 0.683)
    12. Byzantine art (Score: 0.682)
    13. Postmodernism (Score: 0.68)
    14. Eastern Europe (Score: 0.679)
    15. Europe (Score: 0.678)
    16. Cubism (Score: 0.678)
    17. Impressionism (Score: 0.677)
    18. Bauhaus (Score: 0.676)
    19. Surrealism (Score: 0.674)
    20. Expressionism (Score: 0.674)



```python
# This time we'll query using content vector
query_results = query_analyticdb("Famous battles in Scottish history", "Articles", "content_vector")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Battle of Bannockburn (Score: 0.739)
    2. Wars of Scottish Independence (Score: 0.723)
    3. 1651 (Score: 0.705)
    4. First War of Scottish Independence (Score: 0.699)
    5. Robert I of Scotland (Score: 0.692)
    6. 841 (Score: 0.688)
    7. 1716 (Score: 0.688)
    8. 1314 (Score: 0.674)
    9. 1263 (Score: 0.673)
    10. William Wallace (Score: 0.671)
    11. Stirling (Score: 0.663)
    12. 1306 (Score: 0.662)
    13. 1746 (Score: 0.661)
    14. 1040s (Score: 0.656)
    15. 1106 (Score: 0.654)
    16. 1304 (Score: 0.653)
    17. David II of Scotland (Score: 0.65)
    18. Braveheart (Score: 0.649)
    19. 1124 (Score: 0.648)
    20. July 27 (Score: 0.646)



```python

```


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb


key: _node_content
string_value:
{"id_": "743295ce-b397-4d2b-94a4-a213c8571468", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d02d0fe5-b360-4f5b-86d4-479f91c87f08", "node_type": "1", "metadata": {}, "hash": "d7a506978c21fb9b927d19497ba294e0dd3b1522b75af18a64e0397330b37d8d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3195, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Question Answering with Langchain, AnalyticDB and OpenAI
This notebook presents how to implement a Question Answering system with Langchain, AnalyticDB as a knowledge based and OpenAI embeddings. If you are not familiar with AnalyticDB, it’s better to check out the [Getting_started_with_AnalyticDB_and_OpenAI.ipynb](Getting_started_with_AnalyticDB_and_OpenAI.ipynb) notebook.

This notebook presents an end-to-end process of:
- Calculating the embeddings with OpenAI API.
- Storing the embeddings in an AnalyticDB instance to build a knowledge base.
- Converting raw text query to an embedding with OpenAI API.
- Using AnalyticDB to perform the nearest neighbour search in the created collection to find some context.
- Asking LLM to find the answer in a given context.

All the steps will be simplified to calling some corresponding Langchain methods.

## Prerequisites
For the purposes of this exercise we need to prepare a couple of things:
[AnalyticDB cloud instance](https://www.alibabacloud.com/help/en/analyticdb-for-postgresql/latest/product-introduction-overview).
[Langchain](https://github.com/hwchase17/langchain) as a framework.
An OpenAI API key.

### Install requirements
This notebook requires the following Python packages: `openai`, `tiktoken`, `langchain` and `psycopg2cffi`.
- `openai` provides convenient access to the OpenAI API.
- `tiktoken` is a fast BPE tokeniser for use with OpenAI's models.
- `langchain` helps us to build applications with LLM more easily.
- `psycopg2cffi` library is used to interact with the vector database, but any other PostgreSQL client library is also acceptable.


```python
! pip install openai tiktoken langchain psycopg2cffi 
```


```python
! export OPENAI_API_KEY="your API key"
```


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


### Prepare your OpenAI API key
The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://platform.openai.com/account/api-keys ).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY` by running following command:

### Prepare your AnalyticDB connection string
To build the AnalyticDB connection string, you need to have the following parameters: `PG_HOST`, `PG_PORT`, `PG_DATABASE`, `PG_USER`, and `PG_PASSWORD`. You need to export them first to set correct connect string. Then build the connection string.


```python
! export PG_HOST="your AnalyticDB host url"
! export PG_PORT=5432 # Optional, default value is 5432
! export PG_DATABASE=postgres # Optional, default value is postgres
! export PG_USER="your username"
! export PG_PASSWORD="your password"
```


key: _node_content
string_value:
{"id_": "d02d0fe5-b360-4f5b-86d4-479f91c87f08", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "743295ce-b397-4d2b-94a4-a213c8571468", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c93724e2d98d7f06e5f660fb9cb6772be7c92f7becdea467f2802ac98b921844", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a64c5006-b9d0-4ad1-886b-ad5fbf80175f", "node_type": "1", "metadata": {}, "hash": "c9617e590c550fe2d103fc3983b663bb57d96470cf03450c6790b7179a3339be", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1622, "end_char_idx": 4641, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install openai tiktoken langchain psycopg2cffi 
```


```python
! export OPENAI_API_KEY="your API key"
```


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


### Prepare your OpenAI API key
The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://platform.openai.com/account/api-keys ).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY` by running following command:

### Prepare your AnalyticDB connection string
To build the AnalyticDB connection string, you need to have the following parameters: `PG_HOST`, `PG_PORT`, `PG_DATABASE`, `PG_USER`, and `PG_PASSWORD`. You need to export them first to set correct connect string. Then build the connection string.


```python
! export PG_HOST="your AnalyticDB host url"
! export PG_PORT=5432 # Optional, default value is 5432
! export PG_DATABASE=postgres # Optional, default value is postgres
! export PG_USER="your username"
! export PG_PASSWORD="your password"
```


```python
import os
from langchain.vectorstores.analyticdb import AnalyticDB

CONNECTION_STRING = AnalyticDB.connection_string_from_db_params(
    driver=os.environ.get("PG_DRIVER", "psycopg2cffi"),
    host=os.environ.get("PG_HOST", "localhost"),
    port=int(os.environ.get("PG_PORT", "5432")),
    database=os.environ.get("PG_DATABASE", "postgres"),
    user=os.environ.get("PG_USER", "postgres"),
    password=os.environ.get("PG_PASSWORD", "postgres"),
)
```


```python
import json

with open("questions.json", "r") as fp:
    questions = json.load(fp)

with open("answers.json", "r") as fp:
    answers = json.load(fp)
```

## Load data
In this section we are going to load the data containing some natural questions and answers to them. All the data will be used to create a Langchain application with AnalyticDB being the knowledge base.


```python
print(questions[0])
```

    when is the last episode of season 8 of the walking dead



```python
import wget

# All the examples come from https://ai.google.com/research/NaturalQuestions
# This is a sample of the training set that we download and extract for some
# further processing.
wget.download("https://storage.googleapis.com/dataset-natural-questions/questions.json")
wget.download("https://storage.googleapis.com/dataset-natural-questions/answers.json")
```


```python
print(answers[0])
```

    No . overall No. in season Title Directed by Written by Original air date U.S.


key: _node_content
string_value:
{"id_": "a64c5006-b9d0-4ad1-886b-ad5fbf80175f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d02d0fe5-b360-4f5b-86d4-479f91c87f08", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "63101a5efe5c17802f11aca690048e664c04e004d6f8444522393d6771bd897a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b8f3ef4f-f4f6-44be-bc03-5084d01dfdbe", "node_type": "1", "metadata": {}, "hash": "17c202e3bcc232572bf173037fc46d2538d48c0ccdb49247e493b304d12248c2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3198, "end_char_idx": 6355, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import os
from langchain.vectorstores.analyticdb import AnalyticDB

CONNECTION_STRING = AnalyticDB.connection_string_from_db_params(
    driver=os.environ.get("PG_DRIVER", "psycopg2cffi"),
    host=os.environ.get("PG_HOST", "localhost"),
    port=int(os.environ.get("PG_PORT", "5432")),
    database=os.environ.get("PG_DATABASE", "postgres"),
    user=os.environ.get("PG_USER", "postgres"),
    password=os.environ.get("PG_PASSWORD", "postgres"),
)
```


```python
import json

with open("questions.json", "r") as fp:
    questions = json.load(fp)

with open("answers.json", "r") as fp:
    answers = json.load(fp)
```

## Load data
In this section we are going to load the data containing some natural questions and answers to them. All the data will be used to create a Langchain application with AnalyticDB being the knowledge base.


```python
print(questions[0])
```

    when is the last episode of season 8 of the walking dead



```python
import wget

# All the examples come from https://ai.google.com/research/NaturalQuestions
# This is a sample of the training set that we download and extract for some
# further processing.
wget.download("https://storage.googleapis.com/dataset-natural-questions/questions.json")
wget.download("https://storage.googleapis.com/dataset-natural-questions/answers.json")
```


```python
print(answers[0])
```

    No . overall No. in season Title Directed by Written by Original air date U.S. viewers ( millions ) 100 `` Mercy '' Greg Nicotero Scott M. Gimple October 22 , 2017 ( 2017 - 10 - 22 ) 11.44 Rick , Maggie , and Ezekiel rally their communities together to take down Negan . Gregory attempts to have the Hilltop residents side with Negan , but they all firmly stand behind Maggie . The group attacks the Sanctuary , taking down its fences and flooding the compound with walkers . With the Sanctuary defaced , everyone leaves except Gabriel , who reluctantly stays to save Gregory , but is left behind when Gregory abandons him . Surrounded by walkers , Gabriel hides in a trailer , where he is trapped inside with Negan . 101 `` The Damned '' Rosemary Rodriguez Matthew Negrete & Channing Powell October 29 , 2017 ( 2017 - 10 - 29 ) 8.92 Rick 's forces split into separate parties to attack several of the Saviors ' outposts , during which many members of the group are killed ; Eric is critically injured and rushed away by Aaron . Jesus stops Tara and Morgan from executing a group of surrendered Saviors . While clearing an outpost with Daryl , Rick is confronted and held at gunpoint by Morales , a survivor he met in the initial Atlanta camp , who is now with the Saviors . 102 `` Monsters '' Greg Nicotero Matthew Negrete & Channing Powell November 5 , 2017 ( 2017 - 11 - 05 ) 8.52 Daryl finds Morales threatening Rick and kills him ; the duo then pursue a group of Saviors who are transporting weapons to another outpost . Gregory returns to Hilltop , and after a heated argument , Maggie ultimately allows him back in the community . Eric dies from his injuries , leaving Aaron distraught . Despite Tara and Morgan 's objections , Jesus leads the group of surrendered Saviors to Hilltop .


key: _node_content
string_value:
{"id_": "b8f3ef4f-f4f6-44be-bc03-5084d01dfdbe", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a64c5006-b9d0-4ad1-886b-ad5fbf80175f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "820ba7fd1668911b5209614d0a86b7b5491a5810fc175dc6c6fb98c16bc71d9b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "417481c0-a564-4482-862f-63b117fa825c", "node_type": "1", "metadata": {}, "hash": "73dbbccf7f3338adc8c15bf29974e78704fc37f172cc4fad6cf91b73ca10884e", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4834, "end_char_idx": 8053, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Gregory attempts to have the Hilltop residents side with Negan , but they all firmly stand behind Maggie . The group attacks the Sanctuary , taking down its fences and flooding the compound with walkers . With the Sanctuary defaced , everyone leaves except Gabriel , who reluctantly stays to save Gregory , but is left behind when Gregory abandons him . Surrounded by walkers , Gabriel hides in a trailer , where he is trapped inside with Negan . 101 `` The Damned '' Rosemary Rodriguez Matthew Negrete & Channing Powell October 29 , 2017 ( 2017 - 10 - 29 ) 8.92 Rick 's forces split into separate parties to attack several of the Saviors ' outposts , during which many members of the group are killed ; Eric is critically injured and rushed away by Aaron . Jesus stops Tara and Morgan from executing a group of surrendered Saviors . While clearing an outpost with Daryl , Rick is confronted and held at gunpoint by Morales , a survivor he met in the initial Atlanta camp , who is now with the Saviors . 102 `` Monsters '' Greg Nicotero Matthew Negrete & Channing Powell November 5 , 2017 ( 2017 - 11 - 05 ) 8.52 Daryl finds Morales threatening Rick and kills him ; the duo then pursue a group of Saviors who are transporting weapons to another outpost . Gregory returns to Hilltop , and after a heated argument , Maggie ultimately allows him back in the community . Eric dies from his injuries , leaving Aaron distraught . Despite Tara and Morgan 's objections , Jesus leads the group of surrendered Saviors to Hilltop . Ezekiel 's group attacks another Savior compound , during which several Kingdommers are shot while protecting Ezekiel . 103 `` Some Guy '' Dan Liu David Leslie Johnson November 12 , 2017 ( 2017 - 11 - 12 ) 8.69 Ezekiel 's group is overwhelmed by the Saviors , who kill all of them except for Ezekiel himself and Jerry . Carol clears the inside of the compound , killing all but two Saviors , who almost escape but are eventually caught by Rick and Daryl . En route to the Kingdom , Ezekiel , Jerry , and Carol are surrounded by walkers , but Shiva sacrifices herself to save them . The trio returns to the Kingdom , where Ezekiel 's confidence in himself as a leader has diminished . 104 5 `` The Big Scary U '' Michael E. Satrazemis Story by : Scott M. Gimple & David Leslie Johnson & Angela Kang Teleplay by : David Leslie Johnson & Angela Kang November 19 , 2017 ( 2017 - 11 - 19 ) 7.85 After confessing their sins to each other , Gabriel and Negan manage to escape from the trailer . Simon and the other lieutenants grow suspicious of each other , knowing that Rick 's forces must have inside information . The workers in the Sanctuary become increasingly frustrated with their living conditions , and a riot nearly ensues , until Negan returns and restores order . Gabriel is locked in a cell , where Eugene discovers him sick and suffering . Meanwhile , Rick and Daryl argue over how to take out the Saviors , leading Daryl to abandon Rick . 105 6 `` The King , the Widow , and Rick '' John Polson Angela Kang & Corey Reed November 26 , 2017 ( 2017 - 11 - 26 ) 8.28 Rick visits Jadis in hopes of convincing her to turn against Negan ; Jadis refuses , and locks Rick in a shipping container .


key: _node_content
string_value:
{"id_": "417481c0-a564-4482-862f-63b117fa825c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b8f3ef4f-f4f6-44be-bc03-5084d01dfdbe", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1fad615f63ad9696368dad579388c8335365d28f0bf84602e1c3e18bc148b89a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "29f1011f-575f-47d9-aa9a-6f3a643f93ba", "node_type": "1", "metadata": {}, "hash": "cb60107c065b20a505236b4a783526f253147b90402d4c55b8dd05c8edabd56c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6476, "end_char_idx": 9569, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
103 `` Some Guy '' Dan Liu David Leslie Johnson November 12 , 2017 ( 2017 - 11 - 12 ) 8.69 Ezekiel 's group is overwhelmed by the Saviors , who kill all of them except for Ezekiel himself and Jerry . Carol clears the inside of the compound , killing all but two Saviors , who almost escape but are eventually caught by Rick and Daryl . En route to the Kingdom , Ezekiel , Jerry , and Carol are surrounded by walkers , but Shiva sacrifices herself to save them . The trio returns to the Kingdom , where Ezekiel 's confidence in himself as a leader has diminished . 104 5 `` The Big Scary U '' Michael E. Satrazemis Story by : Scott M. Gimple & David Leslie Johnson & Angela Kang Teleplay by : David Leslie Johnson & Angela Kang November 19 , 2017 ( 2017 - 11 - 19 ) 7.85 After confessing their sins to each other , Gabriel and Negan manage to escape from the trailer . Simon and the other lieutenants grow suspicious of each other , knowing that Rick 's forces must have inside information . The workers in the Sanctuary become increasingly frustrated with their living conditions , and a riot nearly ensues , until Negan returns and restores order . Gabriel is locked in a cell , where Eugene discovers him sick and suffering . Meanwhile , Rick and Daryl argue over how to take out the Saviors , leading Daryl to abandon Rick . 105 6 `` The King , the Widow , and Rick '' John Polson Angela Kang & Corey Reed November 26 , 2017 ( 2017 - 11 - 26 ) 8.28 Rick visits Jadis in hopes of convincing her to turn against Negan ; Jadis refuses , and locks Rick in a shipping container . Carl encounters Siddiq in the woods and recruits him to Alexandria . Daryl and Tara plot to deviate from Rick 's plans by destroying the Sanctuary . Ezekiel isolates himself at the Kingdom , where Carol tries to encourage him to be the leader his people need . Maggie has the group of captured Saviors placed in a holding area and forces Gregory to join them as punishment for betraying Hilltop . 106 7 `` Time for After '' Larry Teng Matthew Negrete & Corey Reed December 3 , 2017 ( 2017 - 12 - 03 ) 7.47 After learning of Dwight 's association with Rick 's group , Eugene affirms his loyalty to Negan and outlines a plan to get rid of the walkers surrounding the Sanctuary . With help from Morgan and Tara , Daryl drives a truck through the Sanctuary 's walls , flooding its interior with walkers , killing many Saviors . Rick finally convinces Jadis and the Scavengers to align with him , and they plan to force the Saviors to surrender . However , when they arrive at the Sanctuary , Rick is horrified to see the breached walls and no sign of the walker herd . 107 8 `` How It 's Gotta Be '' Michael E. Satrazemis David Leslie Johnson & Angela Kang December 10 , 2017 ( 2017 - 12 - 10 ) 7.89 Eugene 's plan allows the Saviors to escape , and separately , the Saviors waylay the Alexandria , Hilltop , and Kingdom forces . The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors .


key: _node_content
string_value:
{"id_": "29f1011f-575f-47d9-aa9a-6f3a643f93ba", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "417481c0-a564-4482-862f-63b117fa825c", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "93488f260aad9845748b9f93c8e56bd44f926650467251cfdcadecc1a7690fc9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2c318687-2364-47b5-81f4-95d07fb1b858", "node_type": "1", "metadata": {}, "hash": "fb3744ea7d5f5bf0abea2d5e69e6ccfb42772b1baf08b548458e0081534db0d6", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8054, "end_char_idx": 10845, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Carl encounters Siddiq in the woods and recruits him to Alexandria . Daryl and Tara plot to deviate from Rick 's plans by destroying the Sanctuary . Ezekiel isolates himself at the Kingdom , where Carol tries to encourage him to be the leader his people need . Maggie has the group of captured Saviors placed in a holding area and forces Gregory to join them as punishment for betraying Hilltop . 106 7 `` Time for After '' Larry Teng Matthew Negrete & Corey Reed December 3 , 2017 ( 2017 - 12 - 03 ) 7.47 After learning of Dwight 's association with Rick 's group , Eugene affirms his loyalty to Negan and outlines a plan to get rid of the walkers surrounding the Sanctuary . With help from Morgan and Tara , Daryl drives a truck through the Sanctuary 's walls , flooding its interior with walkers , killing many Saviors . Rick finally convinces Jadis and the Scavengers to align with him , and they plan to force the Saviors to surrender . However , when they arrive at the Sanctuary , Rick is horrified to see the breached walls and no sign of the walker herd . 107 8 `` How It 's Gotta Be '' Michael E. Satrazemis David Leslie Johnson & Angela Kang December 10 , 2017 ( 2017 - 12 - 10 ) 7.89 Eugene 's plan allows the Saviors to escape , and separately , the Saviors waylay the Alexandria , Hilltop , and Kingdom forces . The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors . Eugene aids Gabriel and Doctor Carson in escaping the Sanctuary in order to ease his conscience . Negan attacks Alexandria , but Carl devises a plan to allow the Alexandria residents to escape into the sewers . Carl reveals he was bitten by a walker while escorting Siddiq to Alexandria . 108 9 `` Honor '' Greg Nicotero Matthew Negrete & Channing Powell February 25 , 2018 ( 2018 - 02 - 25 ) 8.28 After the Saviors leave Alexandria , the survivors make for the Hilltop while Rick and Michonne stay behind to say their final goodbyes to a dying Carl , who pleads with Rick to build a better future alongside the Saviors before killing himself . In the Kingdom , Morgan and Carol launch a rescue mission for Ezekiel . Although they are successful and retake the Kingdom , the Saviors ' lieutenant Gavin is killed by Benjamin 's vengeful brother Henry . 109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


key: _node_content
string_value:
{"id_": "2c318687-2364-47b5-81f4-95d07fb1b858", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "29f1011f-575f-47d9-aa9a-6f3a643f93ba", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "efcae00efce3e045032e29a5591c2c602daf65171594bab7fe57e34d35c18b89", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d0c8afe6-91a3-45ab-b13c-2c50c5963117", "node_type": "1", "metadata": {}, "hash": "bf4975ede6e495c31704beb202fac9e8b0f102496b5ad3f3340fa0bfc87ea09f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9380, "end_char_idx": 11953, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors . Eugene aids Gabriel and Doctor Carson in escaping the Sanctuary in order to ease his conscience . Negan attacks Alexandria , but Carl devises a plan to allow the Alexandria residents to escape into the sewers . Carl reveals he was bitten by a walker while escorting Siddiq to Alexandria . 108 9 `` Honor '' Greg Nicotero Matthew Negrete & Channing Powell February 25 , 2018 ( 2018 - 02 - 25 ) 8.28 After the Saviors leave Alexandria , the survivors make for the Hilltop while Rick and Michonne stay behind to say their final goodbyes to a dying Carl , who pleads with Rick to build a better future alongside the Saviors before killing himself . In the Kingdom , Morgan and Carol launch a rescue mission for Ezekiel . Although they are successful and retake the Kingdom , the Saviors ' lieutenant Gavin is killed by Benjamin 's vengeful brother Henry . 109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


```python
from langchain.vectorstores import AnalyticDB
from langchain.embeddings import OpenAIEmbeddings
from langchain import VectorDBQA, OpenAI

embeddings = OpenAIEmbeddings()
doc_store = AnalyticDB.from_texts(
    texts=answers, embedding=embeddings, connection_string=CONNECTION_STRING,
    pre_delete_collection=True,
)
```

At this stage all the possible answers are already stored in  AnalyticDB, so we can define the whole QA chain.


```python
from langchain.chains import RetrievalQA

llm = OpenAI()
qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
)
```

## Search data

Once the data is put into AnalyticDB we can start asking some questions. A question will be automatically vectorized by OpenAI model, and the created vector will be used to find some possibly matching answers in AnalyticDB. Once retrieved, the most similar answers will be incorporated into the prompt sent to OpenAI Large Language Model.



```python
import random

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


key: _node_content
string_value:
{"id_": "d0c8afe6-91a3-45ab-b13c-2c50c5963117", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2c318687-2364-47b5-81f4-95d07fb1b858", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "20434d0ed3c2f2f3280784ab505598875d9b5fe91e0eb248430d95a196842aa7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6d5ae9f4-6e3a-4592-8978-2a217acc885b", "node_type": "1", "metadata": {}, "hash": "aad65fce997a6de8e7b106fe59e4a38d8f6ef9d2503faa1363469b2d4cbe69fa", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10422, "end_char_idx": 13073, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


```python
from langchain.vectorstores import AnalyticDB
from langchain.embeddings import OpenAIEmbeddings
from langchain import VectorDBQA, OpenAI

embeddings = OpenAIEmbeddings()
doc_store = AnalyticDB.from_texts(
    texts=answers, embedding=embeddings, connection_string=CONNECTION_STRING,
    pre_delete_collection=True,
)
```

At this stage all the possible answers are already stored in  AnalyticDB, so we can define the whole QA chain.


```python
from langchain.chains import RetrievalQA

llm = OpenAI()
qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
)
```

## Search data

Once the data is put into AnalyticDB we can start asking some questions. A question will be automatically vectorized by OpenAI model, and the created vector will be used to find some possibly matching answers in AnalyticDB. Once retrieved, the most similar answers will be incorporated into the prompt sent to OpenAI Large Language Model.



```python
import random

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


```python
for question in selected_questions:
    print(">", question)
    print(qa.run(question), end="\n\n")
```

    > where do frankenstein and the monster first meet
     Victor retreats into the mountains, and that is where the Creature finds him and pleads for Victor to hear his tale.
    
    > who are the actors in fast and furious
     The main cast of Fast & Furious includes Vin Diesel as Dominic Toretto, Paul Walker as Brian O'Conner, Michelle Rodriguez as Letty Ortiz, Jordana Brewster as Mia Toretto, Tyrese Gibson as Roman Pearce, and Ludacris as Tej Parker.
    
    > properties of red black tree in data structure
     The properties of a red-black tree in data structure are that each node is either red or black, the root is black, all leaves (NIL) are black, and if a node is red, then both its children are black. Additionally, every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
    
    > who designed the national coat of arms of south africa
     Iaan Bekker
    
    > caravaggio's death of the virgin pamela askew
     I don't know.


key: _node_content
string_value:
{"id_": "6d5ae9f4-6e3a-4592-8978-2a217acc885b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d0c8afe6-91a3-45ab-b13c-2c50c5963117", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c1fa656a7a873459a73fdb14eafb48f2d2f286501aec293a9a19a80cfbb219c0", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "02197752-f58a-42a1-8e05-7906204d92df", "node_type": "1", "metadata": {}, "hash": "f767bc0fa77f0557356063d5c75229949db86aaa02dee23adda395d428f974d2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11857, "end_char_idx": 14690, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import random

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


```python
for question in selected_questions:
    print(">", question)
    print(qa.run(question), end="\n\n")
```

    > where do frankenstein and the monster first meet
     Victor retreats into the mountains, and that is where the Creature finds him and pleads for Victor to hear his tale.
    
    > who are the actors in fast and furious
     The main cast of Fast & Furious includes Vin Diesel as Dominic Toretto, Paul Walker as Brian O'Conner, Michelle Rodriguez as Letty Ortiz, Jordana Brewster as Mia Toretto, Tyrese Gibson as Roman Pearce, and Ludacris as Tej Parker.
    
    > properties of red black tree in data structure
     The properties of a red-black tree in data structure are that each node is either red or black, the root is black, all leaves (NIL) are black, and if a node is red, then both its children are black. Additionally, every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
    
    > who designed the national coat of arms of south africa
     Iaan Bekker
    
    > caravaggio's death of the virgin pamela askew
     I don't know.
    


### Custom prompt templates

The `stuff` chain type in Langchain uses a specific prompt with question and context documents incorporated. This is what the default prompt looks like:

```text
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:
```

We can, however, provide our prompt template and change the behaviour of the OpenAI LLM, while still using the `stuff` chain type. It is important to keep `{context}` and `{question}` as placeholders.

#### Experimenting with custom prompts

We can try using a different prompt template, so the model:
1. Responds with a single-sentence answer if it knows it.
2. Suggests a random song title if it doesn't know the answer to our question.


```python
from langchain.prompts import PromptTemplate
custom_prompt = """
Use the following pieces of context to answer the question at the end. Please provide
a short single-sentence summary answer only. If you don't know the answer or if it's
not present in given context, don't try to make up an answer, but suggest me a random
unrelated song title I could listen to.
Context: {context}
Question: {question}
Helpful Answer:
"""

custom_prompt_template = PromptTemplate(
    template=custom_prompt, input_variables=["context", "question"]
)
```


```python
custom_qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
    chain_type_kwargs={"prompt": custom_prompt_template},
)
```


key: _node_content
string_value:
{"id_": "02197752-f58a-42a1-8e05-7906204d92df", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c5547728-56d8-4ee0-a273-cfb705341f5b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8199fc7bc08996c70008990b9b86977ab403c583667423b770ca2dd6a2906b8f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6d5ae9f4-6e3a-4592-8978-2a217acc885b", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "459fcdae48138f0f3ef53869123152f3bdad65d7f499710fe8f54b7a4c3b9b57", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 13081, "end_char_idx": 15674, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
### Custom prompt templates

The `stuff` chain type in Langchain uses a specific prompt with question and context documents incorporated. This is what the default prompt looks like:

```text
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:
```

We can, however, provide our prompt template and change the behaviour of the OpenAI LLM, while still using the `stuff` chain type. It is important to keep `{context}` and `{question}` as placeholders.

#### Experimenting with custom prompts

We can try using a different prompt template, so the model:
1. Responds with a single-sentence answer if it knows it.
2. Suggests a random song title if it doesn't know the answer to our question.


```python
from langchain.prompts import PromptTemplate
custom_prompt = """
Use the following pieces of context to answer the question at the end. Please provide
a short single-sentence summary answer only. If you don't know the answer or if it's
not present in given context, don't try to make up an answer, but suggest me a random
unrelated song title I could listen to.
Context: {context}
Question: {question}
Helpful Answer:
"""

custom_prompt_template = PromptTemplate(
    template=custom_prompt, input_variables=["context", "question"]
)
```


```python
custom_qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
    chain_type_kwargs={"prompt": custom_prompt_template},
)
```


```python
random.seed(41)
for question in random.choices(questions, k=5):
    print(">", question)
    print(custom_qa.run(question), end="\n\n")
```

    > what was uncle jesse's original last name on full house
    Uncle Jesse's original last name on Full House was Cochran.
    
    > when did the volcano erupt in indonesia 2018
    No information about a volcano erupting in Indonesia in 2018 is present in the given context. Suggested song title: "Volcano" by U2.
    
    > what does a dualist way of thinking mean
    A dualist way of thinking means believing that humans possess a non-physical mind or soul which is distinct from their physical body.
    
    > the first civil service commission in india was set up on the basis of recommendation of
    The first Civil Service Commission in India was not set up on the basis of a recommendation.
    
    > how old do you have to be to get a tattoo in utah
    In Utah, you must be at least 18 years old to get a tattoo.


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb


key: title
string_value:
**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.


key: _node_content
string_value:
{"id_": "3d804e8e-1951-4665-83e6-13eea7abae91", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb", "title": "**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.", "tokens": 321, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "8ae1ee3d-9937-4645-abc6-153c509e031a", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb", "title": "**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.", "tokens": 321, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "797b964b1055e3b5396f19fac04f11937dc10c24748dcfd77cfe2df5ae6af153", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 1587, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.

SingleStoreDB supports vectors and vector similarity search using dot_product (for cosine similarity) and euclidean_distance functions. These functions are used by our customers for applications including face recognition, visual product photo search and text-based semantic search. With the explosion of generative AI technology, these capabilities form a firm foundation for text-based AI chatbots.

But remember, SingleStoreDB is a high-performance, scalable, modern SQL DBMS that supports multiple data models including structured data, semi-structured data based on JSON, time-series, full text, spatial, key-value and of course vector data. Start powering your next intelligent application with SingleStoreDB today!

![SingleStore Open AI](https://user-images.githubusercontent.com/8846480/236985121-48980956-fdc5-49c8-b006-f3a412142676.png)

## Example

This folder contains examples of using SingleStoreDB and OpenAI together. We will keep adding more scenarios so stay tuned!

| Name | Description |
| --- | --- |
| [OpenAI wikipedia semantic search](./OpenAI_wikipedia_semantic_search.ipynb) | Improve ChatGPT accuracy through SingleStoreDB semantic Search in QA |


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb


key: _node_content
string_value:
{"id_": "e91744b8-fdc8-43b7-83de-34af4d1a24c1", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c4afbc60-c41e-4d8b-a9a4-8599ce3b0b0d", "node_type": "1", "metadata": {}, "hash": "d5c8f8e718c12d68584c4473cf5f11c3ff3ca39839e8477c72ec58b1c2b6663c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2157, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Intro
This notebook is an example on how you can use SingleStoreDB vector storage and functions to build an interactive Q&A application with ChatGPT. If you start a [Trial](https://www.singlestore.com/cloud-trial/) in SingleStoreDB, you can find the same notebook in our sample notebooks with native connection.

## First let's talk directly to ChatGPT and try and get back a response


```python
!pip install openai --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import openai

EMBEDDING_MODEL = "text-embedding-3-small"
GPT_MODEL = "gpt-3.5-turbo"

```

## Let's connect to OpenAI and see the result we get when asking for a date beyond 2021


```python
openai.api_key = 'OPENAI API KEY'

response = openai.ChatCompletion.create(
  model=GPT_MODEL,
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the gold medal for curling in Olymics 2022?"},
    ]
)

print(response['choices'][0]['message']['content'])

```

    I'm sorry, I cannot provide information about events that have not occurred yet. The Winter Olympics 2022 will be held in Beijing, China from February 4 to 20, 2022. The curling events will take place during this time and the results will not be known until after the competition has concluded.


# Get the data about Winter Olympics and provide the information to ChatGPT as context

## 1. Setup


```python
!pip install matplotlib plotly.express scikit-learn tabulate tiktoken wget --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import pandas as pd
import os
import wget
import ast

```

## Step 1 - Grab the data from CSV and prepare it


key: _node_content
string_value:
{"id_": "c4afbc60-c41e-4d8b-a9a4-8599ce3b0b0d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e91744b8-fdc8-43b7-83de-34af4d1a24c1", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "64fba8facf7a831b2e965f02c206e39054f52f6264f8545441689a85066896c4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "68887ab6-0c86-42d6-97ae-eb82babc73e5", "node_type": "1", "metadata": {}, "hash": "6868e16bae47ee695ce04c5f615a6c3deca324327212a018020fe4a5b5d0be68", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 907, "end_char_idx": 3527, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
openai.api_key = 'OPENAI API KEY'

response = openai.ChatCompletion.create(
  model=GPT_MODEL,
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the gold medal for curling in Olymics 2022?"},
    ]
)

print(response['choices'][0]['message']['content'])

```

    I'm sorry, I cannot provide information about events that have not occurred yet. The Winter Olympics 2022 will be held in Beijing, China from February 4 to 20, 2022. The curling events will take place during this time and the results will not be known until after the competition has concluded.


# Get the data about Winter Olympics and provide the information to ChatGPT as context

## 1. Setup


```python
!pip install matplotlib plotly.express scikit-learn tabulate tiktoken wget --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import pandas as pd
import os
import wget
import ast

```

## Step 1 - Grab the data from CSV and prepare it


```python
# download pre-chunked text and pre-computed embeddings
# this file is ~200 MB, so may take a minute depending on your connection speed
embeddings_path = "https://cdn.openai.com/API/examples/data/winter_olympics_2022.csv"
file_path = "winter_olympics_2022.csv"

if not os.path.exists(file_path):
    wget.download(embeddings_path, file_path)
    print("File downloaded successfully.")
else:
    print("File already exists in the local file system.")

```

    File downloaded successfully.



```python
df = pd.read_csv(
    "winter_olympics_2022.csv"
)

# convert embeddings from CSV str type back to list type
df['embedding'] = df['embedding'].apply(ast.literal_eval)

```


```python
df

```


```python
df.info(show_counts=True)

```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 6059 entries, 0 to 6058
    Data columns (total 2 columns):
     #   Column     Non-Null Count  Dtype 
    ---  ------     --------------  ----- 
     0   text       6059 non-null   object
     1   embedding  6059 non-null   object
    dtypes: object(2)
    memory usage: 94.8+ KB


## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1


key: _node_content
string_value:
{"id_": "68887ab6-0c86-42d6-97ae-eb82babc73e5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c4afbc60-c41e-4d8b-a9a4-8599ce3b0b0d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "e290877e2242889184ad99e4c552b9ba7a116a1a527128f6b9cab1ecff58e80e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c7af435e-7afb-47a8-936a-5b9513a0bdfe", "node_type": "1", "metadata": {}, "hash": "a57a92669caa4650b0c77d82e9aff33c26a3b1a9c4fb0d5218a93768d82e006d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2160, "end_char_idx": 4670, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# download pre-chunked text and pre-computed embeddings
# this file is ~200 MB, so may take a minute depending on your connection speed
embeddings_path = "https://cdn.openai.com/API/examples/data/winter_olympics_2022.csv"
file_path = "winter_olympics_2022.csv"

if not os.path.exists(file_path):
    wget.download(embeddings_path, file_path)
    print("File downloaded successfully.")
else:
    print("File already exists in the local file system.")

```

    File downloaded successfully.



```python
df = pd.read_csv(
    "winter_olympics_2022.csv"
)

# convert embeddings from CSV str type back to list type
df['embedding'] = df['embedding'].apply(ast.literal_eval)

```


```python
df

```


```python
df.info(show_counts=True)

```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 6059 entries, 0 to 6058
    Data columns (total 2 columns):
     #   Column     Non-Null Count  Dtype 
    ---  ------     --------------  ----- 
     0   text       6059 non-null   object
     1   embedding  6059 non-null   object
    dtypes: object(2)
    memory usage: 94.8+ KB


## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1




```python
#create table
stmt = """
CREATE TABLE IF NOT EXISTS winter_wikipedia2.winter_olympics_2022 (
    id INT PRIMARY KEY,
    text TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
    embedding BLOB
);"""

cur.execute(stmt)

```




    0



## 3. Populate the Table with our dataframe df and use JSON_ARRAY_PACK to compact it


```python
%%time

# Prepare the statement
stmt = """
    INSERT INTO winter_wikipedia2.winter_olympics_2022 (
        id,
        text,
        embedding
    )
    VALUES (
        %s,
        %s,
        JSON_ARRAY_PACK_F64(%s)
    )
"""

# Convert the DataFrame to a NumPy record array
record_arr = df.to_records(index=True)

# Set the batch size
batch_size = 1000

# Iterate over the rows of the record array in batches
for i in range(0, len(record_arr), batch_size):
    batch = record_arr[i:i+batch_size]
    values = [(row[0], row[1], str(row[2])) for row in batch]
    cur.executemany(stmt, values)

```

    CPU times: user 8.79 s, sys: 4.63 s, total: 13.4 s
    Wall time: 11min 4s


## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again


key: _node_content
string_value:
{"id_": "c7af435e-7afb-47a8-936a-5b9513a0bdfe", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "68887ab6-0c86-42d6-97ae-eb82babc73e5", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "6c4bd841899b4aeec9122714edccf8bb999d63584c1ec7bcb67b153dd59b37da", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "68ce4364-3ebd-4ac7-a62f-34e14de08b4d", "node_type": "1", "metadata": {}, "hash": "ff28753621ec3841c246af62822f6e3ba40131e52fe48f564d0a96ec8163e6b1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3250, "end_char_idx": 6124, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1




```python
#create table
stmt = """
CREATE TABLE IF NOT EXISTS winter_wikipedia2.winter_olympics_2022 (
    id INT PRIMARY KEY,
    text TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
    embedding BLOB
);"""

cur.execute(stmt)

```




    0



## 3. Populate the Table with our dataframe df and use JSON_ARRAY_PACK to compact it


```python
%%time

# Prepare the statement
stmt = """
    INSERT INTO winter_wikipedia2.winter_olympics_2022 (
        id,
        text,
        embedding
    )
    VALUES (
        %s,
        %s,
        JSON_ARRAY_PACK_F64(%s)
    )
"""

# Convert the DataFrame to a NumPy record array
record_arr = df.to_records(index=True)

# Set the batch size
batch_size = 1000

# Iterate over the rows of the record array in batches
for i in range(0, len(record_arr), batch_size):
    batch = record_arr[i:i+batch_size]
    values = [(row[0], row[1], str(row[2])) for row in batch]
    cur.executemany(stmt, values)

```

    CPU times: user 8.79 s, sys: 4.63 s, total: 13.4 s
    Wall time: 11min 4s


## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again



```python
from utils.embeddings_utils import get_embedding

def strings_ranked_by_relatedness(
    query: str,
    df: pd.DataFrame,
    relatedness_fn=lambda x, y: 1 - spatial.distance.cosine(x, y),
    top_n: int = 100
) -> tuple:
    """Returns a list of strings and relatednesses, sorted from most related to least."""

    # Get the embedding of the query.
    query_embedding_response = get_embedding(query, EMBEDDING_MODEL)

    # Create the SQL statement.
    stmt = """
        SELECT
            text,
            DOT_PRODUCT_F64(JSON_ARRAY_PACK_F64(%s), embedding) AS score
        FROM winter_wikipedia2.winter_olympics_2022
        ORDER BY score DESC
        LIMIT %s
    """

    # Execute the SQL statement.
    results = cur.execute(stmt, [str(query_embedding_response), top_n])

    # Fetch the results
    results = cur.fetchall()

    strings = []
    relatednesses = []

    for row in results:
        strings.append(row[0])
        relatednesses.append(row[1])

    # Return the results.
    return strings[:top_n], relatednesses[:top_n]

```


```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


key: _node_content
string_value:
{"id_": "68ce4364-3ebd-4ac7-a62f-34e14de08b4d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c7af435e-7afb-47a8-936a-5b9513a0bdfe", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ab297e24f4188d35a8ad6ddd95579808d80f5041599dea68d1dd0dfd97719f3f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "32f3e14e-736a-445a-bb35-fd0518c4a853", "node_type": "1", "metadata": {}, "hash": "bed5d2b8ddc037a43392043dd3772f3118e411cc29019b0382c226efe677b2ce", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4565, "end_char_idx": 7222, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again



```python
from utils.embeddings_utils import get_embedding

def strings_ranked_by_relatedness(
    query: str,
    df: pd.DataFrame,
    relatedness_fn=lambda x, y: 1 - spatial.distance.cosine(x, y),
    top_n: int = 100
) -> tuple:
    """Returns a list of strings and relatednesses, sorted from most related to least."""

    # Get the embedding of the query.
    query_embedding_response = get_embedding(query, EMBEDDING_MODEL)

    # Create the SQL statement.
    stmt = """
        SELECT
            text,
            DOT_PRODUCT_F64(JSON_ARRAY_PACK_F64(%s), embedding) AS score
        FROM winter_wikipedia2.winter_olympics_2022
        ORDER BY score DESC
        LIMIT %s
    """

    # Execute the SQL statement.
    results = cur.execute(stmt, [str(query_embedding_response), top_n])

    # Fetch the results
    results = cur.fetchall()

    strings = []
    relatednesses = []

    for row in results:
        strings.append(row[0])
        relatednesses.append(row[1])

    # Return the results.
    return strings[:top_n], relatednesses[:top_n]

```


```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


```python
import tiktoken

def num_tokens(text: str, model: str = GPT_MODEL) -> int:
    """Return the number of tokens in a string."""
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))


def query_message(
    query: str,
    df: pd.DataFrame,
    model: str,
    token_budget: int
) -> str:
    """Return a message for GPT, with relevant source texts pulled from SingleStoreDB."""
    strings, relatednesses = strings_ranked_by_relatedness(query, df, "winter_olympics_2022")
    introduction = 'Use the below articles on the 2022 Winter Olympics to answer the subsequent question. If the answer cannot be found in the articles, write "I could not find an answer."'
    question = f"\n\nQuestion: {query}"
    message = introduction
    for string in strings:
        next_article = f'\n\nWikipedia article section:\n"""\n{string}\n"""'
        if (
            num_tokens(message + next_article + question, model=model)
            > token_budget
        ):
            break
        else:
            message += next_article
    return message + question


key: _node_content
string_value:
{"id_": "32f3e14e-736a-445a-bb35-fd0518c4a853", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "b73c1fec-7955-4a17-aa98-9e34c897e5a7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "ae4000940ca04e2078675be698d67cc5a38847d7b8dd35179351ed26e8a14b20", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "68ce4364-3ebd-4ac7-a62f-34e14de08b4d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b688a4c1beeddef8f81120afdf0f7c5f033c354019c64bffad0a1014a6c71b95", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5742, "end_char_idx": 8615, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


```python
import tiktoken

def num_tokens(text: str, model: str = GPT_MODEL) -> int:
    """Return the number of tokens in a string."""
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))


def query_message(
    query: str,
    df: pd.DataFrame,
    model: str,
    token_budget: int
) -> str:
    """Return a message for GPT, with relevant source texts pulled from SingleStoreDB."""
    strings, relatednesses = strings_ranked_by_relatedness(query, df, "winter_olympics_2022")
    introduction = 'Use the below articles on the 2022 Winter Olympics to answer the subsequent question. If the answer cannot be found in the articles, write "I could not find an answer."'
    question = f"\n\nQuestion: {query}"
    message = introduction
    for string in strings:
        next_article = f'\n\nWikipedia article section:\n"""\n{string}\n"""'
        if (
            num_tokens(message + next_article + question, model=model)
            > token_budget
        ):
            break
        else:
            message += next_article
    return message + question


def ask(
    query: str,
    df: pd.DataFrame = df,
    model: str = GPT_MODEL,
    token_budget: int = 4096 - 500,
    print_message: bool = False,
) -> str:
    """Answers a query using GPT and a table of relevant texts and embeddings in SingleStoreDB."""
    message = query_message(query, df, model=model, token_budget=token_budget)
    if print_message:
        print(message)
    messages = [
        {"role": "system", "content": "You answer questions about the 2022 Winter Olympics."},
        {"role": "user", "content": message},
    ]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0
    )
    response_message = response["choices"][0]["message"]["content"]
    return response_message

```

## 6. Get an answer from Chat GPT


```python
from pprint import pprint

answer = ask('Who won the gold medal for curling in Olymics 2022?')

pprint(answer)

```

    ("There were three curling events at the 2022 Winter Olympics: men's, women's, "
     'and mixed doubles. The gold medalists for each event are:\n'
     '\n'
     "- Men's: Sweden (Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer "
     'Sundgren, Daniel Magnusson)\n'
     "- Women's: Great Britain (Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey "
     'Duff, Mili Smith)\n'
     '- Mixed doubles: Italy (Stefania Constantini, Amos Mosaner)')


key: _node_content
string_value:
{"id_": "17ca44b7-6100-4af1-96de-938df140f448", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "4cc31500-e12a-4cd6-aaec-398f8328ad9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8e4b8a90618bfb977e7d32aa91d1d8392f1789e25f3df872233dcb8c3f0889fd", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a28190ae-a96a-4d94-a2ae-b1e0caef793b", "node_type": "1", "metadata": {}, "hash": "57bc55f44a6a25af369c7278bf71eb19b27eaa309b97169e4cb41e7e670ec786", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3342, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure audio whisper (preview) example

The example shows how to use the Azure OpenAI Whisper model to transcribe audio files.


## Setup

First, we install the necessary dependencies and import the libraries we will be using.


```python
! pip install "openai>=1.0.0,<2.0.0"
! pip install python-dotenv
```


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure Active Directory token credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment using the `whisper-1` model to transcribe audio files.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with whisper. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for whisper. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "a28190ae-a96a-4d94-a2ae-b1e0caef793b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "4cc31500-e12a-4cd6-aaec-398f8328ad9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "8e4b8a90618bfb977e7d32aa91d1d8392f1789e25f3df872233dcb8c3f0889fd", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "17ca44b7-6100-4af1-96de-938df140f448", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "045d747a7f03b2f72c6355c0fadba4140a7c8ffb5594176153e3ea73b8cdc9ab", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2001, "end_char_idx": 4311, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment using the `whisper-1` model to transcribe audio files.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with whisper. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for whisper. The deployment name that you give the model will be used in the code below.


```python
deployment = "whisper-deployment" # Fill in the deployment name from the portal here
```

## Audio transcription

Audio transcription, or speech-to-text, is the process of converting spoken words into text. Use the `openai.Audio.transcribe` method to transcribe an audio file stream to text.

You can get sample audio files from the [Azure AI Speech SDK repository at GitHub](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/sampledata/audiofiles).


```python
# download sample audio file
import requests

sample_audio_url = "https://github.com/Azure-Samples/cognitive-services-speech-sdk/raw/master/sampledata/audiofiles/wikipediaOcelot.wav"
audio_file = requests.get(sample_audio_url)
with open("wikipediaOcelot.wav", "wb") as f:
    f.write(audio_file.content)
```


```python
transcription = client.audio.transcriptions.create(
    file=open("wikipediaOcelot.wav", "rb"),
    model=deployment,
)
print(transcription.text)
```


key: _node_content
string_value:
{"id_": "6d47a3b3-cde5-4a91-b5a0-25285fa146d5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c69a2058-2a4c-4430-afbf-5653863f88ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "086cf0d64bdf19aa254495b517b9c8b4694b72751a67ae2416b85edd8aba1b1d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "194883bb-1149-41e8-8d21-af8f998d37bf", "node_type": "1", "metadata": {}, "hash": "9509a12086c57c95c51bd6dfd8acc8f2b51ac55339e8e69854136c2f7c8a1dc0", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3344, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure chat completions example

This example will cover chat completions using the Azure OpenAI service. It also includes information on content filtering.

## Setup

First, we install the necessary dependencies and import the libraries we will be using.


```python
! pip install "openai>=1.0.0,<2.0.0"
! pip install python-dotenv
```


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure Active Directory token credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to create chat completions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "194883bb-1149-41e8-8d21-af8f998d37bf", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c69a2058-2a4c-4430-afbf-5653863f88ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "086cf0d64bdf19aa254495b517b9c8b4694b72751a67ae2416b85edd8aba1b1d", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6d47a3b3-cde5-4a91-b5a0-25285fa146d5", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b63a3d6c3ae0b56cd3b6afe5ff435c27c6e7542d89f4bff82d331d3833bdd8d3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e27a6c91-065b-4625-b861-69c35c12d6b2", "node_type": "1", "metadata": {}, "hash": "540d3b88d334816fb891daa3d65776d1a0a5bc04a1a550a47a3392d845854c05", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2030, "end_char_idx": 5375, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to create chat completions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## Create chat completions

Now let's create a chat completion using the client we built.


```python
# For all possible arguments see https://platform.openai.com/docs/api-reference/chat-completions/create
response = client.chat.completions.create(
    model=deployment,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
)

print(f"{response.choices[0].message.role}: {response.choices[0].message.content}")
```

### Create a streaming chat completion

We can also stream the response.


```python
response = client.chat.completions.create(
    model=deployment,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
    stream=True
)

for chunk in response:
    if len(chunk.choices) > 0:
        delta = chunk.choices[0].delta

        if delta.role:
            print(delta.role + ": ", end="", flush=True)
        if delta.content:
            print(delta.content, end="", flush=True)
```

### Content filtering

Azure OpenAI service includes content filtering of prompts and completion responses. You can learn more about content filtering and how to configure it [here](https://learn.microsoft.com/azure/ai-services/openai/concepts/content-filter).

If the prompt is flagged by the content filter, the library will raise a `BadRequestError` exception with a `content_filter` error code. Otherwise, you can access the `prompt_filter_results` and `content_filter_results` on the response to see the results of the content filtering and what categories were flagged.

#### Prompt flagged by content filter


key: _node_content
string_value:
{"id_": "e27a6c91-065b-4625-b861-69c35c12d6b2", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c69a2058-2a4c-4430-afbf-5653863f88ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "086cf0d64bdf19aa254495b517b9c8b4694b72751a67ae2416b85edd8aba1b1d", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "194883bb-1149-41e8-8d21-af8f998d37bf", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "77a0f4e1b3e0955b2913cb563a789beeb19fd6652c4c85b23cdabb6fe31a4b95", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4138, "end_char_idx": 7123, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
response = client.chat.completions.create(
    model=deployment,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
    stream=True
)

for chunk in response:
    if len(chunk.choices) > 0:
        delta = chunk.choices[0].delta

        if delta.role:
            print(delta.role + ": ", end="", flush=True)
        if delta.content:
            print(delta.content, end="", flush=True)
```

### Content filtering

Azure OpenAI service includes content filtering of prompts and completion responses. You can learn more about content filtering and how to configure it [here](https://learn.microsoft.com/azure/ai-services/openai/concepts/content-filter).

If the prompt is flagged by the content filter, the library will raise a `BadRequestError` exception with a `content_filter` error code. Otherwise, you can access the `prompt_filter_results` and `content_filter_results` on the response to see the results of the content filtering and what categories were flagged.

#### Prompt flagged by content filter


```python
import json

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "<text violating the content policy>"}
]

try:
    completion = client.chat.completions.create(
        messages=messages,
        model=deployment,
    )
except openai.BadRequestError as e:
    err = json.loads(e.response.text)
    if err["error"]["code"] == "content_filter":
        print("Content filter triggered!")
        content_filter_result = err["error"]["innererror"]["content_filter_result"]
        for category, details in content_filter_result.items():
            print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")
```

### Checking the result of the content filter


```python
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What's the biggest city in Washington?"}
]

completion = client.chat.completions.create(
    messages=messages,
    model=deployment,
)
print(f"Answer: {completion.choices[0].message.content}")

# prompt content filter result in "model_extra" for azure
prompt_filter_result = completion.model_extra["prompt_filter_results"][0]["content_filter_results"]
print("\nPrompt content filter results:")
for category, details in prompt_filter_result.items():
    print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")

# completion content filter result
print("\nCompletion content filter results:")
completion_filter_result = completion.choices[0].model_extra["content_filter_results"]
for category, details in completion_filter_result.items():
    print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")
```


key: _node_content
string_value:
{"id_": "7b6b9f12-b381-4696-94e5-1baafb63c9af", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "1c25507a-561e-4c48-a18a-98473cf582ff", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7fe0a7c5a5a05e3f4dcf45a0cb7b6ca5891e8066acbdd6132b75ddb500394eea", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "60f25c3d-ba66-4739-9b5a-37066aa0a4a8", "node_type": "1", "metadata": {}, "hash": "fbd8f45a5a3061b55914161907a16ca286419e6595edc7dc7554805c507e8925", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3308, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure embeddings example

This example will cover embeddings using the Azure OpenAI service.

## Setup

First, we install the necessary dependencies and import the libraries we will be using.


```python
! pip install "openai>=1.0.0,<2.0.0"
! pip install python-dotenv
```


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure Active Directory token credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment of a model that we can use to create embeddings.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with embeddings. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for embeddings. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "60f25c3d-ba66-4739-9b5a-37066aa0a4a8", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "1c25507a-561e-4c48-a18a-98473cf582ff", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7fe0a7c5a5a05e3f4dcf45a0cb7b6ca5891e8066acbdd6132b75ddb500394eea", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7b6b9f12-b381-4696-94e5-1baafb63c9af", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "9e88bed39f8c866b1163d02e8114e3bf8ef7e20a98529ebc2e7fbfc7f0acc071", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1967, "end_char_idx": 3645, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment of a model that we can use to create embeddings.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with embeddings. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for embeddings. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## Embeddings

Now let's create embeddings using the client we built.


```python
embeddings = client.embeddings.create(
    model=deployment,
    input="The food was delicious and the waiter..."
)
                                
print(embeddings)
```


key: _node_content
string_value:
{"id_": "b1dae1ce-442d-4580-8ad5-44573ce68c80", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "6ab98de4-bf90-406c-858b-6a08d9ada54e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d0de622efed18c3134dc1bb74fd204f24f1823f6c027a442308bf7a6d2b7b14b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "50e73423-5a5e-46b4-a4d2-a6450dea57d3", "node_type": "1", "metadata": {}, "hash": "7f3fdbc7c63dcbb05852019f307d7636a7ebaf33392c485545571f75670347c4", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3264, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure functions example

This notebook shows how to use the function calling capability with the Azure OpenAI service. Functions allow a caller of chat completions to define capabilities that the model can use to extend its
functionality into external tools and data sources.

You can read more about chat functions on OpenAI's blog: https://openai.com/blog/function-calling-and-other-api-updates

**NOTE**: Chat functions require model versions beginning with gpt-4 and gpt-35-turbo's `-0613` labels. They are not supported by older versions of the models.

## Setup

First, we install the necessary dependencies and import the libraries we will be using.


```python
! pip install "openai>=1.0.0,<2.0.0"
! pip install python-dotenv
```


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure Active Directory token credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


key: _node_content
string_value:
{"id_": "50e73423-5a5e-46b4-a4d2-a6450dea57d3", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "6ab98de4-bf90-406c-858b-6a08d9ada54e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d0de622efed18c3134dc1bb74fd204f24f1823f6c027a442308bf7a6d2b7b14b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b1dae1ce-442d-4580-8ad5-44573ce68c80", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d6b041abbc08a853f560a9e0a452681f0299b4f542918a608ba6807b01a7bb5b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "755272ac-ae4d-430a-8dec-7619bbb914ea", "node_type": "1", "metadata": {}, "hash": "aca4adeab92c6a47a3a9f517693ef4ccee787d6a4ba39d21fa2aed5f016ddf13", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2432, "end_char_idx": 5725, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to call functions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## Functions

With setup and authentication complete, you can now use functions with the Azure OpenAI service. This will be split into a few steps:

1. Define the function(s)
2. Pass function definition(s) into chat completions API
3. Call function with arguments from the response
4. Feed function response back into chat completions API

#### 1. Define the function(s)

A list of functions can be defined, each containing the name of the function, an optional description, and the parameters the function accepts (described as a JSON schema).


```python
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location.",
                },
            },
            "required": ["location"],
        },
    }
]
```

#### 2. Pass function definition(s) into chat completions API

Now we can pass the function into the chat completions API. If the model determines it should call the function, a `finish_reason` of "tool_calls" will be populated on the choice and the details of which function to call and its arguments will be present in the `message`. Optionally, you can set the `tool_choice` keyword argument to force the model to call a particular function (e.g. `{"type": "function", "function": {"name": get_current_weather}}`). By default, this is set to `auto`, allowing the model to choose whether to call the function or not.


