Ross McNairn commited on
Commit ·
47b3c28
1
Parent(s): 53561cb
cleanup
Browse files- hello_wordsmith/hello_wordsmith.py +68 -51
- setup.py +18 -22
hello_wordsmith/hello_wordsmith.py
CHANGED
|
@@ -1,78 +1,95 @@
|
|
| 1 |
-
#!/
|
|
|
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from llama_index.core.ingestion import IngestionPipeline
|
| 4 |
-
from llama_index.core.query_pipeline import QueryPipeline
|
|
|
|
| 5 |
from llama_index.core.storage.docstore import SimpleDocumentStore
|
| 6 |
-
from llama_index.core import SimpleDirectoryReader
|
| 7 |
from llama_index.cli.rag import RagCLI
|
| 8 |
from llama_index.llms.openai import OpenAI
|
| 9 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 10 |
-
import chromadb
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
from llama_index.core import VectorStoreIndex, StorageContext
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
docs
|
| 30 |
-
)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# docstore = SimpleDocumentStore()
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
custom_ingestion_pipeline = IngestionPipeline(
|
| 37 |
-
vector_store=vector_store,
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
from llama_index.core.query_pipeline import InputComponent
|
| 49 |
-
|
| 50 |
-
# construct vector store and customize storage context
|
| 51 |
-
|
| 52 |
-
retriever = index.as_retriever(similarity_top_k=5)
|
| 53 |
-
summarizer = TreeSummarize(llm=llm)
|
| 54 |
-
query_pipeline.add_modules(
|
| 55 |
-
{
|
| 56 |
-
"input": InputComponent(),
|
| 57 |
-
"retriever": retriever,
|
| 58 |
-
"summarizer": summarizer,
|
| 59 |
-
}
|
| 60 |
-
)
|
| 61 |
-
query_pipeline.add_link("input", "retriever")
|
| 62 |
-
query_pipeline.add_link("input", "summarizer", dest_key="query_str")
|
| 63 |
-
query_pipeline.add_link("retriever", "summarizer", dest_key="nodes")
|
| 64 |
|
| 65 |
-
# you can optionally specify your own custom readers to support additional file types.
|
| 66 |
-
# file_extractor = {".html": ...}
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
rag_cli_instance.cli()
|
| 77 |
|
| 78 |
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
# Standard library imports
|
| 4 |
import os
|
| 5 |
+
|
| 6 |
+
# Third-party imports
|
| 7 |
+
import chromadb
|
| 8 |
+
from llama_index.core import (
|
| 9 |
+
SimpleDirectoryReader,
|
| 10 |
+
VectorStoreIndex,
|
| 11 |
+
StorageContext,
|
| 12 |
+
PromptTemplate,
|
| 13 |
+
)
|
| 14 |
from llama_index.core.ingestion import IngestionPipeline
|
| 15 |
+
from llama_index.core.query_pipeline import QueryPipeline, InputComponent
|
| 16 |
+
from llama_index.core.response_synthesizers import TreeSummarize
|
| 17 |
from llama_index.core.storage.docstore import SimpleDocumentStore
|
|
|
|
| 18 |
from llama_index.cli.rag import RagCLI
|
| 19 |
from llama_index.llms.openai import OpenAI
|
| 20 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
+
def setup_environment():
|
| 24 |
+
"""Set up environment variables, ideally load from .env file for production"""
|
| 25 |
+
# os.environ["OPENAI_API_KEY"] = "sk-xxx"
|
| 26 |
+
pass
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
+
def initialize_chroma_db():
|
| 30 |
+
"""Initialize the ChromaDB client and collection"""
|
| 31 |
+
chroma_client = chromadb.EphemeralClient()
|
| 32 |
+
chroma_collection = chroma_client.create_collection("wordsmith")
|
| 33 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 34 |
+
return vector_store
|
| 35 |
|
| 36 |
|
| 37 |
+
def setup_document_storage(vector_store):
|
| 38 |
+
"""Set up document storage and load data"""
|
| 39 |
+
package_directory = os.path.dirname(os.path.abspath(__file__))
|
| 40 |
+
dataset_path = os.path.join(package_directory, "public_wordsmith_dataset")
|
| 41 |
+
reader = SimpleDirectoryReader(input_dir=dataset_path)
|
| 42 |
+
docs = reader.load_data()
|
| 43 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 44 |
+
index = VectorStoreIndex.from_documents(docs, storage_context=storage_context)
|
| 45 |
+
return index
|
| 46 |
|
|
|
|
| 47 |
|
| 48 |
+
def initialize_llm():
|
| 49 |
+
"""Initialize the Large Language Model"""
|
| 50 |
+
llm = OpenAI(api_key=os.environ["OPENAI_API_KEY"], model="gpt-4")
|
| 51 |
+
return llm
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def configure_query_pipeline(index, llm):
|
| 55 |
+
"""Configure and set up the query pipeline"""
|
| 56 |
+
prompt_str = "Please generate related movies to {query_str}"
|
| 57 |
+
prompt_tmpl = PromptTemplate(prompt_str)
|
| 58 |
+
query_pipeline = QueryPipeline(verbose=True)
|
| 59 |
|
| 60 |
+
retriever = index.as_retriever(similarity_top_k=5)
|
| 61 |
+
summarizer = TreeSummarize(llm=llm, streaming=True)
|
| 62 |
|
| 63 |
+
query_pipeline.add_modules(
|
| 64 |
+
{
|
| 65 |
+
"input": InputComponent(),
|
| 66 |
+
"retriever": retriever,
|
| 67 |
+
"summarizer": summarizer,
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
query_pipeline.add_link("input", "retriever")
|
| 71 |
+
query_pipeline.add_link("input", "summarizer", dest_key="query_str")
|
| 72 |
+
query_pipeline.add_link("retriever", "summarizer", dest_key="nodes")
|
| 73 |
|
| 74 |
+
return query_pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
def create_rag_cli(ingestion_pipeline, llm, query_pipeline):
|
| 78 |
+
"""Create the RAG CLI instance"""
|
| 79 |
+
rag_cli_instance = RagCLI(
|
| 80 |
+
ingestion_pipeline=ingestion_pipeline, llm=llm, query_pipeline=query_pipeline
|
| 81 |
+
)
|
| 82 |
+
return rag_cli_instance
|
| 83 |
|
| 84 |
|
| 85 |
def main():
|
| 86 |
+
setup_environment()
|
| 87 |
+
vector_store = initialize_chroma_db()
|
| 88 |
+
index = setup_document_storage(vector_store)
|
| 89 |
+
llm = initialize_llm()
|
| 90 |
+
query_pipeline = configure_query_pipeline(index, llm)
|
| 91 |
+
ingestion_pipeline = IngestionPipeline(vector_store=vector_store)
|
| 92 |
+
rag_cli_instance = create_rag_cli(ingestion_pipeline, llm, query_pipeline)
|
| 93 |
rag_cli_instance.cli()
|
| 94 |
|
| 95 |
|
setup.py
CHANGED
|
@@ -1,32 +1,28 @@
|
|
| 1 |
from setuptools import setup, find_packages
|
| 2 |
|
| 3 |
setup(
|
| 4 |
-
name=
|
| 5 |
-
version=
|
| 6 |
-
description=
|
| 7 |
-
long_description=open(
|
| 8 |
-
long_description_content_type=
|
| 9 |
-
url=
|
| 10 |
-
author=
|
| 11 |
-
author_email=
|
| 12 |
-
license=
|
| 13 |
packages=find_packages(),
|
| 14 |
-
install_requires=[
|
| 15 |
-
"llama-index",
|
| 16 |
-
"chromadb",
|
| 17 |
-
"llama-index-vector-stores-chroma"
|
| 18 |
-
],
|
| 19 |
classifiers=[
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
],
|
| 26 |
entry_points={
|
| 27 |
-
|
| 28 |
-
|
| 29 |
],
|
| 30 |
},
|
| 31 |
-
python_requires=
|
| 32 |
)
|
|
|
|
| 1 |
from setuptools import setup, find_packages
|
| 2 |
|
| 3 |
setup(
|
| 4 |
+
name="hello-wordsmith",
|
| 5 |
+
version="0.1.0",
|
| 6 |
+
description="A simple Python package to interface with llama-index RAG over wordsmith data.",
|
| 7 |
+
long_description=open("README.md").read(),
|
| 8 |
+
long_description_content_type="text/markdown",
|
| 9 |
+
url="https://huggingface.co/datasets/derek-at-work/test/source env/bin/activate",
|
| 10 |
+
author="Derek Johnston",
|
| 11 |
+
author_email="derek@wordsmith.ai",
|
| 12 |
+
license="MIT",
|
| 13 |
packages=find_packages(),
|
| 14 |
+
install_requires=["llama-index", "chromadb", "llama-index-vector-stores-chroma"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
classifiers=[
|
| 16 |
+
"Intended Audience :: End Users",
|
| 17 |
+
"Topic :: Software Development :: Build Tools",
|
| 18 |
+
"License :: OSI Approved :: MIT License",
|
| 19 |
+
"Programming Language :: Python :: 3",
|
| 20 |
+
"Programming Language :: Python :: 3.7",
|
| 21 |
],
|
| 22 |
entry_points={
|
| 23 |
+
"console_scripts": [
|
| 24 |
+
"hello-wordsmith=hello_wordsmith.hello_wordsmith:main",
|
| 25 |
],
|
| 26 |
},
|
| 27 |
+
python_requires=">=3.6",
|
| 28 |
)
|