Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,13 @@ from pinecone import Pinecone, ServerlessSpec
|
|
| 7 |
from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder
|
| 8 |
from haystack.components.writers import DocumentWriter
|
| 9 |
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore
|
| 10 |
-
from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
|
| 11 |
from haystack import Pipeline
|
| 12 |
from haystack.components.generators import OpenAIGenerator
|
| 13 |
from haystack.components.builders import PromptBuilder
|
| 14 |
from haystack.components.converters import TextFileToDocument
|
| 15 |
from haystack.components.preprocessors import DocumentSplitter
|
|
|
|
| 16 |
|
| 17 |
# --- Logging ---
|
| 18 |
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
@@ -40,7 +41,7 @@ if index_name not in [idx['name'] for idx in pc.list_indexes()]:
|
|
| 40 |
metric="euclidean",
|
| 41 |
spec=ServerlessSpec(cloud="aws", region="us-east-1")
|
| 42 |
)
|
| 43 |
-
|
| 44 |
# --- Document Loading and Processing ---
|
| 45 |
os.makedirs("data/paul_graham", exist_ok=True)
|
| 46 |
file_path = "data/paul_graham/paul_graham_essay.txt"
|
|
@@ -51,7 +52,7 @@ if not os.path.exists(file_path):
|
|
| 51 |
f.write(r.text)
|
| 52 |
|
| 53 |
# --- Haystack Pipeline for Indexing ---
|
| 54 |
-
document_store = PineconeDocumentStore(api_key=
|
| 55 |
|
| 56 |
indexing_pipeline = Pipeline()
|
| 57 |
indexing_pipeline.add_component("converter", TextFileToDocument())
|
|
@@ -84,7 +85,7 @@ query_pipeline = Pipeline()
|
|
| 84 |
query_pipeline.add_component("embedder", OpenAITextEmbedder())
|
| 85 |
query_pipeline.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store))
|
| 86 |
query_pipeline.add_component("prompt_builder", PromptBuilder(template=template))
|
| 87 |
-
query_pipeline.add_component("llm", OpenAIGenerator(api_key=
|
| 88 |
|
| 89 |
query_pipeline.connect("embedder.query", "retriever.query")
|
| 90 |
query_pipeline.connect("retriever.documents", "prompt_builder.documents")
|
|
|
|
| 7 |
from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder
|
| 8 |
from haystack.components.writers import DocumentWriter
|
| 9 |
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore
|
| 10 |
+
from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
|
| 11 |
from haystack import Pipeline
|
| 12 |
from haystack.components.generators import OpenAIGenerator
|
| 13 |
from haystack.components.builders import PromptBuilder
|
| 14 |
from haystack.components.converters import TextFileToDocument
|
| 15 |
from haystack.components.preprocessors import DocumentSplitter
|
| 16 |
+
from haystack.utils import Secret # Import the Secret class
|
| 17 |
|
| 18 |
# --- Logging ---
|
| 19 |
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
|
|
| 41 |
metric="euclidean",
|
| 42 |
spec=ServerlessSpec(cloud="aws", region="us-east-1")
|
| 43 |
)
|
| 44 |
+
|
| 45 |
# --- Document Loading and Processing ---
|
| 46 |
os.makedirs("data/paul_graham", exist_ok=True)
|
| 47 |
file_path = "data/paul_graham/paul_graham_essay.txt"
|
|
|
|
| 52 |
f.write(r.text)
|
| 53 |
|
| 54 |
# --- Haystack Pipeline for Indexing ---
|
| 55 |
+
document_store = PineconeDocumentStore(api_key=Secret.from_env_var("PINECONE_API_KEY"), index=index_name) # Use Secret object
|
| 56 |
|
| 57 |
indexing_pipeline = Pipeline()
|
| 58 |
indexing_pipeline.add_component("converter", TextFileToDocument())
|
|
|
|
| 85 |
query_pipeline.add_component("embedder", OpenAITextEmbedder())
|
| 86 |
query_pipeline.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store))
|
| 87 |
query_pipeline.add_component("prompt_builder", PromptBuilder(template=template))
|
| 88 |
+
query_pipeline.add_component("llm", OpenAIGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY"))) # Use Secret object
|
| 89 |
|
| 90 |
query_pipeline.connect("embedder.query", "retriever.query")
|
| 91 |
query_pipeline.connect("retriever.documents", "prompt_builder.documents")
|