Client for interacting with Dgraph for vector search.
| 10 | |
| 11 | |
| 12 | class DgraphVectorClient: |
| 13 | """Client for interacting with Dgraph for vector search.""" |
| 14 | |
| 15 | def __init__(self, config: DgraphConfig): |
| 16 | """ |
| 17 | Initialize Dgraph client. |
| 18 | |
| 19 | Args: |
| 20 | config: Dgraph configuration |
| 21 | """ |
| 22 | self.config = config |
| 23 | |
| 24 | # Increase gRPC buffer limits for large vector batches |
| 25 | # Default is 4MB, increase to 256MB for batch inserts |
| 26 | grpc_options = [ |
| 27 | ('grpc.max_send_message_length', 256 * 1024 * 1024), |
| 28 | ('grpc.max_receive_message_length', 256 * 1024 * 1024), |
| 29 | ] |
| 30 | self.client_stub = pydgraph.DgraphClientStub( |
| 31 | addr=config.grpc_endpoint, |
| 32 | options=grpc_options |
| 33 | ) |
| 34 | self.client = pydgraph.DgraphClient(self.client_stub) |
| 35 | print(f"Connected to Dgraph at {config.grpc_endpoint} (256MB gRPC buffers)") |
| 36 | |
| 37 | def setup_schema(self, hnsw_config: HNSWConfig): |
| 38 | """ |
| 39 | Set up the Dgraph schema for BEIR documents. |
| 40 | |
| 41 | Args: |
| 42 | hnsw_config: HNSW index configuration |
| 43 | """ |
| 44 | print("Setting up Dgraph schema...") |
| 45 | |
| 46 | schema = f""" |
| 47 | doc_id: string @index(hash) . |
| 48 | title: string @index(term, fulltext) . |
| 49 | text: string @index(fulltext) . |
| 50 | embedding: float32vector @index({hnsw_config.to_index_string()}) . |
| 51 | |
| 52 | type Document {{ |
| 53 | doc_id |
| 54 | title |
| 55 | text |
| 56 | embedding |
| 57 | }} |
| 58 | """ |
| 59 | |
| 60 | op = pydgraph.Operation(schema=schema) |
| 61 | self.client.alter(op) |
| 62 | print("Schema setup complete") |
| 63 | |
| 64 | def reset_database(self): |
| 65 | """Drop all data and schema.""" |
| 66 | print("Dropping all data and schema...") |
| 67 | op = pydgraph.Operation(drop_all=True) |
| 68 | self.client.alter(op) |
| 69 | print("Database reset complete") |