set up Fireworks.ai Key

```python
!pip install -q llama-index llama-index-vector-stores-mongodb llama-index-embeddings-fireworks==0.1.2 llama-index-llms-fireworks
!pip install -q pymongo datasets pandas
```

```python
# set up Fireworks.ai Key
import os
import getpass

fw_api_key = getpass.getpass("Fireworks API Key:")
os.environ["FIREWORKS_API_KEY"] = fw_api_key
```

```python
from datasets import load_dataset
import pandas as pd

# https://huggingface.co./datasets/AIatMongoDB/whatscooking.restaurants
dataset = load_dataset("AIatMongoDB/whatscooking.restaurants")

# Convert the dataset to a pandas dataframe
dataset_df = pd.DataFrame(dataset["train"])

dataset_df.head(5)
```

    /mnt/disks/data/llama_index/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: 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

      restaurant_id
      attributes
      cuisine
      DogsAllowed
      embedding
      OutdoorSeating
      borough
      address
      _id
      name
      menu
      TakeOut
      location
      PriceRange
      HappyHour
      review_count
      sponsored
      stars

      0
      40366661
      {'Alcohol': ''none'', 'Ambience': '{'romantic'...
      Tex-Mex
      None
      [-0.14520384, 0.018315623, -0.018330636, -0.10...
      True
      Manhattan
      {'building': '627', 'coord': [-73.975980999999...
      {'$oid': '6095a34a7c34416a90d3206b'}
      Baby Bo'S Burritos
      None
      True
      {'coordinates': [-73.97598099999999, 40.745132...
      1.0
      None
      10
      NaN
      2.5

      1
      40367442
      {'Alcohol': ''beer_and_wine'', 'Ambience': '{'...
      American
      True
      [-0.11977468, -0.02157107, 0.0038846824, -0.09...
      True
      Staten Island
      {'building': '17', 'coord': [-74.1350211, 40.6...
      {'$oid': '6095a34a7c34416a90d3209e'}
      Buddy'S Wonder Bar
      [Grilled cheese sandwich, Baked potato, Lasagn...
      True
      {'coordinates': [-74.1350211, 40.6369042], 'ty...
      2.0
      None
      62
      NaN
      3.5

      2
      40364610
      {'Alcohol': ''none'', 'Ambience': '{'touristy'...
      American
      None
      [-0.1004329, -0.014882699, -0.033005167, -0.09...
      True
      Staten Island
      {'building': '37', 'coord': [-74.138263, 40.54...
      {'$oid': '6095a34a7c34416a90d31ff6'}
      Great Kills Yacht Club
      [Mozzarella sticks, Mushroom swiss burger, Spi...
      True
      {'coordinates': [-74.138263, 40.546681], 'type...
      1.0
      None
      72
      NaN
      4.0

      3
      40365288
      {'Alcohol': None, 'Ambience': '{'touristy': Fa...
      American
      None
      [-0.11735515, -0.0397448, -0.0072645755, -0.09...
      True
      Manhattan
      {'building': '842', 'coord': [-73.970637000000...
      {'$oid': '6095a34a7c34416a90d32017'}
      Keats Restaurant
      [French fries, Chicken pot pie, Mac & cheese, ...
      True
      {'coordinates': [-73.97063700000001, 40.751495...
      2.0
      True
      149
      NaN
      4.0

      4
      40363151
      {'Alcohol': None, 'Ambience': None, 'BYOB': No...
      Bakery
      None
      [-0.096541286, -0.009661355, 0.04402167, -0.12...
      True
      Manhattan
      {'building': '120', 'coord': [-73.9998042, 40....
      {'$oid': '6095a34a7c34416a90d31fbd'}
      Olive'S
      [doughnuts, chocolate chip cookies, chocolate ...
      True
      {'coordinates': [-73.9998042, 40.7251256], 'ty...
      1.0
      None
      7
      NaN
      5.0

```python
from llama_index.core.settings import Settings
from llama_index.llms.fireworks import Fireworks
from llama_index.embeddings.fireworks import FireworksEmbedding

embed_model = FireworksEmbedding(
    embed_batch_size=512,
    model_name="nomic-ai/nomic-embed-text-v1.5",
    api_key=fw_api_key,
)
llm = Fireworks(
    temperature=0,
    model="accounts/fireworks/models/mixtral-8x7b-instruct",
    api_key=fw_api_key,
)

Settings.llm = llm
Settings.embed_model = embed_model
```

```python
import json
from llama_index.core import Document
from llama_index.core.schema import MetadataMode

# Convert the DataFrame to a JSON string representation
documents_json = dataset_df.to_json(orient="records")
# Load the JSON string into a Python list of dictionaries
documents_list = json.loads(documents_json)

llama_documents = []

for document in documents_list:
    # Value for metadata must be one of (str, int, float, None)
    document["name"] = json.dumps(document["name"])
    document["cuisine"] = json.dumps(document["cuisine"])
    document["attributes"] = json.dumps(document["attributes"])
    document["menu"] = json.dumps(document["menu"])
    document["borough"] = json.dumps(document["borough"])
    document["address"] = json.dumps(document["address"])
    document["PriceRange"] = json.dumps(document["PriceRange"])
    document["HappyHour"] = json.dumps(document["HappyHour"])
    document["review_count"] = json.dumps(document["review_count"])
    document["TakeOut"] = json.dumps(document["TakeOut"])
    # these two fields are not relevant to the question we want to answer,
    # so I will skip it for now
    del document["embedding"]
    del document["location"]

    # Create a Document object with the text and excluded metadata for llm and embedding models
    llama_document = Document(
        text=json.dumps(document),
        metadata=document,
        metadata_template="{key}=>{value}",
        text_template="Metadata: {metadata_str}\n-----\nContent: {content}",
    )

    llama_documents.append(llama_document)

# Observing an example of what the LLM and Embedding model receive as input
print(
    "\nThe LLM sees this: \n",
    llama_documents[0].get_content(metadata_mode=MetadataMode.LLM),
)
print(
    "\nThe Embedding model sees this: \n",
    llama_documents[0].get_content(metadata_mode=MetadataMode.EMBED),
)
```

    The LLM sees this:
     Metadata: restaurant_id=>40366661
    attributes=>{"Alcohol": "'none'", "Ambience": "{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}", "BYOB": null, "BestNights": null, "BikeParking": null, "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": null, "BusinessParking": "None", "Caters": "True", "DriveThru": null, "GoodForDancing": null, "GoodForKids": "True", "GoodForMeal": null, "HasTV": "True", "Music": null, "NoiseLevel": "'average'", "RestaurantsAttire": "'casual'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "True", "RestaurantsTableService": "False", "WheelchairAccessible": "True", "WiFi": "'free'"}
    cuisine=>"Tex-Mex"
    DogsAllowed=>None
    OutdoorSeating=>True
    borough=>"Manhattan"
    address=>{"building": "627", "coord": [-73.975981, 40.745132], "street": "2 Avenue", "zipcode": "10016"}
    _id=>{'$oid': '6095a34a7c34416a90d3206b'}
    name=>"Baby Bo'S Burritos"
    menu=>null
    TakeOut=>true
    PriceRange=>1.0
    HappyHour=>null
    review_count=>10
    sponsored=>None
    stars=>2.5
    -----
    Content: {"restaurant_id": "40366661", "attributes": "{\"Alcohol\": \"'none'\", \"Ambience\": \"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": null, \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"None\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": null, \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"'average'\", \"RestaurantsAttire\": \"'casual'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"False\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"'free'\"}", "cuisine": "\"Tex-Mex\"", "DogsAllowed": null, "OutdoorSeating": true, "borough": "\"Manhattan\"", "address": "{\"building\": \"627\", \"coord\": [-73.975981, 40.745132], \"street\": \"2 Avenue\", \"zipcode\": \"10016\"}", "_id": {"$oid": "6095a34a7c34416a90d3206b"}, "name": "\"Baby Bo'S Burritos\"", "menu": "null", "TakeOut": "true", "PriceRange": "1.0", "HappyHour": "null", "review_count": "10", "sponsored": null, "stars": 2.5}

    The Embedding model sees this:
     Metadata: restaurant_id=>40366661
    attributes=>{"Alcohol": "'none'", "Ambience": "{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}", "BYOB": null, "BestNights": null, "BikeParking": null, "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": null, "BusinessParking": "None", "Caters": "True", "DriveThru": null, "GoodForDancing": null, "GoodForKids": "True", "GoodForMeal": null, "HasTV": "True", "Music": null, "NoiseLevel": "'average'", "RestaurantsAttire": "'casual'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "True", "RestaurantsTableService": "False", "WheelchairAccessible": "True", "WiFi": "'free'"}
    cuisine=>"Tex-Mex"
    DogsAllowed=>None
    OutdoorSeating=>True
    borough=>"Manhattan"
    address=>{"building": "627", "coord": [-73.975981, 40.745132], "street": "2 Avenue", "zipcode": "10016"}
    _id=>{'$oid': '6095a34a7c34416a90d3206b'}
    name=>"Baby Bo'S Burritos"
    menu=>null
    TakeOut=>true
    PriceRange=>1.0
    HappyHour=>null
    review_count=>10
    sponsored=>None
    stars=>2.5
    -----
    Content: {"restaurant_id": "40366661", "attributes": "{\"Alcohol\": \"'none'\", \"Ambience\": \"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": null, \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"None\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": null, \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"'average'\", \"RestaurantsAttire\": \"'casual'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"False\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"'free'\"}", "cuisine": "\"Tex-Mex\"", "DogsAllowed": null, "OutdoorSeating": true, "borough": "\"Manhattan\"", "address": "{\"building\": \"627\", \"coord\": [-73.975981, 40.745132], \"street\": \"2 Avenue\", \"zipcode\": \"10016\"}", "_id": {"$oid": "6095a34a7c34416a90d3206b"}, "name": "\"Baby Bo'S Burritos\"", "menu": "null", "TakeOut": "true", "PriceRange": "1.0", "HappyHour": "null", "review_count": "10", "sponsored": null, "stars": 2.5}

```python
llama_documents[0]
```

    Document(id_='93d3f08d-85f3-494d-a057-19bc834abc29', embedding=None, metadata={'restaurant_id': '40366661', 'attributes': '{"Alcohol": "\'none\'", "Ambience": "{\'romantic\': False, \'intimate\': False, \'classy\': False, \'hipster\': False, \'divey\': False, \'touristy\': False, \'trendy\': False, \'upscale\': False, \'casual\': False}", "BYOB": null, "BestNights": null, "BikeParking": null, "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": null, "BusinessParking": "None", "Caters": "True", "DriveThru": null, "GoodForDancing": null, "GoodForKids": "True", "GoodForMeal": null, "HasTV": "True", "Music": null, "NoiseLevel": "\'average\'", "RestaurantsAttire": "\'casual\'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "True", "RestaurantsTableService": "False", "WheelchairAccessible": "True", "WiFi": "\'free\'"}', 'cuisine': '"Tex-Mex"', 'DogsAllowed': None, 'OutdoorSeating': True, 'borough': '"Manhattan"', 'address': '{"building": "627", "coord": [-73.975981, 40.745132], "street": "2 Avenue", "zipcode": "10016"}', '_id': {'$oid': '6095a34a7c34416a90d3206b'}, 'name': '"Baby Bo\'S Burritos"', 'menu': 'null', 'TakeOut': 'true', 'PriceRange': '1.0', 'HappyHour': 'null', 'review_count': '10', 'sponsored': None, 'stars': 2.5}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, text='{"restaurant_id": "40366661", "attributes": "{\\"Alcohol\\": \\"\'none\'\\", \\"Ambience\\": \\"{\'romantic\': False, \'intimate\': False, \'classy\': False, \'hipster\': False, \'divey\': False, \'touristy\': False, \'trendy\': False, \'upscale\': False, \'casual\': False}\\", \\"BYOB\\": null, \\"BestNights\\": null, \\"BikeParking\\": null, \\"BusinessAcceptsBitcoin\\": null, \\"BusinessAcceptsCreditCards\\": null, \\"BusinessParking\\": \\"None\\", \\"Caters\\": \\"True\\", \\"DriveThru\\": null, \\"GoodForDancing\\": null, \\"GoodForKids\\": \\"True\\", \\"GoodForMeal\\": null, \\"HasTV\\": \\"True\\", \\"Music\\": null, \\"NoiseLevel\\": \\"\'average\'\\", \\"RestaurantsAttire\\": \\"\'casual\'\\", \\"RestaurantsDelivery\\": \\"True\\", \\"RestaurantsGoodForGroups\\": \\"True\\", \\"RestaurantsReservations\\": \\"True\\", \\"RestaurantsTableService\\": \\"False\\", \\"WheelchairAccessible\\": \\"True\\", \\"WiFi\\": \\"\'free\'\\"}", "cuisine": "\\"Tex-Mex\\"", "DogsAllowed": null, "OutdoorSeating": true, "borough": "\\"Manhattan\\"", "address": "{\\"building\\": \\"627\\", \\"coord\\": [-73.975981, 40.745132], \\"street\\": \\"2 Avenue\\", \\"zipcode\\": \\"10016\\"}", "_id": {"$oid": "6095a34a7c34416a90d3206b"}, "name": "\\"Baby Bo\'S Burritos\\"", "menu": "null", "TakeOut": "true", "PriceRange": "1.0", "HappyHour": "null", "review_count": "10", "sponsored": null, "stars": 2.5}', start_char_idx=None, end_char_idx=None, text_template='Metadata: {metadata_str}\n-----\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\n')

```python
from llama_index.core.node_parser import SentenceSplitter

parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(llama_documents)
# 25k nodes takes about 10 minutes, will trim it down to 2.5k
new_nodes = nodes[:2500]

# There are 25k documents, so we need to do batching. Fortunately LlamaIndex provides good batching
# for embedding models, and we are going to rely on the __call__ method for the model to handle this
node_embeddings = embed_model(new_nodes)
```

```python
for idx, n in enumerate(new_nodes):
    n.embedding = node_embeddings[idx].embedding
    if "_id" in n.metadata:
        del n.metadata["_id"]
```

Ensure your databse, collection and vector store index is setup on MongoDB Atlas for the collection or the following step won't work appropriately on MongoDB.

 - For assistance with database cluster setup and obtaining the URI, refer to this [guide](https://www.mongodb.com/docs/guides/atlas/cluster/) for setting up a MongoDB cluster, and this [guide](https://www.mongodb.com/docs/guides/atlas/connection-string/) to get your connection string.

 - Once you have successfully created a cluster, create the database and collection within the MongoDB Atlas cluster by clicking “+ Create Database”. The database will be named movies, and the collection will be named movies_records.

 - Creating a vector search index within the movies_records collection is essential for efficient document retrieval from MongoDB into our development environment. To achieve this, refer to the official [guide](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/) on vector search index creation.

```python
import pymongo

def get_mongo_client(mongo_uri):
    """Establish connection to the MongoDB."""
    try:
        client = pymongo.MongoClient(mongo_uri)
        print("Connection to MongoDB successful")
        return client
    except pymongo.errors.ConnectionFailure as e:
        print(f"Connection failed: {e}")
        return None

# set up Fireworks.ai Key
import os
import getpass

mongo_uri = getpass.getpass("MONGO_URI:")
if not mongo_uri:
    print("MONGO_URI not set")

mongo_client = get_mongo_client(mongo_uri)

DB_NAME = "whatscooking"
COLLECTION_NAME = "restaurants"

db = mongo_client[DB_NAME]
collection = db[COLLECTION_NAME]
```

    Connection to MongoDB successful

```python
# To ensure we are working with a fresh collection
# delete any existing records in the collection
collection.delete_many({})
```

    DeleteResult({'n': 0, 'electionId': ObjectId('7fffffff00000000000001ce'), 'opTime': {'ts': Timestamp(1708970193, 3), 't': 462}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1708970193, 3), 'signature': {'hash': b'\x9a3H8\xa1\x1b\xb6\xbb\xa9\xc3x\x17\x1c\xeb\xe9\x03\xaa\xf8\xf17', 'keyId': 7294687148333072386}}, 'operationTime': Timestamp(1708970193, 3)}, acknowledged=True)

```python
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch

vector_store = MongoDBAtlasVectorSearch(
    mongo_client,
    db_name=DB_NAME,
    collection_name=COLLECTION_NAME,
    index_name="vector_index",
)
vector_store.add(new_nodes)
```

# now make sure you create the search index with the right name here

```python
from llama_index.core import VectorStoreIndex, StorageContext

index = VectorStoreIndex.from_vector_store(vector_store)
```

```python
%pip install -q matplotlib
```

    Note: you may need to restart the kernel to use updated packages.

```python
import pprint
from llama_index.core.response.notebook_utils import display_response

query_engine = index.as_query_engine()

query = "search query: Anything that doesn't have alcohol in it"

response = query_engine.query(query)
display_response(response)
pprint.pprint(response.source_nodes)
```

**`Final Response:`** Based on the context provided, two restaurant options that don't serve alcohol are:

1. "Academy Restauraunt" in Brooklyn, which serves American cuisine and has a variety of dishes such as Mozzarella sticks, Cheeseburger, Baked potato, Breadsticks, Caesar salad, Chicken parmesan, Pigs in a blanket, Chicken soup, Mac & cheese, Mushroom swiss burger, Spaghetti with meatballs, and Mashed potatoes.

2. "Gabriel'S Bar & Grill" in Manhattan, which specializes in Italian cuisine and offers dishes like Cheese Ravioli, Neapolitan Pizza, assorted gelato, Vegetarian Baked Ziti, Vegetarian Broccoli Pizza, Lasagna, Buca Trio Platter, Spinach Ravioli, Pasta with ricotta cheese, Spaghetti, Fried calamari, and Alfredo Pizza.

Both restaurants offer outdoor seating, are kid-friendly, and have a casual dress code. They also provide take-out service and have happy hour promotions.

    [NodeWithScore(node=TextNode(id_='5405e68c-19f2-4a65-95d7-f880fa6a8deb', embedding=None, metadata={'restaurant_id': '40385767', 'attributes': '{"Alcohol": "u\'beer_and_wine\'", "Ambience": "{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': None, \'upscale\': False, \'classy\': False, \'casual\': True}", "BYOB": null, "BestNights": "{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': True}", "BikeParking": "True", "BusinessAcceptsBitcoin": "False", "BusinessAcceptsCreditCards": "True", "BusinessParking": "{\'garage\': False, \'street\': False, \'validated\': False, \'lot\': True, \'valet\': False}", "Caters": "True", "DriveThru": null, "GoodForDancing": "False", "GoodForKids": "True", "GoodForMeal": "{\'dessert\': False, \'latenight\': False, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}", "HasTV": "True", "Music": "{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}", "NoiseLevel": "u\'average\'", "RestaurantsAttire": "u\'casual\'", "RestaurantsDelivery": "None", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "True", "RestaurantsTableService": "True", "WheelchairAccessible": "True", "WiFi": "u\'free\'"}', 'cuisine': '"American"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '"Brooklyn"', 'address': '{"building": "69", "coord": [-73.9757464, 40.687295], "street": "Lafayette Avenue", "zipcode": "11217"}', 'name': '"Academy Restauraunt"', 'menu': '["Mozzarella sticks", "Cheeseburger", "Baked potato", "Breadsticks", "Caesar salad", "Chicken parmesan", "Pigs in a blanket", "Chicken soup", "Mac & cheese", "Mushroom swiss burger", "Spaghetti with meatballs", "Mashed potatoes"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '173', 'sponsored': None, 'stars': 4.5}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='bbfc4bf5-d9c3-4f3b-8c1f-ddcf94f3b5df', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'restaurant_id': '40385767', 'attributes': '{"Alcohol": "u\'beer_and_wine\'", "Ambience": "{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': None, \'upscale\': False, \'classy\': False, \'casual\': True}", "BYOB": null, "BestNights": "{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': True}", "BikeParking": "True", "BusinessAcceptsBitcoin": "False", "BusinessAcceptsCreditCards": "True", "BusinessParking": "{\'garage\': False, \'street\': False, \'validated\': False, \'lot\': True, \'valet\': False}", "Caters": "True", "DriveThru": null, "GoodForDancing": "False", "GoodForKids": "True", "GoodForMeal": "{\'dessert\': False, \'latenight\': False, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}", "HasTV": "True", "Music": "{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}", "NoiseLevel": "u\'average\'", "RestaurantsAttire": "u\'casual\'", "RestaurantsDelivery": "None", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "True", "RestaurantsTableService": "True", "WheelchairAccessible": "True", "WiFi": "u\'free\'"}', 'cuisine': '"American"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '"Brooklyn"', 'address': '{"building": "69", "coord": [-73.9757464, 40.687295], "street": "Lafayette Avenue", "zipcode": "11217"}', '_id': {'$oid': '6095a34a7c34416a90d322d1'}, 'name': '"Academy Restauraunt"', 'menu': '["Mozzarella sticks", "Cheeseburger", "Baked potato", "Breadsticks", "Caesar salad", "Chicken parmesan", "Pigs in a blanket", "Chicken soup", "Mac & cheese", "Mushroom swiss burger", "Spaghetti with meatballs", "Mashed potatoes"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '173', 'sponsored': None, 'stars': 4.5}, hash='df7870b3103572b05e98091e4d4b52b238175eb08558831b621b6832c0472c2e'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='5fbb14fe-c8a8-4c4c-930d-2e07e4f77b47', node_type=<ObjectType.TEXT: '1'>, metadata={'restaurant_id': '40377111', 'attributes': '{"Alcohol": null, "Ambience": null, "BYOB": null, "BestNights": null, "BikeParking": "True", "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": "False", "BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}", "Caters": null, "DriveThru": "True", "GoodForDancing": null, "GoodForKids": null, "GoodForMeal": null, "HasTV": null, "Music": null, "NoiseLevel": null, "RestaurantsAttire": null, "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": null, "RestaurantsReservations": null, "RestaurantsTableService": null, "WheelchairAccessible": null, "WiFi": null}', 'cuisine': '"American"', 'DogsAllowed': None, 'OutdoorSeating': None, 'borough': '"Manhattan"', 'address': '{"building": "1207", "coord": [-73.9592644, 40.8088612], "street": "Amsterdam Avenue", "zipcode": "10027"}', '_id': {'$oid': '6095a34a7c34416a90d321d6'}, 'name': '"Amsterdam Restaurant & Tapas Lounge"', 'menu': '["Green salad", "Cheddar Biscuits", "Lasagna", "Chicken parmesan", "Chicken soup", "Pigs in a blanket", "Caesar salad", "French fries", "Baked potato", "Mushroom swiss burger", "Grilled cheese sandwich", "Fried chicken"]', 'TakeOut': 'true', 'PriceRange': '1.0', 'HappyHour': 'null', 'review_count': '6', 'sponsored': None, 'stars': 5.0}, hash='1261332dd67be495d0639f41b5f6462f87a41aabe20367502ef28074bf13e561'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='10ad1a23-3237-4b68-808d-58fd7b7e5cb6', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='bc64dca2f9210693c3d5174aec305f25b68d080be65a0ae52f9a560f99992bb0')}, text='{"restaurant_id": "40385767", "attributes": "{\\"Alcohol\\": \\"u\'beer_and_wine\'\\", \\"Ambience\\": \\"{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': None, \'upscale\': False, \'classy\': False, \'casual\': True}\\", \\"BYOB\\": null, \\"BestNights\\": \\"{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': True}\\", \\"BikeParking\\": \\"True\\", \\"BusinessAcceptsBitcoin\\": \\"False\\", \\"BusinessAcceptsCreditCards\\": \\"True\\", \\"BusinessParking\\": \\"{\'garage\': False, \'street\': False, \'validated\': False, \'lot\': True, \'valet\': False}\\", \\"Caters\\": \\"True\\", \\"DriveThru\\": null, \\"GoodForDancing\\": \\"False\\", \\"GoodForKids\\": \\"True\\", \\"GoodForMeal\\": \\"{\'dessert\': False, \'latenight\': False, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}\\", \\"HasTV\\": \\"True\\", \\"Music\\": \\"{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}\\", \\"NoiseLevel\\": \\"u\'average\'\\", \\"RestaurantsAttire\\": \\"u\'casual\'\\", \\"RestaurantsDelivery\\": \\"None\\", \\"RestaurantsGoodForGroups\\": \\"True\\", \\"RestaurantsReservations\\": \\"True\\", \\"RestaurantsTableService\\": \\"True\\", \\"WheelchairAccessible\\": \\"True\\", \\"WiFi\\": \\"u\'free\'\\"}", "cuisine": "\\"American\\"", "DogsAllowed": true, "OutdoorSeating": true, "borough": "\\"Brooklyn\\"",', start_char_idx=0, end_char_idx=1415, text_template='Metadata: {metadata_str}\n-----\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\n'), score=0.7296431064605713),
     NodeWithScore(node=TextNode(id_='9cd153ba-2ab8-40aa-90f0-9da5ae24c632', embedding=None, metadata={'restaurant_id': '40392690', 'attributes': '{"Alcohol": "u\'full_bar\'", "Ambience": "{\'touristy\': None, \'hipster\': True, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': True, \'upscale\': None, \'classy\': True, \'casual\': True}", "BYOB": "False", "BestNights": "{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': False}", "BikeParking": "True", "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": "True", "BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}", "Caters": "True", "DriveThru": "False", "GoodForDancing": "False", "GoodForKids": "True", "GoodForMeal": "{\'dessert\': None, \'latenight\': None, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}", "HasTV": "False", "Music": "{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}", "NoiseLevel": "u\'average\'", "RestaurantsAttire": "\'casual\'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "False", "RestaurantsTableService": "True", "WheelchairAccessible": "True", "WiFi": "\'free\'"}', 'cuisine': '"Italian"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '"Manhattan"', 'address': '{"building": "11", "coord": [-73.9828696, 40.7693649], "street": "West   60 Street", "zipcode": "10023"}', 'name': '"Gabriel\'S Bar & Grill"', 'menu': '["Cheese Ravioli", "Neapolitan Pizza", "assorted gelato", "Vegetarian Baked Ziti", "Vegetarian Broccoli Pizza", "Lasagna", "Buca Trio Platter", "Spinach Ravioli", "Pasta with ricotta cheese", "Spaghetti", "Fried calimari", "Alfredo Pizza"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '333', 'sponsored': None, 'stars': 4.0}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='77584933-8286-4277-bc56-bed76adcfd37', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'restaurant_id': '40392690', 'attributes': '{"Alcohol": "u\'full_bar\'", "Ambience": "{\'touristy\': None, \'hipster\': True, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': True, \'upscale\': None, \'classy\': True, \'casual\': True}", "BYOB": "False", "BestNights": "{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': False}", "BikeParking": "True", "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": "True", "BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}", "Caters": "True", "DriveThru": "False", "GoodForDancing": "False", "GoodForKids": "True", "GoodForMeal": "{\'dessert\': None, \'latenight\': None, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}", "HasTV": "False", "Music": "{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}", "NoiseLevel": "u\'average\'", "RestaurantsAttire": "\'casual\'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "True", "RestaurantsReservations": "False", "RestaurantsTableService": "True", "WheelchairAccessible": "True", "WiFi": "\'free\'"}', 'cuisine': '"Italian"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '"Manhattan"', 'address': '{"building": "11", "coord": [-73.9828696, 40.7693649], "street": "West   60 Street", "zipcode": "10023"}', '_id': {'$oid': '6095a34b7c34416a90d3243a'}, 'name': '"Gabriel\'S Bar & Grill"', 'menu': '["Cheese Ravioli", "Neapolitan Pizza", "assorted gelato", "Vegetarian Baked Ziti", "Vegetarian Broccoli Pizza", "Lasagna", "Buca Trio Platter", "Spinach Ravioli", "Pasta with ricotta cheese", "Spaghetti", "Fried calimari", "Alfredo Pizza"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '333', 'sponsored': None, 'stars': 4.0}, hash='c4dcc57a697cd2fe3047a280573c0f54bc5236e1d5af2228737af77613c9dbf7'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='6e1ead27-3679-48fb-b160-b47db523a3ce', node_type=<ObjectType.TEXT: '1'>, metadata={'restaurant_id': '40392496', 'attributes': '{"Alcohol": "u\'none\'", "Ambience": "{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'intimate\': None, \'trendy\': False, \'upscale\': False, \'classy\': False, \'casual\': True}", "BYOB": null, "BestNights": null, "BikeParking": "True", "BusinessAcceptsBitcoin": null, "BusinessAcceptsCreditCards": null, "BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}", "Caters": "False", "DriveThru": null, "GoodForDancing": null, "GoodForKids": "True", "GoodForMeal": "{\'dessert\': False, \'latenight\': False, \'lunch\': True, \'dinner\': True, \'brunch\': None, \'breakfast\': False}", "HasTV": "True", "Music": null, "NoiseLevel": "u\'average\'", "RestaurantsAttire": "u\'casual\'", "RestaurantsDelivery": "True", "RestaurantsGoodForGroups": "False", "RestaurantsReservations": "False", "RestaurantsTableService": "True", "WheelchairAccessible": null, "WiFi": "\'free\'"}', 'cuisine': '"English"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '"Manhattan"', 'address': '{"building": "253", "coord": [-74.0034571, 40.736351], "street": "West   11 Street", "zipcode": "10014"}', '_id': {'$oid': '6095a34b7c34416a90d32435'}, 'name': '"Tartine"', 'menu': 'null', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '436', 'sponsored': None, 'stars': 4.5}, hash='146bffad5c816926ec1008d966caab7c0df675251ccca5de860f8a2160bb7a34'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='6640911b-3d8e-4bad-a016-4c3d91444b0c', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='39984a7534d6755344f0887e0d6a200eaab562a7dc492afe292040c0022282bd')}, text='{"restaurant_id": "40392690", "attributes": "{\\"Alcohol\\": \\"u\'full_bar\'\\", \\"Ambience\\": \\"{\'touristy\': None, \'hipster\': True, \'romantic\': False, \'divey\': False, \'intimate\': None, \'trendy\': True, \'upscale\': None, \'classy\': True, \'casual\': True}\\", \\"BYOB\\": \\"False\\", \\"BestNights\\": \\"{\'monday\': False, \'tuesday\': False, \'friday\': True, \'wednesday\': False, \'thursday\': False, \'sunday\': False, \'saturday\': False}\\", \\"BikeParking\\": \\"True\\", \\"BusinessAcceptsBitcoin\\": null, \\"BusinessAcceptsCreditCards\\": \\"True\\", \\"BusinessParking\\": \\"{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}\\", \\"Caters\\": \\"True\\", \\"DriveThru\\": \\"False\\", \\"GoodForDancing\\": \\"False\\", \\"GoodForKids\\": \\"True\\", \\"GoodForMeal\\": \\"{\'dessert\': None, \'latenight\': None, \'lunch\': True, \'dinner\': True, \'brunch\': False, \'breakfast\': False}\\", \\"HasTV\\": \\"False\\", \\"Music\\": \\"{\'dj\': False, \'background_music\': False, \'no_music\': False, \'jukebox\': False, \'live\': False, \'video\': False, \'karaoke\': False}\\", \\"NoiseLevel\\": \\"u\'average\'\\", \\"RestaurantsAttire\\": \\"\'casual\'\\", \\"RestaurantsDelivery\\": \\"True\\", \\"RestaurantsGoodForGroups\\": \\"True\\", \\"RestaurantsReservations\\": \\"False\\", \\"RestaurantsTableService\\": \\"True\\", \\"WheelchairAccessible\\": \\"True\\", \\"WiFi\\": \\"\'free\'\\"}", "cuisine": "\\"Italian\\"", "DogsAllowed": true, "OutdoorSeating": true,', start_char_idx=0, end_char_idx=1382, text_template='Metadata: {metadata_str}\n-----\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\n'), score=0.7284677028656006)]


# Amazon Neptune - Neptune Analytics vector store

If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

```python
%pip install llama-index-vector-stores-neptune
```

## Initiate Neptune Analytics vector wrapper

```python
from llama_index.vector_stores.neptune import NeptuneAnalyticsVectorStore

graph_identifier = ""
embed_dim = 1536

neptune_vector_store = NeptuneAnalyticsVectorStore(
    graph_identifier=graph_identifier, embedding_dimension=1536
)
```

## Load documents, build the VectorStoreIndex

```python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from IPython.display import Markdown, display
```

Download Data

```python
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
```

```python
# load documents
documents = SimpleDirectoryReader("./data/paul_graham").load_data()
```

```python
from llama_index.core import StorageContext

storage_context = StorageContext.from_defaults(
    vector_store=neptune_vector_store
)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)
```

```python
query_engine = index.as_query_engine()
response = query_engine.query("What happened at interleaf?")
display(Markdown(f"<b>{response}</b>"))
```


Simple Vector Store

# Simple Vector Store

If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

```python
!pip install llama-index
```

```python
import os
import openai

os.environ["OPENAI_API_KEY"] = "sk-..."
openai.api_key = os.environ["OPENAI_API_KEY"]
```

#### Load documents, build the VectorStoreIndex

```python
import nltk

nltk.download("stopwords")
```

    [nltk_data] Downloading package stopwords to
    [nltk_data]     /Users/jerryliu/nltk_data...
    [nltk_data]   Package stopwords is already up-to-date!

    True

```python
import llama_index.core
```

    [nltk_data] Downloading package stopwords to /Users/jerryliu/Programmi
    [nltk_data]     ng/gpt_index/.venv/lib/python3.10/site-
    [nltk_data]     packages/llama_index/core/_static/nltk_cache...
    [nltk_data]   Unzipping corpora/stopwords.zip.
    [nltk_data] Downloading package punkt to /Users/jerryliu/Programming/g
    [nltk_data]     pt_index/.venv/lib/python3.10/site-
    [nltk_data]     packages/llama_index/core/_static/nltk_cache...
    [nltk_data]   Unzipping tokenizers/punkt.zip.

```python
import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    load_index_from_storage,
    StorageContext,
)
from IPython.display import Markdown, display
```

Download Data

```python
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
```

    --2024-02-12 13:21:13--  https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt
    Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...
    Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 75042 (73K) [text/plain]
    Saving to: ‘data/paul_graham/paul_graham_essay.txt’

    data/paul_graham/pa 100%[===================>]  73.28K  --.-KB/s    in 0.02s

    2024-02-12 13:21:13 (4.76 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]

```python
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
```

```python
index = VectorStoreIndex.from_documents(documents)
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

```python
# save index to disk
index.set_index_id("vector_index")
index.storage_context.persist("./storage")
```

```python
# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="storage")
# load index
index = load_index_from_storage(storage_context, index_id="vector_index")
```

    INFO:llama_index.core.indices.loading:Loading indices with ids: ['vector_index']
    Loading indices with ids: ['vector_index']

#### Query Index

```python
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(response_mode="tree_summarize")
response = query_engine.query("What did the author do growing up?")
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"

```python
display(Markdown(f"<b>{response}</b>"))
```

The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They later transitioned to working with microcomputers, starting with a kit-built microcomputer and eventually acquiring a TRS-80. They wrote simple games, a program to predict rocket heights, and even a word processor. Although the author initially planned to study philosophy in college, they eventually switched to studying AI.

**Query Index with SVM/Linear Regression**

Use Karpathy's [SVM-based](https://twitter.com/karpathy/status/1647025230546886658?s=20) approach. Set query as positive example, all other datapoints as negative examples, and then fit a hyperplane.

```python
query_modes = [
    "svm",
    "linear_regression",
    "logistic_regression",
]
for query_mode in query_modes:
    # set Logging to DEBUG for more detailed outputs
    query_engine = index.as_query_engine(vector_store_query_mode=query_mode)
    response = query_engine.query("What did the author do growing up?")
    print(f"Query mode: {query_mode}")
    display(Markdown(f"<b>{response}</b>"))
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

    /Users/jerryliu/Programming/gpt_index/.venv/lib/python3.10/site-packages/sklearn/svm/_classes.py:31: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning.
      warnings.warn(

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    Query mode: svm

The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They later got a microcomputer and started programming on it, writing simple games and a word processor. They initially planned to study philosophy in college but ended up switching to AI.

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

    /Users/jerryliu/Programming/gpt_index/.venv/lib/python3.10/site-packages/sklearn/svm/_classes.py:31: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning.
      warnings.warn(

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    Query mode: linear_regression

The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They later got a microcomputer and started programming on it, writing simple games and a word processor. They initially planned to study philosophy in college but ended up switching to AI.

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

    /Users/jerryliu/Programming/gpt_index/.venv/lib/python3.10/site-packages/sklearn/svm/_classes.py:31: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning.
      warnings.warn(

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    Query mode: logistic_regression

The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They later got a microcomputer and started programming on it, writing simple games and a word processor. They initially planned to study philosophy in college but eventually switched to AI.

```python
display(Markdown(f"<b>{response}</b>"))
```

The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They later got a microcomputer and started programming on it, writing simple games and a word processor. They initially planned to study philosophy in college but eventually switched to AI.

```python
print(response.source_nodes[0].text)
```

    What I Worked On

    February 2021

    Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.

    The first programs I tried writing were on the IBM 1401 that our school district used for what was then called "data processing." This was in 9th grade, so I was 13 or 14. The school district's 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain's lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.

    The language we used was an early version of Fortran. You had to type programs on punch cards, then stack them in the card reader and press a button to load the program into memory and run it. The result would ordinarily be to print something on the spectacularly loud printer.

    I was puzzled by the 1401. I couldn't figure out what to do with it. And in retrospect there's not much I could have done with it. The only form of input to programs was data stored on punched cards, and I didn't have any data stored on punched cards. The only other option was to do things that didn't rely on any input, like calculate approximations of pi, but I didn't know enough math to do anything interesting of that type. So I'm not surprised I can't remember any programs I wrote, because they can't have done much. My clearest memory is of the moment I learned it was possible for programs not to terminate, when one of mine didn't. On a machine without time-sharing, this was a social as well as a technical error, as the data center manager's expression made clear.

    With microcomputers, everything changed. Now you could have a computer sitting right in front of you, on a desk, that could respond to your keystrokes as it was running instead of just churning through a stack of punch cards and then stopping. [1]

    The first of my friends to get a microcomputer built it himself. It was sold as a kit by Heathkit. I remember vividly how impressed and envious I felt watching him sitting in front of it, typing programs right into the computer.

    Computers were expensive in those days and it took me years of nagging before I convinced my father to buy one, a TRS-80, in about 1980. The gold standard then was the Apple II, but a TRS-80 was good enough. This was when I really started programming. I wrote simple games, a program to predict how high my model rockets would fly, and a word processor that my father used to write at least one book. There was only room in memory for about 2 pages of text, so he'd write 2 pages at a time and then print them out, but it was a lot better than a typewriter.

    Though I liked programming, I didn't plan to study it in college. In college I was going to study philosophy, which sounded much more powerful. It seemed, to my naive high school self, to be the study of the ultimate truths, compared to which the things studied in other fields would be mere domain knowledge. What I discovered when I got to college was that the other fields took up so much of the space of ideas that there wasn't much left for these supposed ultimate truths. All that seemed left for philosophy were edge cases that people in other fields felt could safely be ignored.

    I couldn't have put this into words when I was 18. All I knew at the time was that I kept taking philosophy courses and they kept being boring. So I decided to switch to AI.

    AI was in the air in the mid 1980s, but there were two things especially that made me want to work on it: a novel by Heinlein called The Moon is a Harsh Mistress, which featured an intelligent computer called Mike, and a PBS documentary that showed Terry Winograd using SHRDLU. I haven't tried rereading The Moon is a Harsh Mistress, so I don't know how well it has aged, but when I read it I was drawn entirely into its world. It seemed only a matter of time before we'd have Mike, and when I saw Winograd using SHRDLU, it seemed like that time would be a few years at most.

**Query Index with custom embedding string**

```python
from llama_index.core import QueryBundle
```

```python
query_bundle = QueryBundle(
    query_str="What did the author do growing up?",
    custom_embedding_strs=["The author grew up painting."],
)
query_engine = index.as_query_engine()
response = query_engine.query(query_bundle)
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"

```python
display(Markdown(f"<b>{response}</b>"))
```

The context does not provide information about what the author did growing up.

**Use maximum marginal relevance**

Instead of ranking vectors purely by similarity, adds diversity to the documents by penalizing documents similar to ones that have already been found based on MMR . A lower mmr_treshold increases diversity.

```python
query_engine = index.as_query_engine(
    vector_store_query_mode="mmr", vector_store_kwargs={"mmr_threshold": 0.2}
)
response = query_engine.query("What did the author do growing up?")
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"

#### Get Sources

```python
print(response.get_formatted_sources())
```

    > Source (Doc id: c4118521-8f55-4a4d-819a-2db546b6491e): What I Worked On

    February 2021

    Before college the two main things I worked on, outside of schoo...

    > Source (Doc id: 74f77233-e4fe-4389-9820-76dd9f765af6): Which meant being easy to use and inexpensive. It was lucky for us that we were poor, because tha...

#### Query Index with Filters

We can also filter our queries using metadata

```python
from llama_index.core import Document

doc = Document(text="target", metadata={"tag": "target"})

index.insert(doc)
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

```python
from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters

filters = MetadataFilters(
    filters=[ExactMatchFilter(key="tag", value="target")]
)

retriever = index.as_retriever(
    similarity_top_k=20,
    filters=filters,
)

source_nodes = retriever.retrieve("What did the author do growing up?")
```

    INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
    HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"

```python
# retrieves only our target node, even though we set the top k to 20
print(len(source_nodes))
```

    1

```python
print(source_nodes[0].text)
print(source_nodes[0].metadata)
```

    target
    {'tag': 'target'}