gabrielaltay commited on
Commit
6c729fe
·
1 Parent(s): 385ebea

chain format

Browse files
Files changed (1) hide show
  1. app.py +18 -42
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from collections import defaultdict
2
  import json
 
3
  import os
4
  import re
5
 
@@ -50,25 +51,25 @@ PROMPT_TEMPLATES = {
50
 
51
  {context}
52
 
53
- Question: {question}""",
54
  "v2": PREAMBLE
55
  + """ Each snippet starts with a header that includes a unique snippet number (snippet_num), a legis_id, and a title. Your response should cite particular snippets using legis_id and title. If you don't know how to respond, just tell the user.
56
 
57
  {context}
58
 
59
- Question: {question}""",
60
  "v3": PREAMBLE
61
  + """ Each excerpt starts with a header that includes a legis_id, and a title followed by one or more text snippets. When using text snippets in your response, you should cite the legis_id and title. If you don't know how to respond, just tell the user.
62
 
63
  {context}
64
 
65
- Question: {question}""",
66
  "v4": PREAMBLE
67
  + """ The excerpts are formatted as a JSON list. Each JSON object has "legis_id", "title", and "snippets" keys. If a snippet is useful in writing part of your response, then cite the "title" and "legis_id" in the response. If you don't know how to respond, just tell the user.
68
 
69
  {context}
70
 
71
- Query: {question}""",
72
  }
73
 
74
 
@@ -421,44 +422,19 @@ with query_tab:
421
  search_kwargs={"k": SS["n_ret_docs"], "filter": vs_filter},
422
  )
423
  prompt = PromptTemplate.from_template(SS["prompt_template"])
424
-
425
- # # takes in a dict. adds context key with formatted docs
426
- # rag_chain_from_docs = (
427
- # RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
428
- # | prompt
429
- # | llm
430
- # | StrOutputParser()
431
- # )
432
-
433
- # # takes in a query string.
434
- # # passes to retriever and passthru
435
- # # assign answer
436
- # rag_chain_with_source = RunnableParallel(
437
- # {"context": retriever, "question": RunnablePassthrough()}
438
- # ).assign(answer=rag_chain_from_docs)
439
-
440
-
441
- # takes in a dict. adds context key with formatted docs
442
- rag_chain_from_docs = (
443
- RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
444
- | prompt
445
- | llm
446
- | StrOutputParser()
447
  )
448
 
449
- # takes in a query string.
450
- # passes to retriever and passthru
451
- # assign answer
452
- rag_chain_with_source = RunnableParallel(
453
- {"context": retriever, "question": RunnablePassthrough()}
454
- ).assign(answer=rag_chain_from_docs)
455
-
456
-
457
-
458
- print(rag_chain_with_source)
459
-
460
  with get_openai_callback() as cb:
461
- SS["out"] = rag_chain_with_source.invoke(SS["query"])
462
  SS["cb"] = cb
463
 
464
  if "out" in SS:
@@ -476,12 +452,12 @@ with query_tab:
476
  st.warning(SS["cb"])
477
 
478
  with st.container(border=True):
479
- doc_grps = group_docs(SS["out"]["context"])
480
  st.write(
481
  "Retrieved Chunks (note that you may need to 'right click' on links in the expanders to follow them)"
482
  )
483
  for legis_id, doc_grp in doc_grps:
484
  write_doc_grp(legis_id, doc_grp)
485
 
486
- # with st.expander("Debug doc format"):
487
- # st.text_area("formatted docs", value=format_docs(SS["out"]["context"]), height=600)
 
1
  from collections import defaultdict
2
  import json
3
+ from operator import itemgetter
4
  import os
5
  import re
6
 
 
51
 
52
  {context}
53
 
54
+ Question: {query}""",
55
  "v2": PREAMBLE
56
  + """ Each snippet starts with a header that includes a unique snippet number (snippet_num), a legis_id, and a title. Your response should cite particular snippets using legis_id and title. If you don't know how to respond, just tell the user.
57
 
58
  {context}
59
 
60
+ Question: {query}""",
61
  "v3": PREAMBLE
62
  + """ Each excerpt starts with a header that includes a legis_id, and a title followed by one or more text snippets. When using text snippets in your response, you should cite the legis_id and title. If you don't know how to respond, just tell the user.
63
 
64
  {context}
65
 
66
+ Question: {query}""",
67
  "v4": PREAMBLE
68
  + """ The excerpts are formatted as a JSON list. Each JSON object has "legis_id", "title", and "snippets" keys. If a snippet is useful in writing part of your response, then cite the "title" and "legis_id" in the response. If you don't know how to respond, just tell the user.
69
 
70
  {context}
71
 
72
+ Query: {query}""",
73
  }
74
 
75
 
 
422
  search_kwargs={"k": SS["n_ret_docs"], "filter": vs_filter},
423
  )
424
  prompt = PromptTemplate.from_template(SS["prompt_template"])
425
+ rag_chain = (
426
+ RunnableParallel(
427
+ {
428
+ "docs": retriever, # list of docs
429
+ "query": RunnablePassthrough(), # str
430
+ }
431
+ )
432
+ .assign(context=(lambda x: format_docs(x["docs"])))
433
+ .assign(answer=prompt | llm | StrOutputParser())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
  )
435
 
 
 
 
 
 
 
 
 
 
 
 
436
  with get_openai_callback() as cb:
437
+ SS["out"] = rag_chain.invoke(SS["query"])
438
  SS["cb"] = cb
439
 
440
  if "out" in SS:
 
452
  st.warning(SS["cb"])
453
 
454
  with st.container(border=True):
455
+ doc_grps = group_docs(SS["out"]["docs"])
456
  st.write(
457
  "Retrieved Chunks (note that you may need to 'right click' on links in the expanders to follow them)"
458
  )
459
  for legis_id, doc_grp in doc_grps:
460
  write_doc_grp(legis_id, doc_grp)
461
 
462
+ with st.expander("Debug"):
463
+ st.write(SS["out"])