|
5 | 5 | # the root directory of this source tree. |
6 | 6 |
|
7 | 7 | import asyncio |
8 | | -import json |
9 | 8 | import logging |
10 | 9 | import re |
11 | 10 | import sqlite3 |
@@ -506,140 +505,6 @@ async def unregister_vector_db(self, vector_db_id: str) -> None: |
506 | 505 | await self.cache[vector_db_id].index.delete() |
507 | 506 | del self.cache[vector_db_id] |
508 | 507 |
|
509 | | - async def _save_openai_vector_store_file( |
510 | | - self, store_id: str, file_id: str, file_info: dict[str, Any], file_contents: list[dict[str, Any]] |
511 | | - ) -> None: |
512 | | - """Save vector store file metadata to SQLite database.""" |
513 | | - |
514 | | - def _create_or_store(): |
515 | | - connection = _create_sqlite_connection(self.config.db_path) |
516 | | - cur = connection.cursor() |
517 | | - try: |
518 | | - # Create a table to persist OpenAI vector store files. |
519 | | - cur.execute(""" |
520 | | - CREATE TABLE IF NOT EXISTS openai_vector_store_files ( |
521 | | - store_id TEXT, |
522 | | - file_id TEXT, |
523 | | - metadata TEXT, |
524 | | - PRIMARY KEY (store_id, file_id) |
525 | | - ); |
526 | | - """) |
527 | | - cur.execute(""" |
528 | | - CREATE TABLE IF NOT EXISTS openai_vector_store_files_contents ( |
529 | | - store_id TEXT, |
530 | | - file_id TEXT, |
531 | | - contents TEXT, |
532 | | - PRIMARY KEY (store_id, file_id) |
533 | | - ); |
534 | | - """) |
535 | | - connection.commit() |
536 | | - cur.execute( |
537 | | - "INSERT OR REPLACE INTO openai_vector_store_files (store_id, file_id, metadata) VALUES (?, ?, ?)", |
538 | | - (store_id, file_id, json.dumps(file_info)), |
539 | | - ) |
540 | | - cur.execute( |
541 | | - "INSERT OR REPLACE INTO openai_vector_store_files_contents (store_id, file_id, contents) VALUES (?, ?, ?)", |
542 | | - (store_id, file_id, json.dumps(file_contents)), |
543 | | - ) |
544 | | - connection.commit() |
545 | | - except Exception as e: |
546 | | - logger.error(f"Error saving openai vector store file {store_id} {file_id}: {e}") |
547 | | - raise |
548 | | - finally: |
549 | | - cur.close() |
550 | | - connection.close() |
551 | | - |
552 | | - try: |
553 | | - await asyncio.to_thread(_create_or_store) |
554 | | - except Exception as e: |
555 | | - logger.error(f"Error saving openai vector store file {store_id} {file_id}: {e}") |
556 | | - raise |
557 | | - |
558 | | - async def _load_openai_vector_store_file(self, store_id: str, file_id: str) -> dict[str, Any]: |
559 | | - """Load vector store file metadata from SQLite database.""" |
560 | | - |
561 | | - def _load(): |
562 | | - connection = _create_sqlite_connection(self.config.db_path) |
563 | | - cur = connection.cursor() |
564 | | - try: |
565 | | - cur.execute( |
566 | | - "SELECT metadata FROM openai_vector_store_files WHERE store_id = ? AND file_id = ?", |
567 | | - (store_id, file_id), |
568 | | - ) |
569 | | - row = cur.fetchone() |
570 | | - if row is None: |
571 | | - return None |
572 | | - (metadata,) = row |
573 | | - return metadata |
574 | | - finally: |
575 | | - cur.close() |
576 | | - connection.close() |
577 | | - |
578 | | - stored_data = await asyncio.to_thread(_load) |
579 | | - return json.loads(stored_data) if stored_data else {} |
580 | | - |
581 | | - async def _load_openai_vector_store_file_contents(self, store_id: str, file_id: str) -> list[dict[str, Any]]: |
582 | | - """Load vector store file contents from SQLite database.""" |
583 | | - |
584 | | - def _load(): |
585 | | - connection = _create_sqlite_connection(self.config.db_path) |
586 | | - cur = connection.cursor() |
587 | | - try: |
588 | | - cur.execute( |
589 | | - "SELECT contents FROM openai_vector_store_files_contents WHERE store_id = ? AND file_id = ?", |
590 | | - (store_id, file_id), |
591 | | - ) |
592 | | - row = cur.fetchone() |
593 | | - if row is None: |
594 | | - return None |
595 | | - (contents,) = row |
596 | | - return contents |
597 | | - finally: |
598 | | - cur.close() |
599 | | - connection.close() |
600 | | - |
601 | | - stored_contents = await asyncio.to_thread(_load) |
602 | | - return json.loads(stored_contents) if stored_contents else [] |
603 | | - |
604 | | - async def _update_openai_vector_store_file(self, store_id: str, file_id: str, file_info: dict[str, Any]) -> None: |
605 | | - """Update vector store file metadata in SQLite database.""" |
606 | | - |
607 | | - def _update(): |
608 | | - connection = _create_sqlite_connection(self.config.db_path) |
609 | | - cur = connection.cursor() |
610 | | - try: |
611 | | - cur.execute( |
612 | | - "UPDATE openai_vector_store_files SET metadata = ? WHERE store_id = ? AND file_id = ?", |
613 | | - (json.dumps(file_info), store_id, file_id), |
614 | | - ) |
615 | | - connection.commit() |
616 | | - finally: |
617 | | - cur.close() |
618 | | - connection.close() |
619 | | - |
620 | | - await asyncio.to_thread(_update) |
621 | | - |
622 | | - async def _delete_openai_vector_store_file_from_storage(self, store_id: str, file_id: str) -> None: |
623 | | - """Delete vector store file metadata from SQLite database.""" |
624 | | - |
625 | | - def _delete(): |
626 | | - connection = _create_sqlite_connection(self.config.db_path) |
627 | | - cur = connection.cursor() |
628 | | - try: |
629 | | - cur.execute( |
630 | | - "DELETE FROM openai_vector_store_files WHERE store_id = ? AND file_id = ?", (store_id, file_id) |
631 | | - ) |
632 | | - cur.execute( |
633 | | - "DELETE FROM openai_vector_store_files_contents WHERE store_id = ? AND file_id = ?", |
634 | | - (store_id, file_id), |
635 | | - ) |
636 | | - connection.commit() |
637 | | - finally: |
638 | | - cur.close() |
639 | | - connection.close() |
640 | | - |
641 | | - await asyncio.to_thread(_delete) |
642 | | - |
643 | 508 | async def insert_chunks(self, vector_db_id: str, chunks: list[Chunk], ttl_seconds: int | None = None) -> None: |
644 | 509 | index = await self._get_and_cache_vector_db_index(vector_db_id) |
645 | 510 | if not index: |
|
0 commit comments