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
Branches
Tags
No related merge requests found
%% 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>
%% Cell type:markdown id:307804a3-c02b-4a57-ac0d-172c30ddc851 tags:
# Qdrant Vector Store
%% Cell type:markdown id:f7010b1d-d1bb-4f08-9309-a328bb4ea396 tags:
#### Creating a Qdrant client
%% Cell type:code id:0dc39c0b tags:
``` python
%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:
``` python
import logging
import sys
import os
import qdrant_client
from IPython.display import Markdown, display
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.embeddings.fastembed import FastEmbedEmbedding
from llama_index.core import Settings
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5")
```
%% Cell type:markdown id:07489add tags:
If running for the first, time, install the dependencies using:
```
!pip install -U qdrant_client fastembed
```
%% Cell type:markdown id:wGLkbqIm4XIe tags:
Set your OpenAI key for authenticating the LLM
%% Cell type:markdown id:e199eb1b tags:
Follow these set the OpenAI API key to the OPENAI_API_KEY environment variable -
%% Cell type:markdown id:d5d27466 tags:
1. Using Terminal
%% Cell type:code id:f77cce7d tags:
``` python
export OPENAI_API_KEY=your_api_key_here
```
%% Cell type:markdown id:3a772e25 tags:
2. Using IPython Magic Command in Jupyter Notebook
%% Cell type:code id:uI25Wj6x4SrT tags:
``` python
%env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
```
%% Cell type:markdown id:e0d84caf tags:
3. Using Python Script
%% Cell type:code id:28329227 tags:
``` python
import os
os.environ["OPENAI_API_KEY"] = "your_api_key_here"
```
%% 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.
%% Cell type:code id:9c79df94 tags:
``` python
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
```
%% Cell type:markdown id:c475d645 tags:
Download Data
%% Cell type:code id:0d3e939d tags:
``` 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'
```
%% Cell type:markdown id:01c44da1 tags:
#### Load the documents
%% Cell type:code id:7cbe1384 tags:
``` python
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
```
%% Cell type:markdown id:8ee4473a-094f-4d0a-a825-e1213db07240 tags:
#### Build the VectorStoreIndex
%% Cell type:code id:b873b936 tags:
``` python
client = qdrant_client.QdrantClient(
# you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1
location=":memory:"
# location=":memory:"
# 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
# api_key="<qdrant-api-key>",
)
```
%% Cell type:code id:ba1558b3 tags:
``` python
vector_store = QdrantVectorStore(client=client, collection_name="paul_graham")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
)
```
%% Cell type:markdown id:36862545 tags:
#### Query Index
%% Cell type:code id:7bf3e2b9 tags:
``` python
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
```
%% Cell type:code id:77a864c7 tags:
``` python
display(Markdown(f"<b>{response}</b>"))
```
%% Output
<b>The author worked on writing and programming before college.</b>
%% Cell type:code id:64e35c1e tags:
``` python
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query(
"What did the author do after his time at Viaweb?"
)
```
%% Cell type:code id:926b79da tags:
``` python
display(Markdown(f"<b>{response}</b>"))
```
%% Output
<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:
#### Build the VectorStoreIndex asynchronously
%% Cell type:code id:08af428d tags:
``` python
# To connect to the same event-loop,
# allows async events to run on notebook
import nest_asyncio
nest_asyncio.apply()
```
%% Cell type:code id:13fe7e09 tags:
``` python
aclient = qdrant_client.AsyncQdrantClient(
# you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1
location=":memory:"
# otherwise set Qdrant instance address with:
# uri="http://<host>:<port>"
# set API KEY for Qdrant Cloud
# api_key="<qdrant-api-key>",
)
```
%% Cell type:code id:1918d705 tags:
``` python
vector_store = QdrantVectorStore(
aclient=aclient, collection_name="paul_graham", prefer_grpc=True
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
use_async=True,
)
```
%% Cell type:markdown id:2d0401ab tags:
#### Async Query Index
%% Cell type:code id:88af9cf2 tags:
``` python
query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery("What did the author do growing up?")
```
%% Cell type:code id:c88ff475 tags:
``` python
display(Markdown(f"<b>{response}</b>"))
```
%% 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>
%% Cell type:code id:5e8f1146 tags:
``` python
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery(
"What did the author do after his time at Viaweb?"
)
```
%% Cell type:code id:90e1fa0e tags:
``` python
display(Markdown(f"<b>{response}</b>"))
```
%% Output
<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.
Please register or to comment