Ross McNairn commited on
Commit ·
ce36114
1
Parent(s): 1a1f63d
fix persistence
Browse files- hello_wordsmith/wordsmith.py +26 -28
hello_wordsmith/wordsmith.py
CHANGED
|
@@ -2,43 +2,38 @@ import os
|
|
| 2 |
import sys
|
| 3 |
|
| 4 |
import chromadb
|
| 5 |
-
from llama_index.cli.rag import RagCLI
|
| 6 |
-
from llama_index.core import (
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
VectorStoreIndex
|
| 10 |
-
)
|
| 11 |
from llama_index.core.base.llms.types import ChatMessage, MessageRole
|
| 12 |
-
from llama_index.core.ingestion import IngestionPipeline
|
| 13 |
from llama_index.core.query_pipeline import InputComponent, QueryPipeline
|
| 14 |
from llama_index.core.response_synthesizers import TreeSummarize
|
| 15 |
-
from llama_index.core import
|
| 16 |
-
from llama_index.embeddings.openai import (
|
| 17 |
-
|
| 18 |
-
OpenAIEmbeddingModelType
|
| 19 |
-
)
|
| 20 |
from llama_index.llms.openai import OpenAI
|
| 21 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 22 |
|
| 23 |
-
|
| 24 |
-
Settings.embed_model = OpenAIEmbedding(
|
| 25 |
-
model="text-embedding-3-small"
|
| 26 |
-
)
|
| 27 |
|
| 28 |
|
| 29 |
def initialize_chroma_db():
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 33 |
return vector_store
|
| 34 |
|
| 35 |
|
| 36 |
-
def setup_document_storage(vector_store):
|
| 37 |
package_directory = os.path.dirname(os.path.abspath(__file__))
|
| 38 |
dataset_path = os.path.join(package_directory, "public_wordsmith_dataset")
|
| 39 |
reader = SimpleDirectoryReader(input_dir=dataset_path)
|
| 40 |
docs = reader.load_data()
|
| 41 |
-
index = VectorStoreIndex.from_documents(docs)
|
| 42 |
return index
|
| 43 |
|
| 44 |
|
|
@@ -84,9 +79,7 @@ _chat_template_messages = [
|
|
| 84 |
|
| 85 |
def configure_query_pipeline(index, llm):
|
| 86 |
"""Configure and set up the query pipeline"""
|
| 87 |
-
text_qa_chat_template = ChatPromptTemplate.from_messages(
|
| 88 |
-
_chat_template_messages
|
| 89 |
-
)
|
| 90 |
query_pipeline = QueryPipeline()
|
| 91 |
|
| 92 |
retriever = index.as_retriever(similarity_top_k=20)
|
|
@@ -122,14 +115,19 @@ class WordsmithRAGCLI(RagCLI):
|
|
| 122 |
|
| 123 |
def main():
|
| 124 |
vector_store = initialize_chroma_db()
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
llm = initialize_llm()
|
| 127 |
query_pipeline = configure_query_pipeline(index, llm)
|
| 128 |
-
ingestion_pipeline = IngestionPipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
rag_cli_instance = WordsmithRAGCLI(
|
| 130 |
-
ingestion_pipeline=ingestion_pipeline,
|
| 131 |
-
llm=llm,
|
| 132 |
-
query_pipeline=query_pipeline
|
| 133 |
)
|
| 134 |
rag_cli_instance.cli()
|
| 135 |
|
|
|
|
| 2 |
import sys
|
| 3 |
|
| 4 |
import chromadb
|
| 5 |
+
from llama_index.cli.rag import RagCLI, default_ragcli_persist_dir
|
| 6 |
+
from llama_index.core import (ChatPromptTemplate, Settings,
|
| 7 |
+
SimpleDirectoryReader, StorageContext,
|
| 8 |
+
VectorStoreIndex)
|
|
|
|
|
|
|
| 9 |
from llama_index.core.base.llms.types import ChatMessage, MessageRole
|
| 10 |
+
from llama_index.core.ingestion import IngestionCache, IngestionPipeline
|
| 11 |
from llama_index.core.query_pipeline import InputComponent, QueryPipeline
|
| 12 |
from llama_index.core.response_synthesizers import TreeSummarize
|
| 13 |
+
from llama_index.core.storage.docstore import SimpleDocumentStore
|
| 14 |
+
from llama_index.embeddings.openai import (OpenAIEmbedding,
|
| 15 |
+
OpenAIEmbeddingModelType)
|
|
|
|
|
|
|
| 16 |
from llama_index.llms.openai import OpenAI
|
| 17 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 18 |
|
| 19 |
+
Settings.embed_model = OpenAIEmbedding(model=OpenAIEmbeddingModelType.TEXT_EMBED_3_SMALL)
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def initialize_chroma_db():
|
| 23 |
+
db = chromadb.PersistentClient(
|
| 24 |
+
path=os.path.join(default_ragcli_persist_dir(), "chroma")
|
| 25 |
+
)
|
| 26 |
+
chroma_collection = db.get_or_create_collection("wordsmith_rag_demo_index")
|
| 27 |
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 28 |
return vector_store
|
| 29 |
|
| 30 |
|
| 31 |
+
def setup_document_storage(*, vector_store, storage_context):
|
| 32 |
package_directory = os.path.dirname(os.path.abspath(__file__))
|
| 33 |
dataset_path = os.path.join(package_directory, "public_wordsmith_dataset")
|
| 34 |
reader = SimpleDirectoryReader(input_dir=dataset_path)
|
| 35 |
docs = reader.load_data()
|
| 36 |
+
index = VectorStoreIndex.from_documents(docs, storage_context=storage_context)
|
| 37 |
return index
|
| 38 |
|
| 39 |
|
|
|
|
| 79 |
|
| 80 |
def configure_query_pipeline(index, llm):
|
| 81 |
"""Configure and set up the query pipeline"""
|
| 82 |
+
text_qa_chat_template = ChatPromptTemplate.from_messages(_chat_template_messages)
|
|
|
|
|
|
|
| 83 |
query_pipeline = QueryPipeline()
|
| 84 |
|
| 85 |
retriever = index.as_retriever(similarity_top_k=20)
|
|
|
|
| 115 |
|
| 116 |
def main():
|
| 117 |
vector_store = initialize_chroma_db()
|
| 118 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 119 |
+
index = setup_document_storage(
|
| 120 |
+
vector_store=vector_store, storage_context=storage_context
|
| 121 |
+
)
|
| 122 |
llm = initialize_llm()
|
| 123 |
query_pipeline = configure_query_pipeline(index, llm)
|
| 124 |
+
ingestion_pipeline = IngestionPipeline(
|
| 125 |
+
vector_store=vector_store,
|
| 126 |
+
cache=IngestionCache(),
|
| 127 |
+
docstore=SimpleDocumentStore(),
|
| 128 |
+
)
|
| 129 |
rag_cli_instance = WordsmithRAGCLI(
|
| 130 |
+
ingestion_pipeline=ingestion_pipeline, llm=llm, query_pipeline=query_pipeline
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
rag_cli_instance.cli()
|
| 133 |
|