feat: add private weaviate api_key support on from_texts (#3139)

This PR adds support for providing a Weaviate API Key to the VectorStore
methods `from_documents` and `from_texts`. With this addition, users can
authenticate to Weaviate and make requests to private Weaviate servers
when using these methods.

## Motivation
Currently, LangChain's VectorStore methods do not provide a way to
authenticate to Weaviate. This limits the functionality of the library
and makes it more difficult for users to take advantage of Weaviate's
features.

This PR addresses this issue by adding support for providing a Weaviate
API Key as extra parameter used in the `from_texts` method.

## Contributing Guidelines
I have read the [contributing
guidelines](72b7d76d79/.github/CONTRIBUTING.md)
and the PR code passes the following tests:

- [x] make format
- [x] make lint
- [x] make coverage
- [x] make test
This commit is contained in:
Felipe Lopes 2023-04-24 17:55:34 -07:00 committed by GitHub
parent 239dc10852
commit 8c56e92566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,35 @@ def _default_schema(index_name: str) -> Dict:
} }
def _create_weaviate_client(**kwargs: Any) -> Any:
client = kwargs.get("client")
if client is not None:
return client
weaviate_url = get_from_dict_or_env(kwargs, "weaviate_url", "WEAVIATE_URL")
weaviate_api_key = get_from_dict_or_env(
kwargs, "weaviate_api_key", "WEAVIATE_API_KEY", None
)
try:
import weaviate
except ImportError:
raise ValueError(
"Could not import weaviate python package. "
"Please install it with `pip instal weaviate-client`"
)
auth = (
weaviate.auth.AuthApiKey(api_key=weaviate_api_key)
if weaviate_api_key is not None
else None
)
client = weaviate.Client(weaviate_url, auth_client_secret=auth)
return client
class Weaviate(VectorStore): class Weaviate(VectorStore):
"""Wrapper around Weaviate vector database. """Wrapper around Weaviate vector database.
@ -248,18 +277,11 @@ class Weaviate(VectorStore):
weaviate_url="http://localhost:8080" weaviate_url="http://localhost:8080"
) )
""" """
weaviate_url = get_from_dict_or_env(kwargs, "weaviate_url", "WEAVIATE_URL")
try: client = _create_weaviate_client(**kwargs)
from weaviate import Client
from weaviate.util import get_valid_uuid from weaviate.util import get_valid_uuid
except ImportError:
raise ValueError(
"Could not import weaviate python package. "
"Please install it with `pip instal weaviate-client`"
)
client = Client(weaviate_url)
index_name = kwargs.get("index_name", f"LangChain_{uuid4().hex}") index_name = kwargs.get("index_name", f"LangChain_{uuid4().hex}")
embeddings = embedding.embed_documents(texts) if embedding else None embeddings = embedding.embed_documents(texts) if embedding else None
text_key = "text" text_key = "text"