Ross McNairn commited on
Commit
feb2d65
·
1 Parent(s): 6921f91

add basic prompts

Browse files
Files changed (2) hide show
  1. hello_wordsmith/wordsmith.py +46 -16
  2. setup.py +2 -2
hello_wordsmith/wordsmith.py CHANGED
@@ -1,19 +1,16 @@
1
- from argparse import ArgumentParser
2
  import os
3
  import sys
4
 
5
  import chromadb
6
- from llama_index.core import (
7
- SimpleDirectoryReader,
8
- VectorStoreIndex,
9
- StorageContext,
10
- PromptTemplate,
11
- )
12
  from llama_index.core.ingestion import IngestionPipeline
13
- from llama_index.core.query_pipeline import QueryPipeline, InputComponent
 
14
  from llama_index.core.response_synthesizers import TreeSummarize
15
  from llama_index.core.storage.docstore import SimpleDocumentStore
16
- from llama_index.cli.rag import RagCLI
17
  from llama_index.llms.openai import OpenAI
18
  from llama_index.vector_stores.chroma import ChromaVectorStore
19
 
@@ -43,14 +40,50 @@ def initialize_llm():
43
  return llm
44
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def configure_query_pipeline(index, llm):
47
  """Configure and set up the query pipeline"""
48
- prompt_str = "Please generate related movies to {query_str}"
49
- prompt_tmpl = PromptTemplate(prompt_str)
50
  query_pipeline = QueryPipeline()
51
 
52
  retriever = index.as_retriever(similarity_top_k=5)
53
- summarizer = TreeSummarize(llm=llm, streaming=True)
 
 
54
 
55
  query_pipeline.add_modules(
56
  {
@@ -67,7 +100,6 @@ def configure_query_pipeline(index, llm):
67
 
68
 
69
  class WordsmithRAGCLI(RagCLI):
70
-
71
  def cli(self) -> None:
72
  """
73
  Entrypoint for CLI tool.
@@ -86,9 +118,7 @@ def main():
86
  query_pipeline = configure_query_pipeline(index, llm)
87
  ingestion_pipeline = IngestionPipeline(vector_store=vector_store)
88
  rag_cli_instance = WordsmithRAGCLI(
89
- ingestion_pipeline=ingestion_pipeline,
90
- llm=llm,
91
- query_pipeline=query_pipeline
92
  )
93
  rag_cli_instance.cli()
94
 
 
 
1
  import os
2
  import sys
3
 
4
  import chromadb
5
+ from llama_index.cli.rag import RagCLI
6
+ from llama_index.core import (ChatPromptTemplate, SimpleDirectoryReader,
7
+ StorageContext, VectorStoreIndex)
8
+ from llama_index.core.base.llms.types import ChatMessage, MessageRole
 
 
9
  from llama_index.core.ingestion import IngestionPipeline
10
+ from llama_index.core.prompts.base import ChatPromptTemplate
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.llms.openai import OpenAI
15
  from llama_index.vector_stores.chroma import ChromaVectorStore
16
 
 
40
  return llm
41
 
42
 
43
+ _system_prompt = ChatMessage(
44
+ content=(
45
+ "You are an expert Q&A analyst representing Wordsmith in front of "
46
+ "potentially interested users.\n"
47
+ "If the question is related to Wordsmith in any way, "
48
+ "answer the query using the provided context information.\n"
49
+ "If you can't find the answer in the provided context information, "
50
+ "simply say you don't have enough information to answer the query.\n"
51
+ "Always be polite and professional.\n"
52
+ "Some rules to follow:\n"
53
+ "1. Never directly reference the given context in your answer.\n"
54
+ "2. Avoid statements like 'Based on the context, ...' or "
55
+ "'The context information ...', etc."
56
+ ),
57
+ role=MessageRole.SYSTEM,
58
+ )
59
+
60
+ _chat_template_messages = [
61
+ _system_prompt,
62
+ ChatMessage(
63
+ content=(
64
+ "Context information from multiple sources is below.\n"
65
+ "---------------------\n"
66
+ "{context_str}\n"
67
+ "---------------------\n"
68
+ "Given the information from multiple sources and not prior knowledge, "
69
+ "answer the query.\n"
70
+ "Query: {query_str}\n"
71
+ "Answer: "
72
+ ),
73
+ role=MessageRole.USER,
74
+ ),
75
+ ]
76
+
77
+
78
  def configure_query_pipeline(index, llm):
79
  """Configure and set up the query pipeline"""
80
+ text_qa_chat_template = ChatPromptTemplate.from_messages(_chat_template_messages)
 
81
  query_pipeline = QueryPipeline()
82
 
83
  retriever = index.as_retriever(similarity_top_k=5)
84
+ summarizer = TreeSummarize(
85
+ llm=llm, streaming=True, summary_template=text_qa_chat_template
86
+ )
87
 
88
  query_pipeline.add_modules(
89
  {
 
100
 
101
 
102
  class WordsmithRAGCLI(RagCLI):
 
103
  def cli(self) -> None:
104
  """
105
  Entrypoint for CLI tool.
 
118
  query_pipeline = configure_query_pipeline(index, llm)
119
  ingestion_pipeline = IngestionPipeline(vector_store=vector_store)
120
  rag_cli_instance = WordsmithRAGCLI(
121
+ ingestion_pipeline=ingestion_pipeline, llm=llm, query_pipeline=query_pipeline
 
 
122
  )
123
  rag_cli_instance.cli()
124
 
setup.py CHANGED
@@ -1,4 +1,4 @@
1
- from setuptools import setup, find_packages
2
 
3
  setup(
4
  name="hello-wordsmith",
@@ -12,7 +12,7 @@ setup(
12
  license="MIT",
13
  packages=find_packages(),
14
  package_data={
15
- 'hello_wordsmith': ['public_wordsmith_dataset/*'],
16
  },
17
  install_requires=[
18
  "chromadb~=0.5.0",
 
1
+ from setuptools import find_packages, setup
2
 
3
  setup(
4
  name="hello-wordsmith",
 
12
  license="MIT",
13
  packages=find_packages(),
14
  package_data={
15
+ "hello_wordsmith": ["public_wordsmith_dataset/*"],
16
  },
17
  install_requires=[
18
  "chromadb~=0.5.0",