lillybak commited on
Commit
42bea4f
1 Parent(s): a0059e2

added on_chat_start

Browse files
Files changed (1) hide show
  1. app.py +43 -27
app.py CHANGED
@@ -79,47 +79,63 @@ recursive_index_faiss = VectorStoreIndex(nodes=base_nodes+objects, storage_conte
79
  # We'll need to do a couple steps:
80
  # 1. Initalize our reranker using `FlagEmbeddingReranker` powered by the `BAAI/bge-reranker-large`.
81
  # 2. Set up our recursive query engine!
82
-
83
  reranker = FlagEmbeddingReranker(
84
  top_n=5,
85
  model="BAAI/bge-reranker-large",
86
  )
87
 
88
- # recursive_query_engine = recursive_index_faiss.as_query_engine(
89
- # similarity_top_k=5,
90
- # node_postprocessors=[reranker],
91
- # verbose=True
92
- # )
93
-
94
 
95
- # ChatOpenAI Templates
96
- system_template = """Use the following pieces of context to answer the user's question.
97
- If you don't know the answer, say that you don't know, do not try to make up an answer.
98
- ALWAYS return a "SOURCES" part in your answer.
99
- The "SOURCES" part should be a reference to the source inside the document from which you got your answer.
100
- You are a helpful assistant who always speaks in a pleasant tone! """
101
 
102
- user_template = """ Think through your response step by step."""
103
 
104
- #user_query = "Who are the E-VP, Operations - and how old are they?"
105
- # prompt=system_template + user_query + user_template
106
 
107
- async def recursive_fn_call(reranker, system_template, user_template, user_query=None):
108
- recursive_query_engine = recursive_index_faiss.as_query_engine(
109
- similarity_top_k=5,
110
- node_postprocessors=[reranker],
111
- verbose=True
112
- )
113
- response = recursive_query_engine.query(system_template + user_query + user_template)
114
- return response
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
117
  async def main(message: cl.Message):
118
  settings = cl.user_session.get("settings")
119
-
120
- user_query = message.content
121
 
122
- response = await recursive_fn_call(reranker, system_template, user_template, user_query=user_query)
 
 
 
 
 
 
 
 
 
 
123
  str_resp ="{}".format(response)
 
 
124
  msg = cl.Message(content= str_resp)
 
 
125
  await msg.send()
 
79
  # We'll need to do a couple steps:
80
  # 1. Initalize our reranker using `FlagEmbeddingReranker` powered by the `BAAI/bge-reranker-large`.
81
  # 2. Set up our recursive query engine!
 
82
  reranker = FlagEmbeddingReranker(
83
  top_n=5,
84
  model="BAAI/bge-reranker-large",
85
  )
86
 
 
 
 
 
 
 
87
 
88
+ def resursive_fn(reranker):
89
+ recursive_query_engine = recursive_index_faiss.as_query_engine(
90
+ similarity_top_k=5,
91
+ node_postprocessors=[reranker],
92
+ verbose=True
93
+ )
94
 
95
+ return recursive_query_engine
96
 
97
+ recursive_fn_val = resursive_fn(reranker)
 
98
 
99
+ @cl.on_chat_start
100
+ async def start_chat():
101
+ print("A new chat session has started!")
102
+
103
+
104
+
105
+
106
+ # ChatOpenAI Templates
107
+ system_template = """Use the following pieces of context to answer the user's question.
108
+ If you don't know the answer, say that you don't know, do not try to make up an answer.
109
+ ALWAYS return a "SOURCES" part in your answer.
110
+ The "SOURCES" part should be a reference to the source inside the document from which you got your answer.
111
+ You are a helpful assistant who always speaks in a pleasant tone! """
112
+
113
+ user_template = """ Think through your response step by step."""
114
+
115
+ #user_query = "Who are the E-VP, Operations - and how old are they?"
116
+
117
+ cl.user_session.set("recursive_query_engine", recursive_fn_val)
118
+
119
 
120
  @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
121
  async def main(message: cl.Message):
122
  settings = cl.user_session.get("settings")
 
 
123
 
124
+ user_query = message.content
125
+ print("inside on_message - user_query: ",user_query)
126
+ prompt=system_template + user_query + user_template
127
+
128
+ recursive_query_engine = cl.user_session.get("recursive_query_engine")
129
+ print("inside on_message - recursive_query_engine: ",recursive_query_engine)
130
+
131
+ response = await recursive_query_engine.query(prompt)
132
+
133
+ print("inside on_message - response: ",response)
134
+
135
  str_resp ="{}".format(response)
136
+
137
+ # response = await recursive_fn_call(recursive_query_engine, system_template, user_template, user_query=user_query)
138
  msg = cl.Message(content= str_resp)
139
+ print("inside on_message - after msg: ",msg)
140
+
141
  await msg.send()