-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgenerate_answer_node_k_level.py
177 lines (149 loc) · 6.45 KB
/
generate_answer_node_k_level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
GenerateAnswerNodeKLevel Module
"""
from typing import List, Optional
from langchain.prompts import PromptTemplate
from langchain_aws import ChatBedrock
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.runnables import RunnableParallel
from langchain_mistralai import ChatMistralAI
from langchain_openai import ChatOpenAI
from tqdm import tqdm
from ..prompts import (
TEMPLATE_CHUNKS,
TEMPLATE_CHUNKS_MD,
TEMPLATE_MERGE,
TEMPLATE_MERGE_MD,
TEMPLATE_NO_CHUNKS,
TEMPLATE_NO_CHUNKS_MD,
)
from ..utils.output_parser import (
get_pydantic_output_parser,
get_structured_output_parser,
)
from .base_node import BaseNode
class GenerateAnswerNodeKLevel(BaseNode):
"""
A node responsible for compressing the input tokens and storing the document
in a vector database for retrieval. Relevant chunks are stored in the state.
It allows scraping of big documents without exceeding the token limit of the language model.
Attributes:
llm_model: An instance of a language model client, configured for generating answers.
verbose (bool): A flag indicating whether to show print statements during execution.
Args:
input (str): Boolean expression defining the input keys needed from the state.
output (List[str]): List of output keys to be updated in the state.
node_config (dict): Additional configuration for the node.
node_name (str): The unique identifier name for the node, defaulting to "Parse".
"""
def __init__(
self,
input: str,
output: List[str],
node_config: Optional[dict] = None,
node_name: str = "GANLK",
):
super().__init__(node_name, "node", input, output, 2, node_config)
self.llm_model = node_config["llm_model"]
if isinstance(node_config["llm_model"], ChatOllama):
if node_config.get("schema", None) is None:
self.llm_model.format = "json"
else:
self.llm_model.format = self.node_config["schema"].model_json_schema()
self.embedder_model = node_config.get("embedder_model", None)
self.verbose = node_config.get("verbose", False)
self.force = node_config.get("force", False)
self.script_creator = node_config.get("script_creator", False)
self.is_md_scraper = node_config.get("is_md_scraper", False)
self.additional_info = node_config.get("additional_info")
def execute(self, state: dict) -> dict:
self.logger.info(f"--- Executing {self.node_name} Node ---")
user_prompt = state.get("user_prompt")
if self.node_config.get("schema", None) is not None:
if isinstance(self.llm_model, (ChatOpenAI, ChatMistralAI)):
self.llm_model = self.llm_model.with_structured_output(
schema=self.node_config["schema"]
)
output_parser = get_structured_output_parser(self.node_config["schema"])
format_instructions = "NA"
else:
if not isinstance(self.llm_model, ChatBedrock):
output_parser = get_pydantic_output_parser(
self.node_config["schema"]
)
format_instructions = output_parser.get_format_instructions()
else:
output_parser = None
format_instructions = ""
else:
if not isinstance(self.llm_model, ChatBedrock):
output_parser = JsonOutputParser()
format_instructions = output_parser.get_format_instructions()
else:
output_parser = None
format_instructions = ""
if (
not self.script_creator
or self.force
and not self.script_creator
or self.is_md_scraper
):
template_no_chunks_prompt = TEMPLATE_NO_CHUNKS_MD
template_chunks_prompt = TEMPLATE_CHUNKS_MD
template_merge_prompt = TEMPLATE_MERGE_MD
else:
template_no_chunks_prompt = TEMPLATE_NO_CHUNKS
template_chunks_prompt = TEMPLATE_CHUNKS
template_merge_prompt = TEMPLATE_MERGE
if self.additional_info is not None:
template_no_chunks_prompt = self.additional_info + template_no_chunks_prompt
template_chunks_prompt = self.additional_info + template_chunks_prompt
template_merge_prompt = self.additional_info + template_merge_prompt
client = state["vectorial_db"]
if state.get("embeddings"):
import openai
openai_client = openai.Client()
answer_db = client.search(
collection_name="collection",
query_vector=openai_client.embeddings.create(
input=["What is the best to use for vector search scaling?"],
model=state.get("embeddings").get("model"),
)
.data[0]
.embedding,
)
else:
answer_db = client.query(
collection_name="vectorial_collection", query_text=user_prompt
)
chains_dict = {}
elems = [
state.get("docs")[elem.id - 1] for elem in answer_db if elem.score > 0.5
]
for i, chunk in enumerate(
tqdm(elems, desc="Processing chunks", disable=not self.verbose)
):
prompt = PromptTemplate(
template=template_chunks_prompt,
input_variables=["format_instructions"],
partial_variables={
"context": chunk.get("document"),
"chunk_id": i + 1,
},
)
chain_name = f"chunk{i + 1}"
chains_dict[chain_name] = prompt | self.llm_model
async_runner = RunnableParallel(**chains_dict)
batch_results = async_runner.invoke({"format_instructions": user_prompt})
merge_prompt = PromptTemplate(
template=template_merge_prompt,
input_variables=["context", "question"],
partial_variables={"format_instructions": format_instructions},
)
merge_chain = merge_prompt | self.llm_model
if output_parser:
merge_chain = merge_chain | output_parser
answer = merge_chain.invoke({"context": batch_results, "question": user_prompt})
state["answer"] = answer
return state