Joshua Sundance Bailey commited on
Commit
a190cd4
·
unverified ·
2 Parent(s): d992641 f8e9121

Merge pull request #38 from joshuasundance-swca/lcel

Browse files
langchain-streamlit-demo/app.py CHANGED
@@ -7,7 +7,6 @@ import anthropic
7
  import langsmith.utils
8
  import openai
9
  import streamlit as st
10
- from langchain.callbacks import StreamlitCallbackHandler
11
  from langchain.callbacks.base import BaseCallbackHandler
12
  from langchain.callbacks.tracers.langchain import LangChainTracer, wait_for_all_tracers
13
  from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
@@ -26,8 +25,8 @@ from langchain.vectorstores import FAISS
26
  from langsmith.client import Client
27
  from streamlit_feedback import streamlit_feedback
28
 
29
- from qagen import get_qa_gen_chain, combine_qa_pair_lists
30
- from summarize import get_summarization_chain
31
 
32
  __version__ = "0.0.10"
33
 
@@ -124,12 +123,15 @@ MIN_CHUNK_OVERLAP = 0
124
  MAX_CHUNK_OVERLAP = 10000
125
  DEFAULT_CHUNK_OVERLAP = 0
126
 
 
 
127
 
128
  @st.cache_data
129
  def get_texts_and_retriever(
130
  uploaded_file_bytes: bytes,
131
  chunk_size: int = DEFAULT_CHUNK_SIZE,
132
  chunk_overlap: int = DEFAULT_CHUNK_OVERLAP,
 
133
  ) -> Tuple[List[Document], BaseRetriever]:
134
  with NamedTemporaryFile() as temp_file:
135
  temp_file.write(uploaded_file_bytes)
