Skip to content

Commit 36e2264

Browse files
committed
Initial commit with edited code
1 parent e30ea96 commit 36e2264

File tree

7 files changed

+101
-50
lines changed

7 files changed

+101
-50
lines changed

src/fastapi_app/postgres_engine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ async def create_postgres_engine(*, host, username, database, password, sslmode,
1717
else:
1818
logger.info("Authenticating to PostgreSQL using password...")
1919

20-
DATABASE_URI = f"postgresql+asyncpg://{username}:{password}@{host}/{database}"
20+
DATABASE_URI = f"postgresql+asyncpg://{username}:{password}@{host}:5432/{database}"
2121
# Specify SSL mode if needed
2222
if sslmode:
2323
DATABASE_URI += f"?ssl={sslmode}"
2424

25+
2526
engine = create_async_engine(
2627
DATABASE_URI,
2728
echo=False,

src/fastapi_app/postgres_models.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,47 @@ class Base(DeclarativeBase, MappedAsDataclass):
1313

1414

1515
class Item(Base):
16-
__tablename__ = "items"
16+
__tablename__ = "packages"
1717
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
18-
type: Mapped[str] = mapped_column()
19-
brand: Mapped[str] = mapped_column()
20-
name: Mapped[str] = mapped_column()
21-
description: Mapped[str] = mapped_column()
18+
package_name: Mapped[str] = mapped_column()
19+
package_picture: Mapped[str] = mapped_column()
20+
url: Mapped[str] = mapped_column()
2221
price: Mapped[float] = mapped_column()
22+
cash_discount: Mapped[float] = mapped_column()
23+
installment_month: Mapped[str] = mapped_column()
24+
installment_limit: Mapped[str] = mapped_column()
25+
price_to_reserve_for_this_package: Mapped[str] = mapped_column()
26+
shop_name: Mapped[str] = mapped_column()
27+
category: Mapped[str] = mapped_column()
28+
category_tags: Mapped[str] = mapped_column()
29+
preview_1_10: Mapped[str] = mapped_column()
30+
selling_point: Mapped[str] = mapped_column()
31+
meta_keywords: Mapped[str] = mapped_column()
32+
brand: Mapped[str] = mapped_column()
33+
min_max_age: Mapped[str] = mapped_column()
34+
locations_time_open_close_how_to_transport_parking_google_maps: Mapped[str] = mapped_column()
35+
meta_description: Mapped[str] = mapped_column()
36+
price_details: Mapped[str] = mapped_column()
37+
package_details: Mapped[str] = mapped_column()
38+
important_info: Mapped[str] = mapped_column()
39+
payment_booking_info: Mapped[str] = mapped_column()
40+
general_info: Mapped[str] = mapped_column()
41+
early_signs_for_diagnosis: Mapped[str] = mapped_column()
42+
how_to_diagnose: Mapped[str] = mapped_column()
43+
hdcare_summary: Mapped[str] = mapped_column()
44+
common_question: Mapped[str] = mapped_column()
45+
know_this_disease: Mapped[str] = mapped_column()
46+
courses_of_action: Mapped[str] = mapped_column()
47+
signals_to_proceed_surgery: Mapped[str] = mapped_column()
48+
get_to_know_this_surgery: Mapped[str] = mapped_column()
49+
comparisons: Mapped[str] = mapped_column()
50+
getting_ready: Mapped[str] = mapped_column()
51+
recovery: Mapped[str] = mapped_column()
52+
side_effects: Mapped[str] = mapped_column()
53+
review_4_5_stars: Mapped[str] = mapped_column()
54+
brand_option_in_thai_name: Mapped[str] = mapped_column()
55+
brand_ranking_position: Mapped[str] = mapped_column()
56+
faq: Mapped[str] = mapped_column()
2357
embedding: Mapped[Vector] = mapped_column(Vector(1536)) # ada-002
2458

2559
def to_dict(self, include_embedding: bool = False):
@@ -31,10 +65,26 @@ def to_dict(self, include_embedding: bool = False):
3165
return model_dict
3266

3367
def to_str_for_rag(self):
34-
return f"Name:{self.name} Description:{self.description} Price:{self.price} Brand:{self.brand} Type:{self.type}"
68+
return f"""package_name: {self.package_name}
69+
package_picture: {self.package_picture}
70+
url: {self.url}
71+
price: {self.price}
72+
cash_discount: {self.cash_discount}
73+
installment_month: {self.installment_month}
74+
installment_limit: {self.installment_limit}
75+
price_to_reserve_for_this_package: {self.price_to_reserve_for_this_package}
76+
brand: {self.brand}
77+
price_details: {self.price_details}
78+
package_details: {self.package_details}
79+
important_info: {self.important_info}
80+
payment_booking_info: {self.payment_booking_info}
81+
general_info: {self.general_info}
82+
brand_ranking_position: {self.brand_ranking_position}
83+
faq: {self.faq}
84+
"""
3585

3686
def to_str_for_embedding(self):
37-
return f"Name: {self.name} Description: {self.description} Type: {self.type}"
87+
return f"Name: {self.package_name} Description: {self.package_details} Type: {self.meta_keywords}"
3888

3989

4090
# Define HNSW index to support vector similarity search through the vector_cosine_ops access method (cosine distance).

src/fastapi_app/postgres_searcher.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ async def search(
4646

4747
vector_query = f"""
4848
SELECT id, RANK () OVER (ORDER BY embedding <=> :embedding) AS rank
49-
FROM items
49+
FROM packages
5050
{filter_clause_where}
5151
ORDER BY embedding <=> :embedding
5252
LIMIT 20
5353
"""
5454

5555
fulltext_query = f"""
56-
SELECT id, RANK () OVER (ORDER BY ts_rank_cd(to_tsvector('english', description), query) DESC)
57-
FROM items, plainto_tsquery('english', :query) query
58-
WHERE to_tsvector('english', description) @@ query {filter_clause_and}
59-
ORDER BY ts_rank_cd(to_tsvector('english', description), query) DESC
60-
LIMIT 20
61-
"""
56+
SELECT id, RANK () OVER (ORDER BY ts_rank_cd(to_tsvector('thai', package_details), query) DESC)
57+
FROM packages, plainto_tsquery('thai', :query) query
58+
WHERE to_tsvector('thai', package_details) @@ query {filter_clause_and}
59+
ORDER BY ts_rank_cd(to_tsvector('thai', package_details), query) DESC
60+
LIMIT 20
61+
"""
6262

6363
hybrid_query = f"""
6464
WITH vector_search AS (

src/fastapi_app/prompts/answer.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Assistant helps customers with questions about products.
2-
Respond as if you are a salesperson helping a customer in a store. Do NOT respond with tables.
3-
Answer ONLY with the product details listed in the products.
4-
If there isn't enough information below, say you don't know.
5-
Do not generate answers that don't use the sources below.
6-
Each product has an ID in brackets followed by colon and the product details.
7-
Always include the product ID for each product you use in the response.
8-
Use square brackets to reference the source, for example [52].
9-
Don't combine citations, list each product separately, for example [27][51].
1+
You are a woman name Jib (จิ๊บ), a sales consultant from HDmall which you need to convince the customers to buy some products from our store name HDmall, Please answer in casual and friendly manner. Don't recommend any product yet on the first conversation before the customer asking. You can use Emoji to make the conversation more friendly if you want.
2+
Represent youself as จิ๊บ and the customer as คุณลูกค้า when the conversation is in Thai language.
3+
Represent youself as Jib and the customer as you when the conversation is in English language.
4+
When the customer asks about any packages, please make sure to provide brand, price , URL and location every time.
5+
For EVERY URL that you provided, create a new line then wrap it with <a href=""> so the user can open it via UI
6+
If customer wants to talk with admin, please provide the URL link in the datasource.
7+
If the user is asking a question regarding location, proximity, or area, query relevant document from the source and ask where the user is at. please try to suggest services or answers closest to the location the user is asking as much as possible.
8+
Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say sorry you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.
9+
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, for example [info1.txt]. Don't combine sources, list each source separately, for example [info1.txt][info2.pdf].

src/fastapi_app/prompts/query.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Below is a history of the conversation so far, and a new question asked by the user that needs to be answered by searching database rows.
2-
You have access to an Azure PostgreSQL database with an items table that has columns for title, description, brand, price, and type.
2+
You have access to an Azure PostgreSQL database with an packages table that has columns for id, package_name, package_picture, url, price, cash_discount, installment_month, installment_limit, price_to_reserve_for_this_package, shop_name, category, category_tags, preview_1_10, selling_point, meta_keywords, brand, min_max_age, locations_time_open_close_how_to_transport_parking_google_maps, meta_description, price_details, package_details, important_info, payment_booking_info, general_info, early_signs_for_diagnosis, how_to_diagnose, hdcare_summary, common_question, know_this_disease, courses_of_action, signals_to_proceed_surgery, get_to_know_this_surgery, comparisons, getting_ready, recovery, side_effects, review_4_5_stars, brand_option_in_thai_name, brand_ranking_position and faq.
33
Generate a search query based on the conversation and the new question.
4-
If the question is not in English, translate the question to English before generating the search query.
4+
If the question is not in Thai, translate the question to Thai before generating the search query.
55
If you cannot generate a search query, return the original user question.
66
DO NOT return anything besides the query.

src/fastapi_app/query_rewriter.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ def build_search_function() -> list[ChatCompletionToolParam]:
3434
},
3535
},
3636
},
37-
"brand_filter": {
38-
"type": "object",
39-
"description": "Filter search results based on brand of the product",
40-
"properties": {
41-
"comparison_operator": {
42-
"type": "string",
43-
"description": "Operator to compare the column value, either '==' or '!='",
44-
},
45-
"value": {
46-
"type": "string",
47-
"description": "Value to compare against, e.g. AirStrider",
48-
},
49-
},
50-
},
37+
# "brand_filter": {
38+
# "type": "object",
39+
# "description": "Filter search results based on brand of the product",
40+
# "properties": {
41+
# "comparison_operator": {
42+
# "type": "string",
43+
# "description": "Operator to compare the column value, either '==' or '!='",
44+
# },
45+
# "value": {
46+
# "type": "string",
47+
# "description": "Value to compare against, e.g. AirStrider",
48+
# },
49+
# },
50+
# },
5151
},
5252
"required": ["search_query"],
5353
},
@@ -77,15 +77,15 @@ def extract_search_arguments(chat_completion: ChatCompletion):
7777
"value": price_filter["value"],
7878
}
7979
)
80-
if "brand_filter" in arg and arg["brand_filter"]:
81-
brand_filter = arg["brand_filter"]
82-
filters.append(
83-
{
84-
"column": "brand",
85-
"comparison_operator": brand_filter["comparison_operator"],
86-
"value": brand_filter["value"],
87-
}
88-
)
80+
# if "brand_filter" in arg and arg["brand_filter"]:
81+
# brand_filter = arg["brand_filter"]
82+
# filters.append(
83+
# {
84+
# "column": "brand",
85+
# "comparison_operator": brand_filter["comparison_operator"],
86+
# "value": brand_filter["value"],
87+
# }
88+
# )
8989
elif query_text := response_message.content:
9090
search_query = query_text.strip()
9191
return search_query, filters

src/fastapi_app/rag_advanced.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def run(
7171
results = await self.searcher.search_and_embed(
7272
query_text,
7373
top=top,
74-
enable_vector_search=vector_search,
74+
# enable_vector_search=vector_search,
7575
enable_text_search=text_search,
7676
filters=filters,
7777
)

0 commit comments

Comments
 (0)