Skip to content

Commit 65b12c1

Browse files
committed
Add a thought step for product cards
1 parent 7c9397f commit 65b12c1

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/fastapi_app/postgres_searcher.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,25 @@ async def simple_sql_search(
311311
item = await session.execute(select(Item).where(Item.id == item_id))
312312
items.append(item.scalar())
313313
return items
314+
315+
316+
async def get_product_cards_info(self, urls: list[str]) -> list[dict]:
317+
"""
318+
Fetch detailed information about items using their URLs as identifiers.
319+
"""
320+
sql = """
321+
SELECT package_name, package_picture, url, price FROM packages WHERE url = ANY(:urls)
322+
"""
323+
324+
async with self.async_session_maker() as session:
325+
results = (
326+
await session.execute(
327+
text(sql), {"urls": urls}
328+
)
329+
).fetchall()
330+
331+
# Convert results to dictionaries
332+
items = []
333+
for result in results:
334+
items.append(dict(result._mapping))
335+
return items

src/fastapi_app/rag_advanced.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import copy
23
import logging
34
import pathlib
@@ -97,6 +98,9 @@ async def hybrid_search(self, messages, top, vector_search, text_search):
9798
]
9899
return sources_content, thought_steps
99100

101+
async def get_product_cards_details(self, urls: list[str]) -> list[dict]:
102+
return await self.searcher.get_product_cards_info(urls)
103+
100104
async def run(
101105
self, messages: list[dict], overrides: dict[str, Any] = {}
102106
) -> dict[str, Any] | AsyncGenerator[dict[str, Any], None]:
@@ -172,8 +176,22 @@ async def run(
172176
)
173177
chat_resp = chat_completion_response.model_dump()
174178

179+
chat_resp_content = chat_resp["choices"][0]["message"]["content"]
180+
package_urls = re.findall(r'https:\/\/hdmall\.co\.th\/[\w.,@?^=%&:\/~+#-]+', chat_resp_content)
181+
182+
if package_urls:
183+
product_cards_details = await self.get_product_cards_details(package_urls)
184+
else:
185+
product_cards_details = []
186+
175187
chat_resp["choices"][0]["context"] = {
176188
"data_points": {"text": sources_content},
177-
"thoughts": thought_steps
189+
"thoughts": thought_steps + [
190+
ThoughtStep(
191+
title="Additional details for mentioned URLs",
192+
description=product_cards_details,
193+
props={}
194+
)
195+
]
178196
}
179-
return chat_resp
197+
return chat_resp

0 commit comments

Comments
 (0)