Skip to content

Commit 1ed75f2

Browse files
authored
[AAQ-797] Refactor search base (#410)
* refactor search_base func * cleanup json schema displays
1 parent 4d9f6dd commit 1ed75f2

File tree

10 files changed

+174
-184
lines changed

10 files changed

+174
-184
lines changed

.secrets.baseline

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,6 @@
231231
"line_number": 63
232232
}
233233
],
234-
"core_backend/app/question_answer/schemas.py": [
235-
{
236-
"type": "Secret Keyword",
237-
"filename": "core_backend/app/question_answer/schemas.py",
238-
"hashed_secret": "5b8b7a620e54e681c584f5b5c89152773c10c253",
239-
"is_verified": false,
240-
"line_number": 67
241-
}
242-
],
243234
"core_backend/migrations/versions/2023_09_16_c5a948963236_create_query_table.py": [
244235
{
245236
"type": "Hex High Entropy String",
@@ -430,7 +421,7 @@
430421
"filename": "core_backend/tests/api/test_dashboard_overview.py",
431422
"hashed_secret": "6367c48dd193d56ea7b0baad25b19455e529f5ee",
432423
"is_verified": false,
433-
"line_number": 291
424+
"line_number": 290
434425
}
435426
],
436427
"core_backend/tests/api/test_dashboard_performance.py": [
@@ -590,5 +581,5 @@
590581
}
591582
]
592583
},
593-
"generated_at": "2024-08-23T09:41:17Z"
584+
"generated_at": "2024-08-26T14:03:01Z"
594585
}

core_backend/app/contents/schemas.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from datetime import datetime
2-
from typing import Annotated, List
2+
from typing import List
33

4-
from pydantic import BaseModel, ConfigDict, Field, StringConstraints
4+
from pydantic import BaseModel, ConfigDict, Field
55

66

77
class ContentCreate(BaseModel):
88
"""
99
Pydantic model for content creation request
1010
"""
1111

