Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
chatbot canifa
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
chatbot canifa
Commits
37643d07
Commit
37643d07
authored
Jan 30, 2026
by
Vũ Hoàng Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: resolve empty product_ids, fix sql bug, handle decimal json error & add check_is_stock tool
parent
1090ad4e
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
207 additions
and
41 deletions
+207
-41
controller.py
backend/agent/controller.py
+7
-3
helper.py
backend/agent/helper.py
+152
-37
check_is_stock.py
backend/agent/tools/check_is_stock.py
+45
-0
get_tools.py
backend/agent/tools/get_tools.py
+2
-1
product_search_helpers.py
backend/agent/tools/product_search_helpers.py
+1
-0
No files found.
backend/agent/controller.py
View file @
37643d07
...
...
@@ -17,7 +17,7 @@ from config import DEFAULT_MODEL, REDIS_CACHE_TURN_ON
from
langfuse
import
propagate_attributes
from
.graph
import
build_graph
from
.helper
import
extract_product_ids
,
handle_post_chat_async
,
parse_ai_response
from
.helper
import
extract_product_ids
,
handle_post_chat_async
,
parse_ai_response
_async
from
.models
import
AgentState
,
get_config
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -44,6 +44,7 @@ async def chat_controller(
"""
effective_identity_key
=
identity_key
or
user_id
logger
.
info
(
"chat_controller start: model=
%
s, user_id=
%
s, identity_key=
%
s"
,
model_name
,
user_id
,
effective_identity_key
...
...
@@ -129,10 +130,13 @@ async def chat_controller(
result
=
await
graph
.
ainvoke
(
initial_state
,
config
=
exec_config
)
# Parse Response
all_product_ids
=
extract_product_ids
(
result
.
get
(
"messages"
,
[]))
final_messages
=
result
.
get
(
"messages"
,
[])
# Combine history + current messages to find ALL products mentioned in conversation
full_conversation
=
messages
+
final_messages
all_product_ids
=
extract_product_ids
(
full_conversation
)
ai_raw_content
=
result
.
get
(
"ai_response"
)
.
content
if
result
.
get
(
"ai_response"
)
else
""
# Unpack 3 values now
ai_text_response
,
final_product_ids
,
new_insight
=
parse_ai_response
(
ai_raw_content
,
all_product_ids
)
ai_text_response
,
final_product_ids
,
new_insight
=
await
parse_ai_response_async
(
ai_raw_content
,
all_product_ids
)
# Save new insight to Redis if available
if
new_insight
and
effective_identity_key
:
...
...
backend/agent/helper.py
View file @
37643d07
This diff is collapsed.
Click to expand it.
backend/agent/tools/check_is_stock.py
0 → 100644
View file @
37643d07
import
logging
import
httpx
from
langchain_core.tools
import
tool
from
pydantic
import
BaseModel
,
Field
logger
=
logging
.
getLogger
(
__name__
)
class
StockCheckInput
(
BaseModel
):
skus
:
str
=
Field
(
description
=
"Danh sách mã SKU sản phẩm cần kiểm tra tồn kho, phân cách bằng dấu phẩy. Ví dụ: '6ST25W005-SE091-L,6ST25W005-SE091-M'"
)
@
tool
(
"check_is_stock"
,
args_schema
=
StockCheckInput
)
async
def
check_is_stock
(
skus
:
str
)
->
str
:
"""
Kiểm tra tình trạng tồn kho của các mã sản phẩm (SKU) thực tế từ hệ thống Canifa.
Sử dụng tool này khi người dùng hỏi về tình trạng còn hàng, hết hàng của sản phẩm cụ thể.
Input nhận vào là chuỗi các SKU phân cách bởi dấu phẩy.
"""
logger
.
info
(
f
"🔍 [Stock Check] Checking stock for SKUs: {skus}"
)
url
=
"https://canifa.com/v1/middleware/stock_get_stock_list"
params
=
{
"skus"
:
skus
}
try
:
async
with
httpx
.
AsyncClient
()
as
client
:
response
=
await
client
.
get
(
url
,
params
=
params
,
timeout
=
10.0
)
response
.
raise_for_status
()
data
=
response
.
json
()
logger
.
info
(
f
"✅ Stock Check response: {str(data)[:200]}..."
)
# Trả về raw JSON để LLM tự xử lý thông tin
return
str
(
data
)
except
httpx
.
RequestError
as
e
:
logger
.
error
(
f
"❌ Network error checking stock: {e}"
)
return
f
"Lỗi kết nối khi kiểm tra tồn kho: {str(e)}"
except
httpx
.
HTTPStatusError
as
e
:
logger
.
error
(
f
"❌ HTTP error {e.response.status_code}: {e}"
)
return
f
"Lỗi server khi kiểm tra tồn kho (Status {e.response.status_code})"
except
Exception
as
e
:
logger
.
error
(
f
"❌ Unexpected error in check_is_stock: {e}"
)
return
f
"Lỗi không xác định khi kiểm tra tồn kho: {str(e)}"
backend/agent/tools/get_tools.py
View file @
37643d07
...
...
@@ -9,11 +9,12 @@ from .brand_knowledge_tool import canifa_knowledge_search
from
.customer_info_tool
import
collect_customer_info
from
.data_retrieval_tool
import
data_retrieval_tool
from
.promotion_canifa_tool
import
canifa_get_promotions
from
.check_is_stock
import
check_is_stock
def
get_retrieval_tools
()
->
list
[
Tool
]:
"""Các tool chỉ dùng để đọc/truy vấn dữ liệu (Có thể cache)"""
return
[
data_retrieval_tool
,
canifa_knowledge_search
,
canifa_get_promotions
]
return
[
data_retrieval_tool
,
canifa_knowledge_search
,
canifa_get_promotions
,
check_is_stock
]
def
get_collection_tools
()
->
list
[
Tool
]:
...
...
backend/agent/tools/product_search_helpers.py
View file @
37643d07
...
...
@@ -99,6 +99,7 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
product_line_en,
1.0 as max_score
FROM shared_source.magento_product_dimension_with_text_embedding
WHERE internal_ref_code =
%
s OR magento_ref_code =
%
s
"""
return
sql
,
[
magento_code
,
magento_code
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment