@@ -123,14 +123,6 @@ def search_with_filtering(uuid_list: list[str], query: str) -> List[dict]:
123123 )
124124 docs = retriever .invoke (query )
125125
126- if not docs :
127- # 필터링된 결과가 없으면 필터 없이 다시 검색
128- retriever = self .chroma_db .as_retriever (
129- search_type = "similarity" ,
130- search_kwargs = {"k" : 5 }
131- )
132- docs = retriever .invoke (query )
133-
134126 return [doc .model_dump () for doc in docs ]
135127
136128 return [elastic_search , search_with_filtering ]
@@ -162,17 +154,24 @@ def get_response(self, user_message: str, session_id: str):
162154 response = agent_executor .invoke ({"input" : user_message })
163155 output = response .get ("output" , "죄송합니다. 답변을 생성하지 못했습니다." )
164156
165- # LLM이 JSON 형식으로 응답하지 않았을 경우 처리
166- try :
167- # The output from the agent should be a string that is a valid JSON.
168- # We parse it to ensure it's a JSON object before returning.
169- if isinstance (output , str ):
170- return json .loads (output )
157+ if not isinstance (output , str ):
171158 return output
159+
160+ # LLM 응답에서 JSON 객체만 추출 시도
161+ try :
162+ # 가장 먼저 나오는 '{'와 가장 마지막에 나오는 '}'를 찾아 JSON 추출
163+ start_index = output .find ('{' )
164+ end_index = output .rfind ('}' )
165+ if start_index != - 1 and end_index != - 1 :
166+ json_str = output [start_index :end_index + 1 ]
167+ return json .loads (json_str )
172168 except json .JSONDecodeError :
173- # If parsing fails, wrap the raw output in the specified JSON format.
174- return {
175- "str1" : output ,
176- "placeid" : [],
177- "str2" : ""
178- }
169+ # JSON 추출에 실패하면 전체 출력을 메시지로 사용
170+ pass
171+
172+ # JSON 파싱에 실패한 경우, LLM의 응답이 순수 텍스트라고 간주하고 포맷에 맞춰 반환
173+ return {
174+ "str1" : output ,
175+ "placeid" : [],
176+ "str2" : ""
177+ }
0 commit comments