|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# ================================================== # |
| 4 | +# This file is a part of PYGPT package # |
| 5 | +# Website: https://pygpt.net # |
| 6 | +# GitHub: https://github.com/szczyglis-dev/py-gpt # |
| 7 | +# MIT License # |
| 8 | +# Created By : Marcin Szczygliński # |
| 9 | +# Updated Date: 2025.09.30 13:00:00 # |
| 10 | +# ================================================== # |
| 11 | + |
| 12 | +import datetime |
| 13 | +import os.path |
| 14 | +from typing import Optional |
| 15 | + |
| 16 | +from llama_index.core.indices.base import BaseIndex |
| 17 | +from llama_index.core import StorageContext |
| 18 | + |
| 19 | +from pygpt_net.utils import parse_args |
| 20 | +from .base import BaseStore |
| 21 | + |
| 22 | + |
| 23 | +class QdrantProvider(BaseStore): |
| 24 | + def __init__(self, *args, **kwargs): |
| 25 | + super(QdrantProvider, self).__init__(*args, **kwargs) |
| 26 | + """ |
| 27 | + Qdrant vector store provider |
| 28 | +
|
| 29 | + :param args: args |
| 30 | + :param kwargs: kwargs |
| 31 | + """ |
| 32 | + self.window = kwargs.get('window', None) |
| 33 | + self.id = "QdrantVectorStore" |
| 34 | + self.prefix = "qdrant_" # prefix for index directory |
| 35 | + self.indexes = {} |
| 36 | + |
| 37 | + def create(self, id: str): |
| 38 | + """ |
| 39 | + Create empty index |
| 40 | +
|
| 41 | + :param id: index name |
| 42 | + """ |
| 43 | + path = self.get_path(id) |
| 44 | + if not os.path.exists(path): |
| 45 | + os.makedirs(path) |
| 46 | + self.store(id) |
| 47 | + |
| 48 | + def get_qdrant_store(self, id: str): |
| 49 | + """ |
| 50 | + Get Qdrant vector store |
| 51 | +
|
| 52 | + :param id: index name |
| 53 | + :return: QdrantVectorStore instance |
| 54 | + """ |
| 55 | + from llama_index.vector_stores.qdrant import QdrantVectorStore |
| 56 | + |
| 57 | + additional_args = parse_args( |
| 58 | + self.window.core.config.get('llama.idx.storage.args', []), |
| 59 | + ) |
| 60 | + |
| 61 | + # Extract Qdrant connection parameters |
| 62 | + url = additional_args.get('url', 'http://localhost:6333') |
| 63 | + api_key = additional_args.get('api_key', None) |
| 64 | + |
| 65 | + store_args = {k: v for k, v in additional_args.items() if k not in ['url', 'api_key', 'collection_name']} |
| 66 | + |
| 67 | + return QdrantVectorStore( |
| 68 | + url=url, |
| 69 | + api_key=api_key, |
| 70 | + collection_name=id, |
| 71 | + **store_args |
| 72 | + ) |
| 73 | + |
| 74 | + def get( |
| 75 | + self, |
| 76 | + id: str, |
| 77 | + llm: Optional = None, |
| 78 | + embed_model: Optional = None, |
| 79 | + ) -> BaseIndex: |
| 80 | + """ |
| 81 | + Get index |
| 82 | +
|
| 83 | + :param id: index name |
| 84 | + :param llm: LLM instance |
| 85 | + :param embed_model: Embedding model instance |
| 86 | + :return: index instance |
| 87 | + """ |
| 88 | + if not self.exists(id): |
| 89 | + self.create(id) |
| 90 | + vector_store = self.get_qdrant_store(id) |
| 91 | + storage_context = StorageContext.from_defaults( |
| 92 | + vector_store=vector_store, |
| 93 | + ) |
| 94 | + self.indexes[id] = self.index_from_store( |
| 95 | + vector_store=vector_store, |
| 96 | + storage_context=storage_context, |
| 97 | + llm=llm, |
| 98 | + embed_model=embed_model, |
| 99 | + ) |
| 100 | + return self.indexes[id] |
| 101 | + |
| 102 | + def store( |
| 103 | + self, |
| 104 | + id: str, |
| 105 | + index: Optional[BaseIndex] = None |
| 106 | + ): |
| 107 | + """ |
| 108 | + Store index |
| 109 | +
|
| 110 | + :param id: index name |
| 111 | + :param index: index instance |
| 112 | + """ |
| 113 | + path = self.get_path(id) |
| 114 | + lock_file = os.path.join(path, 'store.lock') |
| 115 | + with open(lock_file, 'w') as f: |
| 116 | + f.write(id + ': ' + str(datetime.datetime.now())) |
| 117 | + self.indexes[id] = index |
0 commit comments