Skip to content

Commit

Permalink
Init Chroma and LLamaIndex Multi-Modal Demo (run-llama#8897)
Browse files Browse the repository at this point in the history
* Init Chroma and LLamaIndex Multi-Modal Demo

* cr

* Create fleet_libraries_context.md (run-llama#8849)

* cr

* cr

* linting

---------

Co-authored-by: haotian zhang <haotianzhang@haotians-MacBook-Pro-3.local>
Co-authored-by: Andrew Zhou <44193474+adrwz@users.noreply.github.com>
Co-authored-by: Logan <logan.markewich@live.com>
  • Loading branch information
4 people authored and Ryan Peach committed Feb 7, 2024
1 parent 98a024b commit 55a45a8
Show file tree
Hide file tree
Showing 4 changed files with 563 additions and 30 deletions.
84 changes: 54 additions & 30 deletions docs/community/integrations/fleet_libraries_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ In this guide, we will be using Fleet Context to download the embeddings for Lla
<br><br>

## Pre-requisites
``` python
!pip install llama-index

```
!pip install llama-index
!pip install --upgrade fleet-context
```
``` python

```
import os
import openai
Expand All @@ -20,6 +22,7 @@ openai.api_key = os.environ["OPENAI_API_KEY"]
<br><br>

## Download Embeddings from Fleet Context

We will be using Fleet Context to download the embeddings for the
entirety of LlamaIndex\'s documentation (\~12k chunks, \~100mb of
content). You can download for any of the top 1220 libraries by
Expand All @@ -34,33 +37,36 @@ type (class/function/attribute/etc), the parent section, and more. You
can read more about this on their [Github
page](https://github.com/fleet-ai/context/tree/main).

``` python
```python
from context import download_embeddings

df = download_embeddings("llamaindex")
```

**Output**:

```shell
100%|██████████| 83.7M/83.7M [00:03<00:00, 27.4MiB/s]
id \
0 e268e2a1-9193-4e7b-bb9b-7a4cb88fc735
1 e495514b-1378-4696-aaf9-44af948de1a1
2 e804f616-7db0-4455-9a06-49dd275f3139
3 eb85c854-78f1-4116-ae08-53b2a2a9fa41
4 edfc116e-cf58-4118-bad4-c4bc0ca1495e
0 e268e2a1-9193-4e7b-bb9b-7a4cb88fc735
1 e495514b-1378-4696-aaf9-44af948de1a1
2 e804f616-7db0-4455-9a06-49dd275f3139
3 eb85c854-78f1-4116-ae08-53b2a2a9fa41
4 edfc116e-cf58-4118-bad4-c4bc0ca1495e
```

``` python
#Show some examples of the metadata
```python
# Show some examples of the metadata
df["metadata"][0]
display(Markdown(f"{df['metadata'][8000]['text']}"))
```

**Output**:

```shell
classmethod from_dict(data: Dict[str, Any], kwargs: Any) → Self classmethod from_json(data_str: str, kwargs: Any) → Self classmethod from_orm(obj: Any) → Model json(, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, models_as_dict: bool = True*, dumps_kwargs: Any) → unicode Generate a JSON representation of the model, include and exclude arguments as per dict().
```
<br><br>
## Create Pinecone Index for Hybrid Search in LlamaIndex
Expand All @@ -70,7 +76,7 @@ that we can do hybrid retrieval with both sparse vectors and dense
vectors. Make sure you have a [Pinecone account](https://pinecone.io)
before you proceed.
``` python
```python
import logging
import sys

Expand All @@ -79,27 +85,34 @@ logging.getLogger().handlers = []
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
```
``` python
```python
import pinecone

api_key = "..." # Add your Pinecone API key here
pinecone.init(api_key=api_key, environment='us-east-1-aws') # Add your db region here
api_key = "..." # Add your Pinecone API key here
pinecone.init(
api_key=api_key, environment="us-east-1-aws"
) # Add your db region here
```
``` python
```python
# Fleet Context uses the text-embedding-ada-002 model from OpenAI with 1536 dimensions.

# NOTE: Pinecone requires dotproduct similarity for hybrid search
pinecone.create_index(
"quickstart-fleet-context", dimension=1536, metric="dotproduct", pod_type="p1"
"quickstart-fleet-context",
dimension=1536,
metric="dotproduct",
pod_type="p1",
)

pinecone.describe_index("quickstart-fleet-context") # Make sure you create an index in pinecone
pinecone.describe_index(
"quickstart-fleet-context"
) # Make sure you create an index in pinecone
```
<br>
``` python
```python
from llama_index.vector_stores import PineconeVectorStore

pinecone_index = pinecone.Index("quickstart-fleet-context")
Expand All @@ -109,12 +122,14 @@ vector_store = PineconeVectorStore(pinecone_index, add_sparse_vector=True)
<br><br>
## Batch upsert vectors into Pinecone
Pinecone recommends upserting 100 vectors at a time. We\'re going to do that after we modify the format of the data a bit.
``` python
```python
import random
import itertools


def chunks(iterable, batch_size=100):
"""A helper function to break an iterable into chunks of size batch_size."""
it = iter(iterable)
Expand All @@ -123,16 +138,22 @@ def chunks(iterable, batch_size=100):
yield chunk
chunk = tuple(itertools.islice(it, batch_size))


# generator that generates many (id, vector, metadata, sparse_values) pairs
data_generator = map(lambda row: {'id': row[1]['id'],
'values': row[1]['values'],
'metadata': row[1]['metadata'],
'sparse_values': row[1]['sparse_values']}, df.iterrows())
data_generator = map(
lambda row: {
"id": row[1]["id"],
"values": row[1]["values"],
"metadata": row[1]["metadata"],
"sparse_values": row[1]["sparse_values"],
},
df.iterrows(),
)

# Upsert data with 1000 vectors per upsert request
for ids_vectors_chunk in chunks(data_generator, batch_size=100):
print(f"Upserting {len(ids_vectors_chunk)} vectors...")
pinecone_index.upsert(vectors=ids_vectors_chunk)
pinecone_index.upsert(vectors=ids_vectors_chunk)
```
<br><br>
Expand All @@ -142,29 +163,32 @@ for ids_vectors_chunk in chunks(data_generator, batch_size=100):
Finally, we\'re going to build the Pinecone vector store via LlamaIndex
and query it to get results.
``` python
```python
from llama_index import VectorStoreIndex
from IPython.display import Markdown, display
```
``` python
```python
index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
```
<br><br>
## Query Your Index!
``` python
query_engine = index.as_query_engine(vector_store_query_mode="hybrid", similarity_top_k=8)
```python
query_engine = index.as_query_engine(
vector_store_query_mode="hybrid", similarity_top_k=8
)
response = query_engine.query("How do I use llama_index SimpleDirectoryReader")
```
``` python
```python
display(Markdown(f"<b>{response}</b>"))
```
**Output**:
```shell
<b>To use the SimpleDirectoryReader in llama_index, you need to import it from the llama_index library. Once imported, you can create an instance of the SimpleDirectoryReader class by providing the directory path as an argument. Then, you can use the `load_data()` method on the SimpleDirectoryReader instance to load the documents from the specified directory.</b>
```
Loading

0 comments on commit 55a45a8

Please sign in to comment.