Upload 2 files
Browse files- app.py +27 -9
- autoqa_chains.py +4 -46
app.py
CHANGED
|
@@ -15,7 +15,7 @@ from chat_chains import (
|
|
| 15 |
parse_context_and_question,
|
| 16 |
ai_response_format,
|
| 17 |
)
|
| 18 |
-
from autoqa_chains import auto_qa_chain
|
| 19 |
from chain_of_density import chain_of_density_chain
|
| 20 |
from insights_bullet_chain import insights_bullet_chain
|
| 21 |
from insights_mind_map_chain import insights_mind_map_chain
|
|
@@ -292,16 +292,34 @@ def auto_qa_chain_wrapper(inputs):
|
|
| 292 |
raise InvalidArgumentError("Please provide snippet ids")
|
| 293 |
document = "\n\n".join([st.session_state.documents[c].page_content for c in inputs])
|
| 294 |
llm = ChatOpenAI(model=st.session_state.model, temperature=0)
|
|
|
|
|
|
|
| 295 |
with get_openai_callback() as cb:
|
| 296 |
-
auto_qa_response =
|
| 297 |
-
auto_qa_chain(llm).invoke({"paper": document})
|
| 298 |
-
)["questions"]
|
| 299 |
-
formated_response = "\n\n".join(
|
| 300 |
-
f"#### {qa['question']}\n\n{qa['answer']}" for qa in auto_qa_response
|
| 301 |
-
)
|
| 302 |
stats = cb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
st.session_state.messages.append(
|
| 304 |
-
(f"/auto-insight {inputs}",
|
| 305 |
)
|
| 306 |
st.session_state.costing.append(
|
| 307 |
{
|
|
@@ -311,7 +329,7 @@ def auto_qa_chain_wrapper(inputs):
|
|
| 311 |
}
|
| 312 |
)
|
| 313 |
return (
|
| 314 |
-
|
| 315 |
"identity",
|
| 316 |
)
|
| 317 |
|
|
|
|
| 15 |
parse_context_and_question,
|
| 16 |
ai_response_format,
|
| 17 |
)
|
| 18 |
+
from autoqa_chains import auto_qa_chain
|
| 19 |
from chain_of_density import chain_of_density_chain
|
| 20 |
from insights_bullet_chain import insights_bullet_chain
|
| 21 |
from insights_mind_map_chain import insights_mind_map_chain
|
|
|
|
| 292 |
raise InvalidArgumentError("Please provide snippet ids")
|
| 293 |
document = "\n\n".join([st.session_state.documents[c].page_content for c in inputs])
|
| 294 |
llm = ChatOpenAI(model=st.session_state.model, temperature=0)
|
| 295 |
+
retriever = st.session_state.retriever
|
| 296 |
+
formatted_response = ""
|
| 297 |
with get_openai_callback() as cb:
|
| 298 |
+
auto_qa_response = auto_qa_chain(llm).invoke({"paper": document})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
stats = cb
|
| 300 |
+
for section in auto_qa_response:
|
| 301 |
+
section_name = section["section_name"]
|
| 302 |
+
formatted_response += f"# {section_name}\n"
|
| 303 |
+
for question in section["questions"]:
|
| 304 |
+
response = (
|
| 305 |
+
qa_chain(ChatOpenAI(model=st.session_state.model, temperature=0))
|
| 306 |
+
.invoke(
|
| 307 |
+
{
|
| 308 |
+
"context": format_docs(
|
| 309 |
+
retriever.get_relevant_documents(question)
|
| 310 |
+
),
|
| 311 |
+
"question": question,
|
| 312 |
+
}
|
| 313 |
+
)
|
| 314 |
+
.content
|
| 315 |
+
)
|
| 316 |
+
answer = parse_model_response(response)["answer"]
|
| 317 |
+
formatted_response += f"## {question}\n"
|
| 318 |
+
formatted_response += f"* {answer}\n"
|
| 319 |
+
formatted_response = "```\n" + formatted_response + "\n```"
|
| 320 |
+
|
| 321 |
st.session_state.messages.append(
|
| 322 |
+
(f"/auto-insight {inputs}", formatted_response, "identity")
|
| 323 |
)
|
| 324 |
st.session_state.costing.append(
|
| 325 |
{
|
|
|
|
| 329 |
}
|
| 330 |
)
|
| 331 |
return (
|
| 332 |
+
formatted_response,
|
| 333 |
"identity",
|
| 334 |
)
|
| 335 |
|
autoqa_chains.py
CHANGED
|
@@ -1,57 +1,15 @@
|
|
| 1 |
-
from langchain_core.pydantic_v1 import BaseModel, Field
|
| 2 |
-
from typing import List
|
| 3 |
from langchain_core.output_parsers import JsonOutputParser
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
|
| 6 |
-
|
| 7 |
-
class QA(BaseModel):
|
| 8 |
-
question: str = Field(description="question")
|
| 9 |
-
answer: str = Field(description="answer")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
class AutoQA(BaseModel):
|
| 13 |
-
questions: List[QA] = Field(description="list of question and answers")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
qa_prompt_template = """
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
The answers must be based on the content of the research paper, offering clear and comprehensive insights for readers.
|
| 20 |
-
Ensure that the questions cover a broad range of topics related to the paper, including but not limited to the introduction, literature review, \
|
| 21 |
-
methodology, results, discussion, and conclusions.
|
| 22 |
-
The goal is to capture the essence of the paper in a way that is accessible to an expert audience.
|
| 23 |
-
Your response should be recorded in the following json format: {format_instructions}.
|
| 24 |
|
| 25 |
-
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
auto_qa_output_parser = JsonOutputParser(pydantic_object=AutoQA)
|
| 29 |
qa_prompt = PromptTemplate(
|
| 30 |
template=qa_prompt_template,
|
| 31 |
input_variables=["paper"],
|
| 32 |
-
partial_variables={
|
| 33 |
-
"format_instructions": auto_qa_output_parser.get_format_instructions()
|
| 34 |
-
},
|
| 35 |
-
)
|
| 36 |
-
auto_qa_chain = lambda model: qa_prompt | model
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
followup_prompt_template = """
|
| 40 |
-
Question: {question}
|
| 41 |
-
Answer: {answer}
|
| 42 |
-
Based on the above question and answer and the research paper as your context, come up with a followup question and its answer.
|
| 43 |
-
The answer should be a bit detailed and strictly based on the research paper.
|
| 44 |
-
Your response should be recorded in the following json format: {format_instructions}.
|
| 45 |
-
|
| 46 |
-
here is the research paper: ####{paper}####
|
| 47 |
-
"""
|
| 48 |
-
|
| 49 |
-
followup_prompt = PromptTemplate(
|
| 50 |
-
template=followup_prompt_template,
|
| 51 |
-
input_variables=["paper", "question", "answer"],
|
| 52 |
-
partial_variables={
|
| 53 |
-
"format_instructions": auto_qa_output_parser.get_format_instructions()
|
| 54 |
-
},
|
| 55 |
)
|
| 56 |
-
|
| 57 |
-
followup_qa_chain = lambda model: followup_prompt | model
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_core.output_parsers import JsonOutputParser
|
| 2 |
from langchain_core.prompts import PromptTemplate
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
qa_prompt_template = """
|
| 5 |
+
Create a mind map of questions (based on the given abstract) that will help understand a machine learning research paper.
|
| 6 |
+
Ensure that the outline is structured in the following JSON array for clarity, such that each section should have two keys: "section_name" and "questions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
Here is the research paper abstract: ####{paper}####
|
| 9 |
"""
|
| 10 |
|
|
|
|
| 11 |
qa_prompt = PromptTemplate(
|
| 12 |
template=qa_prompt_template,
|
| 13 |
input_variables=["paper"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
+
auto_qa_chain = lambda model: qa_prompt | model | JsonOutputParser()
|
|
|