Skip to content

Commit 47df3c4

Browse files
committed
Use the Qdrant vector db instead of the json file
1 parent c460ef4 commit 47df3c4

File tree

6 files changed

+72
-49
lines changed

6 files changed

+72
-49
lines changed

infra/aks.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ module "aks" {
3838
acr = azurerm_container_registry.acr.id
3939
}
4040

41-
network_policy = "azure"
42-
net_profile_dns_service_ip = "10.0.0.10"
43-
net_profile_service_cidr = "10.0.0.0/16"
41+
network_policy = "azure"
42+
net_profile_dns_service_ip = "10.0.0.10"
43+
net_profile_service_cidr = "10.0.0.0/16"
4444

4545
key_vault_secrets_provider_enabled = true
4646
secret_rotation_enabled = true

infra/identity.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resource azurerm_user_assigned_identity chatbot {
1+
resource "azurerm_user_assigned_identity" "chatbot" {
22
name = "chatbot"
33
resource_group_name = azurerm_resource_group.this.name
44
location = azurerm_resource_group.this.location

infra/installation_script.tftpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/bin/bash
22
az aks get-credentials --resource-group ${resourceGroup} --name ${aksName} --overwrite-existing
3+
4+
kubectl create namespace chatbot --dry-run=client -o yaml | kubectl apply -f -
5+
6+
helm repo add qdrant https://qdrant.github.io/qdrant-helm
7+
helm repo update
8+
helm upgrade --install --namespace chatbot --set image.tag=v1.11.3 chatbot qdrant/qdrant
9+
310
az acr build --registry ${registry} --image chatbot:latest ./../sample-application
411

512
az identity federated-credential create --name chatbotfederatedidentity --identity-name chatbot --resource-group ${resourceGroup} --issuer ${oidc_url} --subject system:serviceaccount:chatbot:chatbot

infra/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ variable "chat_model_version" {
1414
}
1515

1616
variable "scale_type" {
17-
type = string
17+
type = string
1818
description = "values: GlobalStandard, Standard"
19-
default = "GlobalStandard"
19+
default = "GlobalStandard"
2020
}

sample-application/chatbot.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
from langchain.chat_models import AzureChatOpenAI
2121
from langchain.embeddings import AzureOpenAIEmbeddings
2222
from azure.identity import ManagedIdentityCredential
23+
import qdrant_client
2324

2425
import streamlit as st
26+
27+
from llama_index.vector_stores.qdrant import QdrantVectorStore
2528
from llama_index.core import (
2629
SimpleDirectoryReader,
27-
GPTVectorStoreIndex,
30+
VectorStoreIndex,
2831
PromptHelper,
2932
ServiceContext,
3033
StorageContext,
31-
load_index_from_storage,
3234
Settings
33-
3435
)
3536

3637
from llama_index.llms.langchain import LangChainLLM
@@ -39,13 +40,26 @@
3940

4041
from dotenv import load_dotenv, dotenv_values
4142

42-
4343
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
4444
logging.getLogger("llama_index").setLevel(logging.DEBUG)
4545

4646
index = None
4747
doc_path = "./data/"
48-
index_file = "index.json"
48+
49+
client = qdrant_client.QdrantClient(
50+
# you can use :memory: mode for fast and light-weight experiments,
51+
# it does not require to have Qdrant deployed anywhere
52+
# but requires qdrant-client >= 1.1.1
53+
# location=":memory:"
54+
# otherwise set Qdrant instance address with:
55+
# url="http://<host>:<port>"
56+
# otherwise set Qdrant instance with host and port:
57+
host="chatbot-qdrant",
58+
port=6333
59+
# set API KEY for Qdrant Cloud
60+
# api_key="<qdrant-api-key>",
61+
)
62+
4963

5064
if "config" not in st.session_state:
5165
# Read the environment variables
@@ -119,47 +133,48 @@ def send_click():
119133
)
120134

121135
if uploaded_file and uploaded_file.name != st.session_state.current_file:
122-
# Ingest the document and create the index
123-
with st.spinner('Ingesting the file..'):
124-
doc_files = os.listdir(doc_path)
125-
for doc_file in doc_files:
126-
os.remove(doc_path + doc_file)
127-
128-
bytes_data = uploaded_file.read()
129-
with open(f"{doc_path}{uploaded_file.name}", "wb") as f:
130-
f.write(bytes_data)
131-
132-
loader = SimpleDirectoryReader(doc_path, recursive=True, exclude_hidden=True)
133-
documents = loader.load_data()
134-
sidebar_placeholder.header("Current Processing Document:")
135-
sidebar_placeholder.subheader(uploaded_file.name)
136-
sidebar_placeholder.write(documents[0].get_text()[:500] + "...")
137-
138-
index = GPTVectorStoreIndex.from_documents(
139-
documents, service_context=service_context
140-
)
141-
142-
index.set_index_id("vector_index")
143-
index.storage_context.persist(index_file)
144-
st.session_state.current_file = uploaded_file.name
145-
st.session_state.response = "" # clean up the response when new file is uploaded
146-
st.success('Done!')
147-
148-
elif os.path.exists(index_file):
149-
# Read from storage context
150-
storage_context = StorageContext.from_defaults(persist_dir=index_file)
151-
index = load_index_from_storage(
152-
storage_context, index_id="vector_index", service_context=service_context
136+
st.session_state.current_file = uploaded_file.name
137+
st.session_state.response = "" # clean up the response when new file is uploaded
138+
if not client.collection_exists(collection_name=uploaded_file.name):
139+
# Ingest the document and create the index
140+
with st.spinner('Ingesting the file..'):
141+
doc_files = os.listdir(doc_path)
142+
for doc_file in doc_files:
143+
os.remove(doc_path + doc_file)
144+
145+
bytes_data = uploaded_file.read()
146+
with open(f"{doc_path}{uploaded_file.name}", "wb") as f:
147+
f.write(bytes_data)
148+
149+
loader = SimpleDirectoryReader(doc_path, recursive=True, exclude_hidden=True)
150+
documents = loader.load_data()
151+
sidebar_placeholder.header("Current Processing Document:")
152+
sidebar_placeholder.subheader(uploaded_file.name)
153+
154+
vector_store = QdrantVectorStore(client=client, collection_name=uploaded_file.name)
155+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
156+
index = VectorStoreIndex.from_documents(
157+
documents,
158+
service_context=service_context,
159+
storage_context=storage_context,
160+
)
161+
index.set_index_id("vector_index")
162+
st.success('Done!')
163+
164+
if st.session_state.current_file:
165+
vector_store = QdrantVectorStore(client=client, collection_name=uploaded_file.name)
166+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
167+
index = VectorStoreIndex.from_vector_store(
168+
vector_store=vector_store,
169+
service_context=service_context,
170+
storage_context=storage_context,
171+
index_id="vector_index",
153172
)
154-
155-
loader = SimpleDirectoryReader(doc_path, recursive=True, exclude_hidden=True)
156-
documents = loader.load_data()
157-
doc_filename = os.listdir(doc_path)[0]
173+
doc_filename = st.session_state.current_file
158174
sidebar_placeholder.header("Current Processing Document:")
159-
sidebar_placeholder.subheader(doc_filename)
160-
sidebar_placeholder.write(documents[0].get_text()[:500] + "...")
175+
sidebar_placeholder.subheader(uploaded_file.name)
161176

162-
if index:
177+
if index or st.session_state.response != "":
163178
st.text_input("Ask something: ", key="prompt", on_change=send_click)
164179
st.button("Send", on_click=send_click)
165180
if st.session_state.response:

sample-application/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ python-dotenv==1.0.0
99
pypdf==4.1.0
1010
azure-core==1.26.4
1111
azure-identity==1.13.0
12+
llama-index-vector-stores-qdrant==0.2.17

0 commit comments

Comments
 (0)