Skip to content
Snippets Groups Projects
Unverified Commit cb5e941a authored by Harsha S's avatar Harsha S Committed by GitHub
Browse files

Update QdrantClient usage in documentation (#12602)

parent 1eeff297
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:dd821a8d tags: %% Cell type:markdown id:dd821a8d tags:
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/vector_stores/QdrantIndexDemo.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a> <a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/vector_stores/QdrantIndexDemo.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
%% Cell type:markdown id:307804a3-c02b-4a57-ac0d-172c30ddc851 tags: %% Cell type:markdown id:307804a3-c02b-4a57-ac0d-172c30ddc851 tags:
# Qdrant Vector Store # Qdrant Vector Store
%% Cell type:markdown id:f7010b1d-d1bb-4f08-9309-a328bb4ea396 tags: %% Cell type:markdown id:f7010b1d-d1bb-4f08-9309-a328bb4ea396 tags:
#### Creating a Qdrant client #### Creating a Qdrant client
%% Cell type:code id:0dc39c0b tags: %% Cell type:code id:0dc39c0b tags:
``` python ``` python
%pip install llama-index-vector-stores-qdrant llama-index-readers-file llama-index-embeddings-fastembed llama-index-llms-openai %pip install llama-index-vector-stores-qdrant llama-index-readers-file llama-index-embeddings-fastembed llama-index-llms-openai
``` ```
%% Cell type:code id:d5527d3d tags: %% Cell type:code id:d5527d3d tags:
``` python ``` python
import logging import logging
import sys import sys
import os import os
import qdrant_client import qdrant_client
from IPython.display import Markdown, display from IPython.display import Markdown, display
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import StorageContext from llama_index.core import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.embeddings.fastembed import FastEmbedEmbedding from llama_index.embeddings.fastembed import FastEmbedEmbedding
from llama_index.core import Settings from llama_index.core import Settings
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5") Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5")
``` ```
%% Cell type:markdown id:07489add tags: %% Cell type:markdown id:07489add tags:
If running for the first, time, install the dependencies using: If running for the first, time, install the dependencies using:
``` ```
!pip install -U qdrant_client fastembed !pip install -U qdrant_client fastembed
``` ```
%% Cell type:markdown id:wGLkbqIm4XIe tags: %% Cell type:markdown id:wGLkbqIm4XIe tags:
Set your OpenAI key for authenticating the LLM Set your OpenAI key for authenticating the LLM
%% Cell type:markdown id:e199eb1b tags: %% Cell type:markdown id:e199eb1b tags:
Follow these set the OpenAI API key to the OPENAI_API_KEY environment variable - Follow these set the OpenAI API key to the OPENAI_API_KEY environment variable -
%% Cell type:markdown id:d5d27466 tags: %% Cell type:markdown id:d5d27466 tags:
1. Using Terminal 1. Using Terminal
%% Cell type:code id:f77cce7d tags: %% Cell type:code id:f77cce7d tags:
``` python ``` python
export OPENAI_API_KEY=your_api_key_here export OPENAI_API_KEY=your_api_key_here
``` ```
%% Cell type:markdown id:3a772e25 tags: %% Cell type:markdown id:3a772e25 tags:
2. Using IPython Magic Command in Jupyter Notebook 2. Using IPython Magic Command in Jupyter Notebook
%% Cell type:code id:uI25Wj6x4SrT tags: %% Cell type:code id:uI25Wj6x4SrT tags:
``` python ``` python
%env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> %env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
``` ```
%% Cell type:markdown id:e0d84caf tags: %% Cell type:markdown id:e0d84caf tags:
3. Using Python Script 3. Using Python Script
%% Cell type:code id:28329227 tags: %% Cell type:code id:28329227 tags:
``` python ``` python
import os import os
os.environ["OPENAI_API_KEY"] = "your_api_key_here" os.environ["OPENAI_API_KEY"] = "your_api_key_here"
``` ```
%% Cell type:markdown id:f8b6361e tags: %% Cell type:markdown id:f8b6361e tags:
Note: It's generally recommended to set sensitive information like API keys as environment variables rather than hardcoding them into scripts. Note: It's generally recommended to set sensitive information like API keys as environment variables rather than hardcoding them into scripts.
%% Cell type:code id:9c79df94 tags: %% Cell type:code id:9c79df94 tags:
``` python ``` python
logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
``` ```
%% Cell type:markdown id:c475d645 tags: %% Cell type:markdown id:c475d645 tags:
Download Data Download Data
%% Cell type:code id:0d3e939d tags: %% Cell type:code id:0d3e939d tags:
``` python ``` python
!mkdir -p 'data/paul_graham/' !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' !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'
``` ```
%% Cell type:markdown id:01c44da1 tags: %% Cell type:markdown id:01c44da1 tags:
#### Load the documents #### Load the documents
%% Cell type:code id:7cbe1384 tags: %% Cell type:code id:7cbe1384 tags:
``` python ``` python
# load documents # load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data() documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
``` ```
%% Cell type:markdown id:8ee4473a-094f-4d0a-a825-e1213db07240 tags: %% Cell type:markdown id:8ee4473a-094f-4d0a-a825-e1213db07240 tags:
#### Build the VectorStoreIndex #### Build the VectorStoreIndex
%% Cell type:code id:b873b936 tags: %% Cell type:code id:b873b936 tags:
``` python ``` python
client = qdrant_client.QdrantClient( client = qdrant_client.QdrantClient(
# you can use :memory: mode for fast and light-weight experiments, # you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere # it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1 # but requires qdrant-client >= 1.1.1
location=":memory:" # location=":memory:"
# otherwise set Qdrant instance address with: # otherwise set Qdrant instance address with:
# uri="http://<host>:<port>" # url="http://<host>:<port>"
# otherwise set Qdrant instance with host and port:
host="localhost",
port=6333
# set API KEY for Qdrant Cloud # set API KEY for Qdrant Cloud
# api_key="<qdrant-api-key>", # api_key="<qdrant-api-key>",
) )
``` ```
%% Cell type:code id:ba1558b3 tags: %% Cell type:code id:ba1558b3 tags:
``` python ``` python
vector_store = QdrantVectorStore(client=client, collection_name="paul_graham") vector_store = QdrantVectorStore(client=client, collection_name="paul_graham")
storage_context = StorageContext.from_defaults(vector_store=vector_store) storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents( index = VectorStoreIndex.from_documents(
documents, documents,
storage_context=storage_context, storage_context=storage_context,
) )
``` ```
%% Cell type:markdown id:36862545 tags: %% Cell type:markdown id:36862545 tags:
#### Query Index #### Query Index
%% Cell type:code id:7bf3e2b9 tags: %% Cell type:code id:7bf3e2b9 tags:
``` python ``` python
# set Logging to DEBUG for more detailed outputs # set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine() query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?") response = query_engine.query("What did the author do growing up?")
``` ```
%% Cell type:code id:77a864c7 tags: %% Cell type:code id:77a864c7 tags:
``` python ``` python
display(Markdown(f"<b>{response}</b>")) display(Markdown(f"<b>{response}</b>"))
``` ```
%% Output %% Output
<b>The author worked on writing and programming before college.</b> <b>The author worked on writing and programming before college.</b>
%% Cell type:code id:64e35c1e tags: %% Cell type:code id:64e35c1e tags:
``` python ``` python
# set Logging to DEBUG for more detailed outputs # set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine() query_engine = index.as_query_engine()
response = query_engine.query( response = query_engine.query(
"What did the author do after his time at Viaweb?" "What did the author do after his time at Viaweb?"
) )
``` ```
%% Cell type:code id:926b79da tags: %% Cell type:code id:926b79da tags:
``` python ``` python
display(Markdown(f"<b>{response}</b>")) display(Markdown(f"<b>{response}</b>"))
``` ```
%% Output %% Output
<b>The author arranged to do freelance work for a group that did projects for customers after his time at Viaweb.</b> <b>The author arranged to do freelance work for a group that did projects for customers after his time at Viaweb.</b>
%% Cell type:markdown id:7b4d27fc tags: %% Cell type:markdown id:7b4d27fc tags:
#### Build the VectorStoreIndex asynchronously #### Build the VectorStoreIndex asynchronously
%% Cell type:code id:08af428d tags: %% Cell type:code id:08af428d tags:
``` python ``` python
# To connect to the same event-loop, # To connect to the same event-loop,
# allows async events to run on notebook # allows async events to run on notebook
import nest_asyncio import nest_asyncio
nest_asyncio.apply() nest_asyncio.apply()
``` ```
%% Cell type:code id:13fe7e09 tags: %% Cell type:code id:13fe7e09 tags:
``` python ``` python
aclient = qdrant_client.AsyncQdrantClient( aclient = qdrant_client.AsyncQdrantClient(
# you can use :memory: mode for fast and light-weight experiments, # you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere # it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1 # but requires qdrant-client >= 1.1.1
location=":memory:" location=":memory:"
# otherwise set Qdrant instance address with: # otherwise set Qdrant instance address with:
# uri="http://<host>:<port>" # uri="http://<host>:<port>"
# set API KEY for Qdrant Cloud # set API KEY for Qdrant Cloud
# api_key="<qdrant-api-key>", # api_key="<qdrant-api-key>",
) )
``` ```
%% Cell type:code id:1918d705 tags: %% Cell type:code id:1918d705 tags:
``` python ``` python
vector_store = QdrantVectorStore( vector_store = QdrantVectorStore(
aclient=aclient, collection_name="paul_graham", prefer_grpc=True aclient=aclient, collection_name="paul_graham", prefer_grpc=True
) )
storage_context = StorageContext.from_defaults(vector_store=vector_store) storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents( index = VectorStoreIndex.from_documents(
documents, documents,
storage_context=storage_context, storage_context=storage_context,
use_async=True, use_async=True,
) )
``` ```
%% Cell type:markdown id:2d0401ab tags: %% Cell type:markdown id:2d0401ab tags:
#### Async Query Index #### Async Query Index
%% Cell type:code id:88af9cf2 tags: %% Cell type:code id:88af9cf2 tags:
``` python ``` python
query_engine = index.as_query_engine(use_async=True) query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery("What did the author do growing up?") response = await query_engine.aquery("What did the author do growing up?")
``` ```
%% Cell type:code id:c88ff475 tags: %% Cell type:code id:c88ff475 tags:
``` python ``` python
display(Markdown(f"<b>{response}</b>")) display(Markdown(f"<b>{response}</b>"))
``` ```
%% Output %% Output
<b>The author worked on writing short stories and programming, particularly on an IBM 1401 computer in 9th grade using an early version of Fortran. Later, the author transitioned to working on microcomputers, starting with a TRS-80 in about 1980, where they wrote simple games, programs, and a word processor.</b> <b>The author worked on writing short stories and programming, particularly on an IBM 1401 computer in 9th grade using an early version of Fortran. Later, the author transitioned to working on microcomputers, starting with a TRS-80 in about 1980, where they wrote simple games, programs, and a word processor.</b>
%% Cell type:code id:5e8f1146 tags: %% Cell type:code id:5e8f1146 tags:
``` python ``` python
# set Logging to DEBUG for more detailed outputs # set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(use_async=True) query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery( response = await query_engine.aquery(
"What did the author do after his time at Viaweb?" "What did the author do after his time at Viaweb?"
) )
``` ```
%% Cell type:code id:90e1fa0e tags: %% Cell type:code id:90e1fa0e tags:
``` python ``` python
display(Markdown(f"<b>{response}</b>")) display(Markdown(f"<b>{response}</b>"))
``` ```
%% Output %% Output
<b>The author went on to co-found Y Combinator after his time at Viaweb.</b> <b>The author went on to co-found Y Combinator after his time at Viaweb.</b>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment