add time measure
Browse files
climateqa/engine/chains/answer_rag.py
CHANGED
@@ -7,6 +7,7 @@ from langchain_core.prompts.base import format_document
|
|
7 |
|
8 |
from climateqa.engine.chains.prompts import answer_prompt_template,answer_prompt_without_docs_template,answer_prompt_images_template
|
9 |
from climateqa.engine.chains.prompts import papers_prompt_template
|
|
|
10 |
from ..utils import rename_chain, pass_values
|
11 |
|
12 |
|
@@ -42,6 +43,7 @@ def make_rag_chain(llm):
|
|
42 |
prompt = ChatPromptTemplate.from_template(answer_prompt_template)
|
43 |
chain = ({
|
44 |
"context":lambda x : _combine_documents(x["documents"]),
|
|
|
45 |
"query":itemgetter("query"),
|
46 |
"language":itemgetter("language"),
|
47 |
"audience":itemgetter("audience"),
|
@@ -53,7 +55,6 @@ def make_rag_chain_without_docs(llm):
|
|
53 |
chain = prompt | llm | StrOutputParser()
|
54 |
return chain
|
55 |
|
56 |
-
|
57 |
def make_rag_node(llm,with_docs = True):
|
58 |
|
59 |
if with_docs:
|
@@ -63,9 +64,16 @@ def make_rag_node(llm,with_docs = True):
|
|
63 |
|
64 |
async def answer_rag(state,config):
|
65 |
print("---- Answer RAG ----")
|
|
|
66 |
|
67 |
answer = await rag_chain.ainvoke(state,config)
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
return {"answer":answer}
|
70 |
|
71 |
return answer_rag
|
|
|
7 |
|
8 |
from climateqa.engine.chains.prompts import answer_prompt_template,answer_prompt_without_docs_template,answer_prompt_images_template
|
9 |
from climateqa.engine.chains.prompts import papers_prompt_template
|
10 |
+
import time
|
11 |
from ..utils import rename_chain, pass_values
|
12 |
|
13 |
|
|
|
43 |
prompt = ChatPromptTemplate.from_template(answer_prompt_template)
|
44 |
chain = ({
|
45 |
"context":lambda x : _combine_documents(x["documents"]),
|
46 |
+
"context_length":lambda x : print("CONTEXT LENGTH : " , len(_combine_documents(x["documents"]))),
|
47 |
"query":itemgetter("query"),
|
48 |
"language":itemgetter("language"),
|
49 |
"audience":itemgetter("audience"),
|
|
|
55 |
chain = prompt | llm | StrOutputParser()
|
56 |
return chain
|
57 |
|
|
|
58 |
def make_rag_node(llm,with_docs = True):
|
59 |
|
60 |
if with_docs:
|
|
|
64 |
|
65 |
async def answer_rag(state,config):
|
66 |
print("---- Answer RAG ----")
|
67 |
+
start_time = time.time()
|
68 |
|
69 |
answer = await rag_chain.ainvoke(state,config)
|
70 |
+
|
71 |
+
end_time = time.time()
|
72 |
+
elapsed_time = end_time - start_time
|
73 |
+
print("RAG elapsed time: ", elapsed_time)
|
74 |
+
print("Answer size : ", len(answer))
|
75 |
+
# print(f"\n\nAnswer:\n{answer}")
|
76 |
+
|
77 |
return {"answer":answer}
|
78 |
|
79 |
return answer_rag
|