Upload folder using huggingface_hub
Browse files- modules/answerer.py +14 -9
modules/answerer.py
CHANGED
@@ -6,6 +6,7 @@ from pydantic import BaseModel
|
|
6 |
from openai import OpenAI
|
7 |
from modules.retriever import retrieve_videos
|
8 |
|
|
|
9 |
# -------------------------------
|
10 |
# Structured Output Classes
|
11 |
# -------------------------------
|
@@ -15,10 +16,12 @@ class VideoItem(BaseModel):
|
|
15 |
channel: str
|
16 |
description: str
|
17 |
|
|
|
18 |
class LLMAnswer(BaseModel):
|
19 |
answer_text: str
|
20 |
top_videos: List[VideoItem]
|
21 |
|
|
|
22 |
# -------------------------------
|
23 |
# Main Function
|
24 |
# -------------------------------
|
@@ -43,14 +46,13 @@ def answer_query(query: str, collection, top_k: int = 5) -> LLMAnswer:
|
|
43 |
title = r.get("video_title") or r.get("title", "")
|
44 |
channel = r.get("channel") or r.get("channel_title", "")
|
45 |
description = r.get("description", "")
|
46 |
-
context_lines.append(
|
|
|
|
|
47 |
|
48 |
top_videos_list.append(
|
49 |
VideoItem(
|
50 |
-
video_id=vid_id,
|
51 |
-
title=title,
|
52 |
-
channel=channel,
|
53 |
-
description=description
|
54 |
)
|
55 |
)
|
56 |
|
@@ -66,14 +68,17 @@ def answer_query(query: str, collection, top_k: int = 5) -> LLMAnswer:
|
|
66 |
"content": (
|
67 |
"You are a helpful assistant that answers questions using YouTube video metadata. "
|
68 |
"Return your response strictly as the LLMAnswer class, including 'answer_text' and a list of 'top_videos'."
|
69 |
-
|
|
|
|
|
|
|
70 |
},
|
71 |
{
|
72 |
"role": "user",
|
73 |
-
"content": f"Question: {query}\n\nRelevant videos:\n{context_text}\n\nAnswer based only on this."
|
74 |
-
}
|
75 |
],
|
76 |
-
response_format=LLMAnswer
|
77 |
)
|
78 |
|
79 |
llm_answer = response.choices[0].message.parsed # already LLMAnswer object
|
|
|
6 |
from openai import OpenAI
|
7 |
from modules.retriever import retrieve_videos
|
8 |
|
9 |
+
|
10 |
# -------------------------------
|
11 |
# Structured Output Classes
|
12 |
# -------------------------------
|
|
|
16 |
channel: str
|
17 |
description: str
|
18 |
|
19 |
+
|
20 |
class LLMAnswer(BaseModel):
|
21 |
answer_text: str
|
22 |
top_videos: List[VideoItem]
|
23 |
|
24 |
+
|
25 |
# -------------------------------
|
26 |
# Main Function
|
27 |
# -------------------------------
|
|
|
46 |
title = r.get("video_title") or r.get("title", "")
|
47 |
channel = r.get("channel") or r.get("channel_title", "")
|
48 |
description = r.get("description", "")
|
49 |
+
context_lines.append(
|
50 |
+
f"- {title} ({channel}) (https://youtube.com/watch?v={vid_id})\n description: {description}"
|
51 |
+
)
|
52 |
|
53 |
top_videos_list.append(
|
54 |
VideoItem(
|
55 |
+
video_id=vid_id, title=title, channel=channel, description=description
|
|
|
|
|
|
|
56 |
)
|
57 |
)
|
58 |
|
|
|
68 |
"content": (
|
69 |
"You are a helpful assistant that answers questions using YouTube video metadata. "
|
70 |
"Return your response strictly as the LLMAnswer class, including 'answer_text' and a list of 'top_videos'."
|
71 |
+
"""- `answer_text` MUST be very short and concise and in natural language. (max 1–2 sentences).\n
|
72 |
+
- The detailed data will be shown separately in a table using `top_videos`.\n
|
73 |
+
- Avoid repeating the same rows/columns in text."""
|
74 |
+
),
|
75 |
},
|
76 |
{
|
77 |
"role": "user",
|
78 |
+
"content": f"Question: {query}\n\nRelevant videos:\n{context_text}\n\nAnswer based only on this.",
|
79 |
+
},
|
80 |
],
|
81 |
+
response_format=LLMAnswer,
|
82 |
)
|
83 |
|
84 |
llm_answer = response.choices[0].message.parsed # already LLMAnswer object
|