Skip to content

Commit 896c89d

Browse files
committed
Iterate on pinecone changes
1 parent 21c4dac commit 896c89d

File tree

10 files changed

+89
-29
lines changed

10 files changed

+89
-29
lines changed

llm-complete-guide/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ export ZENML_PROJECT_SECRET_NAME=llm-complete
5959

6060
[Pinecone](https://www.pinecone.io/) is the default vector store used in this project. It's a cloud-native vector database that's optimized for machine learning applications. You'll need to create a Pinecone account and get an API key to use it.
6161

62-
Once you have your Pinecone account set up, you'll need to store your API key and index name as a ZenML secret (with name `pinecone-zenml`). You can do this by running the following command:
62+
Once you have your Pinecone account set up, you'll need to store your API key and index name as a ZenML secret. You can do this by running the following command:
6363

6464
```shell
65-
zenml secret create pinecone-zenml --pinecone_api_key=<YOUR_PINECONE_API_KEY> --pinecone_env=<YOUR_PINECONE_ENV> --pinecone_index=<YOUR_INDEX_NAME>
65+
zenml secret update llm-complete -v '{"pinecone_api_key": "YOUR_PINECONE_API_KEY", "pinecone_env": "YOUR_PINECONE_ENV", "pinecone_index": "YOUR_INDEX_NAME"}'
66+
6667
```
6768

68-
The `pinecone_index` value you specify will be used for all your development pipeline runs. When you promote your ZenML model to production and run your ingestion pipeline again, it will automatically create a new production index called `<YOUR_INDEX_NAME>-prod`. This separation ensures that your development and production environments remain isolated.
69+
The `pinecone_index` value you specify will be used for all your development pipeline runs. Make sure the value consists only of alphanumeric characters and dashes. When you promote your ZenML model to production and run your ingestion pipeline again, it will automatically create a new production index called `<YOUR_INDEX_NAME>-prod`. This separation ensures that your development and production environments remain isolated.
6970

7071
### Choosing Your Vector Store
7172

llm-complete-guide/configs/dev/rag.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ steps:
3030
use_dev_set: true
3131
index_generator:
3232
parameters:
33-
index_type: postgres
33+
index_type: postgres # Options: pinecone, postgres, elasticsearch

llm-complete-guide/configs/dev/rag_eval.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ settings:
1515
- pygithub
1616
- elasticsearch
1717
python_package_installer: "uv"
18+
19+
steps:
20+
url_scraper:
21+
parameters:
22+
docs_url: https://docs.zenml.io/
23+
use_dev_set: true
24+
index_generator:
25+
parameters:
26+
index_type: postgres

llm-complete-guide/deployment_hf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
logger = logging.getLogger(__name__)
1313

1414
APP_ENVIRONMENT = os.getenv("GRADIO_ZENML_APP_ENVIRONMENT", "dev")
15+
print(os.getenv("ZENML_STORE_API_KEY"), os.getenv("ZENML_STORE_URL"))
1516

1617
# Initialize ZenML client and verify secret access
1718
try:

llm-complete-guide/run.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
import warnings
1717
from pathlib import Path
18+
import os
1819

1920
# Suppress the specific FutureWarning from huggingface_hub
2021
warnings.filterwarnings(
@@ -39,7 +40,7 @@
3940
from typing import Optional
4041

4142
import click
42-
from constants import OPENAI_MODEL
43+
from constants import OPENAI_MODEL, SECRET_NAME
4344
from materializers.document_materializer import DocumentMaterializer
4445
from pipelines import (
4546
finetune_embeddings,
@@ -54,9 +55,34 @@
5455
from structures import Document
5556
from zenml import Model
5657
from zenml.materializers.materializer_registry import materializer_registry
58+
from zenml.client import Client
5759

5860
logger = get_logger(__name__)
5961

62+
# First try to get from environment variables
63+
LANGFUSE_PUBLIC_KEY = os.getenv("LANGFUSE_PUBLIC_KEY")
64+
LANGFUSE_SECRET_KEY = os.getenv("LANGFUSE_SECRET_KEY")
65+
LANGFUSE_HOST = os.getenv("LANGFUSE_HOST")
66+
67+
# If any are not set, get from ZenML secrets and set the env vars
68+
if not all([LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_HOST]):
69+
secret = Client().get_secret(SECRET_NAME)
70+
71+
if not LANGFUSE_PUBLIC_KEY:
72+
LANGFUSE_PUBLIC_KEY = secret.secret_values.get("langfuse_public_key")
73+
if LANGFUSE_PUBLIC_KEY:
74+
os.environ["LANGFUSE_PUBLIC_KEY"] = LANGFUSE_PUBLIC_KEY
75+
76+
if not LANGFUSE_SECRET_KEY:
77+
LANGFUSE_SECRET_KEY = secret.secret_values.get("langfuse_secret_key")
78+
if LANGFUSE_SECRET_KEY:
79+
os.environ["LANGFUSE_SECRET_KEY"] = LANGFUSE_SECRET_KEY
80+
81+
if not LANGFUSE_HOST:
82+
LANGFUSE_HOST = secret.secret_values.get("langfuse_host")
83+
if LANGFUSE_HOST:
84+
os.environ["LANGFUSE_HOST"] = LANGFUSE_HOST
85+
6086

6187
@click.command(
6288
help="""

llm-complete-guide/steps/eval_retrieval.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def process_single_pair(
140140
question, url_ending, urls = query_similar_docs(
141141
pair["question"], pair["url_ending"], use_reranking
142142
)
143+
print(f"question: {question}, url_ending: {url_ending}, urls: {urls}")
143144
is_failure = all(url_ending not in url for url in urls)
144145
return is_failure, question, url_ending, urls
145146

@@ -189,7 +190,7 @@ def process_with_progress(
189190
)
190191

191192
results = []
192-
with Pool(processes=n_processes) as pool:
193+
with Pool(processes=n_processes) as pool: # Sleep for 3 seconds before starting processing
193194
for i, result in enumerate(pool.imap(worker_fn, items), 1):
194195
results.append(result)
195196
logger.info(f"Completed {i}/{len(items)} tests")

llm-complete-guide/steps/populate_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def _log_metadata(index_type: IndexType) -> None:
927927
connection_details = {
928928
"api_key": "**********",
929929
"environment": client.get_secret(
930-
SECRET_NAME_PINECONE
930+
SECRET_NAME
931931
).secret_values["pinecone_env"],
932932
}
933933

llm-complete-guide/steps/rag_deployment.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@
1010
from zenml.integrations.registry import integration_registry
1111

1212
# Try to get from environment first, otherwise fall back to secret store
13-
ZENML_API_TOKEN = os.environ.get("ZENML_API_TOKEN")
13+
ZENML_STORE_API_KEY = os.environ.get("ZENML_STORE_API_KEY")
1414
ZENML_STORE_URL = os.environ.get("ZENML_STORE_URL")
1515

1616
secret = Client().get_secret(SECRET_NAME)
1717

18-
if not ZENML_API_TOKEN or not ZENML_STORE_URL:
18+
if not ZENML_STORE_API_KEY or not ZENML_STORE_URL:
1919
# Get ZenML server URL and API token from the secret store
20-
ZENML_API_TOKEN = ZENML_API_TOKEN or secret.secret_values.get(
21-
"zenml_api_token"
20+
ZENML_STORE_API_KEY = ZENML_STORE_API_KEY or secret.secret_values.get(
21+
"zenml_store_api_token"
2222
)
23-
ZENML_STORE_URL = ZENML_STORE_URL or secret.secret_values.get(
24-
"zenml_store_url"
25-
)
26-
23+
ZENML_STORE_URL = ZENML_STORE_URL or secret.secret_values.get("zenml_store_url")
2724

2825
LANGFUSE_PUBLIC_KEY = os.environ.get(
2926
"LANGFUSE_PUBLIC_KEY", secret.secret_values.get("LANGFUSE_PUBLIC_KEY")
@@ -110,7 +107,7 @@ def gradio_rag_deployment() -> None:
110107
Starts a web server with a chat interface that echoes back user messages.
111108
The server runs indefinitely until manually stopped.
112109
"""
113-
api = HfApi()
110+
api = HfApi(token=get_hf_token())
114111
api.create_repo(
115112
repo_id=hf_repo_id,
116113
repo_type="space",
@@ -119,13 +116,12 @@ def gradio_rag_deployment() -> None:
119116
exist_ok=True,
120117
token=get_hf_token(),
121118
)
122-
123119
# Ensure values are strings
124-
if ZENML_API_TOKEN is not None:
120+
if ZENML_STORE_API_KEY is not None:
125121
api.add_space_secret(
126122
repo_id=hf_repo_id,
127123
key="ZENML_STORE_API_KEY",
128-
value=str(ZENML_API_TOKEN),
124+
value=str(ZENML_STORE_API_KEY),
129125
)
130126

131127
if ZENML_STORE_URL is not None:

llm-complete-guide/steps/url_scraper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def url_scraper(
5454
docs_urls = get_all_pages(docs_url)
5555

5656
website_urls = get_all_pages(website_url)
57-
# all_urls = docs_urls + website_urls + examples_readme_urls
57+
# all_urls = docs_urls + website_urls
5858
# all_urls = website_urls
5959
all_urls = ["https://zenml.io"]
6060
log_metadata(

llm-complete-guide/utils/llm_utils.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@
6464

6565
logger = logging.getLogger(__name__)
6666

67+
# First try to get from environment variables
68+
LANGFUSE_PUBLIC_KEY = os.getenv("LANGFUSE_PUBLIC_KEY")
69+
LANGFUSE_SECRET_KEY = os.getenv("LANGFUSE_SECRET_KEY")
70+
LANGFUSE_HOST = os.getenv("LANGFUSE_HOST")
71+
72+
# If any are not set, get from ZenML secrets and set the env vars
73+
if not all([LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_HOST]):
74+
secret = Client().get_secret(SECRET_NAME)
75+
76+
if not LANGFUSE_PUBLIC_KEY:
77+
LANGFUSE_PUBLIC_KEY = secret.secret_values.get("langfuse_public_key")
78+
if LANGFUSE_PUBLIC_KEY:
79+
os.environ["LANGFUSE_PUBLIC_KEY"] = LANGFUSE_PUBLIC_KEY
80+
81+
if not LANGFUSE_SECRET_KEY:
82+
LANGFUSE_SECRET_KEY = secret.secret_values.get("langfuse_secret_key")
83+
if LANGFUSE_SECRET_KEY:
84+
os.environ["LANGFUSE_SECRET_KEY"] = LANGFUSE_SECRET_KEY
85+
86+
if not LANGFUSE_HOST:
87+
LANGFUSE_HOST = secret.secret_values.get("langfuse_host")
88+
if LANGFUSE_HOST:
89+
os.environ["LANGFUSE_HOST"] = LANGFUSE_HOST
90+
6791
# logs all litellm requests to langfuse
6892
litellm.callbacks = ["langfuse"]
6993

@@ -293,7 +317,7 @@ def get_pinecone_client(
293317
pinecone.Index: A Pinecone index client.
294318
"""
295319
client = Client()
296-
pinecone_api_key = client.get_secret(SECRET_NAME_PINECONE).secret_values[
320+
pinecone_api_key = client.get_secret(SECRET_NAME).secret_values[
297321
"pinecone_api_key"
298322
]
299323
pc = Pinecone(api_key=pinecone_api_key)
@@ -308,12 +332,15 @@ def get_pinecone_client(
308332
)
309333

310334
index_name_from_secret = client.get_secret(
311-
SECRET_NAME_PINECONE
312-
).secret_values.get("pinecone_index", "zenml-docs")
335+
SECRET_NAME).secret_values.get("pinecone_index", "zenml-docs")
313336

314337
if model_version_name_or_id == "production":
315338
index_name = f"{index_name_from_secret}-prod"
316339

340+
# Initialize vector_store metadata if it doesn't exist
341+
if "vector_store" not in model_version.run_metadata:
342+
model_version.run_metadata["vector_store"] = {}
343+
317344
model_version.run_metadata["vector_store"]["index_name"] = index_name
318345

319346
# delete index if it exists
@@ -329,14 +356,13 @@ def get_pinecone_client(
329356
)
330357
else:
331358
try:
332-
index_name = model_version.run_metadata["vector_store"][
333-
"index_name"
334-
]
359+
index_name = model_version.run_metadata["vector_store"]["index_name"]
335360
except KeyError:
336361
index_name = index_name_from_secret
337-
model_version.run_metadata["vector_store"]["index_name"] = (
338-
index_name
339-
)
362+
# Initialize vector_store metadata if it doesn't exist
363+
if "vector_store" not in model_version.run_metadata:
364+
model_version.run_metadata["vector_store"] = {}
365+
model_version.run_metadata["vector_store"]["index_name"] = index_name
340366

341367
# Create index if it doesn't exist
342368
if index_name not in pc.list_indexes().names():

0 commit comments

Comments
 (0)