@@ -145,10 +147,10 @@ def get_texts_and_retriever(
145
  embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
146
 
147
  bm25_retriever = BM25Retriever.from_documents(texts)
148
- bm25_retriever.k = 4
149
 
150
  faiss_vectorstore = FAISS.from_documents(texts, embeddings)
151
- faiss_retriever = faiss_vectorstore.as_retriever(search_kwargs={"k": 4})
152
 
153
  ensemble_retriever = EnsembleRetriever(
154
  retrievers=[bm25_retriever, faiss_retriever],
@@ -200,15 +202,23 @@ with sidebar:
200
  help="Uploaded document will provide context for the chat.",
201
  )
202
 
 
 
 
 
 
 
 
 
203
  chunk_size = st.slider(
204
- label="chunk_size",
205
  help="Size of each chunk of text",
206
  min_value=MIN_CHUNK_SIZE,
207
  max_value=MAX_CHUNK_SIZE,
208
  value=DEFAULT_CHUNK_SIZE,
209
  )
210
  chunk_overlap = st.slider(
211
- label="chunk_overlap",
212
  help="Number of characters to overlap between chunks",
213
  min_value=MIN_CHUNK_OVERLAP,
214
  max_value=MAX_CHUNK_OVERLAP,
@@ -251,6 +261,7 @@ with sidebar:
251
  uploaded_file_bytes=uploaded_file.getvalue(),
252
  chunk_size=chunk_size,
253
  chunk_overlap=chunk_overlap,
 
254
  )
255
  else:
256
  st.error("Please enter a valid OpenAI API key.", icon="❌")
@@ -311,7 +322,7 @@ with sidebar:
311
  if provider_api_key:
312
  if st.session_state.provider == "OpenAI":
313
  st.session_state.llm = ChatOpenAI(
314
- model=model,
315
  openai_api_key=provider_api_key,
316
  temperature=temperature,
317
  streaming=True,
@@ -319,7 +330,7 @@ if provider_api_key:
319
  )
320
  elif st.session_state.provider == "Anthropic":
321
  st.session_state.llm = ChatAnthropic(
322
- model_name=model,
323
  anthropic_api_key=provider_api_key,
324
  temperature=temperature,
325
  streaming=True,
@@ -327,7 +338,7 @@ if provider_api_key:
327
  )
328
  elif st.session_state.provider == "Anyscale Endpoints":
329
  st.session_state.llm = ChatAnyscale(
330
- model=model,
331
  anyscale_api_key=provider_api_key,
332
  temperature=temperature,
333
  streaming=True,
@@ -348,38 +359,17 @@ for msg in STMEMORY.messages:
348
 
349
  # --- Current Chat ---
350
  if st.session_state.llm:
351
- # --- Document Chat ---
352
- if st.session_state.retriever:
353
- if document_chat_chain_type == "Summarization":
354
- st.session_state.doc_chain = "summarization"
355
- elif document_chat_chain_type == "Q&A Generation":
356
- st.session_state.doc_chain = get_qa_gen_chain(st.session_state.llm)
357
-
358
- else:
359
- st.session_state.doc_chain = RetrievalQA.from_chain_type(
360
- llm=st.session_state.llm,
361
- chain_type=document_chat_chain_type,
362
- retriever=st.session_state.retriever,
363
- memory=MEMORY,
364
- )
365
-
366
- else:
367
- # --- Regular Chat ---
368
- chat_prompt = ChatPromptTemplate.from_messages(
369
- [
370
- (
371
- "system",
372
- system_prompt + "\nIt's currently {time}.",
373
- ),
374
- MessagesPlaceholder(variable_name="chat_history"),
375
- ("human", "{query}"),
376
- ],
377
- ).partial(time=lambda: str(datetime.now()))
378
- st.session_state.chain = LLMChain(
379
- prompt=chat_prompt,
380
- llm=st.session_state.llm,
381
- memory=MEMORY,
382
- )
383
 
384
  # --- Chat Input ---
385
  prompt = st.chat_input(placeholder="Ask me a question!")
@@ -395,89 +385,70 @@ if st.session_state.llm:
395
  if st.session_state.ls_tracer:
396
  callbacks.append(st.session_state.ls_tracer)
397
 
 
 
 
 
 
 
 
398
  use_document_chat = all(
399
  [
400
  document_chat,
401
- st.session_state.doc_chain,
402
  st.session_state.retriever,
403
  ],
404
  )
405
 
406
- try:
407
- full_response: Union[str, None]
408
- if use_document_chat:
409
- if document_chat_chain_type == "Summarization":
410
- st.session_state.doc_chain = get_summarization_chain(
411
- st.session_state.llm,
412
- prompt,
413
- )
414
- full_response = st.session_state.doc_chain.run(
415
- st.session_state.texts,
416
- callbacks=callbacks,
417
- tags=["Streamlit Chat"],
418
- )
419
-
420
- st.markdown(full_response)
421
- elif document_chat_chain_type == "Q&A Generation":
422
- config: Dict[str, Any] = dict(
423
- callbacks=callbacks,
424
- tags=["Streamlit Chat"],
425
- )
426
- if st.session_state.provider == "Anthropic":
427
- config["max_concurrency"] = 5
428
- raw_results = st.session_state.doc_chain.batch(
429
- [
430
- {"input": doc.page_content, "prompt": prompt}
431
- for doc in st.session_state.texts
432
- ],
433
- config,
434
- )
435
- results = combine_qa_pair_lists(raw_results).QuestionAnswerPairs
436
-
437
- def _to_str(idx, qap):
438
- question_piece = f"{idx}. **Q:** {qap.question}"
439
- whitespace = " " * (len(str(idx)) + 2)
440
- answer_piece = f"{whitespace}**A:** {qap.answer}"
441
- return f"{question_piece}\n\n{answer_piece}"
442
-
443
- full_response = "\n\n".join(
444
- [
445
- _to_str(idx, qap)
446
- for idx, qap in enumerate(results, start=1)
447
- ],
448
- )
449
-
450
- st.markdown(full_response)
451
-
452
- else:
453
- st_handler = StreamlitCallbackHandler(st.container())
454
- callbacks.append(st_handler)
455
- full_response = st.session_state.doc_chain(
456
- {"query": prompt},
457
- callbacks=callbacks,
458
- tags=["Streamlit Chat"],
459
- return_only_outputs=True,
460
- )[st.session_state.doc_chain.output_key]
461
- st_handler._complete_current_thought()
462
- st.markdown(full_response)
463
  else:
464
- message_placeholder = st.empty()
465
- stream_handler = StreamHandler(message_placeholder)
466
- callbacks.append(stream_handler)
467
- full_response = st.session_state.chain(
468
- {"query": prompt},
469
- callbacks=callbacks,
470
- tags=["Streamlit Chat"],
471
- return_only_outputs=True,
472
- )[st.session_state.chain.output_key]
473
- message_placeholder.markdown(full_response)
 
 
 
 
 
 
 
 
 
 
 
 
474
  except (openai.error.AuthenticationError, anthropic.AuthenticationError):
475
  st.error(
476
  f"Please enter a valid {st.session_state.provider} API key.",
477
  icon="❌",
478
  )
479
- full_response = None
480
- if full_response:
 
 
481
  # --- Tracing ---
482
  if st.session_state.client:
483
  st.session_state.run = RUN_COLLECTOR.traced_runs[0]
 
7
  import langsmith.utils
8
  import openai
9
  import streamlit as st
 
10
  from langchain.callbacks.base import BaseCallbackHandler
11
  from langchain.callbacks.tracers.langchain import LangChainTracer, wait_for_all_tracers
12
  from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
 
25
  from langsmith.client import Client
26
  from streamlit_feedback import streamlit_feedback
27
 
28
+ from qagen import get_rag_qa_gen_chain
29
+ from summarize import get_rag_summarization_chain
30
 
31
  __version__ = "0.0.10"
32
 
 
123
  MAX_CHUNK_OVERLAP = 10000
124
  DEFAULT_CHUNK_OVERLAP = 0
125
 
126
+ DEFAULT_RETRIEVER_K = 4
127
+
128
 
129
  @st.cache_data
130
  def get_texts_and_retriever(
131
  uploaded_file_bytes: bytes,
132
  chunk_size: int = DEFAULT_CHUNK_SIZE,
133
  chunk_overlap: int = DEFAULT_CHUNK_OVERLAP,
134
+ k: int = DEFAULT_RETRIEVER_K,
135
  ) -> Tuple[List[Document], BaseRetriever]:
136
  with NamedTemporaryFile() as temp_file:
137
  temp_file.write(uploaded_file_bytes)
 
147
  embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
148
 
149
  bm25_retriever = BM25Retriever.from_documents(texts)
150
+ bm25_retriever.k = k
151
 
152
  faiss_vectorstore = FAISS.from_documents(texts, embeddings)
153
+ faiss_retriever = faiss_vectorstore.as_retriever(search_kwargs={"k": k})
154
 
155
  ensemble_retriever = EnsembleRetriever(
156
  retrievers=[bm25_retriever, faiss_retriever],
 
202
  help="Uploaded document will provide context for the chat.",
203
  )
204
 
205
+ k = st.slider(
206
+ label="Number of Chunks",
207
+ help="How many document chunks will be used for context?",
208
+ value=DEFAULT_RETRIEVER_K,
209
+ min_value=1,
210
+ max_value=10,
211
+ )
212
+
213
  chunk_size = st.slider(
214
+ label="Number of Tokens per Chunk",
215
  help="Size of each chunk of text",
216
  min_value=MIN_CHUNK_SIZE,
217
  max_value=MAX_CHUNK_SIZE,
218
  value=DEFAULT_CHUNK_SIZE,
219
  )
220
  chunk_overlap = st.slider(
221
+ label="Chunk Overlap",
222
  help="Number of characters to overlap between chunks",
223
  min_value=MIN_CHUNK_OVERLAP,
224
  max_value=MAX_CHUNK_OVERLAP,
 
261
  uploaded_file_bytes=uploaded_file.getvalue(),
262
  chunk_size=chunk_size,
263
  chunk_overlap=chunk_overlap,
264
+ k=k,
265
  )
266
  else:
267
  st.error("Please enter a valid OpenAI API key.", icon="❌")
 
322
  if provider_api_key:
323
  if st.session_state.provider == "OpenAI":
324
  st.session_state.llm = ChatOpenAI(
325
+ model_name=model,
326
  openai_api_key=provider_api_key,
327
  temperature=temperature,
328
  streaming=True,
 
330
  )
331
  elif st.session_state.provider == "Anthropic":
332
  st.session_state.llm = ChatAnthropic(
333
+ model=model,
334
  anthropic_api_key=provider_api_key,
335
  temperature=temperature,
336
  streaming=True,
 
338
  )
339
  elif st.session_state.provider == "Anyscale Endpoints":
340
  st.session_state.llm = ChatAnyscale(
341
+ model_name=model,
342
  anyscale_api_key=provider_api_key,
343
  temperature=temperature,
344
  streaming=True,
 
359
 
360
  # --- Current Chat ---
361
  if st.session_state.llm:
362
+ # --- Regular Chat ---
363
+ chat_prompt = ChatPromptTemplate.from_messages(
364
+ [
365
+ (
366
+ "system",
367
+ system_prompt + "\nIt's currently {time}.",
368
+ ),
369
+ MessagesPlaceholder(variable_name="chat_history"),
370
+ ("human", "{query}"),
371
+ ],
372
+ ).partial(time=lambda: str(datetime.now()))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
 
374
  # --- Chat Input ---
375
  prompt = st.chat_input(placeholder="Ask me a question!")
 
385
  if st.session_state.ls_tracer:
386
  callbacks.append(st.session_state.ls_tracer)
387
 
388
+ config: Dict[str, Any] = dict(
389
+ callbacks=callbacks,
390
+ tags=["Streamlit Chat"],
391
+ )
392
+ if st.session_state.provider == "Anthropic":
393
+ config["max_concurrency"] = 5
394
+
395
  use_document_chat = all(
396
  [
397
  document_chat,
 
398
  st.session_state.retriever,
399
  ],
400
  )
401
 
402
+ full_response: Union[str, None] = None
403
+
404
+ message_placeholder = st.empty()
405
+ stream_handler = StreamHandler(message_placeholder)
406
+ callbacks.append(stream_handler)
407
+
408
+ def get_rag_runnable():
409
+ if document_chat_chain_type == "Q&A Generation":
410
+ return get_rag_qa_gen_chain(
411
+ st.session_state.retriever,
412
+ st.session_state.llm,
413
+ )
414
+ elif document_chat_chain_type == "Summarization":
415
+ return get_rag_summarization_chain(
416
+ prompt,
417
+ st.session_state.retriever,
418
+ st.session_state.llm,
419
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  else:
421
+ return RetrievalQA.from_chain_type(
422
+ llm=st.session_state.llm,
423
+ chain_type=document_chat_chain_type,
424
+ retriever=st.session_state.retriever,
425
+ memory=MEMORY,
426
+ output_key="output_text",
427
+ ) | (lambda output: output["output_text"])
428
+
429
+ st.session_state.chain = (
430
+ get_rag_runnable()
431
+ if use_document_chat
432
+ else LLMChain(
433
+ prompt=chat_prompt,
434
+ llm=st.session_state.llm,
435
+ memory=MEMORY,
436
+ )
437
+ | (lambda output: output["text"])
438
+ )
439
+
440
+ try:
441
+ full_response = st.session_state.chain.invoke(prompt, config)
442
+
443
  except (openai.error.AuthenticationError, anthropic.AuthenticationError):
444
  st.error(
445
  f"Please enter a valid {st.session_state.provider} API key.",
446
  icon="❌",
447
  )
448
+
449
+ if full_response is not None:
450
+ message_placeholder.markdown(full_response)
451
+
452
  # --- Tracing ---
453
  if st.session_state.client:
454
  st.session_state.run = RUN_COLLECTOR.traced_runs[0]
langchain-streamlit-demo/qagen.py CHANGED
@@ -1,4 +1,3 @@
1
- from functools import reduce
2
  from typing import List
3
 
4
  from langchain.output_parsers import PydanticOutputParser, OutputFixingParser
@@ -6,7 +5,8 @@ from langchain.prompts.chat import (
6
  ChatPromptTemplate,
7
  )
8
  from langchain.schema.language_model import BaseLanguageModel
9
- from langchain.schema.runnable import RunnableSequence
 
10
  from pydantic import BaseModel, Field
11
 
12
 
@@ -14,10 +14,24 @@ class QuestionAnswerPair(BaseModel):
14
  question: str = Field(..., description="The question that will be answered.")
15
  answer: str = Field(..., description="The answer to the question that was asked.")
16
 
 
 
 
 
 
 
17
 
18
  class QuestionAnswerPairList(BaseModel):
19
  QuestionAnswerPairs: List[QuestionAnswerPair]
20
 
 
 
 
 
 
 
 
 
21
 
22
  PYDANTIC_PARSER: PydanticOutputParser = PydanticOutputParser(
23
  pydantic_object=QuestionAnswerPairList,
@@ -35,7 +49,7 @@ Do not provide additional commentary and do not wrap your response in Markdown f
35
  templ2 = """{prompt}
36
  Please create question/answer pairs, in the specified JSON format, for the following text:
37
  ----------------
38
- {input}"""
39
  CHAT_PROMPT = ChatPromptTemplate.from_messages(
40
  [
41
  ("system", templ1),
@@ -44,26 +58,15 @@ CHAT_PROMPT = ChatPromptTemplate.from_messages(
44
  ).partial(format_instructions=PYDANTIC_PARSER.get_format_instructions)
45
 
46
 
47
- def combine_qa_pair_lists(
48
- qa_pair_lists: List[QuestionAnswerPairList],
49
- ) -> QuestionAnswerPairList:
50
- def reducer(
51
- accumulator: QuestionAnswerPairList,
52
- current: QuestionAnswerPairList,
53
- ) -> QuestionAnswerPairList:
54
- return QuestionAnswerPairList(
55
- QuestionAnswerPairs=accumulator.QuestionAnswerPairs
56
- + current.QuestionAnswerPairs,
57
- )
58
-
59
- return reduce(
60
- reducer,
61
- qa_pair_lists,
62
- QuestionAnswerPairList(QuestionAnswerPairs=[]),
63
- )
64
-
65
-
66
- def get_qa_gen_chain(llm: BaseLanguageModel) -> RunnableSequence:
67
  return (
68
- CHAT_PROMPT | llm | OutputFixingParser.from_llm(llm=llm, parser=PYDANTIC_PARSER)
 
 
 
 
69
  )
 
 
1
  from typing import List
2
 
3
  from langchain.output_parsers import PydanticOutputParser, OutputFixingParser
 
5
  ChatPromptTemplate,
6
  )
7
  from langchain.schema.language_model import BaseLanguageModel
8
+ from langchain.schema.retriever import BaseRetriever
9
+ from langchain.schema.runnable import RunnablePassthrough, RunnableSequence
10
  from pydantic import BaseModel, Field
11
 
12
 
 
14
  question: str = Field(..., description="The question that will be answered.")
15
  answer: str = Field(..., description="The answer to the question that was asked.")
16
 
17
+ def to_str(self, idx: int) -> str:
18
+ question_piece = f"{idx}. **Q:** {self.question}"
19
+ whitespace = " " * (len(str(idx)) + 2)
20
+ answer_piece = f"{whitespace}**A:** {self.answer}"
21
+ return f"{question_piece}\n\n{answer_piece}"
22
+
23
 
24
  class QuestionAnswerPairList(BaseModel):
25
  QuestionAnswerPairs: List[QuestionAnswerPair]
26
 
27
+ def to_str(self) -> str:
28
+ return "\n\n".join(
29
+ [
30
+ qap.to_str(idx)
31
+ for idx, qap in enumerate(self.QuestionAnswerPairs, start=1)
32
+ ],
33
+ )
34
+
35
 
36
  PYDANTIC_PARSER: PydanticOutputParser = PydanticOutputParser(
37
  pydantic_object=QuestionAnswerPairList,
 
49
  templ2 = """{prompt}
50
  Please create question/answer pairs, in the specified JSON format, for the following text:
51
  ----------------
52
+ {context}"""
53
  CHAT_PROMPT = ChatPromptTemplate.from_messages(
54
  [
55
  ("system", templ1),
 
58
  ).partial(format_instructions=PYDANTIC_PARSER.get_format_instructions)
59
 
60
 
61
+ def get_rag_qa_gen_chain(
62
+ retriever: BaseRetriever,
63
+ llm: BaseLanguageModel,
64
+ input_key: str = "prompt",
65
+ ) -> RunnableSequence:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  return (
67
+ {"context": retriever, input_key: RunnablePassthrough()}
68
+ | CHAT_PROMPT
69
+ | llm
70
+ | OutputFixingParser.from_llm(llm=llm, parser=PYDANTIC_PARSER)
71
+ | (lambda parsed_output: parsed_output.to_str())
72
  )
langchain-streamlit-demo/summarize.py CHANGED
@@ -2,6 +2,8 @@ from langchain.chains.base import Chain
2
  from langchain.chains.summarize import load_summarize_chain
3
  from langchain.prompts import PromptTemplate
4
  from langchain.schema.language_model import BaseLanguageModel
 
 
5
 
6
  prompt_template = """Write a concise summary of the following text, based on the user input.
7
  User input: {query}
@@ -49,3 +51,16 @@ def get_summarization_chain(
49
  input_key="input_documents",
50
  output_key="output_text",
51
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from langchain.chains.summarize import load_summarize_chain
3
  from langchain.prompts import PromptTemplate
4
  from langchain.schema.language_model import BaseLanguageModel
5
+ from langchain.schema.retriever import BaseRetriever
6
+ from langchain.schema.runnable import RunnableSequence, RunnablePassthrough
7
 
8
  prompt_template = """Write a concise summary of the following text, based on the user input.
9
  User input: {query}
 
51
  input_key="input_documents",
52
  output_key="output_text",
53
  )
54
+
55
+
56
+ def get_rag_summarization_chain(
57
+ prompt: str,
58
+ retriever: BaseRetriever,
59
+ llm: BaseLanguageModel,
60
+ input_key: str = "prompt",
61
+ ) -> RunnableSequence:
62
+ return (
63
+ {"input_documents": retriever, input_key: RunnablePassthrough()}
64
+ | get_summarization_chain(llm, prompt)
65
+ | (lambda output: output["output_text"])
66
+ )