-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weaviate guide - code and examples #1365
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6bb9a3d
first input - tf and basic helm manifest
BRV158 257cf2b
weaviate values updated with authentification
BRV158 6051f8b
comments cleared out
BRV158 db82520
notebook added
BRV158 9fac6fc
notebook and jupiter edited
BRV158 d38da2f
notebook update
BRV158 321b7c5
updates
ganochenkodg 0c01a9f
update jupyter yaml
ganochenkodg 9acc63e
updates
ganochenkodg cb78ec0
updates
ganochenkodg 033eff6
add dashboard
ganochenkodg d9987b3
manifests tagged
BRV158 d2ae9e6
end endpoint envs
ganochenkodg c36d2b6
update tf, add dockerfiles
ganochenkodg 945868b
Merge branch 'GoogleCloudPlatform:main' into Weaviate
ganochenkodg 3e4f4f9
update the code
ganochenkodg 24e7f0c
small update
ganochenkodg 61a11d5
update chatbot.py
ganochenkodg 1952081
docker updates
ganochenkodg 5d588fa
Merge branch 'GoogleCloudPlatform:main' into Weaviate
ganochenkodg 579f00a
update notebook, remove jupyter
ganochenkodg c011551
Merge branch 'main' into Weaviate
ganochenkodg 6d4186a
update notebook
ganochenkodg 4a1b8d6
add ilb
ganochenkodg cc677d4
Merge branch 'main' into Weaviate
ganochenkodg 49e85ba
fix the header
ganochenkodg 7bdf991
Merge branch 'main' into Weaviate
ganochenkodg 583050c
add ci step
ganochenkodg f578917
ci quickfix
ganochenkodg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
name: databases-weaviate-ci.yml | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- '.github/workflows/databases-weaviate-ci.yml' | ||
- 'databases/weaviate/**' | ||
pull_request: | ||
paths: | ||
- '.github/workflows/databases-weaviate-ci.yml' | ||
- 'databases/weaviate/**' | ||
jobs: | ||
job: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Validate GKE Standard TF for Weaviate | ||
run: | | ||
cd databases/weaviate/terraform/gke-standard | ||
terraform init | ||
terraform validate | ||
- name: Validate GKE Autopilot TF for Weaviate | ||
run: | | ||
cd databases/weaviate/terraform/gke-autopilot | ||
terraform init | ||
terraform validate | ||
- name: Build chatbot app container | ||
run: | | ||
cd databases/weaviate/docker/chatbot | ||
docker build --tag chatbot:1.0 . | ||
- name: Build docs embedder container | ||
run: | | ||
cd databases/weaviate/docker/embed-docs | ||
docker build --tag embed-docs:1.0 . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM python:3.12-slim-bookworm | ||
|
||
ENV WEAVIATE_ENDPOINT weaviate.weaviate | ||
ENV WEAVIATE_GRPC_ENDPOINT weaviate-grpc.weaviate | ||
|
||
RUN apt update && \ | ||
apt install -y --no-install-recommends gcc libc6-dev && \ | ||
rm -rf /var/lib/apt/lists/* | ||
WORKDIR /app | ||
COPY requirements.txt requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
COPY . . | ||
|
||
CMD ["run","/app/chat.py"] | ||
ENTRYPOINT ["streamlit"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from langchain_google_vertexai import ChatVertexAI | ||
from langchain.prompts import ChatPromptTemplate | ||
from langchain_google_vertexai import VertexAIEmbeddings | ||
from langchain.memory import ConversationBufferWindowMemory | ||
import weaviate | ||
from weaviate.connect import ConnectionParams | ||
from langchain_weaviate.vectorstores import WeaviateVectorStore | ||
import streamlit as st | ||
import os | ||
|
||
# [START gke_databases_weaviate_docker_chat_model] | ||
vertexAI = ChatVertexAI(model_name="gemini-pro", streaming=True, convert_system_message_to_human=True) | ||
prompt_template = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are a helpful assistant who helps in finding answers to questions using the provided context."), | ||
("human", """ | ||
The answer should be based on the text context given in "text_context" and the conversation history given in "conversation_history" along with its Caption: \n | ||
Base your response on the provided text context and the current conversation history to answer the query. | ||
Select the most relevant information from the context. | ||
Generate a draft response using the selected information. Remove duplicate content from the draft response. | ||
Generate your final response after adjusting it to increase accuracy and relevance. | ||
Now only show your final response! | ||
If you do not know the answer or context is not relevant, response with "I don't know". | ||
|
||
text_context: | ||
{context} | ||
|
||
conversation_history: | ||
{history} | ||
|
||
query: | ||
{query} | ||
"""), | ||
] | ||
) | ||
|
||
embedding_model = VertexAIEmbeddings("textembedding-gecko@001") | ||
# [END gke_databases_weaviate_docker_chat_model] | ||
|
||
# [START gke_databases_weaviate_docker_chat_client] | ||
auth_config = weaviate.auth.AuthApiKey(api_key=os.getenv("APIKEY")) | ||
client = weaviate.WeaviateClient( | ||
connection_params=ConnectionParams.from_params( | ||
http_host=os.getenv("WEAVIATE_ENDPOINT"), | ||
http_port="80", | ||
http_secure=False, | ||
grpc_host=os.getenv("WEAVIATE_GRPC_ENDPOINT"), | ||
grpc_port="50051", | ||
grpc_secure=False, | ||
), | ||
auth_client_secret=auth_config | ||
) | ||
client.connect() | ||
|
||
vector_search = WeaviateVectorStore.from_documents([],embedding_model,client=client, index_name="trainingdocs") | ||
# [END gke_databases_weaviate_docker_chat_client] | ||
|
||
def format_docs(docs): | ||
return "\n\n".join([d.page_content for d in docs]) | ||
|
||
st.title("🤖 Chatbot") | ||
if "messages" not in st.session_state: | ||
st.session_state["messages"] = [{"role": "ai", "content": "How can I help you?"}] | ||
|
||
# [START gke_databases_weaviate_docker_chat_session] | ||
if "memory" not in st.session_state: | ||
st.session_state["memory"] = ConversationBufferWindowMemory( | ||
memory_key="history", | ||
ai_prefix="Bot", | ||
human_prefix="User", | ||
k=3, | ||
) | ||
# [END gke_databases_weaviate_docker_chat_session] | ||
|
||
# [START gke_databases_weaviate_docker_chat_history] | ||
for message in st.session_state.messages: | ||
with st.chat_message(message["role"]): | ||
st.write(message["content"]) | ||
# [END gke_databases_weaviate_docker_chat_history] | ||
|
||
if chat_input := st.chat_input(): | ||
with st.chat_message("human"): | ||
st.write(chat_input) | ||
st.session_state.messages.append({"role": "human", "content": chat_input}) | ||
|
||
found_docs = vector_search.similarity_search(chat_input) | ||
context = format_docs(found_docs) | ||
|
||
prompt_value = prompt_template.format_messages(name="Bot", query=chat_input, context=context, history=st.session_state.memory.load_memory_variables({})) | ||
with st.chat_message("ai"): | ||
with st.spinner("Typing..."): | ||
content = "" | ||
with st.empty(): | ||
for chunk in vertexAI.stream(prompt_value): | ||
content += chunk.content | ||
st.write(content) | ||
st.session_state.messages.append({"role": "ai", "content": content}) | ||
|
||
st.session_state.memory.save_context({"input": chat_input}, {"output": content}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
streamlit==1.34.0 | ||
google-cloud-aiplatform==1.51.0 | ||
langchain==0.1.20 | ||
langchain-community==0.0.38 | ||
langchain-google-vertexai==0.1.3 | ||
langchain-weaviate==0.0.2 | ||
weaviate-client==4.6.5 | ||
arxiv==2.1.0 | ||
pymupdf==1.24.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM python:3.12-slim-bookworm | ||
|
||
ENV WEAVIATE_ENDPOINT weaviate.weaviate | ||
ENV WEAVIATE_GRPC_ENDPOINT weaviate-grpc.weaviate | ||
|
||
RUN apt update && \ | ||
apt install -y --no-install-recommends gcc libc6-dev && \ | ||
rm -rf /var/lib/apt/lists/* | ||
RUN mkdir -p /documents | ||
WORKDIR /app | ||
COPY requirements.txt requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
COPY . . | ||
RUN chmod 765 endpoint.py | ||
EXPOSE 5001 | ||
|
||
CMD ["/app/embedding-job.py"] | ||
ENTRYPOINT ["python"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from langchain_google_vertexai import VertexAIEmbeddings | ||
from langchain_community.document_loaders import PyPDFLoader | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
import weaviate | ||
from weaviate.connect import ConnectionParams | ||
from langchain_weaviate.vectorstores import WeaviateVectorStore | ||
from google.cloud import storage | ||
import os | ||
# [START gke_databases_weaviate_docker_embed_docs_retrieval] | ||
bucketname = os.getenv("BUCKET_NAME") | ||
filename = os.getenv("FILE_NAME") | ||
|
||
storage_client = storage.Client() | ||
bucket = storage_client.bucket(bucketname) | ||
blob = bucket.blob(filename) | ||
blob.download_to_filename("/documents/" + filename) | ||
# [END gke_databases_weaviate_docker_embed_docs_retrieval] | ||
|
||
# [START gke_databases_weaviate_docker_embed_docs_split] | ||
loader = PyPDFLoader("/documents/" + filename) | ||
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | ||
documents = loader.load_and_split(text_splitter) | ||
# [END gke_databases_weaviate_docker_embed_docs_split] | ||
|
||
# [START gke_databases_weaviate_docker_embed_docs_embed] | ||
embeddings = VertexAIEmbeddings("textembedding-gecko@001") | ||
# [END gke_databases_weaviate_docker_embed_docs_embed] | ||
|
||
# [START gke_databases_weaviate_docker_embed_docs_storage] | ||
auth_config = weaviate.auth.AuthApiKey(api_key=os.getenv("APIKEY")) | ||
client = weaviate.WeaviateClient( | ||
connection_params=ConnectionParams.from_params( | ||
http_host=os.getenv("WEAVIATE_ENDPOINT"), | ||
http_port="80", | ||
http_secure=False, | ||
grpc_host=os.getenv("WEAVIATE_GRPC_ENDPOINT"), | ||
grpc_port="50051", | ||
grpc_secure=False, | ||
), | ||
auth_client_secret=auth_config | ||
) | ||
client.connect() | ||
if not client.collections.exists("trainingdocs"): | ||
collection = client.collections.create(name="trainingdocs") | ||
db = WeaviateVectorStore.from_documents(documents, embeddings, client=client, index_name="trainingdocs") | ||
# [END gke_databases_weaviate_docker_embed_docs_storage] | ||
|
||
print(filename + " was successfully embedded") | ||
print(f"# of vectors = {len(documents)}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from flask import Flask, jsonify | ||
from flask import request | ||
import logging | ||
import sys,os, time | ||
from kubernetes import client, config, utils | ||
import kubernetes.client | ||
from kubernetes.client.rest import ApiException | ||
|
||
|
||
app = Flask(__name__) | ||
@app.route('/check') | ||
def message(): | ||
return jsonify({"Message": "Hi there"}) | ||
|
||
|
||
@app.route('/', methods=['POST']) | ||
def bucket(): | ||
request_data = request.get_json() | ||
print(request_data) | ||
bckt = request_data['bucket'] | ||
f_name = request_data['name'] | ||
id = request_data['generation'] | ||
kube_create_job(bckt, f_name, id) | ||
return "ok" | ||
|
||
# Set logging | ||
logging.basicConfig(stream=sys.stdout, level=logging.INFO) | ||
|
||
# Setup K8 configs | ||
config.load_incluster_config() | ||
# [START gke_databases_weaviate_docker_embed_endpoint_job] | ||
def kube_create_job_object(name, container_image, bucket_name, f_name, namespace, container_name="jobcontainer", env_vars={}): | ||
|
||
body = client.V1Job(api_version="batch/v1", kind="Job") | ||
body.metadata = client.V1ObjectMeta(namespace=namespace, name=name) | ||
body.status = client.V1JobStatus() | ||
|
||
template = client.V1PodTemplate() | ||
template.template = client.V1PodTemplateSpec() | ||
env_list = [ | ||
client.V1EnvVar(name="WEAVIATE_ENDPOINT", value=os.getenv("WEAVIATE_ENDPOINT")), | ||
client.V1EnvVar(name="WEAVIATE_GRPC_ENDPOINT", value=os.getenv("WEAVIATE_GRPC_ENDPOINT")), | ||
client.V1EnvVar(name="FILE_NAME", value=f_name), | ||
client.V1EnvVar(name="BUCKET_NAME", value=bucket_name), | ||
client.V1EnvVar(name="APIKEY", value_from=client.V1EnvVarSource(secret_key_ref=client.V1SecretKeySelector(key="AUTHENTICATION_APIKEY_ALLOWED_KEYS", name="apikeys"))), | ||
] | ||
|
||
container = client.V1Container(name=container_name, image=container_image, image_pull_policy='Always', env=env_list) | ||
template.template.spec = client.V1PodSpec(containers=[container], restart_policy='Never', service_account='embed-docs-sa') | ||
|
||
body.spec = client.V1JobSpec(backoff_limit=3, ttl_seconds_after_finished=60, template=template.template) | ||
return body | ||
# [END gke_databases_weaviate_docker_embed_endpoint_job] | ||
def kube_test_credentials(): | ||
try: | ||
api_response = api_instance.get_api_resources() | ||
logging.info(api_response) | ||
except ApiException as e: | ||
print("Exception when calling API: %s\n" % e) | ||
|
||
def kube_create_job(bckt, f_name, id): | ||
container_image = os.getenv("JOB_IMAGE") | ||
namespace = os.getenv("JOB_NAMESPACE") | ||
name = "docs-embedder" + id | ||
body = kube_create_job_object(name, container_image, bckt, f_name, namespace) | ||
v1=client.BatchV1Api() | ||
try: | ||
v1.create_namespaced_job(namespace, body, pretty=True) | ||
except ApiException as e: | ||
print("Exception when calling BatchV1Api->create_namespaced_job: %s\n" % e) | ||
return | ||
|
||
if __name__ == '__main__': | ||
app.run('0.0.0.0', port=5001, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
google-cloud-storage==2.16.0 | ||
google-cloud-aiplatform==1.51.0 | ||
langchain==0.1.20 | ||
langchain-community==0.0.38 | ||
langchain-google-vertexai==0.1.3 | ||
langchain-weaviate==0.0.2 | ||
weaviate-client==4.6.5 | ||
pypdf==3.17.4 | ||
click==8.1.7 | ||
Flask==2.3.3 | ||
itsdangerous==2.2.0 | ||
Jinja2==3.1.4 | ||
MarkupSafe==2.1.5 | ||
Werkzeug==3.0.3 | ||
kubernetes==28.1.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create a new workflow for this weaviate samples, that has 4 steps:
docker build
steps for the two containersterraform validate
steps for the two terraform directoriesYou can find an example here: https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/blob/main/.github/workflows/cost-optimization-gke-vpa-recommendations-ci.yml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, sure, will add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done