Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,18 +26,16 @@ def get_embedding(text: str) -> List[float]:
|
|
| 26 |
"""
|
| 27 |
response = client.embeddings.create(
|
| 28 |
input=text,
|
| 29 |
-
model="text-embedding-
|
| 30 |
)
|
| 31 |
return response.data[0].embedding
|
| 32 |
|
| 33 |
-
def search_similar_chats(query: str, maxResults: int =
|
| 34 |
"""
|
| 35 |
์ ์ฌํ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 36 |
-
|
| 37 |
Args:
|
| 38 |
query (str): ๊ฒ์ํ ์ฟผ๋ฆฌ ํ
์คํธ
|
| 39 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
| 40 |
-
|
| 41 |
Returns:
|
| 42 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 43 |
"""
|
|
@@ -48,7 +46,6 @@ def search_similar_chats(query: str, maxResults: int = 30000) -> List[Dict]:
|
|
| 48 |
cur.execute("""
|
| 49 |
SELECT id, metadata, content, embedding <#> %s AS distance
|
| 50 |
FROM vector_store
|
| 51 |
-
WHERE metadata->>'documentType' = 'chatAnalysis'
|
| 52 |
ORDER BY embedding <#> %s
|
| 53 |
LIMIT %s
|
| 54 |
""", (embedding, embedding, maxResults))
|
|
@@ -64,22 +61,20 @@ def search_similar_chats(query: str, maxResults: int = 30000) -> List[Dict]:
|
|
| 64 |
for row in rows
|
| 65 |
]
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
|
| 69 |
startDate: str = None,
|
| 70 |
endDate: str = None,
|
| 71 |
-
maxResults: int =
|
| 72 |
) -> List[Dict]:
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
Args:
|
| 78 |
-
|
| 79 |
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 80 |
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 81 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
| 82 |
-
|
| 83 |
Returns:
|
| 84 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 85 |
"""
|
|
@@ -87,97 +82,47 @@ def search_chats_by_category_and_date(
|
|
| 87 |
try:
|
| 88 |
start_dt = datetime.strptime(startDate, "%Y-%m-%d")
|
| 89 |
end_dt = datetime.strptime(endDate, "%Y-%m-%d")
|
| 90 |
-
if (end_dt - start_dt).days > 31:
|
| 91 |
-
raise ValueError("๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ๋ฉ๋๋ค.")
|
| 92 |
except Exception as e:
|
| 93 |
-
raise ValueError(f"๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
FROM vector_store
|
| 97 |
-
WHERE metadata->>'documentType' = 'chatAnalysis'
|
| 98 |
-
AND metadata->>'category' = %s
|
| 99 |
-
AND (metadata->>'startTime') IS NOT NULL
|
| 100 |
-
AND (metadata->>'startTime') <> ''
|
| 101 |
-
"""
|
| 102 |
-
params = [category]
|
| 103 |
-
if startDate not in (None, ""):
|
| 104 |
-
query += " AND (metadata->>'startTime')::timestamp >= %s"
|
| 105 |
-
params.append(startDate)
|
| 106 |
-
if endDate not in (None, ""):
|
| 107 |
-
query += " AND (metadata->>'startTime')::timestamp <= %s"
|
| 108 |
-
params.append(endDate)
|
| 109 |
-
query += " LIMIT %s"
|
| 110 |
-
params.append(maxResults)
|
| 111 |
conn = get_db_conn()
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
{
|
| 118 |
-
"id": row[0],
|
| 119 |
-
"metadata": row[1],
|
| 120 |
-
"content": row[2]
|
| 121 |
-
}
|
| 122 |
-
for row in rows
|
| 123 |
-
]
|
| 124 |
-
|
| 125 |
-
def search_chats_by_product_and_date(
|
| 126 |
-
productName: str = None,
|
| 127 |
-
startDate: str = None,
|
| 128 |
-
endDate: str = None,
|
| 129 |
-
maxResults: int = 30000
|
| 130 |
-
) -> List[Dict]:
|
| 131 |
-
"""
|
| 132 |
-
์ํ๋ช
๊ณผ ์ง์ ๋ ๋ ์ง ๋ฒ์์ ํด๋นํ๋ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 133 |
-
(๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ)
|
| 134 |
-
|
| 135 |
-
Args:
|
| 136 |
-
productName (str): ์ํ๋ช
(์ต์
)
|
| 137 |
-
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 138 |
-
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 139 |
-
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
| 140 |
-
|
| 141 |
-
Returns:
|
| 142 |
-
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 143 |
-
"""
|
| 144 |
-
if startDate not in (None, "") and endDate not in (None, ""):
|
| 145 |
-
try:
|
| 146 |
-
start_dt = datetime.strptime(startDate, "%Y-%m-%d")
|
| 147 |
-
end_dt = datetime.strptime(endDate, "%Y-%m-%d")
|
| 148 |
-
if (end_dt - start_dt).days > 31:
|
| 149 |
-
raise ValueError("๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ๋ฉ๋๋ค.")
|
| 150 |
-
except Exception as e:
|
| 151 |
-
raise ValueError(f"๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์๊ฑฐ๋, ๊ธฐ๊ฐ์ด ๋๋ฌด ๊น๋๋ค: {e}")
|
| 152 |
-
query = """
|
| 153 |
-
SELECT id, metadata, content
|
| 154 |
FROM vector_store
|
| 155 |
-
WHERE metadata->>'
|
| 156 |
-
AND (metadata->>'startTime') IS NOT NULL
|
| 157 |
AND (metadata->>'startTime') <> ''
|
| 158 |
"""
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
if startDate not in (None, ""):
|
| 164 |
-
|
| 165 |
params.append(startDate)
|
|
|
|
| 166 |
if endDate not in (None, ""):
|
| 167 |
-
|
| 168 |
params.append(endDate)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
with conn.cursor() as cur:
|
| 173 |
-
cur.execute(
|
| 174 |
rows = cur.fetchall()
|
| 175 |
conn.close()
|
|
|
|
| 176 |
return [
|
| 177 |
{
|
| 178 |
"id": row[0],
|
| 179 |
"metadata": row[1],
|
| 180 |
-
"content": row[2]
|
|
|
|
| 181 |
}
|
| 182 |
for row in rows
|
| 183 |
]
|
|
@@ -186,8 +131,7 @@ def search_chats_by_product_and_date(
|
|
| 186 |
with gr.Blocks() as demo:
|
| 187 |
gr.Markdown("# Chat Analysis Search")
|
| 188 |
gr.Interface(fn=search_similar_chats, inputs=["text", "number"], outputs="json", api_name="search_similar_chats")
|
| 189 |
-
gr.Interface(fn=
|
| 190 |
-
gr.Interface(fn=search_chats_by_product_and_date, inputs=["text", "text", "text", "number"], outputs="json", api_name="search_chats_by_product_and_date")
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo.launch(mcp_server=True)
|
|
|
|
| 26 |
"""
|
| 27 |
response = client.embeddings.create(
|
| 28 |
input=text,
|
| 29 |
+
model="text-embedding-3-small"
|
| 30 |
)
|
| 31 |
return response.data[0].embedding
|
| 32 |
|
| 33 |
+
def search_similar_chats(query: str, maxResults: int = 10000) -> List[Dict]:
|
| 34 |
"""
|
| 35 |
์ ์ฌํ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
|
|
|
| 36 |
Args:
|
| 37 |
query (str): ๊ฒ์ํ ์ฟผ๋ฆฌ ํ
์คํธ
|
| 38 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
|
|
|
| 39 |
Returns:
|
| 40 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 41 |
"""
|
|
|
|
| 46 |
cur.execute("""
|
| 47 |
SELECT id, metadata, content, embedding <#> %s AS distance
|
| 48 |
FROM vector_store
|
|
|
|
| 49 |
ORDER BY embedding <#> %s
|
| 50 |
LIMIT %s
|
| 51 |
""", (embedding, embedding, maxResults))
|
|
|
|
| 61 |
for row in rows
|
| 62 |
]
|
| 63 |
|
| 64 |
+
def search_similar_chats_by_date(
|
| 65 |
+
query: str,
|
| 66 |
startDate: str = None,
|
| 67 |
endDate: str = None,
|
| 68 |
+
maxResults: int = 10000
|
| 69 |
) -> List[Dict]:
|
| 70 |
"""
|
| 71 |
+
์ง์ ๋ ๋ ์ง ๋ฒ์์ ํด๋นํ๋ ์ ์ฌํ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 72 |
+
|
|
|
|
| 73 |
Args:
|
| 74 |
+
query (str): ๊ฒ์ ์ฟผ๋ฆฌ
|
| 75 |
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 76 |
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 77 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
|
|
|
| 78 |
Returns:
|
| 79 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 80 |
"""
|
|
|
|
| 82 |
try:
|
| 83 |
start_dt = datetime.strptime(startDate, "%Y-%m-%d")
|
| 84 |
end_dt = datetime.strptime(endDate, "%Y-%m-%d")
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
raise ValueError(f"๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.: {e}")
|
| 87 |
+
|
| 88 |
+
embedding = np.array(get_embedding(query)) # numpy array๋ก ๋ณํ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
conn = get_db_conn()
|
| 90 |
+
register_vector(conn) # ๋ฒกํฐ ํ์
์๋ ๋ณํ ์ง์
|
| 91 |
+
|
| 92 |
+
# SQL ์ฟผ๋ฆฌ ๊ตฌ์ฑ
|
| 93 |
+
sql_query = """
|
| 94 |
+
SELECT id, metadata, content, embedding <#> %s AS distance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
FROM vector_store
|
| 96 |
+
WHERE (metadata->>'startTime') IS NOT NULL
|
|
|
|
| 97 |
AND (metadata->>'startTime') <> ''
|
| 98 |
"""
|
| 99 |
+
|
| 100 |
+
params = [embedding]
|
| 101 |
+
|
| 102 |
+
# ๋ ์ง ํํฐ ์ถ๊ฐ
|
| 103 |
if startDate not in (None, ""):
|
| 104 |
+
sql_query += " AND (metadata->>'startTime')::timestamp >= %s"
|
| 105 |
params.append(startDate)
|
| 106 |
+
|
| 107 |
if endDate not in (None, ""):
|
| 108 |
+
sql_query += " AND (metadata->>'startTime')::timestamp <= %s"
|
| 109 |
params.append(endDate)
|
| 110 |
+
|
| 111 |
+
# ๋ฒกํฐ ๊ฑฐ๋ฆฌ๋ก ์ ๋ ฌํ๊ณ ๊ฒฐ๊ณผ ์ ํ
|
| 112 |
+
sql_query += " ORDER BY embedding <#> %s LIMIT %s"
|
| 113 |
+
params.extend([embedding, maxResults])
|
| 114 |
+
|
| 115 |
with conn.cursor() as cur:
|
| 116 |
+
cur.execute(sql_query, tuple(params))
|
| 117 |
rows = cur.fetchall()
|
| 118 |
conn.close()
|
| 119 |
+
|
| 120 |
return [
|
| 121 |
{
|
| 122 |
"id": row[0],
|
| 123 |
"metadata": row[1],
|
| 124 |
+
"content": row[2],
|
| 125 |
+
"distance": row[3]
|
| 126 |
}
|
| 127 |
for row in rows
|
| 128 |
]
|
|
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
gr.Markdown("# Chat Analysis Search")
|
| 133 |
gr.Interface(fn=search_similar_chats, inputs=["text", "number"], outputs="json", api_name="search_similar_chats")
|
| 134 |
+
gr.Interface(fn=search_similar_chats_by_date, inputs=["text", "text", "text", "number"], outputs="json", api_name="search_similar_chats_by_date")
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
demo.launch(mcp_server=True)
|