Skip to content

Commit cb91ddc

Browse files
committed
add langfuse credentials pulled from secret
1 parent b3af83a commit cb91ddc

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

llm-complete-guide/deployment_hf.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from utils.llm_utils import process_input_with_retrieval
99
from zenml.client import Client
1010

11-
langfuse = Langfuse()
12-
1311
logging.basicConfig(level=logging.INFO)
1412
logger = logging.getLogger(__name__)
1513

@@ -27,6 +25,23 @@
2725
raise RuntimeError(f"Application startup failed: {e}")
2826

2927

28+
LANGFUSE_PUBLIC_KEY = os.getenv(
29+
"LANGFUSE_PUBLIC_KEY", secret.secret_values["LANGFUSE_PUBLIC_KEY"]
30+
)
31+
LANGFUSE_SECRET_KEY = os.getenv(
32+
"LANGFUSE_SECRET_KEY", secret.secret_values["LANGFUSE_SECRET_KEY"]
33+
)
34+
LANGFUSE_HOST = os.getenv(
35+
"LANGFUSE_HOST", secret.secret_values["LANGFUSE_HOST"]
36+
)
37+
38+
langfuse = Langfuse(
39+
public_key=LANGFUSE_PUBLIC_KEY,
40+
secret_key=LANGFUSE_SECRET_KEY,
41+
host=LANGFUSE_HOST,
42+
)
43+
44+
3045
def get_langfuse_trace_id() -> str | None:
3146
"""Get the trace from Langfuse.
3247

llm-complete-guide/steps/rag_deployment.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@
1313
ZENML_API_TOKEN = os.environ.get("ZENML_API_TOKEN")
1414
ZENML_STORE_URL = os.environ.get("ZENML_STORE_URL")
1515

16+
secret = Client().get_secret(SECRET_NAME)
17+
1618
if not ZENML_API_TOKEN or not ZENML_STORE_URL:
1719
# Get ZenML server URL and API token from the secret store
18-
secret = Client().get_secret(SECRET_NAME)
1920
ZENML_API_TOKEN = ZENML_API_TOKEN or secret.secret_values.get(
2021
"zenml_api_token"
2122
)
2223
ZENML_STORE_URL = ZENML_STORE_URL or secret.secret_values.get(
2324
"zenml_store_url"
2425
)
2526

27+
28+
LANGFUSE_PUBLIC_KEY = os.environ.get(
29+
"LANGFUSE_PUBLIC_KEY", secret.secret_values.get("LANGFUSE_PUBLIC_KEY")
30+
)
31+
LANGFUSE_SECRET_KEY = os.environ.get(
32+
"LANGFUSE_SECRET_KEY", secret.secret_values.get("LANGFUSE_SECRET_KEY")
33+
)
34+
LANGFUSE_HOST = os.environ.get(
35+
"LANGFUSE_HOST", secret.secret_values.get("LANGFUSE_HOST")
36+
)
37+
2638
SPACE_USERNAME = os.environ.get("ZENML_HF_USERNAME", "zenml")
2739
SPACE_NAME = os.environ.get("ZENML_HF_SPACE_NAME", "llm-complete-guide-rag")
2840
SECRET_NAME = os.environ.get("ZENML_PROJECT_SECRET_NAME", "llm-complete")
@@ -51,6 +63,7 @@
5163
huggingface-hub
5264
elasticsearch
5365
tenacity
66+
pinecone
5467
{chr(10).join(gcp_reqs)}
5568
"""
5669

@@ -128,6 +141,27 @@ def gradio_rag_deployment() -> None:
128141
value=str(SECRET_NAME),
129142
)
130143

144+
if LANGFUSE_PUBLIC_KEY is not None:
145+
api.add_space_secret(
146+
repo_id=hf_repo_id,
147+
key="LANGFUSE_PUBLIC_KEY",
148+
value=str(LANGFUSE_PUBLIC_KEY),
149+
)
150+
151+
if LANGFUSE_SECRET_KEY is not None:
152+
api.add_space_secret(
153+
repo_id=hf_repo_id,
154+
key="LANGFUSE_SECRET_KEY",
155+
value=str(LANGFUSE_SECRET_KEY),
156+
)
157+
158+
if LANGFUSE_HOST is not None:
159+
api.add_space_secret(
160+
repo_id=hf_repo_id,
161+
key="LANGFUSE_HOST",
162+
value=str(LANGFUSE_HOST),
163+
)
164+
131165
files_to_upload = {
132166
"deployment_hf.py": "app.py",
133167
"utils/llm_utils.py": "utils/llm_utils.py",

llm-complete-guide/utils/llm_utils.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import logging
2323
import os
24-
import uuid
2524

2625
import pinecone
2726
from elasticsearch import Elasticsearch
@@ -285,7 +284,9 @@ def get_db_conn() -> connection:
285284
raise
286285

287286

288-
def get_pinecone_client(model_version_name_or_id: str = "dev") -> pinecone.Index:
287+
def get_pinecone_client(
288+
model_version_name_or_id: str = "dev",
289+
) -> pinecone.Index:
289290
"""Get a Pinecone index client.
290291
291292
Returns:
@@ -306,7 +307,9 @@ def get_pinecone_client(model_version_name_or_id: str = "dev") -> pinecone.Index
306307
model_version_name_or_number_or_id=model_version_name_or_id,
307308
)
308309

309-
index_name_from_secret = client.get_secret(SECRET_NAME_PINECONE).secret_values.get("pinecone_index", "zenml-docs")
310+
index_name_from_secret = client.get_secret(
311+
SECRET_NAME_PINECONE
312+
).secret_values.get("pinecone_index", "zenml-docs")
310313

311314
if model_version_name_or_id == "production":
312315
index_name = f"{index_name_from_secret}-prod"
@@ -322,7 +325,7 @@ def get_pinecone_client(model_version_name_or_id: str = "dev") -> pinecone.Index
322325
name=index_name,
323326
dimension=EMBEDDING_DIMENSIONALITY,
324327
metric="cosine",
325-
spec=ServerlessSpec(cloud="aws", region="us-east-1")
328+
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
326329
)
327330
else:
328331
try:
@@ -331,7 +334,9 @@ def get_pinecone_client(model_version_name_or_id: str = "dev") -> pinecone.Index
331334
]
332335
except KeyError:
333336
index_name = index_name_from_secret
334-
model_version.run_metadata["vector_store"]["index_name"] = index_name
337+
model_version.run_metadata["vector_store"]["index_name"] = (
338+
index_name
339+
)
335340

336341
# Create index if it doesn't exist
337342
if index_name not in pc.list_indexes().names():
@@ -341,7 +346,7 @@ def get_pinecone_client(model_version_name_or_id: str = "dev") -> pinecone.Index
341346
metric="cosine",
342347
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
343348
)
344-
349+
345350
return pc.Index(index_name)
346351

347352

@@ -672,7 +677,9 @@ def process_input_with_retrieval(
672677
include_metadata=True,
673678
)
674679
elif vector_store == "pinecone":
675-
pinecone_index = get_pinecone_client(model_version_name_or_id=model_version_stage)
680+
pinecone_index = get_pinecone_client(
681+
model_version_name_or_id=model_version_stage
682+
)
676683
similar_docs = get_topn_similar_docs(
677684
query_embedding=query_embedding,
678685
pinecone_index=pinecone_index,

0 commit comments

Comments
 (0)