12-
content_title: Annotated[str, StringConstraints(max_length=150)] = Field(
12+
content_title: str = Field(
13+
max_length=150,
1314
examples=["Example Content Title"],
1415
)
15-
content_text: Annotated[str, StringConstraints(max_length=2000)] = Field(
16-
examples=["This is an example content."]
16+
content_text: str = Field(
17+
max_length=2000,
18+
examples=["This is an example content."],
1719
)
18-
content_tags: list = Field(default=[], examples=[[1, 4]])
19-
content_metadata: dict = Field(default={}, examples=[{"key": "optional_value"}])
20+
content_tags: list = Field(default=[])
21+
content_metadata: dict = Field(default={})
2022
is_archived: bool = False
2123

2224
model_config = ConfigDict(

core_backend/app/llm_call/llm_rag.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Augmented Generation (RAG).
33
"""
44

5-
from typing import Optional
6-
75
from pydantic import ValidationError
86

97
from ..config import LITELLM_MODEL_GENERATION
@@ -18,7 +16,7 @@ async def get_llm_rag_answer(
1816
question: str,
1917
context: str,
2018
original_language: IdentifiedLanguage,
21-
metadata: Optional[dict] = None,
19+
metadata: dict | None = None,
2220
) -> RAG:
2321
"""Get an answer from the LLM model using RAG.
2422
@@ -32,7 +30,6 @@ async def get_llm_rag_answer(
3230
The language of the response.
3331
metadata
3432
Additional metadata to provide to the LLM model.
35-
3633
Returns
3734
-------
3835
RAG

core_backend/app/llm_call/process_input.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def _process_identified_language_response(
121121

122122
error_response = QueryResponseError(
123123
query_id=response.query_id,
124+
session_id=response.session_id,
124125
feedback_secret_key=response.feedback_secret_key,
125126
llm_response=response.llm_response,
126127
search_results=response.search_results,
@@ -206,6 +207,7 @@ async def _translate_question(
206207
else:
207208
error_response = QueryResponseError(
208209
query_id=response.query_id,
210+
session_id=response.session_id,
209211
feedback_secret_key=response.feedback_secret_key,
210212
llm_response=response.llm_response,
211213
search_results=response.search_results,
@@ -275,6 +277,7 @@ async def _classify_safety(
275277
else:
276278
error_response = QueryResponseError(
277279
query_id=response.query_id,
280+
session_id=response.session_id,
278281
feedback_secret_key=response.feedback_secret_key,
279282
llm_response=response.llm_response,
280283
search_results=response.search_results,
@@ -352,6 +355,7 @@ async def _paraphrase_question(
352355
else:
353356
error_response = QueryResponseError(
354357
query_id=response.query_id,
358+
session_id=response.session_id,
355359
feedback_secret_key=response.feedback_secret_key,
356360
llm_response=response.llm_response,
357361
search_results=response.search_results,

core_backend/app/llm_call/process_output.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,7 @@ class AlignScoreData(TypedDict):
4545
claim: str
4646

4747

48-
def generate_llm_response__after(func: Callable) -> Callable:
49-
"""
50-
Decorator to generate the LLM response.
51-
52-
Only runs if the generate_llm_response flag is set to True.
53-
Requires "search_results" and "original_language" in the response.
54-
"""
55-
56-
@wraps(func)
57-
async def wrapper(
58-
query_refined: QueryRefined,
59-
response: QueryResponse | QueryResponseError,
60-
*args: Any,
61-
**kwargs: Any,
62-
) -> QueryResponse | QueryResponseError:
63-
"""
64-
Generate the LLM response
65-
"""
66-
response = await func(query_refined, response, *args, **kwargs)
67-
68-
if not query_refined.generate_llm_response:
69-
return response
70-
71-
metadata = create_langfuse_metadata(
72-
query_id=response.query_id, user_id=query_refined.user_id
73-
)
74-
response = await _generate_llm_response(query_refined, response, metadata)
75-
return response
76-
77-
return wrapper
78-
79-
80-
async def _generate_llm_response(
48+
async def generate_llm_query_response(
8149
query_refined: QueryRefined,
8250
response: QueryResponse,
8351
metadata: Optional[dict] = None,
@@ -114,6 +82,7 @@ async def _generate_llm_response(
11482
else:
11583
response = QueryResponseError(
11684
query_id=response.query_id,
85+
session_id=response.session_id,
11786
feedback_secret_key=response.feedback_secret_key,
11887
llm_response=None,
11988
search_results=response.search_results,
@@ -219,6 +188,7 @@ async def _check_align_score(
219188
)
220189
response = QueryResponseError(
221190
query_id=response.query_id,
191+
session_id=response.session_id,
222192
feedback_secret_key=response.feedback_secret_key,
223193
llm_response=None,
224194
search_results=response.search_results,
@@ -311,6 +281,7 @@ async def wrapper(
311281
)
312282
response = QueryAudioResponse(
313283
query_id=response.query_id,
284+
session_id=response.session_id,
314285
feedback_secret_key=response.feedback_secret_key,
315286
llm_response=response.llm_response,
316287
search_results=response.search_results,
@@ -361,6 +332,7 @@ async def _generate_tts_response(
361332
logger.error(f"Error generating TTS for query_id {response.query_id}: {e}")
362333
return QueryResponseError(
363334
query_id=response.query_id,
335+
session_id=response.session_id,
364336
feedback_secret_key=response.feedback_secret_key,
365337
llm_response=response.llm_response,
366338
search_results=response.search_results,

core_backend/app/llm_call/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional
2-
31
from litellm import acompletion
42

53
from ..config import LITELLM_API_KEY, LITELLM_ENDPOINT, LITELLM_MODEL_DEFAULT
@@ -11,9 +9,9 @@
119
async def _ask_llm_async(
1210
user_message: str,
1311
system_message: str,
14-
litellm_model: Optional[str] = LITELLM_MODEL_DEFAULT,
15-
litellm_endpoint: Optional[str] = LITELLM_ENDPOINT,
16-
metadata: Optional[dict] = None,
12+
litellm_model: str | None = LITELLM_MODEL_DEFAULT,
13+
litellm_endpoint: str | None = LITELLM_ENDPOINT,
14+
metadata: dict | None = None,
1715
json: bool = False,
1816
) -> str:
1917
"""

0 commit comments

Comments
 (0)