1
- import requests
1
+ """
2
+ Generate answer_node
3
+ """
4
+ import re
2
5
import json
3
6
from typing import List , Optional
7
+ import requests
8
+ from tqdm import tqdm
4
9
from langchain .prompts import PromptTemplate
5
10
from langchain_core .output_parsers import JsonOutputParser
6
11
from langchain_core .runnables import RunnableParallel
7
12
from langchain_openai import ChatOpenAI
8
13
from langchain_community .chat_models import ChatOllama
9
- from tqdm import tqdm
10
14
from ..utils .logging import get_logger
15
+ from ..utils import parse_response_to_dict
11
16
from .base_node import BaseNode
12
17
from ..prompts import (
13
18
TEMPLATE_CHUNKS , TEMPLATE_NO_CHUNKS , TEMPLATE_MERGE ,
@@ -52,6 +57,8 @@ def __init__(
52
57
self .additional_info = node_config .get ("additional_info" , "" )
53
58
self .api_key = node_config .get ("config" , {}).get ("llm" , {}).get ("api_key" , "" )
54
59
60
+
61
+
55
62
def execute (self , state : dict ) -> dict :
56
63
self .logger .info (f"--- Executing { self .node_name } Node ---" )
57
64
@@ -86,7 +93,10 @@ def execute(self, state: dict) -> dict:
86
93
"messages" : [{"role" : "user" , "content" : prompt }],
87
94
"temperature" : 0
88
95
}, timeout = 10 )
89
- state .update ({self .output [0 ]: response .json ()})
96
+
97
+ response_text = response .json ()['choices' ][0 ]['message' ]['content' ]
98
+ cleaned_response = parse_response_to_dict (response_text )
99
+ state .update ({self .output [0 ]: cleaned_response })
90
100
return state
91
101
92
102
chunks_responses = []
@@ -105,7 +115,7 @@ def execute(self, state: dict) -> dict:
105
115
"temperature" : 0
106
116
}, timeout = 10 )
107
117
chunk_response = response .json ()['choices' ][0 ]['message' ]['content' ]
108
- cleaned_chunk_response = json . loads (chunk_response . replace ( ' \\ n' , '' ). replace ( ' \\ ' , '' ) )
118
+ cleaned_chunk_response = parse_response_to_dict (chunk_response )
109
119
chunks_responses .append (cleaned_chunk_response )
110
120
111
121
merge_context = " " .join ([json .dumps (chunk ) for chunk in chunks_responses ])
@@ -120,7 +130,7 @@ def execute(self, state: dict) -> dict:
120
130
"temperature" : 0
121
131
}, timeout = 10 )
122
132
response_text = response .json ()['choices' ][0 ]['message' ]['content' ]
123
- cleaned_response = json . loads (response_text . replace ( ' \\ n' , '' ). replace ( ' \\ ' , '' ) )
133
+ cleaned_response = parse_response_to_dict (response_text )
124
134
state .update ({self .output [0 ]: cleaned_response })
125
135
return state
126
136
@@ -146,11 +156,14 @@ def execute(self, state: dict) -> dict:
146
156
return state
147
157
148
158
chains_dict = {}
149
- for i , chunk in enumerate (tqdm (doc , desc = "Processing chunks" , disable = not self .verbose )):
159
+ for i , chunk in enumerate (tqdm (doc ,
160
+ desc = "Processing chunks" ,
161
+ disable = not self .verbose )):
150
162
prompt = PromptTemplate (
151
163
template = templates ['chunks' ],
152
164
input_variables = ["question" ],
153
- partial_variables = {"context" : chunk , "chunk_id" : i + 1 , "format_instructions" : format_instructions }
165
+ partial_variables = {"context" : chunk , "chunk_id" : i + 1 ,
166
+ "format_instructions" : format_instructions }
154
167
)
155
168
chain_name = f"chunk{ i + 1 } "
156
169
chains_dict [chain_name ] = prompt | self .llm_model | output_parser
0 commit comments