From 38bc78e7444c2db5a6888324184f8f4b71c9f357 Mon Sep 17 00:00:00 2001 From: Shukri Date: Wed, 13 Mar 2024 11:01:14 +0100 Subject: [PATCH 01/10] weaviate: migrate from weaviate python client v3 to v4 (#463) * upgrade to latest weaviate server * upgrade to latest weaviate client * reformat code * create client using v4 api * use v4 api to create collection * store collection obj for convenience * upgrade filters to use v4 api * upgrade batch write to use v4 api * use v4 api cursor to retrieve all docs * upgrade query with filters to use v4 api * upgrade filter documents to use v4 API * update weaviate fixture to align with v4 API * update v4 to v3 conversion logic * fix typo * fix date v4 to v3 conversion logic * hardcode limit in query filter * fix typo * upgrade weaviate server * update v4 to v3 object date conversion the property name will still appear in the object's propertities even though it is not set. So, we need to check if it is not None too * fix invert logic bug * upgrade delete function to v4 API * update bm25 search to v4 API * update count docs to v4 API * update _write to use v4 API * support optional filters in bm25 * update embedding retrieval to use v4 API * update from_dict for v4 API * fix write invalid input test * update other test_from_dict for V4 * update test_to_dict for v4 * update test_init for v4 API * try to pas test_init * pass test_init * add exception handling in _query_paginated * remove commented out code * remove dead code * remove commented out code * return weaviate traceback too when query error occurs * make _query_paginated return an iterator * refactor _to_document * remove v4 to v3 object conv fn * update to_dict serialization * update test case * update weaviate server * updates due to latest client changes * update test case due to latest client changes * Fix filter converters return types * Rework query methods * Fix batch writing errors * Handle different vector types in _to_document * Add pagination tests * Fix pagination test --------- Co-authored-by: Silvano Cerza --- integrations/weaviate/docker-compose.yml | 2 +- integrations/weaviate/pyproject.toml | 2 +- .../document_stores/weaviate/_filters.py | 101 ++--- .../weaviate/document_store.py | 370 ++++++++---------- .../weaviate/tests/test_bm25_retriever.py | 8 - .../weaviate/tests/test_document_store.py | 126 +++--- .../tests/test_embedding_retriever.py | 8 - integrations/weaviate/tests/test_filters.py | 2 +- 8 files changed, 273 insertions(+), 346 deletions(-) diff --git a/integrations/weaviate/docker-compose.yml b/integrations/weaviate/docker-compose.yml index c61b0ed57..f7f033eee 100644 --- a/integrations/weaviate/docker-compose.yml +++ b/integrations/weaviate/docker-compose.yml @@ -8,7 +8,7 @@ services: - '8080' - --scheme - http - image: semitechnologies/weaviate:1.23.2 + image: semitechnologies/weaviate:1.24.1 ports: - 8080:8080 - 50051:50051 diff --git a/integrations/weaviate/pyproject.toml b/integrations/weaviate/pyproject.toml index 421c2ce18..54d9ec21b 100644 --- a/integrations/weaviate/pyproject.toml +++ b/integrations/weaviate/pyproject.toml @@ -24,7 +24,7 @@ classifiers = [ ] dependencies = [ "haystack-ai", - "weaviate-client==3.*", + "weaviate-client", "haystack-pydoc-tools", "python-dateutil", ] diff --git a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/_filters.py b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/_filters.py index a192c6947..a2201f0a5 100644 --- a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/_filters.py +++ b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/_filters.py @@ -4,8 +4,11 @@ from haystack.errors import FilterError from pandas import DataFrame +import weaviate +from weaviate.collections.classes.filters import Filter, FilterReturn -def convert_filters(filters: Dict[str, Any]) -> Dict[str, Any]: + +def convert_filters(filters: Dict[str, Any]) -> FilterReturn: """ Convert filters from Haystack format to Weaviate format. """ @@ -14,7 +17,7 @@ def convert_filters(filters: Dict[str, Any]) -> Dict[str, Any]: raise FilterError(msg) if "field" in filters: - return {"operator": "And", "operands": [_parse_comparison_condition(filters)]} + return Filter.all_of([_parse_comparison_condition(filters)]) return _parse_logical_condition(filters) @@ -29,7 +32,7 @@ def convert_filters(filters: Dict[str, Any]) -> Dict[str, Any]: "not in": "in", "AND": "OR", "OR": "AND", - "NOT": "AND", + "NOT": "OR", } @@ -51,7 +54,13 @@ def _invert_condition(filters: Dict[str, Any]) -> Dict[str, Any]: return inverted_condition -def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: +LOGICAL_OPERATORS = { + "AND": Filter.all_of, + "OR": Filter.any_of, +} + + +def _parse_logical_condition(condition: Dict[str, Any]) -> FilterReturn: if "operator" not in condition: msg = f"'operator' key missing in {condition}" raise FilterError(msg) @@ -67,7 +76,7 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: operands.append(_parse_logical_condition(c)) else: operands.append(_parse_comparison_condition(c)) - return {"operator": operator.lower().capitalize(), "operands": operands} + return LOGICAL_OPERATORS[operator](operands) elif operator == "NOT": inverted_conditions = _invert_condition(condition) return _parse_logical_condition(inverted_conditions) @@ -76,28 +85,6 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: raise FilterError(msg) -def _infer_value_type(value: Any) -> str: - if value is None: - return "valueNull" - - if isinstance(value, bool): - return "valueBoolean" - if isinstance(value, int): - return "valueInt" - if isinstance(value, float): - return "valueNumber" - - if isinstance(value, str): - try: - parser.isoparse(value) - return "valueDate" - except ValueError: - return "valueText" - - msg = f"Unknown value type {type(value)}" - raise FilterError(msg) - - def _handle_date(value: Any) -> str: if isinstance(value, str): try: @@ -107,25 +94,22 @@ def _handle_date(value: Any) -> str: return value -def _equal(field: str, value: Any) -> Dict[str, Any]: +def _equal(field: str, value: Any) -> FilterReturn: if value is None: - return {"path": field, "operator": "IsNull", "valueBoolean": True} - return {"path": field, "operator": "Equal", _infer_value_type(value): _handle_date(value)} + return weaviate.classes.query.Filter.by_property(field).is_none(True) + return weaviate.classes.query.Filter.by_property(field).equal(_handle_date(value)) -def _not_equal(field: str, value: Any) -> Dict[str, Any]: +def _not_equal(field: str, value: Any) -> FilterReturn: if value is None: - return {"path": field, "operator": "IsNull", "valueBoolean": False} - return { - "operator": "Or", - "operands": [ - {"path": field, "operator": "NotEqual", _infer_value_type(value): _handle_date(value)}, - {"path": field, "operator": "IsNull", "valueBoolean": True}, - ], - } + return weaviate.classes.query.Filter.by_property(field).is_none(False) + return weaviate.classes.query.Filter.by_property(field).not_equal( + _handle_date(value) + ) | weaviate.classes.query.Filter.by_property(field).is_none(True) -def _greater_than(field: str, value: Any) -> Dict[str, Any]: + +def _greater_than(field: str, value: Any) -> FilterReturn: if value is None: # When the value is None and '>' is used we create a filter that would return a Document # if it has a field set and not set at the same time. @@ -144,10 +128,10 @@ def _greater_than(field: str, value: Any) -> Dict[str, Any]: if type(value) in [list, DataFrame]: msg = f"Filter value can't be of type {type(value)} using operators '>', '>=', '<', '<='" raise FilterError(msg) - return {"path": field, "operator": "GreaterThan", _infer_value_type(value): _handle_date(value)} + return weaviate.classes.query.Filter.by_property(field).greater_than(_handle_date(value)) -def _greater_than_equal(field: str, value: Any) -> Dict[str, Any]: +def _greater_than_equal(field: str, value: Any) -> FilterReturn: if value is None: # When the value is None and '>=' is used we create a filter that would return a Document # if it has a field set and not set at the same time. @@ -166,10 +150,10 @@ def _greater_than_equal(field: str, value: Any) -> Dict[str, Any]: if type(value) in [list, DataFrame]: msg = f"Filter value can't be of type {type(value)} using operators '>', '>=', '<', '<='" raise FilterError(msg) - return {"path": field, "operator": "GreaterThanEqual", _infer_value_type(value): _handle_date(value)} + return weaviate.classes.query.Filter.by_property(field).greater_or_equal(_handle_date(value)) -def _less_than(field: str, value: Any) -> Dict[str, Any]: +def _less_than(field: str, value: Any) -> FilterReturn: if value is None: # When the value is None and '<' is used we create a filter that would return a Document # if it has a field set and not set at the same time. @@ -188,10 +172,10 @@ def _less_than(field: str, value: Any) -> Dict[str, Any]: if type(value) in [list, DataFrame]: msg = f"Filter value can't be of type {type(value)} using operators '>', '>=', '<', '<='" raise FilterError(msg) - return {"path": field, "operator": "LessThan", _infer_value_type(value): _handle_date(value)} + return weaviate.classes.query.Filter.by_property(field).less_than(_handle_date(value)) -def _less_than_equal(field: str, value: Any) -> Dict[str, Any]: +def _less_than_equal(field: str, value: Any) -> FilterReturn: if value is None: # When the value is None and '<=' is used we create a filter that would return a Document # if it has a field set and not set at the same time. @@ -210,22 +194,23 @@ def _less_than_equal(field: str, value: Any) -> Dict[str, Any]: if type(value) in [list, DataFrame]: msg = f"Filter value can't be of type {type(value)} using operators '>', '>=', '<', '<='" raise FilterError(msg) - return {"path": field, "operator": "LessThanEqual", _infer_value_type(value): _handle_date(value)} + return weaviate.classes.query.Filter.by_property(field).less_or_equal(_handle_date(value)) -def _in(field: str, value: Any) -> Dict[str, Any]: +def _in(field: str, value: Any) -> FilterReturn: if not isinstance(value, list): msg = f"{field}'s value must be a list when using 'in' or 'not in' comparators" raise FilterError(msg) - return {"operator": "And", "operands": [_equal(field, v) for v in value]} + return weaviate.classes.query.Filter.by_property(field).contains_any(value) -def _not_in(field: str, value: Any) -> Dict[str, Any]: +def _not_in(field: str, value: Any) -> FilterReturn: if not isinstance(value, list): msg = f"{field}'s value must be a list when using 'in' or 'not in' comparators" raise FilterError(msg) - return {"operator": "And", "operands": [_not_equal(field, v) for v in value]} + operands = [weaviate.classes.query.Filter.by_property(field).not_equal(v) for v in value] + return Filter.all_of(operands) COMPARISON_OPERATORS = { @@ -240,7 +225,7 @@ def _not_in(field: str, value: Any) -> Dict[str, Any]: } -def _parse_comparison_condition(condition: Dict[str, Any]) -> Dict[str, Any]: +def _parse_comparison_condition(condition: Dict[str, Any]) -> FilterReturn: field: str = condition["field"] if field.startswith("meta."): @@ -265,15 +250,11 @@ def _parse_comparison_condition(condition: Dict[str, Any]) -> Dict[str, Any]: return COMPARISON_OPERATORS[operator](field, value) -def _match_no_document(field: str) -> Dict[str, Any]: +def _match_no_document(field: str) -> FilterReturn: """ Returns a filters that will match no Document, this is used to keep the behavior consistent between different Document Stores. """ - return { - "operator": "And", - "operands": [ - {"path": field, "operator": "IsNull", "valueBoolean": False}, - {"path": field, "operator": "IsNull", "valueBoolean": True}, - ], - } + + operands = [weaviate.classes.query.Filter.by_property(field).is_none(val) for val in [False, True]] + return Filter.all_of(operands) diff --git a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py index 071fe336b..34fefa0a5 100644 --- a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py +++ b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: Apache-2.0 import base64 +import datetime +import json from dataclasses import asdict from typing import Any, Dict, List, Optional, Tuple, Union @@ -11,7 +13,8 @@ from haystack.document_stores.types.policy import DuplicatePolicy import weaviate -from weaviate.config import Config, ConnectionConfig +from weaviate.collections.classes.data import DataObject +from weaviate.config import AdditionalConfig from weaviate.embedded import EmbeddedOptions from weaviate.util import generate_uuid5 @@ -42,6 +45,16 @@ {"name": "score", "dataType": ["number"]}, ] +# This is the default limit used when querying documents with WeaviateDocumentStore. +# +# We picked this as QUERY_MAXIMUM_RESULTS defaults to 10000, trying to get that many +# documents at once will fail, even if the query is paginated. +# This value will ensure we get the most documents possible without hitting that limit, it would +# still fail if the user lowers the QUERY_MAXIMUM_RESULTS environment variable for their Weaviate instance. +# +# See WeaviateDocumentStore._query_with_filters() for more information. +DEFAULT_QUERY_LIMIT = 9999 + class WeaviateDocumentStore: """ @@ -54,13 +67,11 @@ def __init__( url: Optional[str] = None, collection_settings: Optional[Dict[str, Any]] = None, auth_client_secret: Optional[AuthCredentials] = None, - timeout_config: TimeoutType = (10, 60), - proxies: Optional[Union[Dict, str]] = None, - trust_env: bool = False, additional_headers: Optional[Dict] = None, - startup_period: Optional[int] = 5, embedded_options: Optional[EmbeddedOptions] = None, - additional_config: Optional[Config] = None, + additional_config: Optional[AdditionalConfig] = None, + grpc_port: int = 50051, + grpc_secure: bool = False, ): """ Create a new instance of WeaviateDocumentStore and connects to the Weaviate instance. @@ -88,46 +99,35 @@ def __init__( - `AuthClientPassword` to use username and password for oidc Resource Owner Password flow - `AuthClientCredentials` to use a client secret for oidc client credential flow - `AuthApiKey` to use an API key - :param timeout_config: Timeout configuration for all requests to the Weaviate server, defaults to (10, 60). - It can be a real number or, a tuple of two real numbers: (connect timeout, read timeout). - If only one real number is passed then both connect and read timeout will be set to - that value, by default (2, 20). - :param proxies: Proxy configuration, defaults to None. - Can be passed as a dict using the - ``requests` format`_, - or a string. If a string is passed it will be used for both HTTP and HTTPS requests. - :param trust_env: Whether to read proxies from the ENV variables, defaults to False. - Proxies will be read from the following ENV variables: - * `HTTP_PROXY` - * `http_proxy` - * `HTTPS_PROXY` - * `https_proxy` - If `proxies` is not None, `trust_env` is ignored. :param additional_headers: Additional headers to include in the requests, defaults to None. Can be used to set OpenAI/HuggingFace keys. OpenAI/HuggingFace key looks like this: ``` {"X-OpenAI-Api-Key": ""}, {"X-HuggingFace-Api-Key": ""} ``` - :param startup_period: How many seconds the client will wait for Weaviate to start before - raising a RequestsConnectionError, defaults to 5. :param embedded_options: If set create an embedded Weaviate cluster inside the client, defaults to None. For a full list of options see `weaviate.embedded.EmbeddedOptions`. :param additional_config: Additional and advanced configuration options for weaviate, defaults to None. + :param grpc_port: The port to use for the gRPC connection, defaults to 50051. + :param grpc_secure: Whether to use a secure channel for the underlying gRPC API. """ - self._client = weaviate.Client( - url=url, + # proxies, timeout_config, trust_env are part of additional_config now + # startup_period has been removed + self._client = weaviate.WeaviateClient( + connection_params=( + weaviate.connect.base.ConnectionParams.from_url(url=url, grpc_port=grpc_port, grpc_secure=grpc_secure) + if url + else None + ), auth_client_secret=auth_client_secret.resolve_value() if auth_client_secret else None, - timeout_config=timeout_config, - proxies=proxies, - trust_env=trust_env, + additional_config=additional_config, additional_headers=additional_headers, - startup_period=startup_period, embedded_options=embedded_options, - additional_config=additional_config, + skip_init_checks=False, ) + self._client.connect() # Test connection, it will raise an exception if it fails. - self._client.schema.get() + self._client.collections._get_all(simple=True) if collection_settings is None: collection_settings = { @@ -141,64 +141,53 @@ def __init__( # Set the properties if they're not set collection_settings["properties"] = collection_settings.get("properties", DOCUMENT_COLLECTION_PROPERTIES) - if not self._client.schema.exists(collection_settings["class"]): - self._client.schema.create_class(collection_settings) + if not self._client.collections.exists(collection_settings["class"]): + self._client.collections.create_from_dict(collection_settings) self._url = url self._collection_settings = collection_settings self._auth_client_secret = auth_client_secret - self._timeout_config = timeout_config - self._proxies = proxies - self._trust_env = trust_env self._additional_headers = additional_headers - self._startup_period = startup_period self._embedded_options = embedded_options self._additional_config = additional_config + self._collection = self._client.collections.get(collection_settings["class"]) def to_dict(self) -> Dict[str, Any]: embedded_options = asdict(self._embedded_options) if self._embedded_options else None - additional_config = asdict(self._additional_config) if self._additional_config else None + additional_config = ( + json.loads(self._additional_config.model_dump_json(by_alias=True)) if self._additional_config else None + ) return default_to_dict( self, url=self._url, collection_settings=self._collection_settings, auth_client_secret=self._auth_client_secret.to_dict() if self._auth_client_secret else None, - timeout_config=self._timeout_config, - proxies=self._proxies, - trust_env=self._trust_env, additional_headers=self._additional_headers, - startup_period=self._startup_period, embedded_options=embedded_options, additional_config=additional_config, ) @classmethod def from_dict(cls, data: Dict[str, Any]) -> "WeaviateDocumentStore": - if (timeout_config := data["init_parameters"].get("timeout_config")) is not None: - data["init_parameters"]["timeout_config"] = ( - tuple(timeout_config) if isinstance(timeout_config, list) else timeout_config - ) if (auth_client_secret := data["init_parameters"].get("auth_client_secret")) is not None: data["init_parameters"]["auth_client_secret"] = AuthCredentials.from_dict(auth_client_secret) if (embedded_options := data["init_parameters"].get("embedded_options")) is not None: data["init_parameters"]["embedded_options"] = EmbeddedOptions(**embedded_options) if (additional_config := data["init_parameters"].get("additional_config")) is not None: - additional_config["connection_config"] = ConnectionConfig(**additional_config["connection_config"]) - data["init_parameters"]["additional_config"] = Config(**additional_config) + data["init_parameters"]["additional_config"] = AdditionalConfig(**additional_config) return default_from_dict( cls, data, ) def count_documents(self) -> int: - collection_name = self._collection_settings["class"] - res = self._client.query.aggregate(collection_name).with_meta_count().do() - return res.get("data", {}).get("Aggregate", {}).get(collection_name, [{}])[0].get("meta", {}).get("count", 0) + total = self._collection.aggregate.over_all(total_count=True).total_count + return total if total else 0 def _to_data_object(self, document: Document) -> Dict[str, Any]: """ - Convert a Document to a Weviate data object ready to be saved. + Convert a Document to a Weaviate data object ready to be saved. """ data = document.to_dict() # Weaviate forces a UUID as an id. @@ -216,95 +205,82 @@ def _to_data_object(self, document: Document) -> Dict[str, Any]: return data - def _to_document(self, data: Dict[str, Any]) -> Document: + def _to_document(self, data: DataObject[Dict[str, Any], None]) -> Document: """ Convert a data object read from Weaviate into a Document. """ - data["id"] = data.pop("_original_id") - data["embedding"] = data["_additional"].pop("vector") if data["_additional"].get("vector") else None + document_data = data.properties + document_data["id"] = document_data.pop("_original_id") + if isinstance(data.vector, List): + document_data["embedding"] = data.vector + elif isinstance(data.vector, Dict): + document_data["embedding"] = data.vector.get("default") + else: + document_data["embedding"] = None - if (blob_data := data.get("blob_data")) is not None: - data["blob"] = { + if (blob_data := document_data.get("blob_data")) is not None: + document_data["blob"] = { "data": base64.b64decode(blob_data), - "mime_type": data.get("blob_mime_type"), + "mime_type": document_data.get("blob_mime_type"), } - # We always delete these fields as they're not part of the Document dataclass - data.pop("blob_data") - data.pop("blob_mime_type") - - # We don't need these fields anymore, this usually only contains the uuid - # used by Weaviate to identify the object and the embedding vector that we already extracted. - del data["_additional"] - - return Document.from_dict(data) - - def _query_paginated(self, properties: List[str], cursor=None): - collection_name = self._collection_settings["class"] - query = ( - self._client.query.get( - collection_name, - properties, - ) - .with_additional(["id vector"]) - .with_limit(100) - ) - - if cursor: - # Fetch the next set of results - result = query.with_after(cursor).do() - else: - # Fetch the first set of results - result = query.do() - - if "errors" in result: - errors = [e["message"] for e in result.get("errors", {})] - msg = "\n".join(errors) - msg = f"Failed to query documents in Weaviate. Errors:\n{msg}" - raise DocumentStoreError(msg) - - return result["data"]["Get"][collection_name] - - def _query_with_filters(self, properties: List[str], filters: Dict[str, Any]) -> List[Dict[str, Any]]: - collection_name = self._collection_settings["class"] - query = ( - self._client.query.get( - collection_name, - properties, - ) - .with_additional(["id vector"]) - .with_where(convert_filters(filters)) - ) - - result = query.do() - if "errors" in result: - errors = [e["message"] for e in result.get("errors", {})] - msg = "\n".join(errors) - msg = f"Failed to query documents in Weaviate. Errors:\n{msg}" - raise DocumentStoreError(msg) + # We always delete these fields as they're not part of the Document dataclass + document_data.pop("blob_data", None) + document_data.pop("blob_mime_type", None) + + for key, value in document_data.items(): + if isinstance(value, datetime.datetime): + document_data[key] = value.strftime("%Y-%m-%dT%H:%M:%SZ") + + return Document.from_dict(document_data) + + def _query(self) -> List[Dict[str, Any]]: + properties = [p.name for p in self._collection.config.get().properties] + try: + result = self._collection.iterator(include_vector=True, return_properties=properties) + except weaviate.exceptions.WeaviateQueryError as e: + msg = f"Failed to query documents in Weaviate. Error: {e.message}" + raise DocumentStoreError(msg) from e + return result - return result["data"]["Get"][collection_name] + def _query_with_filters(self, filters: Dict[str, Any]) -> List[Dict[str, Any]]: + properties = [p.name for p in self._collection.config.get().properties] + # When querying with filters we need to paginate using limit and offset as using + # a cursor with after is not possible. See the official docs: + # https://weaviate.io/developers/weaviate/api/graphql/additional-operators#cursor-with-after + # + # Nonetheless there's also another issue, paginating with limit and offset is not efficient + # and it's still restricted by the QUERY_MAXIMUM_RESULTS environment variable. + # If the sum of limit and offest is greater than QUERY_MAXIMUM_RESULTS an error is raised. + # See the official docs for more: + # https://weaviate.io/developers/weaviate/api/graphql/additional-operators#performance-considerations + offset = 0 + partial_result = None + result = [] + # Keep querying until we get all documents matching the filters + while partial_result is None or len(partial_result.objects) == DEFAULT_QUERY_LIMIT: + try: + partial_result = self._collection.query.fetch_objects( + filters=convert_filters(filters), + include_vector=True, + limit=DEFAULT_QUERY_LIMIT, + offset=offset, + return_properties=properties, + ) + except weaviate.exceptions.WeaviateQueryError as e: + msg = f"Failed to query documents in Weaviate. Error: {e.message}" + raise DocumentStoreError(msg) from e + result.extend(partial_result.objects) + offset += DEFAULT_QUERY_LIMIT + return result def filter_documents(self, filters: Optional[Dict[str, Any]] = None) -> List[Document]: - properties = self._client.schema.get(self._collection_settings["class"]).get("properties", []) - properties = [prop["name"] for prop in properties] - - if filters: - result = self._query_with_filters(properties, filters) - return [self._to_document(doc) for doc in result] - result = [] - - cursor = None - while batch := self._query_paginated(properties, cursor): - # Take the cursor before we convert the batch to Documents as we manipulate - # the batch dictionary and might lose that information. - cursor = batch[-1]["_additional"]["id"] - - for doc in batch: - result.append(self._to_document(doc)) - # Move the cursor to the last returned uuid - return result + if filters: + result = self._query_with_filters(filters) + else: + result = self._query() + return [self._to_document(doc) for doc in result] def _batch_write(self, documents: List[Document]) -> int: """ @@ -312,33 +288,35 @@ def _batch_write(self, documents: List[Document]) -> int: Documents with the same id will be overwritten. Raises in case of errors. """ - statuses = [] - for doc in documents: - if not isinstance(doc, Document): - msg = f"Expected a Document, got '{type(doc)}' instead." - raise ValueError(msg) - if self._client.batch.num_objects() == self._client.batch.recommended_num_objects: - # Batch is full, let's create the objects - statuses.extend(self._client.batch.create_objects()) - self._client.batch.add_data_object( - uuid=generate_uuid5(doc.id), - data_object=self._to_data_object(doc), - class_name=self._collection_settings["class"], - vector=doc.embedding, + + with self._client.batch.dynamic() as batch: + for doc in documents: + if not isinstance(doc, Document): + msg = f"Expected a Document, got '{type(doc)}' instead." + raise ValueError(msg) + + batch.add_object( + properties=self._to_data_object(doc), + collection=self._collection.name, + uuid=generate_uuid5(doc.id), + vector=doc.embedding, + ) + if failed_objects := self._client.batch.failed_objects: + # We fallback to use the UUID if the _original_id is not present, this is just to be + mapped_objects = {} + for obj in failed_objects: + properties = obj.object_.properties or {} + # We get the object uuid just in case the _original_id is not present. + # That's extremely unlikely to happen but let's stay on the safe side. + id_ = properties.get("_original_id", obj.object_.uuid) + mapped_objects[id_] = obj.message + + msg = "\n".join( + [ + f"Failed to write object with id '{id_}'. Error: '{message}'" + for id_, message in mapped_objects.items() + ] ) - # Write remaining documents - statuses.extend(self._client.batch.create_objects()) - - errors = [] - # Gather errors and number of written documents - for status in statuses: - result_status = status.get("result", {}).get("status") - if result_status == "FAILED": - errors.extend([e["message"] for e in status["result"]["errors"]["error"]]) - - if errors: - msg = "\n".join(errors) - msg = f"Failed to write documents in Weaviate. Errors:\n{msg}" raise DocumentStoreError(msg) # If the document already exists we get no status message back from Weaviate. @@ -359,22 +337,19 @@ def _write(self, documents: List[Document], policy: DuplicatePolicy) -> int: msg = f"Expected a Document, got '{type(doc)}' instead." raise ValueError(msg) - if policy == DuplicatePolicy.SKIP and self._client.data_object.exists( - uuid=generate_uuid5(doc.id), - class_name=self._collection_settings["class"], - ): + if policy == DuplicatePolicy.SKIP and self._collection.data.exists(uuid=generate_uuid5(doc.id)): # This Document already exists, we skip it continue try: - self._client.data_object.create( + self._collection.data.insert( uuid=generate_uuid5(doc.id), - data_object=self._to_data_object(doc), - class_name=self._collection_settings["class"], + properties=self._to_data_object(doc), vector=doc.embedding, ) + written += 1 - except weaviate.exceptions.ObjectAlreadyExistsException: + except weaviate.exceptions.UnexpectedStatusCodeError: if policy == DuplicatePolicy.FAIL: duplicate_errors_ids.append(doc.id) if duplicate_errors_ids: @@ -397,37 +372,21 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D return self._write(documents, policy) def delete_documents(self, document_ids: List[str]) -> None: - self._client.batch.delete_objects( - class_name=self._collection_settings["class"], - where={ - "path": ["id"], - "operator": "ContainsAny", - "valueTextArray": [generate_uuid5(doc_id) for doc_id in document_ids], - }, - ) + weaviate_ids = [generate_uuid5(doc_id) for doc_id in document_ids] + self._collection.data.delete_many(where=weaviate.classes.query.Filter.by_id().contains_any(weaviate_ids)) def _bm25_retrieval( self, query: str, filters: Optional[Dict[str, Any]] = None, top_k: Optional[int] = None ) -> List[Document]: - collection_name = self._collection_settings["class"] - properties = self._client.schema.get(self._collection_settings["class"]).get("properties", []) - properties = [prop["name"] for prop in properties] - - query_builder = ( - self._client.query.get(collection_name, properties=properties) - .with_bm25(query=query, properties=["content"]) - .with_additional(["vector"]) + result = self._collection.query.bm25( + query=query, + filters=convert_filters(filters) if filters else None, + limit=top_k, + include_vector=True, + query_properties=["content"], ) - if filters: - query_builder = query_builder.with_where(convert_filters(filters)) - - if top_k: - query_builder = query_builder.with_limit(top_k) - - result = query_builder.do() - - return [self._to_document(doc) for doc in result["data"]["Get"][collection_name]] + return [self._to_document(doc) for doc in result.objects] def _embedding_retrieval( self, @@ -441,30 +400,13 @@ def _embedding_retrieval( msg = "Can't use 'distance' and 'certainty' parameters together" raise ValueError(msg) - collection_name = self._collection_settings["class"] - properties = self._client.schema.get(self._collection_settings["class"]).get("properties", []) - properties = [prop["name"] for prop in properties] - - near_vector: Dict[str, Union[float, List[float]]] = { - "vector": query_embedding, - } - if distance is not None: - near_vector["distance"] = distance - - if certainty is not None: - near_vector["certainty"] = certainty - - query_builder = ( - self._client.query.get(collection_name, properties=properties) - .with_near_vector(near_vector) - .with_additional(["vector"]) + result = self._collection.query.near_vector( + near_vector=query_embedding, + distance=distance, + certainty=certainty, + include_vector=True, + filters=convert_filters(filters) if filters else None, + limit=top_k, ) - if filters: - query_builder = query_builder.with_where(convert_filters(filters)) - - if top_k: - query_builder = query_builder.with_limit(top_k) - - result = query_builder.do() - return [self._to_document(doc) for doc in result["data"]["Get"][collection_name]] + return [self._to_document(doc) for doc in result.objects] diff --git a/integrations/weaviate/tests/test_bm25_retriever.py b/integrations/weaviate/tests/test_bm25_retriever.py index 83f90735b..23b7c8f92 100644 --- a/integrations/weaviate/tests/test_bm25_retriever.py +++ b/integrations/weaviate/tests/test_bm25_retriever.py @@ -38,11 +38,7 @@ def test_to_dict(_mock_weaviate): ], }, "auth_client_secret": None, - "timeout_config": (10, 60), - "proxies": None, - "trust_env": False, "additional_headers": None, - "startup_period": 5, "embedded_options": None, "additional_config": None, }, @@ -76,11 +72,7 @@ def test_from_dict(_mock_weaviate): ], }, "auth_client_secret": None, - "timeout_config": (10, 60), - "proxies": None, - "trust_env": False, "additional_headers": None, - "startup_period": 5, "embedded_options": None, "additional_config": None, }, diff --git a/integrations/weaviate/tests/test_document_store.py b/integrations/weaviate/tests/test_document_store.py index a2b32d578..4c1659a86 100644 --- a/integrations/weaviate/tests/test_document_store.py +++ b/integrations/weaviate/tests/test_document_store.py @@ -7,6 +7,7 @@ from dateutil import parser from haystack.dataclasses.byte_stream import ByteStream from haystack.dataclasses.document import Document +from haystack.document_stores.errors import DocumentStoreError from haystack.testing.document_store import ( TEST_EMBEDDING_1, TEST_EMBEDDING_2, @@ -24,8 +25,10 @@ from numpy import array_equal as np_array_equal from numpy import float32 as np_float32 from pandas import DataFrame -from weaviate.auth import AuthApiKey as WeaviateAuthApiKey -from weaviate.config import Config +from weaviate.collections.classes.data import DataObject + +# from weaviate.auth import AuthApiKey as WeaviateAuthApiKey +from weaviate.config import AdditionalConfig, ConnectionConfig, Proxies, Timeout from weaviate.embedded import ( DEFAULT_BINARY_PATH, DEFAULT_GRPC_PORT, @@ -53,7 +56,7 @@ def document_store(self, request) -> WeaviateDocumentStore: collection_settings=collection_settings, ) yield store - store._client.schema.delete_class(collection_settings["class"]) + store._client.collections.delete(collection_settings["class"]) @pytest.fixture def filterable_docs(self) -> List[Document]: @@ -145,49 +148,48 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do for key in meta_keys: assert received_meta.get(key) == expected_meta.get(key) - @patch("haystack_integrations.document_stores.weaviate.document_store.weaviate.Client") + @patch("haystack_integrations.document_stores.weaviate.document_store.weaviate.WeaviateClient") def test_init(self, mock_weaviate_client_class, monkeypatch): mock_client = MagicMock() - mock_client.schema.exists.return_value = False + mock_client.collections.exists.return_value = False mock_weaviate_client_class.return_value = mock_client monkeypatch.setenv("WEAVIATE_API_KEY", "my_api_key") WeaviateDocumentStore( - url="http://localhost:8080", collection_settings={"class": "My_collection"}, auth_client_secret=AuthApiKey(), - proxies={"http": "http://proxy:1234"}, additional_headers={"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"}, embedded_options=EmbeddedOptions( persistence_data_path=DEFAULT_PERSISTENCE_DATA_PATH, binary_path=DEFAULT_BINARY_PATH, - version="1.23.0", + version="1.23.7", hostname="127.0.0.1", ), - additional_config=Config(grpc_port_experimental=12345), + additional_config=AdditionalConfig( + proxies={"http": "http://proxy:1234"}, trust_env=False, timeout=(10, 60) + ), ) # Verify client is created with correct parameters + mock_weaviate_client_class.assert_called_once_with( - url="http://localhost:8080", - auth_client_secret=WeaviateAuthApiKey("my_api_key"), - timeout_config=(10, 60), - proxies={"http": "http://proxy:1234"}, - trust_env=False, + auth_client_secret=AuthApiKey().resolve_value(), + connection_params=None, additional_headers={"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"}, - startup_period=5, embedded_options=EmbeddedOptions( persistence_data_path=DEFAULT_PERSISTENCE_DATA_PATH, binary_path=DEFAULT_BINARY_PATH, - version="1.23.0", + version="1.23.7", hostname="127.0.0.1", ), - additional_config=Config(grpc_port_experimental=12345), + skip_init_checks=False, + additional_config=AdditionalConfig( + proxies={"http": "http://proxy:1234"}, trust_env=False, timeout=(10, 60) + ), ) # Verify collection is created - mock_client.schema.get.assert_called_once() - mock_client.schema.exists.assert_called_once_with("My_collection") - mock_client.schema.create_class.assert_called_once_with( + mock_client.collections.exists.assert_called_once_with("My_collection") + mock_client.collections.create_from_dict.assert_called_once_with( {"class": "My_collection", "properties": DOCUMENT_COLLECTION_PROPERTIES} ) @@ -197,7 +199,6 @@ def test_to_dict(self, _mock_weaviate, monkeypatch): document_store = WeaviateDocumentStore( url="http://localhost:8080", auth_client_secret=AuthApiKey(), - proxies={"http": "http://proxy:1234"}, additional_headers={"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"}, embedded_options=EmbeddedOptions( persistence_data_path=DEFAULT_PERSISTENCE_DATA_PATH, @@ -205,7 +206,12 @@ def test_to_dict(self, _mock_weaviate, monkeypatch): version="1.23.0", hostname="127.0.0.1", ), - additional_config=Config(grpc_port_experimental=12345), + additional_config=AdditionalConfig( + connection=ConnectionConfig(), + timeout=(30, 90), + trust_env=False, + proxies={"http": "http://proxy:1234"}, + ), ) assert document_store.to_dict() == { "type": "haystack_integrations.document_stores.weaviate.document_store.WeaviateDocumentStore", @@ -229,11 +235,7 @@ def test_to_dict(self, _mock_weaviate, monkeypatch): "api_key": {"env_vars": ["WEAVIATE_API_KEY"], "strict": True, "type": "env_var"} }, }, - "timeout_config": (10, 60), - "proxies": {"http": "http://proxy:1234"}, - "trust_env": False, "additional_headers": {"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"}, - "startup_period": 5, "embedded_options": { "persistence_data_path": DEFAULT_PERSISTENCE_DATA_PATH, "binary_path": DEFAULT_BINARY_PATH, @@ -244,11 +246,14 @@ def test_to_dict(self, _mock_weaviate, monkeypatch): "grpc_port": DEFAULT_GRPC_PORT, }, "additional_config": { - "grpc_port_experimental": 12345, - "connection_config": { + "connection": { "session_pool_connections": 20, - "session_pool_maxsize": 20, + "session_pool_maxsize": 100, + "session_pool_max_retries": 3, }, + "proxies": {"http": "http://proxy:1234", "https": None, "grpc": None}, + "timeout": [30, 90], + "trust_env": False, }, }, } @@ -268,11 +273,7 @@ def test_from_dict(self, _mock_weaviate, monkeypatch): "api_key": {"env_vars": ["WEAVIATE_API_KEY"], "strict": True, "type": "env_var"} }, }, - "timeout_config": [10, 60], - "proxies": {"http": "http://proxy:1234"}, - "trust_env": False, "additional_headers": {"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"}, - "startup_period": 5, "embedded_options": { "persistence_data_path": DEFAULT_PERSISTENCE_DATA_PATH, "binary_path": DEFAULT_BINARY_PATH, @@ -283,11 +284,13 @@ def test_from_dict(self, _mock_weaviate, monkeypatch): "grpc_port": DEFAULT_GRPC_PORT, }, "additional_config": { - "grpc_port_experimental": 12345, - "connection_config": { + "connection": { "session_pool_connections": 20, "session_pool_maxsize": 20, }, + "proxies": {"http": "http://proxy:1234"}, + "timeout": [10, 60], + "trust_env": False, }, }, } @@ -307,11 +310,10 @@ def test_from_dict(self, _mock_weaviate, monkeypatch): ], } assert document_store._auth_client_secret == AuthApiKey() - assert document_store._timeout_config == (10, 60) - assert document_store._proxies == {"http": "http://proxy:1234"} - assert not document_store._trust_env + assert document_store._additional_config.timeout == Timeout(query=10, insert=60) + assert document_store._additional_config.proxies == Proxies(http="http://proxy:1234", https=None, grpc=None) + assert not document_store._additional_config.trust_env assert document_store._additional_headers == {"X-HuggingFace-Api-Key": "MY_HUGGINGFACE_KEY"} - assert document_store._startup_period == 5 assert document_store._embedded_options.persistence_data_path == DEFAULT_PERSISTENCE_DATA_PATH assert document_store._embedded_options.binary_path == DEFAULT_BINARY_PATH assert document_store._embedded_options.version == "1.23.0" @@ -319,9 +321,8 @@ def test_from_dict(self, _mock_weaviate, monkeypatch): assert document_store._embedded_options.hostname == "127.0.0.1" assert document_store._embedded_options.additional_env_vars is None assert document_store._embedded_options.grpc_port == DEFAULT_GRPC_PORT - assert document_store._additional_config.grpc_port_experimental == 12345 - assert document_store._additional_config.connection_config.session_pool_connections == 20 - assert document_store._additional_config.connection_config.session_pool_maxsize == 20 + assert document_store._additional_config.connection.session_pool_connections == 20 + assert document_store._additional_config.connection.session_pool_maxsize == 20 def test_to_data_object(self, document_store, test_files_path): doc = Document(content="test doc") @@ -353,18 +354,18 @@ def test_to_data_object(self, document_store, test_files_path): def test_to_document(self, document_store, test_files_path): image = ByteStream.from_file_path(test_files_path / "robot1.jpg", mime_type="image/jpeg") - data = { - "_additional": { - "vector": [1, 2, 3], + data = DataObject( + properties={ + "_original_id": "123", + "content": "some content", + "blob_data": base64.b64encode(image.data).decode(), + "blob_mime_type": "image/jpeg", + "dataframe": None, + "score": None, + "key": "value", }, - "_original_id": "123", - "content": "some content", - "blob_data": base64.b64encode(image.data).decode(), - "blob_mime_type": "image/jpeg", - "dataframe": None, - "score": None, - "meta": {"key": "value"}, - } + vector={"default": [1, 2, 3]}, + ) doc = document_store._to_document(data) assert doc.id == "123" @@ -626,3 +627,22 @@ def test_embedding_retrieval_with_certainty(self, document_store): def test_embedding_retrieval_with_distance_and_certainty(self, document_store): with pytest.raises(ValueError): document_store._embedding_retrieval(query_embedding=[], distance=0.1, certainty=0.1) + + def test_filter_documents_below_default_limit(self, document_store): + docs = [] + for index in range(9998): + docs.append(Document(content="This is some content", meta={"index": index})) + document_store.write_documents(docs) + result = document_store.filter_documents( + {"field": "content", "operator": "==", "value": "This is some content"} + ) + + assert len(result) == 9998 + + def test_filter_documents_over_default_limit(self, document_store): + docs = [] + for index in range(10000): + docs.append(Document(content="This is some content", meta={"index": index})) + document_store.write_documents(docs) + with pytest.raises(DocumentStoreError): + document_store.filter_documents({"field": "content", "operator": "==", "value": "This is some content"}) diff --git a/integrations/weaviate/tests/test_embedding_retriever.py b/integrations/weaviate/tests/test_embedding_retriever.py index 7f07d8a24..a406c40db 100644 --- a/integrations/weaviate/tests/test_embedding_retriever.py +++ b/integrations/weaviate/tests/test_embedding_retriever.py @@ -49,11 +49,7 @@ def test_to_dict(_mock_weaviate): ], }, "auth_client_secret": None, - "timeout_config": (10, 60), - "proxies": None, - "trust_env": False, "additional_headers": None, - "startup_period": 5, "embedded_options": None, "additional_config": None, }, @@ -89,11 +85,7 @@ def test_from_dict(_mock_weaviate): ], }, "auth_client_secret": None, - "timeout_config": (10, 60), - "proxies": None, - "trust_env": False, "additional_headers": None, - "startup_period": 5, "embedded_options": None, "additional_config": None, }, diff --git a/integrations/weaviate/tests/test_filters.py b/integrations/weaviate/tests/test_filters.py index cf38d84be..c32d69e2f 100644 --- a/integrations/weaviate/tests/test_filters.py +++ b/integrations/weaviate/tests/test_filters.py @@ -19,7 +19,7 @@ def test_invert_conditions(): inverted = _invert_condition(filters) assert inverted == { - "operator": "AND", + "operator": "OR", "conditions": [ {"field": "meta.number", "operator": "!=", "value": 100}, {"field": "meta.name", "operator": "!=", "value": "name_0"}, From b0b71e4f618849889f32131c01e0aaa648caf162 Mon Sep 17 00:00:00 2001 From: Stefano Fiorucci Date: Wed, 13 Mar 2024 13:35:01 +0100 Subject: [PATCH 02/10] Refactor tests (#574) * first refactorings * separate unit tests in pgvector * small change to weaviate * fix format * usefixtures when possible --- .../chroma/tests/test_document_store.py | 30 -- .../tests/test_cohere_chat_generator.py | 12 - integrations/deepeval/tests/test_evaluator.py | 1 + .../tests/test_document_store.py | 58 +-- .../mongodb_atlas/tests/test_retriever.py | 52 ++- .../opensearch/tests/test_document_store.py | 53 +-- integrations/pgvector/tests/conftest.py | 36 ++ .../pgvector/tests/test_document_store.py | 369 +++++++++--------- .../tests/test_embedding_retrieval.py | 1 + integrations/pgvector/tests/test_filters.py | 226 ++++++----- integrations/pgvector/tests/test_retriever.py | 25 +- .../weaviate/tests/test_document_store.py | 1 + 12 files changed, 453 insertions(+), 411 deletions(-) diff --git a/integrations/chroma/tests/test_document_store.py b/integrations/chroma/tests/test_document_store.py index 8d61e63ed..5b827a984 100644 --- a/integrations/chroma/tests/test_document_store.py +++ b/integrations/chroma/tests/test_document_store.py @@ -60,7 +60,6 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do assert doc_received.content == doc_expected.content assert doc_received.meta == doc_expected.meta - @pytest.mark.unit def test_ne_filter(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): """ We customize this test because Chroma consider "not equal" true when @@ -72,14 +71,12 @@ def test_ne_filter(self, document_store: ChromaDocumentStore, filterable_docs: L result, [doc for doc in filterable_docs if doc.meta.get("page", "100") != "100"] ) - @pytest.mark.unit def test_delete_empty(self, document_store: ChromaDocumentStore): """ Deleting a non-existing document should not raise with Chroma """ document_store.delete_documents(["test"]) - @pytest.mark.unit def test_delete_not_empty_nonexisting(self, document_store: ChromaDocumentStore): """ Deleting a non-existing document should not raise with Chroma @@ -131,144 +128,117 @@ def test_same_collection_name_reinitialization(self): ChromaDocumentStore("test_name") @pytest.mark.skip(reason="Filter on array contents is not supported.") - @pytest.mark.unit def test_filter_document_array(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on dataframe contents is not supported.") - @pytest.mark.unit def test_filter_document_dataframe(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on table contents is not supported.") - @pytest.mark.unit def test_eq_filter_table(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on embedding value is not supported.") - @pytest.mark.unit def test_eq_filter_embedding(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$in operator is not supported.") - @pytest.mark.unit def test_in_filter_explicit(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$in operator is not supported. Filter on table contents is not supported.") - @pytest.mark.unit def test_in_filter_table(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$in operator is not supported.") - @pytest.mark.unit def test_in_filter_embedding(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on table contents is not supported.") - @pytest.mark.unit def test_ne_filter_table(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on embedding value is not supported.") - @pytest.mark.unit def test_ne_filter_embedding(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$nin operator is not supported. Filter on table contents is not supported.") - @pytest.mark.unit def test_nin_filter_table(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$nin operator is not supported. Filter on embedding value is not supported.") - @pytest.mark.unit def test_nin_filter_embedding(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="$nin operator is not supported.") - @pytest.mark.unit def test_nin_filter(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_simple_implicit_and_with_multi_key_dict( self, document_store: ChromaDocumentStore, filterable_docs: List[Document] ): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_simple_explicit_and_with_multikey_dict( self, document_store: ChromaDocumentStore, filterable_docs: List[Document] ): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_simple_explicit_and_with_list( self, document_store: ChromaDocumentStore, filterable_docs: List[Document] ): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_simple_implicit_and(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_explicit_and(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_implicit_and(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_simple_or(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_or(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter on table contents is not supported.") - @pytest.mark.unit def test_filter_nested_and_or_explicit(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_and_or_implicit(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_or_and(self, document_store: ChromaDocumentStore, filterable_docs: List[Document]): pass @pytest.mark.skip(reason="Filter syntax not supported.") - @pytest.mark.unit def test_filter_nested_multiple_identical_operators_same_level( self, document_store: ChromaDocumentStore, filterable_docs: List[Document] ): pass @pytest.mark.skip(reason="Duplicate policy not supported.") - @pytest.mark.unit def test_write_duplicate_fail(self, document_store: ChromaDocumentStore): pass @pytest.mark.skip(reason="Duplicate policy not supported.") - @pytest.mark.unit def test_write_duplicate_skip(self, document_store: ChromaDocumentStore): pass @pytest.mark.skip(reason="Duplicate policy not supported.") - @pytest.mark.unit def test_write_duplicate_overwrite(self, document_store: ChromaDocumentStore): pass diff --git a/integrations/cohere/tests/test_cohere_chat_generator.py b/integrations/cohere/tests/test_cohere_chat_generator.py index 7fd588fec..9a822856e 100644 --- a/integrations/cohere/tests/test_cohere_chat_generator.py +++ b/integrations/cohere/tests/test_cohere_chat_generator.py @@ -53,7 +53,6 @@ def chat_messages(): class TestCohereChatGenerator: - @pytest.mark.unit def test_init_default(self, monkeypatch): monkeypatch.setenv("COHERE_API_KEY", "test-api-key") @@ -64,14 +63,12 @@ def test_init_default(self, monkeypatch): assert component.api_base_url == cohere.COHERE_API_URL assert not component.generation_kwargs - @pytest.mark.unit def test_init_fail_wo_api_key(self, monkeypatch): monkeypatch.delenv("COHERE_API_KEY", raising=False) monkeypatch.delenv("CO_API_KEY", raising=False) with pytest.raises(ValueError): CohereChatGenerator() - @pytest.mark.unit def test_init_with_parameters(self): component = CohereChatGenerator( api_key=Secret.from_token("test-api-key"), @@ -86,7 +83,6 @@ def test_init_with_parameters(self): assert component.api_base_url == "test-base-url" assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} - @pytest.mark.unit def test_to_dict_default(self, monkeypatch): monkeypatch.setenv("COHERE_API_KEY", "test-api-key") component = CohereChatGenerator() @@ -102,7 +98,6 @@ def test_to_dict_default(self, monkeypatch): }, } - @pytest.mark.unit def test_to_dict_with_parameters(self, monkeypatch): monkeypatch.setenv("COHERE_API_KEY", "test-api-key") monkeypatch.setenv("CO_API_KEY", "fake-api-key") @@ -125,7 +120,6 @@ def test_to_dict_with_parameters(self, monkeypatch): }, } - @pytest.mark.unit def test_to_dict_with_lambda_streaming_callback(self, monkeypatch): monkeypatch.setenv("COHERE_API_KEY", "test-api-key") component = CohereChatGenerator( @@ -146,7 +140,6 @@ def test_to_dict_with_lambda_streaming_callback(self, monkeypatch): }, } - @pytest.mark.unit def test_from_dict(self, monkeypatch): monkeypatch.setenv("COHERE_API_KEY", "fake-api-key") monkeypatch.setenv("CO_API_KEY", "fake-api-key") @@ -166,7 +159,6 @@ def test_from_dict(self, monkeypatch): assert component.api_base_url == "test-base-url" assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} - @pytest.mark.unit def test_from_dict_fail_wo_env_var(self, monkeypatch): monkeypatch.delenv("COHERE_API_KEY", raising=False) monkeypatch.delenv("CO_API_KEY", raising=False) @@ -183,7 +175,6 @@ def test_from_dict_fail_wo_env_var(self, monkeypatch): with pytest.raises(ValueError): CohereChatGenerator.from_dict(data) - @pytest.mark.unit def test_run(self, chat_messages, mock_chat_response): # noqa: ARG002 component = CohereChatGenerator(api_key=Secret.from_token("test-api-key")) response = component.run(chat_messages) @@ -195,13 +186,11 @@ def test_run(self, chat_messages, mock_chat_response): # noqa: ARG002 assert len(response["replies"]) == 1 assert [isinstance(reply, ChatMessage) for reply in response["replies"]] - @pytest.mark.unit def test_message_to_dict(self, chat_messages): obj = CohereChatGenerator(api_key=Secret.from_token("test-api-key")) dictionary = [obj._message_to_dict(message) for message in chat_messages] assert dictionary == [{"user_name": "Chatbot", "text": "What's the capital of France"}] - @pytest.mark.unit def test_run_with_params(self, chat_messages, mock_chat_response): component = CohereChatGenerator( api_key=Secret.from_token("test-api-key"), generation_kwargs={"max_tokens": 10, "temperature": 0.5} @@ -220,7 +209,6 @@ def test_run_with_params(self, chat_messages, mock_chat_response): assert len(response["replies"]) == 1 assert [isinstance(reply, ChatMessage) for reply in response["replies"]] - @pytest.mark.unit def test_run_streaming(self, chat_messages, mock_chat_response): streaming_call_count = 0 diff --git a/integrations/deepeval/tests/test_evaluator.py b/integrations/deepeval/tests/test_evaluator.py index 8534ef687..7d1946185 100644 --- a/integrations/deepeval/tests/test_evaluator.py +++ b/integrations/deepeval/tests/test_evaluator.py @@ -270,6 +270,7 @@ def test_evaluator_outputs(metric, inputs, expected_outputs, metric_params, monk # OpenAI API. It is parameterized by the metric, the inputs to the evalutor # and the metric parameters. @pytest.mark.skipif("OPENAI_API_KEY" not in os.environ, reason="OPENAI_API_KEY not set") +@pytest.mark.integration @pytest.mark.parametrize( "metric, inputs, metric_params", [ diff --git a/integrations/elasticsearch/tests/test_document_store.py b/integrations/elasticsearch/tests/test_document_store.py index e46e76ed2..308486a78 100644 --- a/integrations/elasticsearch/tests/test_document_store.py +++ b/integrations/elasticsearch/tests/test_document_store.py @@ -15,6 +15,36 @@ from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") +def test_to_dict(_mock_elasticsearch_client): + document_store = ElasticsearchDocumentStore(hosts="some hosts") + res = document_store.to_dict() + assert res == { + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", + "init_parameters": { + "hosts": "some hosts", + "index": "default", + "embedding_similarity_function": "cosine", + }, + } + + +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") +def test_from_dict(_mock_elasticsearch_client): + data = { + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", + "init_parameters": { + "hosts": "some hosts", + "index": "default", + "embedding_similarity_function": "cosine", + }, + } + document_store = ElasticsearchDocumentStore.from_dict(data) + assert document_store._hosts == "some hosts" + assert document_store._index == "default" + assert document_store._embedding_similarity_function == "cosine" + + @pytest.mark.integration class TestDocumentStore(DocumentStoreBaseTests): """ @@ -67,34 +97,6 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do super().assert_documents_are_equal(received, expected) - @patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") - def test_to_dict(self, _mock_elasticsearch_client): - document_store = ElasticsearchDocumentStore(hosts="some hosts") - res = document_store.to_dict() - assert res == { - "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", - "init_parameters": { - "hosts": "some hosts", - "index": "default", - "embedding_similarity_function": "cosine", - }, - } - - @patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") - def test_from_dict(self, _mock_elasticsearch_client): - data = { - "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", - "init_parameters": { - "hosts": "some hosts", - "index": "default", - "embedding_similarity_function": "cosine", - }, - } - document_store = ElasticsearchDocumentStore.from_dict(data) - assert document_store._hosts == "some hosts" - assert document_store._index == "default" - assert document_store._embedding_similarity_function == "cosine" - def test_user_agent_header(self, document_store: ElasticsearchDocumentStore): assert document_store._client._headers["user-agent"].startswith("haystack-py-ds/") diff --git a/integrations/mongodb_atlas/tests/test_retriever.py b/integrations/mongodb_atlas/tests/test_retriever.py index ec44513e2..4ef5222ce 100644 --- a/integrations/mongodb_atlas/tests/test_retriever.py +++ b/integrations/mongodb_atlas/tests/test_retriever.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2023-present deepset GmbH # # SPDX-License-Identifier: Apache-2.0 -from unittest.mock import Mock +from unittest.mock import MagicMock, Mock, patch import pytest from haystack.dataclasses import Document @@ -10,34 +10,48 @@ from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore -@pytest.fixture -def document_store(): - store = MongoDBAtlasDocumentStore( - database_name="haystack_integration_test", - collection_name="test_embeddings_collection", - vector_search_index="cosine_index", - ) - return store +class TestRetriever: + @pytest.fixture + def mock_client(self): + with patch( + "haystack_integrations.document_stores.mongodb_atlas.document_store.MongoClient" + ) as mock_mongo_client: + mock_connection = MagicMock() + mock_database = MagicMock() + mock_collection_names = MagicMock(return_value=["test_embeddings_collection"]) + mock_database.list_collection_names = mock_collection_names + mock_connection.__getitem__.return_value = mock_database + mock_mongo_client.return_value = mock_connection + yield mock_mongo_client -class TestRetriever: - def test_init_default(self, document_store: MongoDBAtlasDocumentStore): - retriever = MongoDBAtlasEmbeddingRetriever(document_store=document_store) - assert retriever.document_store == document_store + def test_init_default(self): + mock_store = Mock(spec=MongoDBAtlasDocumentStore) + retriever = MongoDBAtlasEmbeddingRetriever(document_store=mock_store) + assert retriever.document_store == mock_store assert retriever.filters == {} assert retriever.top_k == 10 - def test_init(self, document_store: MongoDBAtlasDocumentStore): + def test_init(self): + mock_store = Mock(spec=MongoDBAtlasDocumentStore) retriever = MongoDBAtlasEmbeddingRetriever( - document_store=document_store, + document_store=mock_store, filters={"field": "value"}, top_k=5, ) - assert retriever.document_store == document_store + assert retriever.document_store == mock_store assert retriever.filters == {"field": "value"} assert retriever.top_k == 5 - def test_to_dict(self, document_store: MongoDBAtlasDocumentStore): + def test_to_dict(self, mock_client, monkeypatch): # noqa: ARG002 mock_client appears unused but is required + monkeypatch.setenv("MONGO_CONNECTION_STRING", "test_conn_str") + + document_store = MongoDBAtlasDocumentStore( + database_name="haystack_integration_test", + collection_name="test_embeddings_collection", + vector_search_index="cosine_index", + ) + retriever = MongoDBAtlasEmbeddingRetriever(document_store=document_store, filters={"field": "value"}, top_k=5) res = retriever.to_dict() assert res == { @@ -61,7 +75,9 @@ def test_to_dict(self, document_store: MongoDBAtlasDocumentStore): }, } - def test_from_dict(self): + def test_from_dict(self, mock_client, monkeypatch): # noqa: ARG002 mock_client appears unused but is required + monkeypatch.setenv("MONGO_CONNECTION_STRING", "test_conn_str") + data = { "type": "haystack_integrations.components.retrievers.mongodb_atlas.embedding_retriever.MongoDBAtlasEmbeddingRetriever", # noqa: E501 "init_parameters": { diff --git a/integrations/opensearch/tests/test_document_store.py b/integrations/opensearch/tests/test_document_store.py index e3a314141..bc0d1c434 100644 --- a/integrations/opensearch/tests/test_document_store.py +++ b/integrations/opensearch/tests/test_document_store.py @@ -14,6 +14,34 @@ from opensearchpy.exceptions import RequestError +@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch") +def test_to_dict(_mock_opensearch_client): + document_store = OpenSearchDocumentStore(hosts="some hosts") + res = document_store.to_dict() + assert res == { + "type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore", + "init_parameters": { + "hosts": "some hosts", + "index": "default", + }, + } + + +@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch") +def test_from_dict(_mock_opensearch_client): + data = { + "type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore", + "init_parameters": { + "hosts": "some hosts", + "index": "default", + }, + } + document_store = OpenSearchDocumentStore.from_dict(data) + assert document_store._hosts == "some hosts" + assert document_store._index == "default" + + +@pytest.mark.integration class TestDocumentStore(DocumentStoreBaseTests): """ Common test cases will be provided by `DocumentStoreBaseTests` but @@ -87,31 +115,6 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do super().assert_documents_are_equal(received, expected) - @patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch") - def test_to_dict(self, _mock_opensearch_client): - document_store = OpenSearchDocumentStore(hosts="some hosts") - res = document_store.to_dict() - assert res == { - "type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore", - "init_parameters": { - "hosts": "some hosts", - "index": "default", - }, - } - - @patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch") - def test_from_dict(self, _mock_opensearch_client): - data = { - "type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore", - "init_parameters": { - "hosts": "some hosts", - "index": "default", - }, - } - document_store = OpenSearchDocumentStore.from_dict(data) - assert document_store._hosts == "some hosts" - assert document_store._index == "default" - def test_write_documents(self, document_store: OpenSearchDocumentStore): docs = [Document(id="1")] assert document_store.write_documents(docs) == 1 diff --git a/integrations/pgvector/tests/conftest.py b/integrations/pgvector/tests/conftest.py index 068f2ac54..94b35a04d 100644 --- a/integrations/pgvector/tests/conftest.py +++ b/integrations/pgvector/tests/conftest.py @@ -1,4 +1,5 @@ import os +from unittest.mock import patch import pytest from haystack_integrations.document_stores.pgvector import PgvectorDocumentStore @@ -24,3 +25,38 @@ def document_store(request): yield store store.delete_table() + + +@pytest.fixture +def patches_for_unit_tests(): + with patch("haystack_integrations.document_stores.pgvector.document_store.connect") as mock_connect, patch( + "haystack_integrations.document_stores.pgvector.document_store.register_vector" + ) as mock_register, patch( + "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore.delete_table" + ) as mock_delete, patch( + "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore._create_table_if_not_exists" + ) as mock_create, patch( + "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore._handle_hnsw" + ) as mock_hnsw: + + yield mock_connect, mock_register, mock_delete, mock_create, mock_hnsw + + +@pytest.fixture +def mock_store(patches_for_unit_tests, monkeypatch): # noqa: ARG001 patches are not explicitly called but necessary + monkeypatch.setenv("PG_CONN_STR", "some-connection-string") + table_name = "haystack" + embedding_dimension = 768 + vector_function = "cosine_similarity" + recreate_table = True + search_strategy = "exact_nearest_neighbor" + + store = PgvectorDocumentStore( + table_name=table_name, + embedding_dimension=embedding_dimension, + vector_function=vector_function, + recreate_table=recreate_table, + search_strategy=search_strategy, + ) + + yield store diff --git a/integrations/pgvector/tests/test_document_store.py b/integrations/pgvector/tests/test_document_store.py index 1e158f134..bf5ccd5d4 100644 --- a/integrations/pgvector/tests/test_document_store.py +++ b/integrations/pgvector/tests/test_document_store.py @@ -13,6 +13,7 @@ from pandas import DataFrame +@pytest.mark.integration class TestDocumentStore(CountDocumentsTest, WriteDocumentsTest, DeleteDocumentsTest): def test_write_documents(self, document_store: PgvectorDocumentStore): docs = [Document(id="1")] @@ -25,7 +26,6 @@ def test_write_blob(self, document_store: PgvectorDocumentStore): docs = [Document(id="1", blob=bytestream)] document_store.write_documents(docs) - # TODO: update when filters are implemented retrieved_docs = document_store.filter_documents() assert retrieved_docs == docs @@ -35,185 +35,194 @@ def test_write_dataframe(self, document_store: PgvectorDocumentStore): document_store.write_documents(docs) - # TODO: update when filters are implemented retrieved_docs = document_store.filter_documents() assert retrieved_docs == docs - def test_init(self): - document_store = PgvectorDocumentStore( - table_name="my_table", - embedding_dimension=512, - vector_function="l2_distance", - recreate_table=True, - search_strategy="hnsw", - hnsw_recreate_index_if_exists=True, - hnsw_index_creation_kwargs={"m": 32, "ef_construction": 128}, - hnsw_ef_search=50, - ) - - assert document_store.table_name == "my_table" - assert document_store.embedding_dimension == 512 - assert document_store.vector_function == "l2_distance" - assert document_store.recreate_table - assert document_store.search_strategy == "hnsw" - assert document_store.hnsw_recreate_index_if_exists - assert document_store.hnsw_index_creation_kwargs == {"m": 32, "ef_construction": 128} - assert document_store.hnsw_ef_search == 50 - - def test_to_dict(self): - document_store = PgvectorDocumentStore( - table_name="my_table", - embedding_dimension=512, - vector_function="l2_distance", - recreate_table=True, - search_strategy="hnsw", - hnsw_recreate_index_if_exists=True, - hnsw_index_creation_kwargs={"m": 32, "ef_construction": 128}, - hnsw_ef_search=50, - ) - - assert document_store.to_dict() == { - "type": "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore", - "init_parameters": { - "connection_string": {"env_vars": ["PG_CONN_STR"], "strict": True, "type": "env_var"}, - "table_name": "my_table", - "embedding_dimension": 512, - "vector_function": "l2_distance", - "recreate_table": True, - "search_strategy": "hnsw", - "hnsw_recreate_index_if_exists": True, - "hnsw_index_creation_kwargs": {"m": 32, "ef_construction": 128}, - "hnsw_ef_search": 50, - }, - } - - def test_from_haystack_to_pg_documents(self): - haystack_docs = [ - Document( - id="1", - content="This is a text", - meta={"meta_key": "meta_value"}, - embedding=[0.1, 0.2, 0.3], - score=0.5, - ), - Document( - id="2", - dataframe=DataFrame({"col1": [1, 2], "col2": [3, 4]}), - meta={"meta_key": "meta_value"}, - embedding=[0.4, 0.5, 0.6], - score=0.6, - ), - Document( - id="3", - blob=ByteStream(b"test", meta={"blob_meta_key": "blob_meta_value"}, mime_type="mime_type"), - meta={"meta_key": "meta_value"}, - embedding=[0.7, 0.8, 0.9], - score=0.7, - ), - ] - - with patch( - "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore.__init__" - ) as mock_init: - mock_init.return_value = None - ds = PgvectorDocumentStore(connection_string="test") - - pg_docs = ds._from_haystack_to_pg_documents(haystack_docs) - - assert pg_docs[0]["id"] == "1" - assert pg_docs[0]["content"] == "This is a text" - assert pg_docs[0]["dataframe"] is None - assert pg_docs[0]["blob_data"] is None - assert pg_docs[0]["blob_meta"] is None - assert pg_docs[0]["blob_mime_type"] is None - assert pg_docs[0]["meta"].obj == {"meta_key": "meta_value"} - assert pg_docs[0]["embedding"] == [0.1, 0.2, 0.3] - assert "score" not in pg_docs[0] - - assert pg_docs[1]["id"] == "2" - assert pg_docs[1]["content"] is None - assert pg_docs[1]["dataframe"].obj == DataFrame({"col1": [1, 2], "col2": [3, 4]}).to_json() - assert pg_docs[1]["blob_data"] is None - assert pg_docs[1]["blob_meta"] is None - assert pg_docs[1]["blob_mime_type"] is None - assert pg_docs[1]["meta"].obj == {"meta_key": "meta_value"} - assert pg_docs[1]["embedding"] == [0.4, 0.5, 0.6] - assert "score" not in pg_docs[1] - - assert pg_docs[2]["id"] == "3" - assert pg_docs[2]["content"] is None - assert pg_docs[2]["dataframe"] is None - assert pg_docs[2]["blob_data"] == b"test" - assert pg_docs[2]["blob_meta"].obj == {"blob_meta_key": "blob_meta_value"} - assert pg_docs[2]["blob_mime_type"] == "mime_type" - assert pg_docs[2]["meta"].obj == {"meta_key": "meta_value"} - assert pg_docs[2]["embedding"] == [0.7, 0.8, 0.9] - assert "score" not in pg_docs[2] - - def test_from_pg_to_haystack_documents(self): - pg_docs = [ - { - "id": "1", - "content": "This is a text", - "dataframe": None, - "blob_data": None, - "blob_meta": None, - "blob_mime_type": None, - "meta": {"meta_key": "meta_value"}, - "embedding": "[0.1, 0.2, 0.3]", - }, - { - "id": "2", - "content": None, - "dataframe": DataFrame({"col1": [1, 2], "col2": [3, 4]}).to_json(), - "blob_data": None, - "blob_meta": None, - "blob_mime_type": None, - "meta": {"meta_key": "meta_value"}, - "embedding": "[0.4, 0.5, 0.6]", - }, - { - "id": "3", - "content": None, - "dataframe": None, - "blob_data": b"test", - "blob_meta": {"blob_meta_key": "blob_meta_value"}, - "blob_mime_type": "mime_type", - "meta": {"meta_key": "meta_value"}, - "embedding": "[0.7, 0.8, 0.9]", - }, - ] - - with patch( - "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore.__init__" - ) as mock_init: - mock_init.return_value = None - ds = PgvectorDocumentStore(connection_string="test") - - haystack_docs = ds._from_pg_to_haystack_documents(pg_docs) - - assert haystack_docs[0].id == "1" - assert haystack_docs[0].content == "This is a text" - assert haystack_docs[0].dataframe is None - assert haystack_docs[0].blob is None - assert haystack_docs[0].meta == {"meta_key": "meta_value"} - assert haystack_docs[0].embedding == [0.1, 0.2, 0.3] - assert haystack_docs[0].score is None - - assert haystack_docs[1].id == "2" - assert haystack_docs[1].content is None - assert haystack_docs[1].dataframe.equals(DataFrame({"col1": [1, 2], "col2": [3, 4]})) - assert haystack_docs[1].blob is None - assert haystack_docs[1].meta == {"meta_key": "meta_value"} - assert haystack_docs[1].embedding == [0.4, 0.5, 0.6] - assert haystack_docs[1].score is None - - assert haystack_docs[2].id == "3" - assert haystack_docs[2].content is None - assert haystack_docs[2].dataframe is None - assert haystack_docs[2].blob.data == b"test" - assert haystack_docs[2].blob.meta == {"blob_meta_key": "blob_meta_value"} - assert haystack_docs[2].blob.mime_type == "mime_type" - assert haystack_docs[2].meta == {"meta_key": "meta_value"} - assert haystack_docs[2].embedding == [0.7, 0.8, 0.9] - assert haystack_docs[2].score is None + +@pytest.mark.usefixtures("patches_for_unit_tests") +def test_init(monkeypatch): + monkeypatch.setenv("PG_CONN_STR", "some_connection_string") + + document_store = PgvectorDocumentStore( + table_name="my_table", + embedding_dimension=512, + vector_function="l2_distance", + recreate_table=True, + search_strategy="hnsw", + hnsw_recreate_index_if_exists=True, + hnsw_index_creation_kwargs={"m": 32, "ef_construction": 128}, + hnsw_ef_search=50, + ) + + assert document_store.table_name == "my_table" + assert document_store.embedding_dimension == 512 + assert document_store.vector_function == "l2_distance" + assert document_store.recreate_table + assert document_store.search_strategy == "hnsw" + assert document_store.hnsw_recreate_index_if_exists + assert document_store.hnsw_index_creation_kwargs == {"m": 32, "ef_construction": 128} + assert document_store.hnsw_ef_search == 50 + + +@pytest.mark.usefixtures("patches_for_unit_tests") +def test_to_dict(monkeypatch): + monkeypatch.setenv("PG_CONN_STR", "some_connection_string") + + document_store = PgvectorDocumentStore( + table_name="my_table", + embedding_dimension=512, + vector_function="l2_distance", + recreate_table=True, + search_strategy="hnsw", + hnsw_recreate_index_if_exists=True, + hnsw_index_creation_kwargs={"m": 32, "ef_construction": 128}, + hnsw_ef_search=50, + ) + + assert document_store.to_dict() == { + "type": "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore", + "init_parameters": { + "connection_string": {"env_vars": ["PG_CONN_STR"], "strict": True, "type": "env_var"}, + "table_name": "my_table", + "embedding_dimension": 512, + "vector_function": "l2_distance", + "recreate_table": True, + "search_strategy": "hnsw", + "hnsw_recreate_index_if_exists": True, + "hnsw_index_creation_kwargs": {"m": 32, "ef_construction": 128}, + "hnsw_ef_search": 50, + }, + } + + +def test_from_haystack_to_pg_documents(): + haystack_docs = [ + Document( + id="1", + content="This is a text", + meta={"meta_key": "meta_value"}, + embedding=[0.1, 0.2, 0.3], + score=0.5, + ), + Document( + id="2", + dataframe=DataFrame({"col1": [1, 2], "col2": [3, 4]}), + meta={"meta_key": "meta_value"}, + embedding=[0.4, 0.5, 0.6], + score=0.6, + ), + Document( + id="3", + blob=ByteStream(b"test", meta={"blob_meta_key": "blob_meta_value"}, mime_type="mime_type"), + meta={"meta_key": "meta_value"}, + embedding=[0.7, 0.8, 0.9], + score=0.7, + ), + ] + + with patch( + "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore.__init__" + ) as mock_init: + mock_init.return_value = None + ds = PgvectorDocumentStore(connection_string="test") + + pg_docs = ds._from_haystack_to_pg_documents(haystack_docs) + + assert pg_docs[0]["id"] == "1" + assert pg_docs[0]["content"] == "This is a text" + assert pg_docs[0]["dataframe"] is None + assert pg_docs[0]["blob_data"] is None + assert pg_docs[0]["blob_meta"] is None + assert pg_docs[0]["blob_mime_type"] is None + assert pg_docs[0]["meta"].obj == {"meta_key": "meta_value"} + assert pg_docs[0]["embedding"] == [0.1, 0.2, 0.3] + assert "score" not in pg_docs[0] + + assert pg_docs[1]["id"] == "2" + assert pg_docs[1]["content"] is None + assert pg_docs[1]["dataframe"].obj == DataFrame({"col1": [1, 2], "col2": [3, 4]}).to_json() + assert pg_docs[1]["blob_data"] is None + assert pg_docs[1]["blob_meta"] is None + assert pg_docs[1]["blob_mime_type"] is None + assert pg_docs[1]["meta"].obj == {"meta_key": "meta_value"} + assert pg_docs[1]["embedding"] == [0.4, 0.5, 0.6] + assert "score" not in pg_docs[1] + + assert pg_docs[2]["id"] == "3" + assert pg_docs[2]["content"] is None + assert pg_docs[2]["dataframe"] is None + assert pg_docs[2]["blob_data"] == b"test" + assert pg_docs[2]["blob_meta"].obj == {"blob_meta_key": "blob_meta_value"} + assert pg_docs[2]["blob_mime_type"] == "mime_type" + assert pg_docs[2]["meta"].obj == {"meta_key": "meta_value"} + assert pg_docs[2]["embedding"] == [0.7, 0.8, 0.9] + assert "score" not in pg_docs[2] + + +def test_from_pg_to_haystack_documents(): + pg_docs = [ + { + "id": "1", + "content": "This is a text", + "dataframe": None, + "blob_data": None, + "blob_meta": None, + "blob_mime_type": None, + "meta": {"meta_key": "meta_value"}, + "embedding": "[0.1, 0.2, 0.3]", + }, + { + "id": "2", + "content": None, + "dataframe": DataFrame({"col1": [1, 2], "col2": [3, 4]}).to_json(), + "blob_data": None, + "blob_meta": None, + "blob_mime_type": None, + "meta": {"meta_key": "meta_value"}, + "embedding": "[0.4, 0.5, 0.6]", + }, + { + "id": "3", + "content": None, + "dataframe": None, + "blob_data": b"test", + "blob_meta": {"blob_meta_key": "blob_meta_value"}, + "blob_mime_type": "mime_type", + "meta": {"meta_key": "meta_value"}, + "embedding": "[0.7, 0.8, 0.9]", + }, + ] + + with patch( + "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore.__init__" + ) as mock_init: + mock_init.return_value = None + ds = PgvectorDocumentStore(connection_string="test") + + haystack_docs = ds._from_pg_to_haystack_documents(pg_docs) + + assert haystack_docs[0].id == "1" + assert haystack_docs[0].content == "This is a text" + assert haystack_docs[0].dataframe is None + assert haystack_docs[0].blob is None + assert haystack_docs[0].meta == {"meta_key": "meta_value"} + assert haystack_docs[0].embedding == [0.1, 0.2, 0.3] + assert haystack_docs[0].score is None + + assert haystack_docs[1].id == "2" + assert haystack_docs[1].content is None + assert haystack_docs[1].dataframe.equals(DataFrame({"col1": [1, 2], "col2": [3, 4]})) + assert haystack_docs[1].blob is None + assert haystack_docs[1].meta == {"meta_key": "meta_value"} + assert haystack_docs[1].embedding == [0.4, 0.5, 0.6] + assert haystack_docs[1].score is None + + assert haystack_docs[2].id == "3" + assert haystack_docs[2].content is None + assert haystack_docs[2].dataframe is None + assert haystack_docs[2].blob.data == b"test" + assert haystack_docs[2].blob.meta == {"blob_meta_key": "blob_meta_value"} + assert haystack_docs[2].blob.mime_type == "mime_type" + assert haystack_docs[2].meta == {"meta_key": "meta_value"} + assert haystack_docs[2].embedding == [0.7, 0.8, 0.9] + assert haystack_docs[2].score is None diff --git a/integrations/pgvector/tests/test_embedding_retrieval.py b/integrations/pgvector/tests/test_embedding_retrieval.py index 1d5e8e297..2c384f57c 100644 --- a/integrations/pgvector/tests/test_embedding_retrieval.py +++ b/integrations/pgvector/tests/test_embedding_retrieval.py @@ -10,6 +10,7 @@ from numpy.random import rand +@pytest.mark.integration class TestEmbeddingRetrieval: @pytest.fixture def document_store_w_hnsw_index(self, request): diff --git a/integrations/pgvector/tests/test_filters.py b/integrations/pgvector/tests/test_filters.py index 8b2dc8ec9..bda10e3c0 100644 --- a/integrations/pgvector/tests/test_filters.py +++ b/integrations/pgvector/tests/test_filters.py @@ -15,6 +15,7 @@ from psycopg.types.json import Jsonb +@pytest.mark.integration class TestFilters(FilterDocumentsTest): def assert_documents_are_equal(self, received: List[Document], expected: List[Document]): """ @@ -35,6 +36,9 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do received_doc.embedding, expected_doc.embedding = None, None assert received_doc == expected_doc + @pytest.mark.skip(reason="NOT operator is not supported in PgvectorDocumentStore") + def test_not_operator(self, document_store, filterable_docs): ... + def test_complex_filter(self, document_store, filterable_docs): document_store.write_documents(filterable_docs) filters = { @@ -69,111 +73,119 @@ def test_complex_filter(self, document_store, filterable_docs): ], ) - @pytest.mark.skip(reason="NOT operator is not supported in PgvectorDocumentStore") - def test_not_operator(self, document_store, filterable_docs): ... - def test_treat_meta_field(self): - assert _treat_meta_field(field="meta.number", value=9) == "(meta->>'number')::integer" - assert _treat_meta_field(field="meta.number", value=[1, 2, 3]) == "(meta->>'number')::integer" - assert _treat_meta_field(field="meta.name", value="my_name") == "meta->>'name'" - assert _treat_meta_field(field="meta.name", value=["my_name"]) == "meta->>'name'" - assert _treat_meta_field(field="meta.number", value=1.1) == "(meta->>'number')::real" - assert _treat_meta_field(field="meta.number", value=[1.1, 2.2, 3.3]) == "(meta->>'number')::real" - assert _treat_meta_field(field="meta.bool", value=True) == "(meta->>'bool')::boolean" - assert _treat_meta_field(field="meta.bool", value=[True, False, True]) == "(meta->>'bool')::boolean" - - # do not cast the field if its value is not one of the known types, an empty list or None - assert _treat_meta_field(field="meta.other", value={"a": 3, "b": "example"}) == "meta->>'other'" - assert _treat_meta_field(field="meta.empty_list", value=[]) == "meta->>'empty_list'" - assert _treat_meta_field(field="meta.name", value=None) == "meta->>'name'" - - def test_comparison_condition_dataframe_jsonb_conversion(self): - dataframe = DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}) - condition = {"field": "meta.df", "operator": "==", "value": dataframe} - field, values = _parse_comparison_condition(condition) - assert field == "(meta.df)::jsonb = %s" - - # we check each slot of the Jsonb object because it does not implement __eq__ - assert values[0].obj == Jsonb(dataframe.to_json()).obj - assert values[0].dumps == Jsonb(dataframe.to_json()).dumps - - def test_comparison_condition_missing_operator(self): - condition = {"field": "meta.type", "value": "article"} - with pytest.raises(FilterError): - _parse_comparison_condition(condition) - - def test_comparison_condition_missing_value(self): - condition = {"field": "meta.type", "operator": "=="} - with pytest.raises(FilterError): - _parse_comparison_condition(condition) - - def test_comparison_condition_unknown_operator(self): - condition = {"field": "meta.type", "operator": "unknown", "value": "article"} - with pytest.raises(FilterError): - _parse_comparison_condition(condition) - - def test_logical_condition_missing_operator(self): - condition = {"conditions": []} - with pytest.raises(FilterError): - _parse_logical_condition(condition) - - def test_logical_condition_missing_conditions(self): - condition = {"operator": "AND"} - with pytest.raises(FilterError): - _parse_logical_condition(condition) - - def test_logical_condition_unknown_operator(self): - condition = {"operator": "unknown", "conditions": []} - with pytest.raises(FilterError): - _parse_logical_condition(condition) - - def test_logical_condition_nested(self): - condition = { - "operator": "AND", - "conditions": [ - { - "operator": "OR", - "conditions": [ - {"field": "meta.domain", "operator": "!=", "value": "science"}, - {"field": "meta.chapter", "operator": "in", "value": ["intro", "conclusion"]}, - ], - }, - { - "operator": "OR", - "conditions": [ - {"field": "meta.number", "operator": ">=", "value": 90}, - {"field": "meta.author", "operator": "not in", "value": ["John", "Jane"]}, - ], - }, - ], - } - query, values = _parse_logical_condition(condition) - assert query == ( - "((meta->>'domain' IS DISTINCT FROM %s OR meta->>'chapter' = ANY(%s)) " - "AND ((meta->>'number')::integer >= %s OR meta->>'author' IS NULL OR meta->>'author' != ALL(%s)))" - ) - assert values == ["science", [["intro", "conclusion"]], 90, [["John", "Jane"]]] - - def test_convert_filters_to_where_clause_and_params(self): - filters = { - "operator": "AND", - "conditions": [ - {"field": "meta.number", "operator": "==", "value": 100}, - {"field": "meta.chapter", "operator": "==", "value": "intro"}, - ], - } - where_clause, params = _convert_filters_to_where_clause_and_params(filters) - assert where_clause == SQL(" WHERE ") + SQL("((meta->>'number')::integer = %s AND meta->>'chapter' = %s)") - assert params == (100, "intro") - - def test_convert_filters_to_where_clause_and_params_handle_null(self): - filters = { - "operator": "AND", - "conditions": [ - {"field": "meta.number", "operator": "==", "value": None}, - {"field": "meta.chapter", "operator": "==", "value": "intro"}, - ], - } - where_clause, params = _convert_filters_to_where_clause_and_params(filters) - assert where_clause == SQL(" WHERE ") + SQL("(meta->>'number' IS NULL AND meta->>'chapter' = %s)") - assert params == ("intro",) +def test_treat_meta_field(): + assert _treat_meta_field(field="meta.number", value=9) == "(meta->>'number')::integer" + assert _treat_meta_field(field="meta.number", value=[1, 2, 3]) == "(meta->>'number')::integer" + assert _treat_meta_field(field="meta.name", value="my_name") == "meta->>'name'" + assert _treat_meta_field(field="meta.name", value=["my_name"]) == "meta->>'name'" + assert _treat_meta_field(field="meta.number", value=1.1) == "(meta->>'number')::real" + assert _treat_meta_field(field="meta.number", value=[1.1, 2.2, 3.3]) == "(meta->>'number')::real" + assert _treat_meta_field(field="meta.bool", value=True) == "(meta->>'bool')::boolean" + assert _treat_meta_field(field="meta.bool", value=[True, False, True]) == "(meta->>'bool')::boolean" + + # do not cast the field if its value is not one of the known types, an empty list or None + assert _treat_meta_field(field="meta.other", value={"a": 3, "b": "example"}) == "meta->>'other'" + assert _treat_meta_field(field="meta.empty_list", value=[]) == "meta->>'empty_list'" + assert _treat_meta_field(field="meta.name", value=None) == "meta->>'name'" + + +def test_comparison_condition_dataframe_jsonb_conversion(): + dataframe = DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}) + condition = {"field": "meta.df", "operator": "==", "value": dataframe} + field, values = _parse_comparison_condition(condition) + assert field == "(meta.df)::jsonb = %s" + + # we check each slot of the Jsonb object because it does not implement __eq__ + assert values[0].obj == Jsonb(dataframe.to_json()).obj + assert values[0].dumps == Jsonb(dataframe.to_json()).dumps + + +def test_comparison_condition_missing_operator(): + condition = {"field": "meta.type", "value": "article"} + with pytest.raises(FilterError): + _parse_comparison_condition(condition) + + +def test_comparison_condition_missing_value(): + condition = {"field": "meta.type", "operator": "=="} + with pytest.raises(FilterError): + _parse_comparison_condition(condition) + + +def test_comparison_condition_unknown_operator(): + condition = {"field": "meta.type", "operator": "unknown", "value": "article"} + with pytest.raises(FilterError): + _parse_comparison_condition(condition) + + +def test_logical_condition_missing_operator(): + condition = {"conditions": []} + with pytest.raises(FilterError): + _parse_logical_condition(condition) + + +def test_logical_condition_missing_conditions(): + condition = {"operator": "AND"} + with pytest.raises(FilterError): + _parse_logical_condition(condition) + + +def test_logical_condition_unknown_operator(): + condition = {"operator": "unknown", "conditions": []} + with pytest.raises(FilterError): + _parse_logical_condition(condition) + + +def test_logical_condition_nested(): + condition = { + "operator": "AND", + "conditions": [ + { + "operator": "OR", + "conditions": [ + {"field": "meta.domain", "operator": "!=", "value": "science"}, + {"field": "meta.chapter", "operator": "in", "value": ["intro", "conclusion"]}, + ], + }, + { + "operator": "OR", + "conditions": [ + {"field": "meta.number", "operator": ">=", "value": 90}, + {"field": "meta.author", "operator": "not in", "value": ["John", "Jane"]}, + ], + }, + ], + } + query, values = _parse_logical_condition(condition) + assert query == ( + "((meta->>'domain' IS DISTINCT FROM %s OR meta->>'chapter' = ANY(%s)) " + "AND ((meta->>'number')::integer >= %s OR meta->>'author' IS NULL OR meta->>'author' != ALL(%s)))" + ) + assert values == ["science", [["intro", "conclusion"]], 90, [["John", "Jane"]]] + + +def test_convert_filters_to_where_clause_and_params(): + filters = { + "operator": "AND", + "conditions": [ + {"field": "meta.number", "operator": "==", "value": 100}, + {"field": "meta.chapter", "operator": "==", "value": "intro"}, + ], + } + where_clause, params = _convert_filters_to_where_clause_and_params(filters) + assert where_clause == SQL(" WHERE ") + SQL("((meta->>'number')::integer = %s AND meta->>'chapter' = %s)") + assert params == (100, "intro") + + +def test_convert_filters_to_where_clause_and_params_handle_null(): + filters = { + "operator": "AND", + "conditions": [ + {"field": "meta.number", "operator": "==", "value": None}, + {"field": "meta.chapter", "operator": "==", "value": "intro"}, + ], + } + where_clause, params = _convert_filters_to_where_clause_and_params(filters) + assert where_clause == SQL(" WHERE ") + SQL("(meta->>'number' IS NULL AND meta->>'chapter' = %s)") + assert params == ("intro",) diff --git a/integrations/pgvector/tests/test_retriever.py b/integrations/pgvector/tests/test_retriever.py index 8eab10de5..61381c24e 100644 --- a/integrations/pgvector/tests/test_retriever.py +++ b/integrations/pgvector/tests/test_retriever.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from unittest.mock import Mock +import pytest from haystack.dataclasses import Document from haystack.utils.auth import EnvVarSecret from haystack_integrations.components.retrievers.pgvector import PgvectorEmbeddingRetriever @@ -10,25 +11,25 @@ class TestRetriever: - def test_init_default(self, document_store: PgvectorDocumentStore): - retriever = PgvectorEmbeddingRetriever(document_store=document_store) - assert retriever.document_store == document_store + def test_init_default(self, mock_store): + retriever = PgvectorEmbeddingRetriever(document_store=mock_store) + assert retriever.document_store == mock_store assert retriever.filters == {} assert retriever.top_k == 10 - assert retriever.vector_function == document_store.vector_function + assert retriever.vector_function == mock_store.vector_function - def test_init(self, document_store: PgvectorDocumentStore): + def test_init(self, mock_store): retriever = PgvectorEmbeddingRetriever( - document_store=document_store, filters={"field": "value"}, top_k=5, vector_function="l2_distance" + document_store=mock_store, filters={"field": "value"}, top_k=5, vector_function="l2_distance" ) - assert retriever.document_store == document_store + assert retriever.document_store == mock_store assert retriever.filters == {"field": "value"} assert retriever.top_k == 5 assert retriever.vector_function == "l2_distance" - def test_to_dict(self, document_store: PgvectorDocumentStore): + def test_to_dict(self, mock_store): retriever = PgvectorEmbeddingRetriever( - document_store=document_store, filters={"field": "value"}, top_k=5, vector_function="l2_distance" + document_store=mock_store, filters={"field": "value"}, top_k=5, vector_function="l2_distance" ) res = retriever.to_dict() t = "haystack_integrations.components.retrievers.pgvector.embedding_retriever.PgvectorEmbeddingRetriever" @@ -39,7 +40,7 @@ def test_to_dict(self, document_store: PgvectorDocumentStore): "type": "haystack_integrations.document_stores.pgvector.document_store.PgvectorDocumentStore", "init_parameters": { "connection_string": {"env_vars": ["PG_CONN_STR"], "strict": True, "type": "env_var"}, - "table_name": "haystack_test_to_dict", + "table_name": "haystack", "embedding_dimension": 768, "vector_function": "cosine_similarity", "recreate_table": True, @@ -55,7 +56,9 @@ def test_to_dict(self, document_store: PgvectorDocumentStore): }, } - def test_from_dict(self): + @pytest.mark.usefixtures("patches_for_unit_tests") + def test_from_dict(self, monkeypatch): + monkeypatch.setenv("PG_CONN_STR", "some-connection-string") t = "haystack_integrations.components.retrievers.pgvector.embedding_retriever.PgvectorEmbeddingRetriever" data = { "type": t, diff --git a/integrations/weaviate/tests/test_document_store.py b/integrations/weaviate/tests/test_document_store.py index 4c1659a86..a2ae9cb70 100644 --- a/integrations/weaviate/tests/test_document_store.py +++ b/integrations/weaviate/tests/test_document_store.py @@ -38,6 +38,7 @@ ) +@pytest.mark.integration class TestWeaviateDocumentStore(CountDocumentsTest, WriteDocumentsTest, DeleteDocumentsTest, FilterDocumentsTest): @pytest.fixture def document_store(self, request) -> WeaviateDocumentStore: From b8f5e605bb326cfcdd34b3803c5af90559435fd1 Mon Sep 17 00:00:00 2001 From: Stefano Fiorucci Date: Wed, 13 Mar 2024 16:32:14 +0100 Subject: [PATCH 03/10] Notify nightly failures (#577) * first refactorings * separate unit tests in pgvector * small change to weaviate * fix format * wip * retry * try failur * restrict * retry * try using composite action * retry composite action * fix typo * update all workflows * retry * fix test * fix --- .github/actions/send_failure/action.yml | 28 ++++++++++++++++++++++ .github/workflows/astra.yml | 9 ++++++- .github/workflows/chroma.yml | 7 ++++++ .github/workflows/cohere.yml | 7 ++++++ .github/workflows/deepeval.yml | 7 ++++++ .github/workflows/elasticsearch.yml | 7 ++++++ .github/workflows/fastembed.yml | 7 ++++++ .github/workflows/google_ai.yml | 7 ++++++ .github/workflows/google_vertex.yml | 7 ++++++ .github/workflows/gradient.yml | 9 ++++++- .github/workflows/instructor_embedders.yml | 7 ++++++ .github/workflows/jina.yml | 7 ++++++ .github/workflows/llama_cpp.yml | 7 ++++++ .github/workflows/mistral.yml | 7 ++++++ .github/workflows/mongodb_atlas.yml | 7 ++++++ .github/workflows/nvidia.yml | 7 ++++++ .github/workflows/ollama.yml | 7 ++++++ .github/workflows/opensearch.yml | 7 ++++++ .github/workflows/optimum.yml | 7 ++++++ .github/workflows/pgvector.yml | 7 ++++++ .github/workflows/pinecone.yml | 7 ++++++ .github/workflows/qdrant.yml | 7 ++++++ .github/workflows/ragas.yml | 7 ++++++ .github/workflows/unstructured.yml | 7 ++++++ .github/workflows/uptrain.yml | 7 ++++++ .github/workflows/weaviate.yml | 7 ++++++ 26 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 .github/actions/send_failure/action.yml diff --git a/.github/actions/send_failure/action.yml b/.github/actions/send_failure/action.yml new file mode 100644 index 000000000..8481b0b80 --- /dev/null +++ b/.github/actions/send_failure/action.yml @@ -0,0 +1,28 @@ +name: "Send failure event to Datadog" +inputs: + api-key: + description: "Datadog API key" + required: true + title: + description: "Custom title for the event" + required: true +runs: + using: "composite" + steps: + - uses: masci/datadog@v1 + with: + api-key: ${{ inputs.api-key }} + api-url: https://api.datadoghq.eu + events: | + - title: "${{ inputs.title }}" + text: "Job ${{ github.job }} in branch ${{ github.ref_name }}" + alert_type: "error" + source_type_name: "Github" + host: ${{ github.repository_owner }} + tags: + - "project:${{ github.repository }}" + - "job:${{ github.job }}" + - "run_id:${{ github.run_id }}" + - "workflow:${{ github.workflow }}" + - "branch:${{ github.ref_name }}" + - "url:https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \ No newline at end of file diff --git a/.github/workflows/astra.yml b/.github/workflows/astra.yml index d859626ff..e90edc2e9 100644 --- a/.github/workflows/astra.yml +++ b/.github/workflows/astra.yml @@ -61,4 +61,11 @@ jobs: env: ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_API_ENDPOINT }} ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_TOKEN }} - run: hatch run cov \ No newline at end of file + run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/chroma.yml b/.github/workflows/chroma.yml index fec309f6b..e6712d807 100644 --- a/.github/workflows/chroma.yml +++ b/.github/workflows/chroma.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/cohere.yml b/.github/workflows/cohere.yml index fb6b00680..6f23760a0 100644 --- a/.github/workflows/cohere.yml +++ b/.github/workflows/cohere.yml @@ -55,3 +55,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/deepeval.yml b/.github/workflows/deepeval.yml index e2468fa8c..a9efc2f3a 100644 --- a/.github/workflows/deepeval.yml +++ b/.github/workflows/deepeval.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/elasticsearch.yml b/.github/workflows/elasticsearch.yml index 688e5c48f..21efcbc34 100644 --- a/.github/workflows/elasticsearch.yml +++ b/.github/workflows/elasticsearch.yml @@ -56,3 +56,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/fastembed.yml b/.github/workflows/fastembed.yml index fe736029a..a2b076c1a 100644 --- a/.github/workflows/fastembed.yml +++ b/.github/workflows/fastembed.yml @@ -43,3 +43,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: failure() #github.event_name == 'schedule' && + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/google_ai.yml b/.github/workflows/google_ai.yml index 6093df4a4..9efeb8590 100644 --- a/.github/workflows/google_ai.yml +++ b/.github/workflows/google_ai.yml @@ -59,3 +59,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/google_vertex.yml b/.github/workflows/google_vertex.yml index 6f6c6d0d9..03890ed4a 100644 --- a/.github/workflows/google_vertex.yml +++ b/.github/workflows/google_vertex.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/gradient.yml b/.github/workflows/gradient.yml index 61a04be7b..8fbaf6f18 100644 --- a/.github/workflows/gradient.yml +++ b/.github/workflows/gradient.yml @@ -57,4 +57,11 @@ jobs: run: hatch run docs - name: Run tests - run: hatch run cov \ No newline at end of file + run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/instructor_embedders.yml b/.github/workflows/instructor_embedders.yml index 09d04e9d3..5282c8e18 100644 --- a/.github/workflows/instructor_embedders.yml +++ b/.github/workflows/instructor_embedders.yml @@ -36,3 +36,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/jina.yml b/.github/workflows/jina.yml index 1f8e83a7d..1ab0e2a2b 100644 --- a/.github/workflows/jina.yml +++ b/.github/workflows/jina.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/llama_cpp.yml b/.github/workflows/llama_cpp.yml index 89c7e5426..712e91fa2 100644 --- a/.github/workflows/llama_cpp.yml +++ b/.github/workflows/llama_cpp.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/mistral.yml b/.github/workflows/mistral.yml index a02b5ad43..029bb974a 100644 --- a/.github/workflows/mistral.yml +++ b/.github/workflows/mistral.yml @@ -59,3 +59,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/mongodb_atlas.yml b/.github/workflows/mongodb_atlas.yml index af19776cd..bf48a75c2 100644 --- a/.github/workflows/mongodb_atlas.yml +++ b/.github/workflows/mongodb_atlas.yml @@ -56,3 +56,10 @@ jobs: - name: Run tests working-directory: integrations/mongodb_atlas run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/nvidia.yml b/.github/workflows/nvidia.yml index 6e7562c17..8b6ec030a 100644 --- a/.github/workflows/nvidia.yml +++ b/.github/workflows/nvidia.yml @@ -55,3 +55,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/ollama.yml b/.github/workflows/ollama.yml index 28b522890..c977ba116 100644 --- a/.github/workflows/ollama.yml +++ b/.github/workflows/ollama.yml @@ -76,3 +76,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/opensearch.yml b/.github/workflows/opensearch.yml index 72a01d090..da177b83c 100644 --- a/.github/workflows/opensearch.yml +++ b/.github/workflows/opensearch.yml @@ -57,3 +57,10 @@ jobs: - name: Run tests working-directory: integrations/opensearch run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/optimum.yml b/.github/workflows/optimum.yml index 3b0d137da..077413920 100644 --- a/.github/workflows/optimum.yml +++ b/.github/workflows/optimum.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/pgvector.yml b/.github/workflows/pgvector.yml index badb2565b..647f520e1 100644 --- a/.github/workflows/pgvector.yml +++ b/.github/workflows/pgvector.yml @@ -62,3 +62,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/pinecone.yml b/.github/workflows/pinecone.yml index a82fb74de..49d421813 100644 --- a/.github/workflows/pinecone.yml +++ b/.github/workflows/pinecone.yml @@ -57,3 +57,10 @@ jobs: - name: Run tests working-directory: integrations/pinecone run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/qdrant.yml b/.github/workflows/qdrant.yml index 9f031031f..3c72b0f02 100644 --- a/.github/workflows/qdrant.yml +++ b/.github/workflows/qdrant.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/ragas.yml b/.github/workflows/ragas.yml index e2ce46764..d3def92ca 100644 --- a/.github/workflows/ragas.yml +++ b/.github/workflows/ragas.yml @@ -58,3 +58,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/unstructured.yml b/.github/workflows/unstructured.yml index 83cad6dfc..b2778431c 100644 --- a/.github/workflows/unstructured.yml +++ b/.github/workflows/unstructured.yml @@ -70,3 +70,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/uptrain.yml b/.github/workflows/uptrain.yml index bacfa27fb..64453b0fd 100644 --- a/.github/workflows/uptrain.yml +++ b/.github/workflows/uptrain.yml @@ -54,3 +54,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/weaviate.yml b/.github/workflows/weaviate.yml index 051415336..69447b96b 100644 --- a/.github/workflows/weaviate.yml +++ b/.github/workflows/weaviate.yml @@ -55,3 +55,10 @@ jobs: - name: Run tests run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} From 16cfff67938f02e25f4db6cbf6c132cd4be020cf Mon Sep 17 00:00:00 2001 From: Tobias Wochinger Date: Wed, 13 Mar 2024 16:41:00 +0100 Subject: [PATCH 04/10] docs: disable-class-def (#556) Co-authored-by: Madeesh Kannan --- integrations/amazon_bedrock/pydoc/config.yml | 1 + integrations/amazon_sagemaker/pydoc/config.yml | 1 + integrations/astra/pydoc/config.yml | 1 + integrations/chroma/pydoc/config.yml | 1 + integrations/cohere/pydoc/config.yml | 1 + integrations/deepeval/pydoc/config.yml | 1 + integrations/elasticsearch/pydoc/config.yml | 1 + integrations/fastembed/pydoc/config.yml | 1 + integrations/google_ai/pydoc/config.yml | 1 + integrations/google_vertex/pydoc/config.yml | 1 + integrations/gradient/pydoc/config.yml | 1 + integrations/instructor_embedders/pydoc/config.yml | 1 + integrations/jina/pydoc/config.yml | 1 + integrations/llama_cpp/pydoc/config.yml | 1 + integrations/mistral/pydoc/config.yml | 1 + integrations/mongodb_atlas/pydoc/config.yml | 1 + integrations/nvidia/pydoc/config.yml | 1 + integrations/ollama/pydoc/config.yml | 1 + integrations/opensearch/pydoc/config.yml | 1 + integrations/optimum/pydoc/config.yml | 1 + integrations/pgvector/pydoc/config.yml | 1 + integrations/pinecone/pydoc/config.yml | 1 + integrations/qdrant/pydoc/config.yml | 1 + integrations/ragas/pydoc/config.yml | 1 + integrations/unstructured/pydoc/config.yml | 1 + integrations/uptrain/pydoc/config.yml | 1 + integrations/weaviate/pydoc/config.yml | 1 + 27 files changed, 27 insertions(+) diff --git a/integrations/amazon_bedrock/pydoc/config.yml b/integrations/amazon_bedrock/pydoc/config.yml index 04fa3f173..c719c7cfd 100644 --- a/integrations/amazon_bedrock/pydoc/config.yml +++ b/integrations/amazon_bedrock/pydoc/config.yml @@ -28,6 +28,7 @@ renderer: order: 9 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/amazon_sagemaker/pydoc/config.yml b/integrations/amazon_sagemaker/pydoc/config.yml index ad510bcf3..20d51b25e 100644 --- a/integrations/amazon_sagemaker/pydoc/config.yml +++ b/integrations/amazon_sagemaker/pydoc/config.yml @@ -22,6 +22,7 @@ renderer: order: 20 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/astra/pydoc/config.yml b/integrations/astra/pydoc/config.yml index 02764f95a..61fec0523 100644 --- a/integrations/astra/pydoc/config.yml +++ b/integrations/astra/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 30 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/chroma/pydoc/config.yml b/integrations/chroma/pydoc/config.yml index bb8867655..c28902080 100644 --- a/integrations/chroma/pydoc/config.yml +++ b/integrations/chroma/pydoc/config.yml @@ -25,6 +25,7 @@ renderer: order: 40 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/cohere/pydoc/config.yml b/integrations/cohere/pydoc/config.yml index 46af25872..48608625c 100644 --- a/integrations/cohere/pydoc/config.yml +++ b/integrations/cohere/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 50 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/deepeval/pydoc/config.yml b/integrations/deepeval/pydoc/config.yml index 37739fc23..affa23acd 100644 --- a/integrations/deepeval/pydoc/config.yml +++ b/integrations/deepeval/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 60 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/elasticsearch/pydoc/config.yml b/integrations/elasticsearch/pydoc/config.yml index 1c723a658..04e20f992 100644 --- a/integrations/elasticsearch/pydoc/config.yml +++ b/integrations/elasticsearch/pydoc/config.yml @@ -25,6 +25,7 @@ renderer: order: 70 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/fastembed/pydoc/config.yml b/integrations/fastembed/pydoc/config.yml index faca8a9ef..2a4439b84 100644 --- a/integrations/fastembed/pydoc/config.yml +++ b/integrations/fastembed/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 80 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/google_ai/pydoc/config.yml b/integrations/google_ai/pydoc/config.yml index 63b3ac2fa..977a91fab 100644 --- a/integrations/google_ai/pydoc/config.yml +++ b/integrations/google_ai/pydoc/config.yml @@ -23,6 +23,7 @@ renderer: order: 90 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/google_vertex/pydoc/config.yml b/integrations/google_vertex/pydoc/config.yml index cfed76097..bee97fdb8 100644 --- a/integrations/google_vertex/pydoc/config.yml +++ b/integrations/google_vertex/pydoc/config.yml @@ -28,6 +28,7 @@ renderer: order: 100 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/gradient/pydoc/config.yml b/integrations/gradient/pydoc/config.yml index 963ddb80a..a0ec5f72d 100644 --- a/integrations/gradient/pydoc/config.yml +++ b/integrations/gradient/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 110 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/instructor_embedders/pydoc/config.yml b/integrations/instructor_embedders/pydoc/config.yml index f63f8c932..dd5e38faa 100644 --- a/integrations/instructor_embedders/pydoc/config.yml +++ b/integrations/instructor_embedders/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 120 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/jina/pydoc/config.yml b/integrations/jina/pydoc/config.yml index 4a5bd55d4..de3fd74b9 100644 --- a/integrations/jina/pydoc/config.yml +++ b/integrations/jina/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 130 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/llama_cpp/pydoc/config.yml b/integrations/llama_cpp/pydoc/config.yml index bf99ac768..98068e672 100644 --- a/integrations/llama_cpp/pydoc/config.yml +++ b/integrations/llama_cpp/pydoc/config.yml @@ -22,6 +22,7 @@ renderer: order: 140 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/mistral/pydoc/config.yml b/integrations/mistral/pydoc/config.yml index 2d2e497b1..86ad5f1d0 100644 --- a/integrations/mistral/pydoc/config.yml +++ b/integrations/mistral/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 150 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/mongodb_atlas/pydoc/config.yml b/integrations/mongodb_atlas/pydoc/config.yml index e59993881..85694c57f 100644 --- a/integrations/mongodb_atlas/pydoc/config.yml +++ b/integrations/mongodb_atlas/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 160 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/nvidia/pydoc/config.yml b/integrations/nvidia/pydoc/config.yml index 675db0335..392157299 100644 --- a/integrations/nvidia/pydoc/config.yml +++ b/integrations/nvidia/pydoc/config.yml @@ -25,6 +25,7 @@ renderer: order: 50 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/ollama/pydoc/config.yml b/integrations/ollama/pydoc/config.yml index 45c336b85..4207ea997 100644 --- a/integrations/ollama/pydoc/config.yml +++ b/integrations/ollama/pydoc/config.yml @@ -25,6 +25,7 @@ renderer: order: 170 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/opensearch/pydoc/config.yml b/integrations/opensearch/pydoc/config.yml index 02684814e..dfcb23b5f 100644 --- a/integrations/opensearch/pydoc/config.yml +++ b/integrations/opensearch/pydoc/config.yml @@ -25,6 +25,7 @@ renderer: order: 180 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/optimum/pydoc/config.yml b/integrations/optimum/pydoc/config.yml index 996678c55..8597b07ad 100644 --- a/integrations/optimum/pydoc/config.yml +++ b/integrations/optimum/pydoc/config.yml @@ -27,6 +27,7 @@ renderer: order: 185 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/pgvector/pydoc/config.yml b/integrations/pgvector/pydoc/config.yml index 449937629..1be4a1662 100644 --- a/integrations/pgvector/pydoc/config.yml +++ b/integrations/pgvector/pydoc/config.yml @@ -23,6 +23,7 @@ renderer: order: 190 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/pinecone/pydoc/config.yml b/integrations/pinecone/pydoc/config.yml index fff835877..4265eeecc 100644 --- a/integrations/pinecone/pydoc/config.yml +++ b/integrations/pinecone/pydoc/config.yml @@ -24,6 +24,7 @@ renderer: order: 200 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/qdrant/pydoc/config.yml b/integrations/qdrant/pydoc/config.yml index a7d849b29..9f2df08d0 100644 --- a/integrations/qdrant/pydoc/config.yml +++ b/integrations/qdrant/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 210 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/ragas/pydoc/config.yml b/integrations/ragas/pydoc/config.yml index 804df1af9..3a8e843fe 100644 --- a/integrations/ragas/pydoc/config.yml +++ b/integrations/ragas/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 220 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/unstructured/pydoc/config.yml b/integrations/unstructured/pydoc/config.yml index 8312fd776..7179a2607 100644 --- a/integrations/unstructured/pydoc/config.yml +++ b/integrations/unstructured/pydoc/config.yml @@ -22,6 +22,7 @@ renderer: order: 230 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/uptrain/pydoc/config.yml b/integrations/uptrain/pydoc/config.yml index 305b1a760..6e5c9a2b8 100644 --- a/integrations/uptrain/pydoc/config.yml +++ b/integrations/uptrain/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 240 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false diff --git a/integrations/weaviate/pydoc/config.yml b/integrations/weaviate/pydoc/config.yml index 0b1df5877..e62b21591 100644 --- a/integrations/weaviate/pydoc/config.yml +++ b/integrations/weaviate/pydoc/config.yml @@ -26,6 +26,7 @@ renderer: order: 250 markdown: descriptive_class_title: false + classdef_code_block: false descriptive_module_title: true add_method_class_prefix: true add_member_class_prefix: false From aca516cf5e4dcb00da69e10740a79da62e036242 Mon Sep 17 00:00:00 2001 From: Vladimir Blagojevic Date: Fri, 15 Mar 2024 14:42:43 +0100 Subject: [PATCH 05/10] feat: Add AnthropicGenerator and AnthropicChatGenerator (#573) * Initial version * Add workflow file * ANTHROPIC_API_KEY env var declaration fix * Pylint ignore on Secret from_env_var * Initial version of AnthropicGenerator * Add generator module to pydoc * PR feedback - Julian * fix typo in example ChatMessage --------- Co-authored-by: Julian Risch --- .github/workflows/anthropic.yml | 64 +++++ integrations/anthropic/LICENSE.txt | 73 +++++ integrations/anthropic/README.md | 44 +++ .../example/documentation_rag_with_claude.py | 42 +++ integrations/anthropic/pydoc/config.yml | 29 ++ integrations/anthropic/pyproject.toml | 186 ++++++++++++ .../generators/anthropic/__init__.py | 7 + .../generators/anthropic/chat/__init__.py | 3 + .../anthropic/chat/chat_generator.py | 268 ++++++++++++++++++ .../generators/anthropic/generator.py | 187 ++++++++++++ integrations/anthropic/tests/__init__.py | 3 + integrations/anthropic/tests/conftest.py | 23 ++ .../anthropic/tests/test_chat_generator.py | 218 ++++++++++++++ .../anthropic/tests/test_generator.py | 227 +++++++++++++++ 14 files changed, 1374 insertions(+) create mode 100644 .github/workflows/anthropic.yml create mode 100644 integrations/anthropic/LICENSE.txt create mode 100644 integrations/anthropic/README.md create mode 100644 integrations/anthropic/example/documentation_rag_with_claude.py create mode 100644 integrations/anthropic/pydoc/config.yml create mode 100644 integrations/anthropic/pyproject.toml create mode 100644 integrations/anthropic/src/haystack_integrations/components/generators/anthropic/__init__.py create mode 100644 integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/__init__.py create mode 100644 integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/chat_generator.py create mode 100644 integrations/anthropic/src/haystack_integrations/components/generators/anthropic/generator.py create mode 100644 integrations/anthropic/tests/__init__.py create mode 100644 integrations/anthropic/tests/conftest.py create mode 100644 integrations/anthropic/tests/test_chat_generator.py create mode 100644 integrations/anthropic/tests/test_generator.py diff --git a/.github/workflows/anthropic.yml b/.github/workflows/anthropic.yml new file mode 100644 index 000000000..755660bfc --- /dev/null +++ b/.github/workflows/anthropic.yml @@ -0,0 +1,64 @@ +# This workflow comes from https://github.com/ofek/hatch-mypyc +# https://github.com/ofek/hatch-mypyc/blob/5a198c0ba8660494d02716cfc9d79ce4adfb1442/.github/workflows/test.yml +name: Test / anthropic + +on: + schedule: + - cron: "0 0 * * *" + pull_request: + paths: + - "integrations/anthropic/**" + - ".github/workflows/anthropic.yml" + +defaults: + run: + working-directory: integrations/anthropic + +concurrency: + group: cohere-${{ github.head_ref }} + cancel-in-progress: true + +env: + PYTHONUNBUFFERED: "1" + FORCE_COLOR: "1" + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} + +jobs: + run: + name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + python-version: ["3.9", "3.10"] + + steps: + - name: Support longpaths + if: matrix.os == 'windows-latest' + working-directory: . + run: git config --system core.longpaths true + + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Hatch + run: pip install --upgrade hatch + + - name: Lint + if: matrix.python-version == '3.9' && runner.os == 'Linux' + run: hatch run lint:all + + - name: Run tests + run: hatch run cov + + - name: Send event to Datadog for nightly failures + if: github.event_name == 'schedule' && failure() + uses: ./.github/actions/send_failure + with: + title: "core-integrations nightly failure: ${{ github.workflow }}" + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/integrations/anthropic/LICENSE.txt b/integrations/anthropic/LICENSE.txt new file mode 100644 index 000000000..137069b82 --- /dev/null +++ b/integrations/anthropic/LICENSE.txt @@ -0,0 +1,73 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + + (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + + You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/integrations/anthropic/README.md b/integrations/anthropic/README.md new file mode 100644 index 000000000..316d327aa --- /dev/null +++ b/integrations/anthropic/README.md @@ -0,0 +1,44 @@ +# anthropic-haystack + +[![PyPI - Version](https://img.shields.io/pypi/v/amazon-bedrock-haystack.svg)](https://pypi.org/project/amazon-bedrock-haystack) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/amazon-bedrock-haystack.svg)](https://pypi.org/project/amazon-bedrock-haystack) + +----- + +**Table of Contents** + +- [Installation](#installation) +- [Contributing](#contributing) +- [Examples](#examples) +- [License](#license) + +## Installation + +```console +pip install anthropic-haystack +``` + +## Contributing + +`hatch` is the best way to interact with this project, to install it: +```sh +pip install hatch +``` + +With `hatch` installed, to run all the tests: +``` +hatch run test +``` +> Note: there are no integration tests for this project. + +To run the linters `ruff` and `mypy`: +``` +hatch run lint:all +``` + +## Examples +You can find an example of how to do a simple RAG with Claude using online documentation in the `example/` folder of this repo. + +## License + +`anthropic-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. diff --git a/integrations/anthropic/example/documentation_rag_with_claude.py b/integrations/anthropic/example/documentation_rag_with_claude.py new file mode 100644 index 000000000..a3cc452ad --- /dev/null +++ b/integrations/anthropic/example/documentation_rag_with_claude.py @@ -0,0 +1,42 @@ +# To run this example, you will need to set a `ANTHROPIC_API_KEY` environment variable. + +from haystack import Pipeline +from haystack.components.builders import DynamicChatPromptBuilder +from haystack.components.converters import HTMLToDocument +from haystack.components.fetchers import LinkContentFetcher +from haystack.components.generators.utils import print_streaming_chunk +from haystack.dataclasses import ChatMessage +from haystack.utils import Secret + +from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator + +messages = [ + ChatMessage.from_system("You are a prompt expert who answers questions based on the given documents."), + ChatMessage.from_user("Here are the documents: {{documents}} \\n Answer: {{query}}"), +] + +rag_pipeline = Pipeline() +rag_pipeline.add_component("fetcher", LinkContentFetcher()) +rag_pipeline.add_component("converter", HTMLToDocument()) +rag_pipeline.add_component("prompt_builder", DynamicChatPromptBuilder(runtime_variables=["documents"])) +rag_pipeline.add_component( + "llm", + AnthropicChatGenerator( + api_key=Secret.from_env_var("ANTHROPIC_API_KEY"), + model="claude-3-sonnet", + streaming_callback=print_streaming_chunk, + ), +) + + +rag_pipeline.connect("fetcher", "converter") +rag_pipeline.connect("converter", "prompt_builder") +rag_pipeline.connect("prompt_builder", "llm") + +question = "What are the best practices in prompt engineering?" +rag_pipeline.run( + data={ + "fetcher": {"urls": ["https://docs.anthropic.com/claude/docs/prompt-engineering"]}, + "prompt_builder": {"template_variables": {"query": question}, "prompt_source": messages}, + } +) diff --git a/integrations/anthropic/pydoc/config.yml b/integrations/anthropic/pydoc/config.yml new file mode 100644 index 000000000..553dfcaef --- /dev/null +++ b/integrations/anthropic/pydoc/config.yml @@ -0,0 +1,29 @@ +loaders: + - type: haystack_pydoc_tools.loaders.CustomPythonLoader + search_path: [../src] + modules: [ + "haystack_integrations.components.generators.anthropic.generator", + "haystack_integrations.components.generators.anthropic.chat.chat_generator", + ] + ignore_when_discovered: ["__init__"] +processors: + - type: filter + expression: + documented_only: true + do_not_filter_modules: false + skip_empty_modules: true + - type: smart + - type: crossref +renderer: + type: haystack_pydoc_tools.renderers.ReadmePreviewRenderer + excerpt: Anthropic integration for Haystack + category_slug: integrations-api + title: Anthropic + slug: integrations-anthropic + order: 22 + markdown: + descriptive_class_title: false + descriptive_module_title: true + add_method_class_prefix: true + add_member_class_prefix: false + filename: _readme_anthropic.md diff --git a/integrations/anthropic/pyproject.toml b/integrations/anthropic/pyproject.toml new file mode 100644 index 000000000..79cc5850d --- /dev/null +++ b/integrations/anthropic/pyproject.toml @@ -0,0 +1,186 @@ +[build-system] +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" + +[project] +name = "anthropic-haystack" +dynamic = ["version"] +description = 'An integration of Anthropic Claude models into the Haystack framework.' +readme = "README.md" +requires-python = ">=3.8" +license = "Apache-2.0" +keywords = [] +authors = [ + { name = "deepset GmbH", email = "info@deepset.ai" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dependencies = [ + "haystack-ai", + "anthropic", +] + +[project.urls] +Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic#readme" +Issues = "https://github.com/deepset-ai/haystack-core-integrations/issues" +Source = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic" + +[tool.hatch.build.targets.wheel] +packages = ["src/haystack_integrations"] + +[tool.hatch.version] +source = "vcs" +tag-pattern = 'integrations\/anthropic-v(?P.*)' + +[tool.hatch.version.raw-options] +root = "../.." +git_describe_command = 'git describe --tags --match="integrations/anthropic-v[0-9]*"' + +[tool.hatch.envs.default] +dependencies = [ + "coverage[toml]>=6.5", + "pytest", + "haystack-pydoc-tools", +] +[tool.hatch.envs.default.scripts] +test = "pytest {args:tests}" +test-cov = "coverage run -m pytest {args:tests}" +cov-report = [ + "- coverage combine", + "coverage report", +] +cov = [ + "test-cov", + "cov-report", +] +docs = [ + "pydoc-markdown pydoc/config.yml" +] +[[tool.hatch.envs.all.matrix]] +python = ["3.8", "3.9", "3.10", "3.11", "3.12"] + +[tool.hatch.envs.lint] +detached = true +dependencies = [ + "black>=23.1.0", + "mypy>=1.0.0", + "ruff>=0.0.243", +] +[tool.hatch.envs.lint.scripts] +typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}" + +style = [ + "ruff {args:.}", + "black --check --diff {args:.}", +] + +fmt = [ + "black {args:.}", + "ruff --fix {args:.}", + "style", +] + +all = [ + "style", + "typing", +] + +[tool.black] +target-version = ["py37"] +line-length = 120 +skip-string-normalization = true + +[tool.ruff] +target-version = "py37" +line-length = 120 +select = [ + "A", + "ARG", + "B", + "C", + "DTZ", + "E", + "EM", + "F", + "I", + "ICN", + "ISC", + "N", + "PLC", + "PLE", + "PLR", + "PLW", + "Q", + "RUF", + "S", + "T", + "TID", + "UP", + "W", + "YTT", +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Ignore checks for possible passwords + "S105", "S106", "S107", + # Ignore complexity + "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915", + # Ignore unused params + "ARG001", "ARG002", "ARG005", +] +unfixable = [ + # Don't touch unused imports + "F401", +] + +[tool.ruff.isort] +known-first-party = ["haystack_integrations"] + +[tool.ruff.flake8-tidy-imports] +ban-relative-imports = "parents" + +[tool.ruff.per-file-ignores] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + +[tool.coverage.run] +source = ["haystack_integrations"] +branch = true +parallel = true + +[tool.coverage.report] +omit = ["*/tests/*", "*/__init__.py"] +show_missing=true +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] + +[[tool.mypy.overrides]] +module = [ + "anthropic.*", + "haystack.*", + "haystack_integrations.*", + "pytest.*", + "numpy.*", +] +ignore_missing_imports = true + +[tool.pytest.ini_options] +addopts = "--strict-markers" +markers = [ + "unit: unit tests", + "integration: integration tests", +] +log_cli = true diff --git a/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/__init__.py b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/__init__.py new file mode 100644 index 000000000..c2c1ee40d --- /dev/null +++ b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/__init__.py @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 +from .chat.chat_generator import AnthropicChatGenerator +from .generator import AnthropicGenerator + +__all__ = ["AnthropicGenerator", "AnthropicChatGenerator"] diff --git a/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/__init__.py b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/__init__.py new file mode 100644 index 000000000..e873bc332 --- /dev/null +++ b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 diff --git a/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/chat_generator.py b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/chat_generator.py new file mode 100644 index 000000000..6f43855b7 --- /dev/null +++ b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat/chat_generator.py @@ -0,0 +1,268 @@ +import dataclasses +from typing import Any, Callable, ClassVar, Dict, List, Optional, Union + +from haystack import component, default_from_dict, default_to_dict, logging +from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk +from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable + +from anthropic import Anthropic, Stream +from anthropic.types import ( + ContentBlock, + ContentBlockDeltaEvent, + Message, + MessageDeltaEvent, + MessageStartEvent, + MessageStreamEvent, + TextDelta, +) + +logger = logging.getLogger(__name__) + + +@component +class AnthropicChatGenerator: + """ + Enables text generation using Anthropic state-of-the-art Claude 3 family of large language models (LLMs) through + the Anthropic messaging API. + + It supports models like `claude-3-opus`, `claude-3-sonnet`, and `claude-3-haiku`, accessed through the + `/v1/messages` API endpoint using the Claude v2.1 messaging version. + + Users can pass any text generation parameters valid for the Anthropic messaging API directly to this component + via the `generation_kwargs` parameter in `__init__` or the `generation_kwargs` parameter in the `run` method. + + For more details on the parameters supported by the Anthropic API, refer to the + Anthropic Message API [documentation](https://docs.anthropic.com/claude/reference/messages_post). + + ```python + from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator + from haystack.dataclasses import ChatMessage + + messages = [ChatMessage.from_user("What's Natural Language Processing?")] + client = AnthropicChatGenerator(model="claude-3-sonnet-20240229") + response = client.run(messages) + print(response) + + >> {'replies': [ChatMessage(content='Natural Language Processing (NLP) is a field of artificial intelligence that + >> focuses on enabling computers to understand, interpret, and generate human language. It involves developing + >> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and + >> communicate in natural languages like English, Spanish, or Chinese.', role=, + >> name=None, meta={'model': 'claude-3-sonnet-20240229', 'index': 0, 'finish_reason': 'end_turn', + >> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]} + ``` + + For more details on supported models and their capabilities, refer to the Anthropic + [documentation](https://docs.anthropic.com/claude/docs/intro-to-claude). + + Note: We don't yet support vision [capabilities](https://docs.anthropic.com/claude/docs/vision) in the current + implementation. + """ + + # The parameters that can be passed to the Anthropic API https://docs.anthropic.com/claude/reference/messages_post + ALLOWED_PARAMS: ClassVar[List[str]] = [ + "system", + "max_tokens", + "metadata", + "stop_sequences", + "temperature", + "top_p", + "top_k", + ] + + def __init__( + self, + api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), # noqa: B008 + model: str = "claude-3-sonnet-20240229", + streaming_callback: Optional[Callable[[StreamingChunk], None]] = None, + generation_kwargs: Optional[Dict[str, Any]] = None, + ): + """ + Creates an instance of AnthropicChatGenerator. + + :param api_key: The Anthropic API key + :param model: The name of the model to use. + :param streaming_callback: A callback function that is called when a new token is received from the stream. + The callback function accepts StreamingChunk as an argument. + :param generation_kwargs: Other parameters to use for the model. These parameters are all sent directly to + the Anthropic endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) + for more details. + + Supported generation_kwargs parameters are: + - `system`: The system message to be passed to the model. + - `max_tokens`: The maximum number of tokens to generate. + - `metadata`: A dictionary of metadata to be passed to the model. + - `stop_sequences`: A list of strings that the model should stop generating at. + - `temperature`: The temperature to use for sampling. + - `top_p`: The top_p value to use for nucleus sampling. + - `top_k`: The top_k value to use for top-k sampling. + + """ + self.api_key = api_key + self.model = model + self.generation_kwargs = generation_kwargs or {} + self.streaming_callback = streaming_callback + self.client = Anthropic(api_key=self.api_key.resolve_value()) + + def _get_telemetry_data(self) -> Dict[str, Any]: + """ + Data that is sent to Posthog for usage analytics. + """ + return {"model": self.model} + + def to_dict(self) -> Dict[str, Any]: + """ + Serialize this component to a dictionary. + + :returns: + The serialized component as a dictionary. + """ + callback_name = serialize_callable(self.streaming_callback) if self.streaming_callback else None + return default_to_dict( + self, + model=self.model, + streaming_callback=callback_name, + generation_kwargs=self.generation_kwargs, + api_key=self.api_key.to_dict(), + ) + + @classmethod + def from_dict(cls, data: Dict[str, Any]) -> "AnthropicChatGenerator": + """ + Deserialize this component from a dictionary. + + :param data: The dictionary representation of this component. + :returns: + The deserialized component instance. + """ + deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"]) + init_params = data.get("init_parameters", {}) + serialized_callback_handler = init_params.get("streaming_callback") + if serialized_callback_handler: + data["init_parameters"]["streaming_callback"] = deserialize_callable(serialized_callback_handler) + return default_from_dict(cls, data) + + @component.output_types(replies=List[ChatMessage]) + def run(self, messages: List[ChatMessage], generation_kwargs: Optional[Dict[str, Any]] = None): + """ + Invoke the text generation inference based on the provided messages and generation parameters. + + :param messages: A list of ChatMessage instances representing the input messages. + :param generation_kwargs: Additional keyword arguments for text generation. These parameters will + potentially override the parameters passed in the `__init__` method. + For more details on the parameters supported by the Anthropic API, refer to the + Anthropic [documentation](https://www.anthropic.com/python-library). + + :returns: + - `replies`: A list of ChatMessage instances representing the generated responses. + """ + + # update generation kwargs by merging with the generation kwargs passed to the run method + generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})} + filtered_generation_kwargs = {k: v for k, v in generation_kwargs.items() if k in self.ALLOWED_PARAMS} + disallowed_params = set(generation_kwargs) - set(self.ALLOWED_PARAMS) + if disallowed_params: + logger.warning( + f"Model parameters {disallowed_params} are not allowed and will be ignored. " + f"Allowed parameters are {self.ALLOWED_PARAMS}." + ) + + # adapt ChatMessage(s) to the format expected by the Anthropic API + anthropic_formatted_messages = self._convert_to_anthropic_format(messages) + + # system message provided by the user overrides the system message from the self.generation_kwargs + system = messages[0].content if messages and messages[0].is_from(ChatRole.SYSTEM) else None + if system: + anthropic_formatted_messages = anthropic_formatted_messages[1:] + + response: Union[Message, Stream[MessageStreamEvent]] = self.client.messages.create( + max_tokens=filtered_generation_kwargs.pop("max_tokens", 512), + system=system if system else filtered_generation_kwargs.pop("system", ""), + model=self.model, + messages=anthropic_formatted_messages, + stream=self.streaming_callback is not None, + **filtered_generation_kwargs, + ) + + completions: List[ChatMessage] = [] + # if streaming is enabled, the response is a Stream[MessageStreamEvent] + if isinstance(response, Stream): + chunks: List[StreamingChunk] = [] + stream_event, delta, start_event = None, None, None + for stream_event in response: + if isinstance(stream_event, MessageStartEvent): + # capture start message to count input tokens + start_event = stream_event + if isinstance(stream_event, ContentBlockDeltaEvent): + chunk_delta: StreamingChunk = self._build_chunk(stream_event.delta) + chunks.append(chunk_delta) + if self.streaming_callback: + self.streaming_callback(chunk_delta) # invoke callback with the chunk_delta + if isinstance(stream_event, MessageDeltaEvent): + # capture stop reason and stop sequence + delta = stream_event + completions = [self._connect_chunks(chunks, start_event, delta)] + # if streaming is disabled, the response is an Anthropic Message + elif isinstance(response, Message): + completions = [self._build_message(content_block, response) for content_block in response.content] + + return {"replies": completions} + + def _build_message(self, content_block: ContentBlock, message: Message) -> ChatMessage: + """ + Converts the non-streaming Anthropic Message to a ChatMessage. + :param content_block: The content block of the message. + :param message: The non-streaming Anthropic Message. + :returns: The ChatMessage. + """ + chat_message = ChatMessage.from_assistant(content_block.text) + chat_message.meta.update( + { + "model": message.model, + "index": 0, + "finish_reason": message.stop_reason, + "usage": dict(message.usage or {}), + } + ) + return chat_message + + def _convert_to_anthropic_format(self, messages: List[ChatMessage]) -> List[Dict[str, Any]]: + """ + Converts the list of ChatMessage to the list of messages in the format expected by the Anthropic API. + :param messages: The list of ChatMessage. + :returns: The list of messages in the format expected by the Anthropic API. + """ + anthropic_formatted_messages = [] + for m in messages: + message_dict = dataclasses.asdict(m) + filtered_message = {k: v for k, v in message_dict.items() if k in {"role", "content"} and v} + anthropic_formatted_messages.append(filtered_message) + return anthropic_formatted_messages + + def _connect_chunks( + self, chunks: List[StreamingChunk], message_start: MessageStartEvent, delta: MessageDeltaEvent + ) -> ChatMessage: + """ + Connects the streaming chunks into a single ChatMessage. + :param chunks: The list of all chunks returned by the Anthropic API. + :param message_start: The MessageStartEvent. + :param delta: The MessageDeltaEvent. + :returns: The complete ChatMessage. + """ + complete_response = ChatMessage.from_assistant("".join([chunk.content for chunk in chunks])) + complete_response.meta.update( + { + "model": self.model, + "index": 0, + "finish_reason": delta.delta.stop_reason if delta else "end_turn", + "usage": {**dict(message_start.message.usage, **dict(delta.usage))} if delta and message_start else {}, + } + ) + return complete_response + + def _build_chunk(self, delta: TextDelta) -> StreamingChunk: + """ + Converts the ContentBlockDeltaEvent to a StreamingChunk. + :param delta: The ContentBlockDeltaEvent. + :returns: The StreamingChunk. + """ + return StreamingChunk(content=delta.text) diff --git a/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/generator.py b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/generator.py new file mode 100644 index 000000000..aa78dfed1 --- /dev/null +++ b/integrations/anthropic/src/haystack_integrations/components/generators/anthropic/generator.py @@ -0,0 +1,187 @@ +from typing import Any, Callable, ClassVar, Dict, List, Optional, Union + +from haystack import component, default_from_dict, default_to_dict, logging +from haystack.dataclasses import StreamingChunk +from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable + +from anthropic import Anthropic, Stream +from anthropic.types import ( + ContentBlockDeltaEvent, + Message, + MessageDeltaEvent, + MessageParam, + MessageStartEvent, + MessageStreamEvent, +) + +logger = logging.getLogger(__name__) + + +@component +class AnthropicGenerator: + """ + Enables text generation using Anthropic large language models (LLMs). It supports the Claude family of models. + + Although Anthropic natively supports a much richer messaging API, we have intentionally simplified it in this + component so that the main input/output interface is string-based. + For more complete support, consider using the AnthropicChatGenerator. + + ```python + from haystack_integrations.components.generators.anthropic import AnthropicGenerator + + client = AnthropicGenerator(model="claude-2.1") + response = client.run("What's Natural Language Processing? Be brief.") + print(response) + >>{'replies': ['Natural language processing (NLP) is a branch of artificial intelligence focused on enabling + >>computers to understand, interpret, and manipulate human language. The goal of NLP is to read, decipher, + >> understand, and make sense of the human languages in a manner that is valuable.'], 'meta': {'model': + >> 'claude-2.1', 'index': 0, 'finish_reason': 'end_turn', 'usage': {'input_tokens': 18, 'output_tokens': 58}}} + ``` + """ + + # The parameters that can be passed to the Anthropic API https://docs.anthropic.com/claude/reference/messages_post + ALLOWED_PARAMS: ClassVar[List[str]] = [ + "system", + "max_tokens", + "metadata", + "stop_sequences", + "temperature", + "top_p", + "top_k", + ] + + def __init__( + self, + api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), # noqa: B008 + model: str = "claude-3-sonnet-20240229", + streaming_callback: Optional[Callable[[StreamingChunk], None]] = None, + system_prompt: Optional[str] = None, + generation_kwargs: Optional[Dict[str, Any]] = None, + ): + """ + Initialize the AnthropicGenerator. + + :param api_key: The Anthropic API key. + :param model: The name of the Anthropic model to use. + :param streaming_callback: An optional callback function to handle streaming chunks. + :param system_prompt: An optional system prompt to use for generation. + :param generation_kwargs: Additional keyword arguments for generation. + """ + self.api_key = api_key + self.model = model + self.generation_kwargs = generation_kwargs or {} + self.streaming_callback = streaming_callback + self.system_prompt = system_prompt + self.client = Anthropic(api_key=self.api_key.resolve_value()) + + def _get_telemetry_data(self) -> Dict[str, Any]: + """ + Get telemetry data for the component. + + :returns: A dictionary containing telemetry data. + """ + return {"model": self.model} + + def to_dict(self) -> Dict[str, Any]: + """ + Serialize this component to a dictionary. + + :returns: + The serialized component as a dictionary. + """ + callback_name = serialize_callable(self.streaming_callback) if self.streaming_callback else None + return default_to_dict( + self, + model=self.model, + streaming_callback=callback_name, + system_prompt=self.system_prompt, + generation_kwargs=self.generation_kwargs, + api_key=self.api_key.to_dict(), + ) + + @classmethod + def from_dict(cls, data: Dict[str, Any]) -> "AnthropicGenerator": + """ + Deserialize this component from a dictionary. + + :param data: The dictionary representation of this component. + :returns: + The deserialized component instance. + """ + deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"]) + init_params = data.get("init_parameters", {}) + serialized_callback_handler = init_params.get("streaming_callback") + if serialized_callback_handler: + data["init_parameters"]["streaming_callback"] = deserialize_callable(serialized_callback_handler) + return default_from_dict(cls, data) + + @component.output_types(replies=List[str], meta=List[Dict[str, Any]]) + def run(self, prompt: str, generation_kwargs: Optional[Dict[str, Any]] = None): + """ + Generate replies using the Anthropic API. + + :param prompt: The input prompt for generation. + :param generation_kwargs: Additional keyword arguments for generation. + :returns: A dictionary containing: + - `replies`: A list of generated replies. + - `meta`: A list of metadata dictionaries for each reply. + """ + # update generation kwargs by merging with the generation kwargs passed to the run method + generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})} + filtered_generation_kwargs = {k: v for k, v in generation_kwargs.items() if k in self.ALLOWED_PARAMS} + disallowed_params = set(generation_kwargs) - set(self.ALLOWED_PARAMS) + if disallowed_params: + logger.warning( + f"Model parameters {disallowed_params} are not allowed and will be ignored. " + f"Allowed parameters are {self.ALLOWED_PARAMS}." + ) + + response: Union[Message, Stream[MessageStreamEvent]] = self.client.messages.create( + max_tokens=filtered_generation_kwargs.pop("max_tokens", 512), + system=self.system_prompt if self.system_prompt else filtered_generation_kwargs.pop("system", ""), + model=self.model, + messages=[MessageParam(content=prompt, role="user")], + stream=self.streaming_callback is not None, + **filtered_generation_kwargs, + ) + + completions: List[str] = [] + meta: Dict[str, Any] = {} + # if streaming is enabled, the response is a Stream[MessageStreamEvent] + if isinstance(response, Stream): + chunks: List[StreamingChunk] = [] + stream_event, delta, start_event = None, None, None + for stream_event in response: + if isinstance(stream_event, MessageStartEvent): + # capture start message to count input tokens + start_event = stream_event + if isinstance(stream_event, ContentBlockDeltaEvent): + chunk_delta: StreamingChunk = StreamingChunk(content=stream_event.delta.text) + chunks.append(chunk_delta) + if self.streaming_callback: + self.streaming_callback(chunk_delta) # invoke callback with the chunk_delta + if isinstance(stream_event, MessageDeltaEvent): + # capture stop reason and stop sequence + delta = stream_event + completions = ["".join([chunk.content for chunk in chunks])] + meta.update( + { + "model": self.model, + "index": 0, + "finish_reason": delta.delta.stop_reason if delta else "end_turn", + "usage": {**dict(start_event.message.usage, **dict(delta.usage))} if delta and start_event else {}, + } + ) + # if streaming is disabled, the response is an Anthropic Message + elif isinstance(response, Message): + completions = [content_block.text for content_block in response.content] + meta.update( + { + "model": response.model, + "index": 0, + "finish_reason": response.stop_reason, + "usage": dict(response.usage or {}), + } + ) + + return {"replies": completions, "meta": [meta]} diff --git a/integrations/anthropic/tests/__init__.py b/integrations/anthropic/tests/__init__.py new file mode 100644 index 000000000..e873bc332 --- /dev/null +++ b/integrations/anthropic/tests/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 diff --git a/integrations/anthropic/tests/conftest.py b/integrations/anthropic/tests/conftest.py new file mode 100644 index 000000000..e70223143 --- /dev/null +++ b/integrations/anthropic/tests/conftest.py @@ -0,0 +1,23 @@ +from unittest.mock import patch + +import pytest +from anthropic.types import Message + + +@pytest.fixture +def mock_chat_completion(): + """ + Mock the OpenAI API completion response and reuse it for tests + """ + with patch("anthropic.resources.messages.Messages.create") as mock_chat_completion_create: + completion = Message( + id="foo", + content=[{"type": "text", "text": "Hello, world!"}], + model="claude-3-sonnet-20240229", + role="assistant", + type="message", + usage={"input_tokens": 57, "output_tokens": 40}, + ) + + mock_chat_completion_create.return_value = completion + yield mock_chat_completion_create diff --git a/integrations/anthropic/tests/test_chat_generator.py b/integrations/anthropic/tests/test_chat_generator.py new file mode 100644 index 000000000..41cc3eb5d --- /dev/null +++ b/integrations/anthropic/tests/test_chat_generator.py @@ -0,0 +1,218 @@ +import os + +import anthropic +import pytest +from haystack.components.generators.utils import print_streaming_chunk +from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk +from haystack.utils.auth import Secret + +from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator + + +@pytest.fixture +def chat_messages(): + return [ + ChatMessage.from_system("\\nYou are a helpful assistant, be super brief in your responses."), + ChatMessage.from_user("What's the capital of France?"), + ] + + +class TestAnthropicChatGenerator: + def test_init_default(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicChatGenerator() + assert component.client.api_key == "test-api-key" + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is None + assert not component.generation_kwargs + + def test_init_fail_wo_api_key(self, monkeypatch): + monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) + with pytest.raises(ValueError, match="None of the .* environment variables are set"): + AnthropicChatGenerator() + + def test_init_with_parameters(self): + component = AnthropicChatGenerator( + api_key=Secret.from_token("test-api-key"), + model="claude-3-sonnet-20240229", + streaming_callback=print_streaming_chunk, + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + assert component.client.api_key == "test-api-key" + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is print_streaming_chunk + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} + + def test_to_dict_default(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicChatGenerator() + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": None, + "generation_kwargs": {}, + }, + } + + def test_to_dict_with_parameters(self, monkeypatch): + monkeypatch.setenv("ENV_VAR", "test-api-key") + component = AnthropicChatGenerator( + api_key=Secret.from_env_var("ENV_VAR"), + streaming_callback=print_streaming_chunk, + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + + def test_to_dict_with_lambda_streaming_callback(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicChatGenerator( + model="claude-3-sonnet-20240229", + streaming_callback=lambda x: x, + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": "tests.test_chat_generator.", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + + def test_from_dict(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "fake-api-key") + data = { + "type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + component = AnthropicChatGenerator.from_dict(data) + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is print_streaming_chunk + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} + assert component.api_key == Secret.from_env_var("ANTHROPIC_API_KEY") + + def test_from_dict_fail_wo_env_var(self, monkeypatch): + monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) + data = { + "type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + with pytest.raises(ValueError, match="None of the .* environment variables are set"): + AnthropicChatGenerator.from_dict(data) + + def test_run(self, chat_messages, mock_chat_completion): + component = AnthropicChatGenerator(api_key=Secret.from_token("test-api-key")) + response = component.run(chat_messages) + + # check that the component returns the correct ChatMessage response + assert isinstance(response, dict) + assert "replies" in response + assert isinstance(response["replies"], list) + assert len(response["replies"]) == 1 + assert [isinstance(reply, ChatMessage) for reply in response["replies"]] + + def test_run_with_params(self, chat_messages, mock_chat_completion): + component = AnthropicChatGenerator( + api_key=Secret.from_token("test-api-key"), generation_kwargs={"max_tokens": 10, "temperature": 0.5} + ) + response = component.run(chat_messages) + + # check that the component calls the Anthropic API with the correct parameters + _, kwargs = mock_chat_completion.call_args + assert kwargs["max_tokens"] == 10 + assert kwargs["temperature"] == 0.5 + + # check that the component returns the correct response + assert isinstance(response, dict) + assert "replies" in response + assert isinstance(response["replies"], list) + assert len(response["replies"]) == 1 + assert [isinstance(reply, ChatMessage) for reply in response["replies"]] + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_live_run_wrong_model(self, chat_messages): + component = AnthropicChatGenerator(model="something-obviously-wrong") + with pytest.raises(anthropic.NotFoundError): + component.run(chat_messages) + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_default_inference_params(self, chat_messages): + client = AnthropicChatGenerator() + response = client.run(chat_messages) + + assert "replies" in response, "Response does not contain 'replies' key" + replies = response["replies"] + assert isinstance(replies, list), "Replies is not a list" + assert len(replies) > 0, "No replies received" + + first_reply = replies[0] + assert isinstance(first_reply, ChatMessage), "First reply is not a ChatMessage instance" + assert first_reply.content, "First reply has no content" + assert ChatMessage.is_from(first_reply, ChatRole.ASSISTANT), "First reply is not from the assistant" + assert "paris" in first_reply.content.lower(), "First reply does not contain 'paris'" + assert first_reply.meta, "First reply has no metadata" + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_default_inference_with_streaming(self, chat_messages): + streaming_callback_called = False + paris_found_in_response = False + + def streaming_callback(chunk: StreamingChunk): + nonlocal streaming_callback_called, paris_found_in_response + streaming_callback_called = True + assert isinstance(chunk, StreamingChunk) + assert chunk.content is not None + if not paris_found_in_response: + paris_found_in_response = "paris" in chunk.content.lower() + + client = AnthropicChatGenerator(streaming_callback=streaming_callback) + response = client.run(chat_messages) + + assert streaming_callback_called, "Streaming callback was not called" + assert paris_found_in_response, "The streaming callback response did not contain 'paris'" + replies = response["replies"] + assert isinstance(replies, list), "Replies is not a list" + assert len(replies) > 0, "No replies received" + + first_reply = replies[0] + assert isinstance(first_reply, ChatMessage), "First reply is not a ChatMessage instance" + assert first_reply.content, "First reply has no content" + assert ChatMessage.is_from(first_reply, ChatRole.ASSISTANT), "First reply is not from the assistant" + assert "paris" in first_reply.content.lower(), "First reply does not contain 'paris'" + assert first_reply.meta, "First reply has no metadata" diff --git a/integrations/anthropic/tests/test_generator.py b/integrations/anthropic/tests/test_generator.py new file mode 100644 index 000000000..029cd3920 --- /dev/null +++ b/integrations/anthropic/tests/test_generator.py @@ -0,0 +1,227 @@ +import os + +import anthropic +import pytest +from haystack.components.generators.utils import print_streaming_chunk +from haystack.dataclasses import StreamingChunk +from haystack.utils.auth import Secret + +from haystack_integrations.components.generators.anthropic import AnthropicGenerator + + +class TestAnthropicGenerator: + def test_init_default(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicGenerator() + assert component.client.api_key == "test-api-key" + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is None + assert not component.generation_kwargs + + def test_init_fail_wo_api_key(self, monkeypatch): + monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) + with pytest.raises(ValueError, match="None of the .* environment variables are set"): + AnthropicGenerator() + + def test_init_with_parameters(self): + component = AnthropicGenerator( + api_key=Secret.from_token("test-api-key"), + model="claude-3-sonnet-20240229", + streaming_callback=print_streaming_chunk, + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + assert component.client.api_key == "test-api-key" + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is print_streaming_chunk + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} + + def test_to_dict_default(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicGenerator() + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.generator.AnthropicGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": None, + "system_prompt": None, + "generation_kwargs": {}, + }, + } + + def test_to_dict_with_parameters(self, monkeypatch): + monkeypatch.setenv("ENV_VAR", "test-api-key") + component = AnthropicGenerator( + api_key=Secret.from_env_var("ENV_VAR"), + streaming_callback=print_streaming_chunk, + system_prompt="test-prompt", + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.generator.AnthropicGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "system_prompt": "test-prompt", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + + def test_to_dict_with_lambda_streaming_callback(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") + component = AnthropicGenerator( + model="claude-3-sonnet-20240229", + streaming_callback=lambda x: x, + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, + ) + data = component.to_dict() + assert data == { + "type": "haystack_integrations.components.generators.anthropic.generator.AnthropicGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "streaming_callback": "tests.test_generator.", + "system_prompt": None, + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + + def test_from_dict(self, monkeypatch): + monkeypatch.setenv("ANTHROPIC_API_KEY", "fake-api-key") + data = { + "type": "haystack_integrations.components.generators.anthropic.generator.AnthropicGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "system_prompt": "test-prompt", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + component = AnthropicGenerator.from_dict(data) + assert component.model == "claude-3-sonnet-20240229" + assert component.streaming_callback is print_streaming_chunk + assert component.system_prompt == "test-prompt" + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} + assert component.api_key == Secret.from_env_var("ANTHROPIC_API_KEY") + + def test_from_dict_fail_wo_env_var(self, monkeypatch): + monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) + data = { + "type": "haystack_integrations.components.generators.anthropic.generator.AnthropicGenerator", + "init_parameters": { + "api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"}, + "model": "claude-3-sonnet-20240229", + "system_prompt": "test-prompt", + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, + }, + } + with pytest.raises(ValueError, match="None of the .* environment variables are set"): + AnthropicGenerator.from_dict(data) + + def test_run(self, mock_chat_completion): + component = AnthropicGenerator(api_key=Secret.from_token("test-api-key")) + response = component.run("What is the capital of France?") + + # check that the component returns the correct ChatMessage response + assert isinstance(response, dict) + assert "replies" in response + assert "meta" in response + assert isinstance(response["replies"], list) + assert isinstance(response["meta"], list) + assert len(response["replies"]) == 1 + assert len(response["meta"]) == 1 + assert [isinstance(reply, str) for reply in response["replies"]] + assert [isinstance(meta, dict) for meta in response["meta"]] + + def test_run_with_params(self, mock_chat_completion): + component = AnthropicGenerator( + api_key=Secret.from_token("test-api-key"), generation_kwargs={"max_tokens": 10, "temperature": 0.5} + ) + response = component.run("What is the capital of France?") + + # check that the component calls the Anthropic API with the correct parameters + _, kwargs = mock_chat_completion.call_args + assert kwargs["max_tokens"] == 10 + assert kwargs["temperature"] == 0.5 + + # check that the component returns the correct response + assert isinstance(response, dict) + assert "replies" in response + assert isinstance(response["replies"], list) + assert len(response["replies"]) == 1 + assert [isinstance(reply, str) for reply in response["replies"]] + assert "meta" in response + assert isinstance(response["meta"], list) + assert len(response["meta"]) == 1 + assert [isinstance(meta, dict) for meta in response["meta"]] + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_live_run_wrong_model(self): + component = AnthropicGenerator(model="something-obviously-wrong") + with pytest.raises(anthropic.NotFoundError): + component.run("What is the capital of France?") + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_default_inference_params(self): + client = AnthropicGenerator() + response = client.run("What is the capital of France?") + + assert "replies" in response, "Response does not contain 'replies' key" + replies = response["replies"] + assert isinstance(replies, list), "Replies is not a list" + assert len(replies) > 0, "No replies received" + + first_reply = replies[0] + assert isinstance(first_reply, str), "First reply is not a str instance" + assert first_reply, "First reply has no content" + assert "paris" in first_reply.lower(), "First reply does not contain 'paris'" + + assert "meta" in response, "Response does not contain 'meta' key" + meta = response["meta"] + assert isinstance(meta, list), "Meta is not a list" + assert len(meta) > 0, "No meta received" + assert isinstance(meta[0], dict), "First meta is not a dict instance" + + @pytest.mark.skipif( + not os.environ.get("ANTHROPIC_API_KEY", None), + reason="Export an env var called ANTHROPIC_API_KEY containing the Anthropic API key to run this test.", + ) + @pytest.mark.integration + def test_default_inference_with_streaming(self): + streaming_callback_called = False + paris_found_in_response = False + + def streaming_callback(chunk: StreamingChunk): + nonlocal streaming_callback_called, paris_found_in_response + streaming_callback_called = True + assert isinstance(chunk, StreamingChunk) + assert chunk.content is not None + if not paris_found_in_response: + paris_found_in_response = "paris" in chunk.content.lower() + + client = AnthropicGenerator(streaming_callback=streaming_callback) + response = client.run("What is the capital of France?") + + assert streaming_callback_called, "Streaming callback was not called" + assert paris_found_in_response, "The streaming callback response did not contain 'paris'" + replies = response["replies"] + assert isinstance(replies, list), "Replies is not a list" + assert len(replies) > 0, "No replies received" + + first_reply = replies[0] + assert isinstance(first_reply, str), "First reply is not a str instance" + assert first_reply, "First reply has no content" + assert "paris" in first_reply.lower(), "First reply does not contain 'paris'" From 9033dab9a4c2d59c9905b58304543edf8c7ca22b Mon Sep 17 00:00:00 2001 From: Julian Risch Date: Fri, 15 Mar 2024 15:05:59 +0100 Subject: [PATCH 06/10] replace amazon-bedrock with anthropic in readme (#584) --- integrations/anthropic/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/anthropic/README.md b/integrations/anthropic/README.md index 316d327aa..2ed55d4af 100644 --- a/integrations/anthropic/README.md +++ b/integrations/anthropic/README.md @@ -1,7 +1,7 @@ # anthropic-haystack -[![PyPI - Version](https://img.shields.io/pypi/v/amazon-bedrock-haystack.svg)](https://pypi.org/project/amazon-bedrock-haystack) -[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/amazon-bedrock-haystack.svg)](https://pypi.org/project/amazon-bedrock-haystack) +[![PyPI - Version](https://img.shields.io/pypi/v/anthropic-haystack.svg)](https://pypi.org/project/anthropic-haystack) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/anthropic-haystack.svg)](https://pypi.org/project/anthropic-haystack) ----- From f4730e5f536d0d443112a85101a8c380fd0c4f78 Mon Sep 17 00:00:00 2001 From: Julian Risch Date: Fri, 15 Mar 2024 16:05:26 +0100 Subject: [PATCH 07/10] docs: Add anthropic integration to inventory and sort alphabetically (#585) --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2db267f6b..50a4bebc4 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,10 @@ Please check out our [Contribution Guidelines](CONTRIBUTING.md) for all the deta | Package | Type | PyPi Package | Status | | ------------------------------------------------------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [astra-haystack](integrations/astra/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/astra-haystack.svg)](https://pypi.org/project/astra-haystack) | [![Test / astra](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/astra.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/astra.yml) | | [amazon-bedrock-haystack](integrations/amazon-bedrock/) | Generator | [![PyPI - Version](https://img.shields.io/pypi/v/amazon-bedrock-haystack.svg)](https://pypi.org/project/amazon-bedrock-haystack) | [![Test / amazon_bedrock](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/amazon_bedrock.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/amazon_bedrock.yml) | | [amazon-sagemaker-haystack](integrations/amazon_sagemaker/) | Generator | [![PyPI - Version](https://img.shields.io/pypi/v/amazon-sagemaker-haystack.svg)](https://pypi.org/project/amazon-sagemaker-haystack) | [![Test / amazon_sagemaker](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/amazon_sagemaker.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/amazon_sagemaker.yml) | +| [anthropic-haystack](integrations/anthropic/) | Generator | [![PyPI - Version](https://img.shields.io/pypi/v/anthropic-haystack.svg)](https://pypi.org/project/anthropic-haystack) | [![Test / anthropic](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/anthropic.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/anthropic.yml) | +| [astra-haystack](integrations/astra/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/astra-haystack.svg)](https://pypi.org/project/astra-haystack) | [![Test / astra](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/astra.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/astra.yml) | | [chroma-haystack](integrations/chroma/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/chroma-haystack.svg)](https://pypi.org/project/chroma-haystack) | [![Test / chroma](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/chroma.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/chroma.yml) | | [cohere-haystack](integrations/cohere/) | Embedder, Generator | [![PyPI - Version](https://img.shields.io/pypi/v/cohere-haystack.svg)](https://pypi.org/project/cohere-haystack) | [![Test / cohere](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/cohere.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/cohere.yml) | | [deepeval-haystack](integrations/deepeval/) | Evaluator | [![PyPI - Version](https://img.shields.io/pypi/v/deepeval-haystack.svg)](https://pypi.org/project/deepeval-haystack) | [![Test / deepeval](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/deepeval.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/deepeval.yml) | From d2780c9d4b4ee757b41c28b9835f4ea0d3578631 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Fri, 15 Mar 2024 16:06:41 +0100 Subject: [PATCH 08/10] docs: fix docstrings (#586) * fix docstrings for retrievers * document store * fix test * actual fix --- .../retrievers/weaviate/bm25_retriever.py | 45 ++++++++++++-- .../weaviate/embedding_retriever.py | 61 ++++++++++++++++--- .../weaviate/document_store.py | 49 ++++++++++----- .../tests/test_embedding_retriever.py | 4 +- 4 files changed, 130 insertions(+), 29 deletions(-) diff --git a/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/bm25_retriever.py b/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/bm25_retriever.py index c37312604..6deef5eb6 100644 --- a/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/bm25_retriever.py +++ b/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/bm25_retriever.py @@ -7,7 +7,17 @@ @component class WeaviateBM25Retriever: """ - Retriever that uses BM25 to find the most promising documents for a given query. + A component for retrieving documents from Weaviate using the BM25 algorithm. + + Example usage: + ```python + from haystack_integrations.document_stores.weaviate.document_store import WeaviateDocumentStore + from haystack_integrations.components.retrievers.weaviate.bm25_retriever import WeaviateBM25Retriever + + document_store = WeaviateDocumentStore(url="http://localhost:8080") + retriever = WeaviateBM25Retriever(document_store=document_store) + retriever.run(query="How to make a pizza", top_k=3) + ``` """ def __init__( @@ -20,15 +30,24 @@ def __init__( """ Create a new instance of WeaviateBM25Retriever. - :param document_store: Instance of WeaviateDocumentStore that will be associated with this retriever. - :param filters: Custom filters applied when running the retriever, defaults to None - :param top_k: Maximum number of documents to return, defaults to 10 + :param document_store: + Instance of WeaviateDocumentStore that will be used from this retriever. + :param filters: + Custom filters applied when running the retriever + :param top_k: + Maximum number of documents to return """ self._document_store = document_store self._filters = filters or {} self._top_k = top_k def to_dict(self) -> Dict[str, Any]: + """ + Serializes the component to a dictionary. + + :returns: + Dictionary with serialized data. + """ return default_to_dict( self, filters=self._filters, @@ -38,6 +57,14 @@ def to_dict(self) -> Dict[str, Any]: @classmethod def from_dict(cls, data: Dict[str, Any]) -> "WeaviateBM25Retriever": + """ + Deserializes the component from a dictionary. + + :param data: + Dictionary to deserialize from. + :returns: + Deserialized component. + """ data["init_parameters"]["document_store"] = WeaviateDocumentStore.from_dict( data["init_parameters"]["document_store"] ) @@ -45,6 +72,16 @@ def from_dict(cls, data: Dict[str, Any]) -> "WeaviateBM25Retriever": @component.output_types(documents=List[Document]) def run(self, query: str, filters: Optional[Dict[str, Any]] = None, top_k: Optional[int] = None): + """ + Retrieves documents from Weaviate using the BM25 algorithm. + + :param query: + The query text. + :param filters: + Filters to use when running the retriever. + :param top_k: + The maximum number of documents to return. + """ filters = filters or self._filters top_k = top_k or self._top_k documents = self._document_store._bm25_retrieval(query=query, filters=filters, top_k=top_k) diff --git a/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/embedding_retriever.py b/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/embedding_retriever.py index 38f7cd85f..cdf578fee 100644 --- a/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/embedding_retriever.py +++ b/integrations/weaviate/src/haystack_integrations/components/retrievers/weaviate/embedding_retriever.py @@ -20,16 +20,22 @@ def __init__( certainty: Optional[float] = None, ): """ - Create a new instance of WeaviateEmbeddingRetriever. - Raises ValueError if both `distance` and `certainty` are provided. - See the official Weaviate documentation to learn more about the `distance` and `certainty` parameters: - https://weaviate.io/developers/weaviate/api/graphql/search-operators#variables + Creates a new instance of WeaviateEmbeddingRetriever. - :param document_store: Instance of WeaviateDocumentStore that will be associated with this retriever. - :param filters: Custom filters applied when running the retriever, defaults to None - :param top_k: Maximum number of documents to return, defaults to 10 - :param distance: The maximum allowed distance between Documents' embeddings, defaults to None - :param certainty: Normalized distance between the result item and the search vector, defaults to None + :param document_store: + Instance of WeaviateDocumentStore that will be used from this retriever. + :param filters: + Custom filters applied when running the retriever. + :param top_k: + Maximum number of documents to return. + :param distance: + The maximum allowed distance between Documents' embeddings. + :param certainty: + Normalized distance between the result item and the search vector. + :raises ValueError: + If both `distance` and `certainty` are provided. + See https://weaviate.io/developers/weaviate/api/graphql/search-operators#variables to learn more about + `distance` and `certainty` parameters. """ if distance is not None and certainty is not None: msg = "Can't use 'distance' and 'certainty' parameters together" @@ -42,6 +48,12 @@ def __init__( self._certainty = certainty def to_dict(self) -> Dict[str, Any]: + """ + Serializes the component to a dictionary. + + :returns: + Dictionary with serialized data. + """ return default_to_dict( self, filters=self._filters, @@ -53,6 +65,14 @@ def to_dict(self) -> Dict[str, Any]: @classmethod def from_dict(cls, data: Dict[str, Any]) -> "WeaviateEmbeddingRetriever": + """ + Deserializes the component from a dictionary. + + :param data: + Dictionary to deserialize from. + :returns: + Deserialized component. + """ data["init_parameters"]["document_store"] = WeaviateDocumentStore.from_dict( data["init_parameters"]["document_store"] ) @@ -67,10 +87,33 @@ def run( distance: Optional[float] = None, certainty: Optional[float] = None, ): + """ + Retrieves documents from Weaviate using the vector search. + + :param query_embedding: + Embedding of the query. + :param filters: + Filters to use when running the retriever. + :param top_k: + The maximum number of documents to return. + :param distance: + The maximum allowed distance between Documents' embeddings. + :param certainty: + Normalized distance between the result item and the search vector. + :raises ValueError: + If both `distance` and `certainty` are provided. + See https://weaviate.io/developers/weaviate/api/graphql/search-operators#variables to learn more about + `distance` and `certainty` parameters. + """ filters = filters or self._filters top_k = top_k or self._top_k + distance = distance or self._distance certainty = certainty or self._certainty + if distance is not None and certainty is not None: + msg = "Can't use 'distance' and 'certainty' parameters together" + raise ValueError(msg) + documents = self._document_store._embedding_retrieval( query_embedding=query_embedding, filters=filters, diff --git a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py index 34fefa0a5..e7d97ae32 100644 --- a/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py +++ b/integrations/weaviate/src/haystack_integrations/document_stores/weaviate/document_store.py @@ -76,9 +76,11 @@ def __init__( """ Create a new instance of WeaviateDocumentStore and connects to the Weaviate instance. - :param url: The URL to the weaviate instance, defaults to None. - :param collection_settings: The collection settings to use, defaults to None. - If None it will use a collection named `default` with the following properties: + :param url: + The URL to the weaviate instance. + :param collection_settings: + The collection settings to use. If `None`, it will use a collection named `default` with the following + properties: - _original_id: text - content: text - dataframe: text @@ -93,22 +95,27 @@ def __init__( production use. See the official `Weaviate documentation`_ for more information on collections and their properties. - :param auth_client_secret: Authentication credentials, defaults to None. - Can be one of the following types depending on the authentication mode: + :param auth_client_secret: + Authentication credentials. Can be one of the following types depending on the authentication mode: - `AuthBearerToken` to use existing access and (optionally, but recommended) refresh tokens - `AuthClientPassword` to use username and password for oidc Resource Owner Password flow - `AuthClientCredentials` to use a client secret for oidc client credential flow - `AuthApiKey` to use an API key - :param additional_headers: Additional headers to include in the requests, defaults to None. - Can be used to set OpenAI/HuggingFace keys. OpenAI/HuggingFace key looks like this: + :param additional_headers: + Additional headers to include in the requests. Can be used to set OpenAI/HuggingFace keys. + OpenAI/HuggingFace key looks like this: ``` {"X-OpenAI-Api-Key": ""}, {"X-HuggingFace-Api-Key": ""} ``` - :param embedded_options: If set create an embedded Weaviate cluster inside the client, defaults to None. - For a full list of options see `weaviate.embedded.EmbeddedOptions`. - :param additional_config: Additional and advanced configuration options for weaviate, defaults to None. - :param grpc_port: The port to use for the gRPC connection, defaults to 50051. - :param grpc_secure: Whether to use a secure channel for the underlying gRPC API. + :param embedded_options: + If set, create an embedded Weaviate cluster inside the client. For a full list of options see + `weaviate.embedded.EmbeddedOptions`. + :param additional_config: + Additional and advanced configuration options for weaviate. + :param grpc_port: + The port to use for the gRPC connection. + :param grpc_secure: + Whether to use a secure channel for the underlying gRPC API. """ # proxies, timeout_config, trust_env are part of additional_config now # startup_period has been removed @@ -153,6 +160,12 @@ def __init__( self._collection = self._client.collections.get(collection_settings["class"]) def to_dict(self) -> Dict[str, Any]: + """ + Serializes the component to a dictionary. + + :returns: + Dictionary with serialized data. + """ embedded_options = asdict(self._embedded_options) if self._embedded_options else None additional_config = ( json.loads(self._additional_config.model_dump_json(by_alias=True)) if self._additional_config else None @@ -170,6 +183,14 @@ def to_dict(self) -> Dict[str, Any]: @classmethod def from_dict(cls, data: Dict[str, Any]) -> "WeaviateDocumentStore": + """ + Deserializes the component from a dictionary. + + :param data: + Dictionary to deserialize from. + :returns: + Deserialized component. + """ if (auth_client_secret := data["init_parameters"].get("auth_client_secret")) is not None: data["init_parameters"]["auth_client_secret"] = AuthCredentials.from_dict(auth_client_secret) if (embedded_options := data["init_parameters"].get("embedded_options")) is not None: @@ -187,7 +208,7 @@ def count_documents(self) -> int: def _to_data_object(self, document: Document) -> Dict[str, Any]: """ - Convert a Document to a Weaviate data object ready to be saved. + Converts a Document to a Weaviate data object ready to be saved. """ data = document.to_dict() # Weaviate forces a UUID as an id. @@ -207,7 +228,7 @@ def _to_data_object(self, document: Document) -> Dict[str, Any]: def _to_document(self, data: DataObject[Dict[str, Any], None]) -> Document: """ - Convert a data object read from Weaviate into a Document. + Converts a data object read from Weaviate into a Document. """ document_data = data.properties document_data["id"] = document_data.pop("_original_id") diff --git a/integrations/weaviate/tests/test_embedding_retriever.py b/integrations/weaviate/tests/test_embedding_retriever.py index a406c40db..c7c147ba5 100644 --- a/integrations/weaviate/tests/test_embedding_retriever.py +++ b/integrations/weaviate/tests/test_embedding_retriever.py @@ -105,7 +105,7 @@ def test_run(mock_document_store): retriever = WeaviateEmbeddingRetriever(document_store=mock_document_store) query_embedding = [0.1, 0.1, 0.1, 0.1] filters = {"field": "content", "operator": "==", "value": "Some text"} - retriever.run(query_embedding=query_embedding, filters=filters, top_k=5, distance=0.1, certainty=0.1) + retriever.run(query_embedding=query_embedding, filters=filters, top_k=5, distance=0.1) mock_document_store._embedding_retrieval.assert_called_once_with( - query_embedding=query_embedding, filters=filters, top_k=5, distance=0.1, certainty=0.1 + query_embedding=query_embedding, filters=filters, top_k=5, distance=0.1, certainty=None ) From bf2a4af2b9f73c37d3d25f8ec4cb39c975440d5d Mon Sep 17 00:00:00 2001 From: Vladimir Blagojevic Date: Fri, 15 Mar 2024 16:26:55 +0100 Subject: [PATCH 09/10] Use the correct sonnet model name (#587) --- integrations/anthropic/example/documentation_rag_with_claude.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/anthropic/example/documentation_rag_with_claude.py b/integrations/anthropic/example/documentation_rag_with_claude.py index a3cc452ad..98cc9d40e 100644 --- a/integrations/anthropic/example/documentation_rag_with_claude.py +++ b/integrations/anthropic/example/documentation_rag_with_claude.py @@ -23,7 +23,7 @@ "llm", AnthropicChatGenerator( api_key=Secret.from_env_var("ANTHROPIC_API_KEY"), - model="claude-3-sonnet", + model="claude-3-sonnet-20240229", streaming_callback=print_streaming_chunk, ), ) From b7d61fd73ba0810ed41382790076811fde1e87d3 Mon Sep 17 00:00:00 2001 From: Stefano Fiorucci Date: Fri, 15 Mar 2024 18:52:19 +0100 Subject: [PATCH 10/10] Nightly - run unit tests with Haystack main branch (#580) * first refactorings * squash * retry * remove quotes * try failure * another try * try notification * progress * update all integrations * run CI * retry fastembed * update all integrations * add missing generate docs to cohere * remove one notification step * retry * fix wrong syntax * try a better workflow * another try * ternary op * make it work * rm condition --- .github/workflows/amazon_bedrock.yml | 18 ++++++++++++++++++ .github/workflows/amazon_sagemaker.yml | 18 ++++++++++++++++++ .github/workflows/anthropic.yml | 16 +++++++++++++--- .github/workflows/astra.yml | 15 +++++++++++++-- .github/workflows/chroma.yml | 15 +++++++++++++-- .github/workflows/cohere.yml | 19 +++++++++++++++++-- .github/workflows/deepeval.yml | 17 ++++++++++++++--- .github/workflows/elasticsearch.yml | 17 ++++++++++++++--- .github/workflows/fastembed.yml | 17 ++++++++++++++--- .github/workflows/google_ai.yml | 17 ++++++++++++++--- .github/workflows/google_vertex.yml | 17 ++++++++++++++--- .github/workflows/gradient.yml | 15 +++++++++++++-- .github/workflows/instructor_embedders.yml | 17 ++++++++++++++--- .github/workflows/jina.yml | 17 ++++++++++++++--- .github/workflows/llama_cpp.yml | 15 +++++++++++++-- .github/workflows/mistral.yml | 17 ++++++++++++++--- .github/workflows/mongodb_atlas.yml | 18 ++++++++++++++---- .github/workflows/nvidia.yml | 17 ++++++++++++++--- .github/workflows/ollama.yml | 17 ++++++++++++++--- .github/workflows/opensearch.yml | 18 ++++++++++++++---- .github/workflows/optimum.yml | 17 ++++++++++++++--- .github/workflows/pgvector.yml | 17 ++++++++++++++--- .github/workflows/pinecone.yml | 18 ++++++++++++++---- .github/workflows/qdrant.yml | 17 ++++++++++++++--- .github/workflows/ragas.yml | 17 ++++++++++++++--- .github/workflows/unstructured.yml | 19 +++++++++++++++---- .github/workflows/uptrain.yml | 17 ++++++++++++++--- .github/workflows/weaviate.yml | 15 +++++++++++++-- 28 files changed, 398 insertions(+), 76 deletions(-) diff --git a/.github/workflows/amazon_bedrock.yml b/.github/workflows/amazon_bedrock.yml index 8b1651764..fb2a14c5e 100644 --- a/.github/workflows/amazon_bedrock.yml +++ b/.github/workflows/amazon_bedrock.yml @@ -68,4 +68,22 @@ jobs: role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }} - name: Run tests + id: tests run: hatch run cov + + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + + - name: Send event to Datadog for nightly failures + if: failure() && github.event_name == 'schedule' + uses: ./.github/actions/send_failure + with: + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/amazon_sagemaker.yml b/.github/workflows/amazon_sagemaker.yml index a32590b14..2f9106181 100644 --- a/.github/workflows/amazon_sagemaker.yml +++ b/.github/workflows/amazon_sagemaker.yml @@ -57,4 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + + - name: Send event to Datadog for nightly failures + if: failure() && github.event_name == 'schedule' + uses: ./.github/actions/send_failure + with: + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/anthropic.yml b/.github/workflows/anthropic.yml index 755660bfc..db4c4ce18 100644 --- a/.github/workflows/anthropic.yml +++ b/.github/workflows/anthropic.yml @@ -56,9 +56,19 @@ jobs: - name: Run tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/astra.yml b/.github/workflows/astra.yml index e90edc2e9..30d6e8796 100644 --- a/.github/workflows/astra.yml +++ b/.github/workflows/astra.yml @@ -61,11 +61,22 @@ jobs: env: ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_API_ENDPOINT }} ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_TOKEN }} + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/chroma.yml b/.github/workflows/chroma.yml index e6712d807..616fecf3b 100644 --- a/.github/workflows/chroma.yml +++ b/.github/workflows/chroma.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/cohere.yml b/.github/workflows/cohere.yml index 6f23760a0..81bf2356d 100644 --- a/.github/workflows/cohere.yml +++ b/.github/workflows/cohere.yml @@ -53,12 +53,27 @@ jobs: if: matrix.python-version == '3.9' && runner.os == 'Linux' run: hatch run lint:all + - name: Generate docs + if: matrix.python-version == '3.9' && runner.os == 'Linux' + run: hatch run docs + - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/deepeval.yml b/.github/workflows/deepeval.yml index a9efc2f3a..f290125cb 100644 --- a/.github/workflows/deepeval.yml +++ b/.github/workflows/deepeval.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/elasticsearch.yml b/.github/workflows/elasticsearch.yml index 21efcbc34..c0144a202 100644 --- a/.github/workflows/elasticsearch.yml +++ b/.github/workflows/elasticsearch.yml @@ -55,11 +55,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/fastembed.yml b/.github/workflows/fastembed.yml index a2b076c1a..37ee7f953 100644 --- a/.github/workflows/fastembed.yml +++ b/.github/workflows/fastembed.yml @@ -42,11 +42,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: failure() #github.event_name == 'schedule' && + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/google_ai.yml b/.github/workflows/google_ai.yml index 9efeb8590..853bc91df 100644 --- a/.github/workflows/google_ai.yml +++ b/.github/workflows/google_ai.yml @@ -58,11 +58,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/google_vertex.yml b/.github/workflows/google_vertex.yml index 03890ed4a..3677102e4 100644 --- a/.github/workflows/google_vertex.yml +++ b/.github/workflows/google_vertex.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/gradient.yml b/.github/workflows/gradient.yml index 8fbaf6f18..65d206dd8 100644 --- a/.github/workflows/gradient.yml +++ b/.github/workflows/gradient.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/instructor_embedders.yml b/.github/workflows/instructor_embedders.yml index 5282c8e18..70b604eaa 100644 --- a/.github/workflows/instructor_embedders.yml +++ b/.github/workflows/instructor_embedders.yml @@ -35,11 +35,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/jina.yml b/.github/workflows/jina.yml index 1ab0e2a2b..69ef6b294 100644 --- a/.github/workflows/jina.yml +++ b/.github/workflows/jina.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/llama_cpp.yml b/.github/workflows/llama_cpp.yml index 712e91fa2..5d906a251 100644 --- a/.github/workflows/llama_cpp.yml +++ b/.github/workflows/llama_cpp.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }} diff --git a/.github/workflows/mistral.yml b/.github/workflows/mistral.yml index 029bb974a..8348f1d8f 100644 --- a/.github/workflows/mistral.yml +++ b/.github/workflows/mistral.yml @@ -58,11 +58,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/mongodb_atlas.yml b/.github/workflows/mongodb_atlas.yml index bf48a75c2..94a540719 100644 --- a/.github/workflows/mongodb_atlas.yml +++ b/.github/workflows/mongodb_atlas.yml @@ -54,12 +54,22 @@ jobs: run: hatch run docs - name: Run tests - working-directory: integrations/mongodb_atlas + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/nvidia.yml b/.github/workflows/nvidia.yml index 8b6ec030a..eef8e0c31 100644 --- a/.github/workflows/nvidia.yml +++ b/.github/workflows/nvidia.yml @@ -54,11 +54,22 @@ jobs: run: hatch run lint:all - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/ollama.yml b/.github/workflows/ollama.yml index c977ba116..631c155eb 100644 --- a/.github/workflows/ollama.yml +++ b/.github/workflows/ollama.yml @@ -75,11 +75,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/opensearch.yml b/.github/workflows/opensearch.yml index da177b83c..ed7967d79 100644 --- a/.github/workflows/opensearch.yml +++ b/.github/workflows/opensearch.yml @@ -55,12 +55,22 @@ jobs: run: hatch run docs - name: Run tests - working-directory: integrations/opensearch + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/optimum.yml b/.github/workflows/optimum.yml index 077413920..c6ae9a0ee 100644 --- a/.github/workflows/optimum.yml +++ b/.github/workflows/optimum.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/pgvector.yml b/.github/workflows/pgvector.yml index 647f520e1..288da8e9d 100644 --- a/.github/workflows/pgvector.yml +++ b/.github/workflows/pgvector.yml @@ -61,11 +61,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/pinecone.yml b/.github/workflows/pinecone.yml index 49d421813..85f36ae5d 100644 --- a/.github/workflows/pinecone.yml +++ b/.github/workflows/pinecone.yml @@ -55,12 +55,22 @@ jobs: run: hatch run docs - name: Run tests - working-directory: integrations/pinecone + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/qdrant.yml b/.github/workflows/qdrant.yml index 3c72b0f02..5995911fb 100644 --- a/.github/workflows/qdrant.yml +++ b/.github/workflows/qdrant.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/ragas.yml b/.github/workflows/ragas.yml index d3def92ca..10600d17d 100644 --- a/.github/workflows/ragas.yml +++ b/.github/workflows/ragas.yml @@ -57,11 +57,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/unstructured.yml b/.github/workflows/unstructured.yml index b2778431c..a5943c5b5 100644 --- a/.github/workflows/unstructured.yml +++ b/.github/workflows/unstructured.yml @@ -68,12 +68,23 @@ jobs: if: matrix.python-version == '3.9' && runner.os == 'Linux' run: hatch run docs - - name: Run tests + - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/uptrain.yml b/.github/workflows/uptrain.yml index 64453b0fd..c0a805051 100644 --- a/.github/workflows/uptrain.yml +++ b/.github/workflows/uptrain.yml @@ -53,11 +53,22 @@ jobs: run: hatch run lint:all - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" - api-key: ${{ secrets.CORE_DATADOG_API_KEY }} + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} + api-key: ${{ secrets.CORE_DATADOG_API_KEY }} \ No newline at end of file diff --git a/.github/workflows/weaviate.yml b/.github/workflows/weaviate.yml index 69447b96b..2588d4113 100644 --- a/.github/workflows/weaviate.yml +++ b/.github/workflows/weaviate.yml @@ -54,11 +54,22 @@ jobs: run: hatch run docs - name: Run tests + id: tests run: hatch run cov + - name: Nightly - run unit tests with Haystack main branch + if: github.event_name == 'schedule' + id: nightly-haystack-main + run: | + hatch run pip install git+https://github.com/deepset-ai/haystack.git + hatch run test -m "not integration" + - name: Send event to Datadog for nightly failures - if: github.event_name == 'schedule' && failure() + if: failure() && github.event_name == 'schedule' uses: ./.github/actions/send_failure with: - title: "core-integrations nightly failure: ${{ github.workflow }}" + title: | + core-integrations failure: + ${{ (steps.tests.conclusion == 'nightly-haystack-main') && 'nightly-haystack-main' || 'tests' }} + - ${{ github.workflow }} api-key: ${{ secrets.CORE_DATADOG_API_KEY }}