Ross McNairn commited on
Commit
53561cb
·
1 Parent(s): 54b8802

get basic e2e example running

Browse files
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  .mypy_cache
2
  build
3
  env
4
- *.egg-info
 
 
1
  .mypy_cache
2
  build
3
  env
4
+ *.egg-info
5
+ __pycache__
hello_wordsmith/hello_wordsmith.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/path/to/your/virtualenv/bin/python
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
+ # optional, set any API keys your script may need (perhaps using python-dotenv library instead)
14
+ # os.environ["OPENAI_API_KEY"] = "sk-xxx"
15
+
16
+ from llama_index.core import VectorStoreIndex, StorageContext
17
+
18
+ chroma_client = chromadb.EphemeralClient()
19
+ chroma_collection = chroma_client.create_collection("wordsmith")
20
+ vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
21
+ storage_context = StorageContext.from_defaults(vector_store=vector_store)
22
+
23
+
24
+ package_directory = os.path.dirname(os.path.abspath(__file__))
25
+ dataset_path = os.path.join(package_directory, 'public_wordsmith_dataset')
26
+ reader = SimpleDirectoryReader(input_dir=dataset_path)
27
+ docs = reader.load_data()
28
+ index = VectorStoreIndex.from_documents(
29
+ docs, storage_context=storage_context
30
+ )
31
+
32
+ # docstore = SimpleDocumentStore()
33
+
34
+ llm = OpenAI(api_key=os.environ["OPENAI_API_KEY"], model="gpt-4")
35
+
36
+ custom_ingestion_pipeline = IngestionPipeline(
37
+ vector_store=vector_store,
38
+ )
39
+
40
+ from llama_index.core import PromptTemplate
41
+
42
+
43
+ prompt_str = "Please generate related movies to {query_str}"
44
+ prompt_tmpl = PromptTemplate(prompt_str)
45
+ query_pipeline = QueryPipeline(verbose=True)
46
+
47
+ from llama_index.core.response_synthesizers import TreeSummarize
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
+ rag_cli_instance = RagCLI(
69
+ ingestion_pipeline=custom_ingestion_pipeline,
70
+ llm=llm,
71
+ query_pipeline=query_pipeline
72
+ )
73
+
74
+
75
+ def main():
76
+ rag_cli_instance.cli()
77
+
78
+
79
+ if __name__ == "__main__":
80
+ main()
hello_wordsmith/public_wordsmith_dataset/info.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Wordsmith is a company based in Scotland.
2
+ Gigz is CTO.
3
+ Kostis and Derek are ENG.
4
+ It's pretty lit.
setup.py CHANGED
@@ -23,5 +23,10 @@ setup(
23
  'Programming Language :: Python :: 3',
24
  'Programming Language :: Python :: 3.7',
25
  ],
 
 
 
 
 
26
  python_requires='>=3.6',
27
  )
 
23
  'Programming Language :: Python :: 3',
24
  'Programming Language :: Python :: 3.7',
25
  ],
26
+ entry_points={
27
+ 'console_scripts': [
28
+ 'hello-wordsmith=hello_wordsmith.hello_wordsmith:main',
29
+ ],
30
+ },
31
  python_requires='>=3.6',
32
  )