camparchimedes commited on
Commit
fa183f9
ยท
verified ยท
1 Parent(s): da5d584

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -53,8 +53,8 @@ async def init():
53
  msg = cl.Message(content=f"Building Index...")
54
  await msg.send()
55
 
56
- # --build FAISS index from csv
57
- loader = CSVLoader(file_path="./data/total_faq.csv", source_column="Answer") # columns in csv: answer, question, info_url
58
  data = loader.load()
59
  documents = text_splitter.transform_documents(data)
60
  store = LocalFileStore("./cache/")
@@ -83,7 +83,7 @@ async def init():
83
  async def main(message):
84
  chain = cl.user_session.get("chain")
85
  cb = cl.AsyncLangchainCallbackHandler(
86
- stream_final_answer=False, answer_prefix_tokens=["FINAL", "ANSWER"]
87
  )
88
  cb.answer_reached = True
89
  res = await chain.acall(message, callbacks=[cb], )
@@ -93,23 +93,24 @@ async def main(message):
93
  visited_sources = set()
94
  return
95
 
96
- # --get documents from user session
97
  docs = res["source_documents"]
98
  metadatas = [doc.metadata for doc in docs]
99
  all_sources = [m["source"] for m in metadatas]
100
 
101
- for source in all_sources:
102
- if source in visited_sources:
103
- continue
104
- visited_sources.add(source)
105
- # --create text element referenced in message
106
- source_elements.append(
107
- cl.Text(content="https://www.daysoff.no" + source, name="Info_Url")
108
- #cl.Text(content="https://www.imdb.com" + source, name="Review URL")
109
- )
 
110
 
111
  if source_elements:
112
- answer += f"\nSources: {', '.join([e.content.decode('utf-8') for e in source_elements])}"
113
  else:
114
  answer += "\nNo sources found"
115
 
 
53
  msg = cl.Message(content=f"Building Index...")
54
  await msg.send()
55
 
56
+ # --builds FAISS index from csv
57
+ loader = CSVLoader(file_path="./data/total_faq.csv", source_column="Answer") # columns in csv: Answer, Question, Info_Url
58
  data = loader.load()
59
  documents = text_splitter.transform_documents(data)
60
  store = LocalFileStore("./cache/")
 
83
  async def main(message):
84
  chain = cl.user_session.get("chain")
85
  cb = cl.AsyncLangchainCallbackHandler(
86
+ stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
87
  )
88
  cb.answer_reached = True
89
  res = await chain.acall(message, callbacks=[cb], )
 
93
  visited_sources = set()
94
  return
95
 
96
+ # --documents, user session
97
  docs = res["source_documents"]
98
  metadatas = [doc.metadata for doc in docs]
99
  all_sources = [m["source"] for m in metadatas]
100
 
101
+ for doc, metadata in zip(docs, metadatas):
102
+ row_index = metadata.get("row_index", -1) # --when row_index@metadata
103
+ if row_index in [2, 8, 14]: # --incl. only rows 2, 8, and 14
104
+ source = metadata.get("source", "")
105
+ if source and source not in visited_sources:
106
+ visited_sources.add(source)
107
+
108
+ source_elements.append(
109
+ cl.Text(content="https://www.daysoff.no" + source, name="Info_Url")
110
+ )
111
 
112
  if source_elements:
113
+ answer += f"\nSources: {', '.join([e.content for e in source_elements])}"
114
  else:
115
  answer += "\nNo sources found"
116