Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,18 @@ import faiss
|
|
| 16 |
# Initialize OpenAI client
|
| 17 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def initialize_embeddings_from_pkl(pkl_path: str, faiss_path: str):
|
| 20 |
"""從 PKL 檔案載入嵌入,並加載 FAISS 索引"""
|
| 21 |
if not os.path.exists(pkl_path):
|
|
|
|
| 16 |
# Initialize OpenAI client
|
| 17 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 18 |
|
| 19 |
+
def get_embedding(text: str, model="text-embedding-ada-002"):
|
| 20 |
+
"""從 OpenAI API 獲取文本嵌入"""
|
| 21 |
+
try:
|
| 22 |
+
response = client.embeddings.create(
|
| 23 |
+
input=[text],
|
| 24 |
+
model=model
|
| 25 |
+
)
|
| 26 |
+
return response.data[0]['embedding']
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error getting embedding: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
def initialize_embeddings_from_pkl(pkl_path: str, faiss_path: str):
|
| 32 |
"""從 PKL 檔案載入嵌入,並加載 FAISS 索引"""
|
| 33 |
if not os.path.exists(pkl_path):
|