Spaces:
Sleeping
Sleeping
Commit
·
dad916e
1
Parent(s):
bf1521f
clean: delete useless class methods
Browse files- custom_pgvector.py +0 -126
custom_pgvector.py
CHANGED
@@ -431,129 +431,3 @@ class CustomPGVector(VectorStore):
|
|
431 |
pre_delete_collection=pre_delete_collection,
|
432 |
**kwargs,
|
433 |
)
|
434 |
-
|
435 |
-
@classmethod
|
436 |
-
def from_embeddings(
|
437 |
-
cls,
|
438 |
-
text_embeddings: List[Tuple[str, List[float]]],
|
439 |
-
embedding: Embeddings,
|
440 |
-
metadatas: Optional[List[dict]] = None,
|
441 |
-
table_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME,
|
442 |
-
distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY,
|
443 |
-
ids: Optional[List[str]] = None,
|
444 |
-
pre_delete_collection: bool = False,
|
445 |
-
**kwargs: Any,
|
446 |
-
) -> CustomPGVector:
|
447 |
-
"""Construct PGVector wrapper from raw documents and pre-
|
448 |
-
generated embeddings.
|
449 |
-
|
450 |
-
Return VectorStore initialized from documents and embeddings.
|
451 |
-
Postgres connection string is required
|
452 |
-
"Either pass it as a parameter
|
453 |
-
or set the PGVECTOR_CONNECTION_STRING environment variable.
|
454 |
-
|
455 |
-
Example:
|
456 |
-
.. code-block:: python
|
457 |
-
|
458 |
-
from langchain.vectorstores import PGVector
|
459 |
-
from langchain.embeddings import OpenAIEmbeddings
|
460 |
-
embeddings = OpenAIEmbeddings()
|
461 |
-
text_embeddings = embeddings.embed_documents(texts)
|
462 |
-
text_embedding_pairs = list(zip(texts, text_embeddings))
|
463 |
-
faiss = PGVector.from_embeddings(text_embedding_pairs, embeddings)
|
464 |
-
"""
|
465 |
-
texts = [t[0] for t in text_embeddings]
|
466 |
-
embeddings = [t[1] for t in text_embeddings]
|
467 |
-
|
468 |
-
return cls.__from(
|
469 |
-
texts,
|
470 |
-
embeddings,
|
471 |
-
embedding,
|
472 |
-
metadatas=metadatas,
|
473 |
-
ids=ids,
|
474 |
-
table_name=table_name,
|
475 |
-
distance_strategy=distance_strategy,
|
476 |
-
pre_delete_collection=pre_delete_collection,
|
477 |
-
**kwargs,
|
478 |
-
)
|
479 |
-
|
480 |
-
@classmethod
|
481 |
-
def from_existing_index(
|
482 |
-
cls: Type[CustomPGVector],
|
483 |
-
embedding: Embeddings,
|
484 |
-
table_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME,
|
485 |
-
distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY,
|
486 |
-
pre_delete_collection: bool = False,
|
487 |
-
**kwargs: Any,
|
488 |
-
) -> CustomPGVector:
|
489 |
-
"""
|
490 |
-
Get intsance of an existing PGVector store.This method will
|
491 |
-
return the instance of the store without inserting any new
|
492 |
-
embeddings
|
493 |
-
"""
|
494 |
-
|
495 |
-
connection_string = cls.get_connection_string(kwargs)
|
496 |
-
|
497 |
-
store = cls(
|
498 |
-
connection_string=connection_string,
|
499 |
-
table_name=table_name,
|
500 |
-
embedding_function=embedding,
|
501 |
-
distance_strategy=distance_strategy,
|
502 |
-
pre_delete_collection=pre_delete_collection,
|
503 |
-
)
|
504 |
-
|
505 |
-
return store
|
506 |
-
|
507 |
-
@classmethod
|
508 |
-
def get_connection_string(cls, kwargs: Dict[str, Any]) -> str:
|
509 |
-
connection_string: str = get_from_dict_or_env(
|
510 |
-
data=kwargs,
|
511 |
-
key="connection_string",
|
512 |
-
env_key="PGVECTOR_CONNECTION_STRING",
|
513 |
-
)
|
514 |
-
|
515 |
-
if not connection_string:
|
516 |
-
raise ValueError(
|
517 |
-
"Postgres connection string is required"
|
518 |
-
"Either pass it as a parameter"
|
519 |
-
"or set the PGVECTOR_CONNECTION_STRING environment variable."
|
520 |
-
)
|
521 |
-
|
522 |
-
return connection_string
|
523 |
-
|
524 |
-
@classmethod
|
525 |
-
def from_documents(
|
526 |
-
cls: Type[CustomPGVector],
|
527 |
-
documents: List[Document],
|
528 |
-
embedding: Embeddings,
|
529 |
-
table_name: str = "article",
|
530 |
-
column_name: str = "embeding",
|
531 |
-
distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY,
|
532 |
-
ids: Optional[List[str]] = None,
|
533 |
-
pre_delete_collection: bool = False,
|
534 |
-
**kwargs: Any,
|
535 |
-
) -> CustomPGVector:
|
536 |
-
"""
|
537 |
-
Return VectorStore initialized from documents and embeddings.
|
538 |
-
Postgres connection string is required
|
539 |
-
"Either pass it as a parameter
|
540 |
-
or set the PGVECTOR_CONNECTION_STRING environment variable.
|
541 |
-
"""
|
542 |
-
|
543 |
-
texts = [d.page_content for d in documents]
|
544 |
-
metadatas = [d.metadata for d in documents]
|
545 |
-
connection_string = cls.get_connection_string(kwargs)
|
546 |
-
|
547 |
-
kwargs["connection_string"] = connection_string
|
548 |
-
|
549 |
-
return cls.from_texts(
|
550 |
-
texts=texts,
|
551 |
-
pre_delete_collection=pre_delete_collection,
|
552 |
-
embedding=embedding,
|
553 |
-
distance_strategy=distance_strategy,
|
554 |
-
metadatas=metadatas,
|
555 |
-
ids=ids,
|
556 |
-
table_name=table_name,
|
557 |
-
column_name=column_name,
|
558 |
-
**kwargs,
|
559 |
-
)
|
|
|
431 |
pre_delete_collection=pre_delete_collection,
|
432 |
**kwargs,
|
433 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|