ryoshimu
commited on
Commit
·
91b1036
1
Parent(s):
fd79305
commit
Browse files- __pycache__/app.cpython-313.pyc +0 -0
- app.py +20 -20
__pycache__/app.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
from dataclasses import dataclass
|
| 4 |
-
from typing import Dict, Iterable, List, Optional
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
|
@@ -20,7 +20,7 @@ class StorageHit:
|
|
| 20 |
snippet: Optional[str]
|
| 21 |
|
| 22 |
|
| 23 |
-
VECTOR_STORE_ID_RE = re.compile(r"
|
| 24 |
|
| 25 |
|
| 26 |
class OpenAIStorageClient:
|
|
@@ -113,19 +113,19 @@ class OpenAIStorageClient:
|
|
| 113 |
attempts += 1
|
| 114 |
if attempts > retries:
|
| 115 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
def extract_vector_store_context(question: str) -> Tuple[str, List[str]]:
|
| 119 |
-
"""Extract vector store IDs and return the cleaned question text."""
|
| 120 |
ids: List[str] = []
|
| 121 |
-
for
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
cleaned = re.sub(r"\s+", " ", cleaned).strip()
|
| 128 |
-
return cleaned, ids
|
| 129 |
|
| 130 |
|
| 131 |
def generate_search_queries(question: str) -> List[str]:
|
|
@@ -184,17 +184,18 @@ def produce_answer(question: str, top_k: int) -> str:
|
|
| 184 |
|
| 185 |
client = OpenAIStorageClient(api_key=api_key)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
if not queries:
|
| 190 |
return "該当データがありません。"
|
| 191 |
|
| 192 |
try:
|
| 193 |
hits_by_query = []
|
| 194 |
for query in queries:
|
| 195 |
-
filters =
|
| 196 |
-
if vector_store_ids:
|
| 197 |
-
filters = {"vector_store_ids": vector_store_ids}
|
| 198 |
hits = client.storage_search(query=query, top_k=top_k, filters=filters)
|
| 199 |
if hits:
|
| 200 |
hits_by_query.append((query, hits))
|
|
@@ -250,8 +251,7 @@ with gr.Blocks() as demo:
|
|
| 250 |
"""
|
| 251 |
## MCP Storage レスポンダ
|
| 252 |
OpenAI Storage (Files / Vector Store) に登録されたデータを検索し、根拠付きで回答します。
|
| 253 |
-
|
| 254 |
-
OPENAI_API_KEY を環境変数に設定してからご利用ください。
|
| 255 |
"""
|
| 256 |
)
|
| 257 |
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
from dataclasses import dataclass
|
| 4 |
+
from typing import Dict, Iterable, List, Optional
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
|
|
|
| 20 |
snippet: Optional[str]
|
| 21 |
|
| 22 |
|
| 23 |
+
VECTOR_STORE_ID_RE = re.compile(r"vs_[a-zA-Z0-9]{8,}")
|
| 24 |
|
| 25 |
|
| 26 |
class OpenAIStorageClient:
|
|
|
|
| 113 |
attempts += 1
|
| 114 |
if attempts > retries:
|
| 115 |
raise
|
| 116 |
+
def get_vector_store_ids_from_env() -> List[str]:
|
| 117 |
+
"""Read VECTOR_STORE_ID env var and return validated vector store IDs."""
|
| 118 |
+
raw = os.getenv("VECTOR_STORE_ID", "").strip()
|
| 119 |
+
if not raw:
|
| 120 |
+
return []
|
| 121 |
|
|
|
|
|
|
|
|
|
|
| 122 |
ids: List[str] = []
|
| 123 |
+
for token in re.split(r"[,\s]+", raw):
|
| 124 |
+
if not token:
|
| 125 |
+
continue
|
| 126 |
+
if VECTOR_STORE_ID_RE.fullmatch(token) and token not in ids:
|
| 127 |
+
ids.append(token)
|
| 128 |
+
return ids
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
def generate_search_queries(question: str) -> List[str]:
|
|
|
|
| 184 |
|
| 185 |
client = OpenAIStorageClient(api_key=api_key)
|
| 186 |
|
| 187 |
+
vector_store_ids = get_vector_store_ids_from_env()
|
| 188 |
+
if not vector_store_ids:
|
| 189 |
+
return "VECTOR_STORE_ID が設定されていません。環境変数を確認してください。"
|
| 190 |
+
|
| 191 |
+
queries = generate_search_queries(question)
|
| 192 |
if not queries:
|
| 193 |
return "該当データがありません。"
|
| 194 |
|
| 195 |
try:
|
| 196 |
hits_by_query = []
|
| 197 |
for query in queries:
|
| 198 |
+
filters = {"vector_store_ids": vector_store_ids}
|
|
|
|
|
|
|
| 199 |
hits = client.storage_search(query=query, top_k=top_k, filters=filters)
|
| 200 |
if hits:
|
| 201 |
hits_by_query.append((query, hits))
|
|
|
|
| 251 |
"""
|
| 252 |
## MCP Storage レスポンダ
|
| 253 |
OpenAI Storage (Files / Vector Store) に登録されたデータを検索し、根拠付きで回答します。
|
| 254 |
+
OPENAI_API_KEY と VECTOR_STORE_ID を環境変数に設定してからご利用ください。
|
|
|
|
| 255 |
"""
|
| 256 |
)
|
| 257 |
|