oskardotid commited on
Commit
bbcc74a
·
1 Parent(s): 0112b0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -5
app.py CHANGED
@@ -33,15 +33,58 @@ from aimakerspace.openai_utils.prompts import (
33
  AssistantRolePrompt,
34
  )
35
 
36
- # Chainlit App
37
- # ChatOpenAI Templates
38
- system_template = """You are a helpful assistant who always speaks in a pleasant tone!
 
 
 
 
 
 
39
  """
40
 
41
- user_template = """{input}
42
- Think through your response step by step.
 
 
 
43
  """
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  @cl.on_chat_start # marks a function that will be executed at the start of a user session
46
  async def start_chat():
47
  settings = {
 
33
  AssistantRolePrompt,
34
  )
35
 
36
+ RAQA_PROMPT_TEMPLATE = """
37
+ Use the provided context to answer the user's query.
38
+
39
+ You may not answer the user's query unless there is specific context in the following text.
40
+
41
+ If you do not know the answer, or cannot answer, please respond with "I don't know".
42
+
43
+ Context:
44
+ {context}
45
  """
46
 
47
+ raqa_prompt = SystemRolePrompt(RAQA_PROMPT_TEMPLATE)
48
+
49
+ USER_PROMPT_TEMPLATE = """
50
+ User Query:
51
+ {user_query}
52
  """
53
 
54
+ user_prompt = UserRolePrompt(USER_PROMPT_TEMPLATE)
55
+
56
+ class RetrievalAugmentedQAPipeline:
57
+ def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
58
+ self.llm = llm
59
+ self.vector_db_retriever = vector_db_retriever
60
+
61
+ def run_pipeline(self, user_query: str) -> str:
62
+ context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
63
+
64
+ context_prompt = ""
65
+ for context in context_list:
66
+ context_prompt += context[0] + "\n"
67
+
68
+ formatted_system_prompt = raqa_prompt.create_message(context=context_prompt)
69
+
70
+ formatted_user_prompt = user_prompt.create_message(user_query=user_query)
71
+
72
+ return self.llm.run([formatted_system_prompt, formatted_user_prompt])
73
+
74
+
75
+ chat_openai = ChatOpenAI()
76
+
77
+ retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
78
+ vector_db_retriever=vector_db,
79
+ llm=chat_openai
80
+ )
81
+
82
+ # Chainlit App
83
+ # ChatOpenAI Templates
84
+ system_template = RAQA_PROMPT_TEMPLATE
85
+
86
+ user_template = USER_PROMPT_TEMPLATE
87
+
88
  @cl.on_chat_start # marks a function that will be executed at the start of a user session
89
  async def start_chat():
90
  settings = {