|
@@ -376,28 +376,8 @@ def insert_table(data: dataform):
|
|
|
|
|
|
return {"state": 500 , "message" : str(e)}
|
|
|
|
|
|
-# @dbRouter.post("/video_save_into_cache")
|
|
|
-# def message_not_in_cache(video_name : Annotated[str, Field(description="檔案請丟進/home/mia/101/static/video_cache/others/資料夾裡")],client_message_id :str = None,question:str = None):
|
|
|
-# try:
|
|
|
-# data = []
|
|
|
-# if client_message_id :
|
|
|
-# data, count = supabase.table('client_message').select('*').eq("id",client_message_id).execute()
|
|
|
-# elif question:
|
|
|
-# data, count = supabase.table('client_message').select('*').eq("question",question).execute()
|
|
|
-
|
|
|
-# info = data[1][0]
|
|
|
-
|
|
|
-# response = supabase.table('video_cache').insert({"question": info["question"],"answer":info["answer"],"video_url":f"/static/video_cache/others/{video_name}"}).execute()
|
|
|
-
|
|
|
-# response = supabase.table('client_message').delete().eq('id', info["id"]).execute()
|
|
|
-
|
|
|
-# return {"state": 200 , "message" : "success"}
|
|
|
|
|
|
-# except Exception as e:
|
|
|
-
|
|
|
-# return {"state": 500 , "message" : str(e)}
|
|
|
-
|
|
|
-from sherry.semantic_search import ask_question
|
|
|
+from sherry.semantic_search import ask_question,ask_question_find_brand
|
|
|
|
|
|
@dbRouter.post("/video_cache")
|
|
|
def video_cache(client_message :str,language:str ="ch"):
|
|
@@ -413,6 +393,8 @@ def video_cache(client_message :str,language:str ="ch"):
|
|
|
|
|
|
result = ask_question(client_message,language=language)
|
|
|
|
|
|
+ data = search_date(client_message,language=language)
|
|
|
+
|
|
|
# result[0]["answer"]
|
|
|
|
|
|
if result == None :
|
|
@@ -420,55 +402,124 @@ def video_cache(client_message :str,language:str ="ch"):
|
|
|
|
|
|
# data, count = supabase.table("log_record").insert({"question":client_message, "answer":result[0]["answer"]}).execute()
|
|
|
|
|
|
- return {"state": 200 , "message" : result }
|
|
|
+ return {"state": 200 , "message" : result ,"data":data}
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
return {"state": 500 , "message" : str(e)}
|
|
|
|
|
|
|
|
|
-# from openai import OpenAI
|
|
|
-# import json
|
|
|
-
|
|
|
-# client = OpenAI(
|
|
|
-# # This is the default and can be omitted
|
|
|
-# api_key=os.environ.get("OPENAI_API_KEY"),
|
|
|
-# )
|
|
|
-
|
|
|
-# def access_openai(prompt_value):
|
|
|
-# chat_completion = client.chat.completions.create(
|
|
|
-# messages=[
|
|
|
-# {
|
|
|
-# "role": "user",
|
|
|
-# "content": f"請將以下的內容翻譯為英文:\n\n {prompt_value}",
|
|
|
-# }
|
|
|
-# ],
|
|
|
-# model="gpt-3.5-turbo",
|
|
|
-# )
|
|
|
+from openai import OpenAI
|
|
|
+import json
|
|
|
+
|
|
|
+client = OpenAI(
|
|
|
+ # This is the default and can be omitted
|
|
|
+ api_key=os.environ.get("OPENAI_API_KEY"),
|
|
|
+)
|
|
|
+
|
|
|
+def access_openai(prompt_value):
|
|
|
+ chat_completion = client.chat.completions.create(
|
|
|
+ messages=[
|
|
|
+ {
|
|
|
+ "role": "user",
|
|
|
+ "content": f"請將以下的內容翻譯為韓文:\n\n {prompt_value}",
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ model="gpt-3.5-turbo",
|
|
|
+ )
|
|
|
|
|
|
|
|
|
-# return chat_completion.choices[0].message.content
|
|
|
+ return chat_completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
-# @dbRouter.post("/translate")
|
|
|
-# def translate():
|
|
|
-# try:
|
|
|
-# response = supabase.table('video_cache').select('*').eq('language', 'ch').execute()
|
|
|
+@dbRouter.post("/translate")
|
|
|
+def translate():
|
|
|
+ try:
|
|
|
+ response = supabase.table('video_cache').select('*').eq('language', 'ch').execute()
|
|
|
|
|
|
-# datas = response.data
|
|
|
+ datas = response.data
|
|
|
|
|
|
-# for data in datas :
|
|
|
-# translated_question = access_openai(data['question'])
|
|
|
-# translated_answer = access_openai(data['answer'])
|
|
|
+ for data in datas :
|
|
|
+ translated_question = access_openai(data['question'])
|
|
|
+ translated_answer = access_openai(data['answer'])
|
|
|
|
|
|
-# print(data['question'])
|
|
|
-# print(translated_question)
|
|
|
+ print(data['question'])
|
|
|
+ print(translated_question)
|
|
|
|
|
|
-# insert = supabase.table('client_message').insert({"client_id":"0", "question":translated_question,"answer":translated_answer,"language":"en"}).execute()
|
|
|
+ insert = supabase.table('client_message').insert({"client_id":"0", "question":translated_question,"answer":translated_answer,"language":"ko"}).execute()
|
|
|
|
|
|
-# return {"state": 200 }
|
|
|
+ return {"state": 200 }
|
|
|
|
|
|
-# except Exception as e:
|
|
|
+ except Exception as e:
|
|
|
+
|
|
|
+ return {"state": 500 , "message" : str(e)}
|
|
|
+
|
|
|
+import spacy
|
|
|
+import jieba
|
|
|
|
|
|
-# return {"state": 500 , "message" : str(e)}
|
|
|
+@dbRouter.post("/search_date")
|
|
|
+def search_date(question:str,language:str="ch"):
|
|
|
+ try:
|
|
|
+ global nlp,exclude_conditions
|
|
|
+ if language == "ch":
|
|
|
+
|
|
|
+ nlp = spacy.load("zh_core_web_sm")
|
|
|
+ exclude_languages = ["韓文", "日文", "英文"]
|
|
|
+
|
|
|
+ elif language == "en":
|
|
|
+ nlp = spacy.load("en_core_web_sm")
|
|
|
+ exclude_languages = ["韓文", "日文", "中文"]
|
|
|
+
|
|
|
+
|
|
|
+ # 處理輸入
|
|
|
+ doc = jieba.lcut(question)
|
|
|
+
|
|
|
+ # 提取關鍵字
|
|
|
+ keywords = [word for word in doc if len(word) > 1]
|
|
|
+
|
|
|
+ print(keywords)
|
|
|
+
|
|
|
+ # 構築條件
|
|
|
+ brand_query = supabase.from_("101_brand").select("*").eq("language",language)
|
|
|
+
|
|
|
+ keywords_condition = ",".join([f"tags.ilike.%{keyword}%" for keyword in keywords])
|
|
|
+
|
|
|
+ # 查询 101_brand 表
|
|
|
+ brand_query = brand_query.or_(keywords_condition)
|
|
|
+
|
|
|
+ # 排除其他國家語言標籤
|
|
|
+ # for lang in exclude_languages:
|
|
|
+ # brand_query = brand_query.not_.ilike("tags", f"%{lang}%")
|
|
|
+
|
|
|
+ brand_results = brand_query.execute()
|
|
|
+
|
|
|
+ # 查詢 101_ticket 表
|
|
|
+ ticket_query = supabase.from_("101_ticket").select("*").or_(keywords_condition)
|
|
|
+
|
|
|
+ # 排除其他國家語言標籤
|
|
|
+ for lang in exclude_languages:
|
|
|
+ ticket_query = ticket_query.not_.ilike("tags", f"%{lang}%")
|
|
|
+
|
|
|
+ ticket_results = ticket_query.execute()
|
|
|
+
|
|
|
+ merged_results = []
|
|
|
+ for record in ticket_results.data:
|
|
|
+ merged_results.append({
|
|
|
+ "type": record.get("type"),
|
|
|
+ "info": record
|
|
|
+ })
|
|
|
+
|
|
|
+ # 格式化 `101_brand` 的结果
|
|
|
+ for record in brand_results.data:
|
|
|
+ merged_results.append({
|
|
|
+ "type": record.get("type"),
|
|
|
+ "info": record
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ return merged_results
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+
|
|
|
+ return {"state": 500 , "message" : str(e)}
|