From f7ae183f40ae8fc4fd55f440e76b3e81a1f77fb1 Mon Sep 17 00:00:00 2001 From: Alvaro Bartolome Date: Fri, 11 Aug 2023 18:37:06 +0200 Subject: [PATCH] `ArgillaCallbackHandler` to properly use default values for `api_url` and `api_key` (#9113) As of the recent PR at #9043, after some testing we've realised that the default values were not being used for `api_key` and `api_url`. Besides that, the default for `api_key` was set to `argilla.apikey`, but since the default values are intended for people using the Argilla Quickstart (easy to run and setup), the defaults should be instead `owner.apikey` if using Argilla 1.11.0 or higher, or `admin.apikey` if using a lower version of Argilla. Additionally, we've removed the f-string replacements from the docstrings. --------- Co-authored-by: Gabriel Martin --- .../integrations/callbacks/argilla.ipynb | 2 +- .../langchain/callbacks/argilla_callback.py | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/docs/extras/integrations/callbacks/argilla.ipynb b/docs/extras/integrations/callbacks/argilla.ipynb index 7a78b3198c..ff46507151 100644 --- a/docs/extras/integrations/callbacks/argilla.ipynb +++ b/docs/extras/integrations/callbacks/argilla.ipynb @@ -147,7 +147,7 @@ " api_key=os.environ[\"ARGILLA_API_KEY\"],\n", ")\n", "\n", - "dataset.push_to_argilla(\"langchain-dataset\")" + "dataset.push_to_argilla(\"langchain-dataset\");" ] }, { diff --git a/libs/langchain/langchain/callbacks/argilla_callback.py b/libs/langchain/langchain/callbacks/argilla_callback.py index 7ed9af15ba..9420fb46d0 100644 --- a/libs/langchain/langchain/callbacks/argilla_callback.py +++ b/libs/langchain/langchain/callbacks/argilla_callback.py @@ -21,11 +21,10 @@ class ArgillaCallbackHandler(BaseCallbackHandler): default workspace will be used. api_url: URL of the Argilla Server that we want to use, and where the `FeedbackDataset` lives in. Defaults to `None`, which means that either - `ARGILLA_API_URL` environment variable or the default http://localhost:6900 - will be used. + `ARGILLA_API_URL` environment variable or the default will be used. api_key: API Key to connect to the Argilla Server. Defaults to `None`, which means that either `ARGILLA_API_KEY` environment variable or the default - `argilla.apikey` will be used. + will be used. Raises: ImportError: if the `argilla` package is not installed. @@ -58,7 +57,6 @@ class ArgillaCallbackHandler(BaseCallbackHandler): BLOG_URL = "https://docs.argilla.io/en/latest/guides/llms/practical_guides/use_argilla_callback_in_langchain.html" # noqa: E501 DEFAULT_API_URL = "http://localhost:6900" - DEFAULT_API_KEY = "argilla.apikey" def __init__( self, @@ -67,22 +65,22 @@ class ArgillaCallbackHandler(BaseCallbackHandler): api_url: Optional[str] = None, api_key: Optional[str] = None, ) -> None: - f"""Initializes the `ArgillaCallbackHandler`. + """Initializes the `ArgillaCallbackHandler`. Args: dataset_name: name of the `FeedbackDataset` in Argilla. Note that it must exist in advance. If you need help on how to create a `FeedbackDataset` - in Argilla, please visit {self.BLOG_URL}. + in Argilla, please visit + https://docs.argilla.io/en/latest/guides/llms/practical_guides/use_argilla_callback_in_langchain.html. workspace_name: name of the workspace in Argilla where the specified `FeedbackDataset` lives in. Defaults to `None`, which means that the default workspace will be used. api_url: URL of the Argilla Server that we want to use, and where the `FeedbackDataset` lives in. Defaults to `None`, which means that either - `ARGILLA_API_URL` environment variable or `{self.DEFAULT_API_URL}` will - be used. + `ARGILLA_API_URL` environment variable or the default will be used. api_key: API Key to connect to the Argilla Server. Defaults to `None`, which means that either `ARGILLA_API_KEY` environment variable or the default - `{self.DEFAULT_API_KEY}` will be used. + will be used. Raises: ImportError: if the `argilla` package is not installed. @@ -116,16 +114,27 @@ class ArgillaCallbackHandler(BaseCallbackHandler): warnings.warn( ( "Since `api_url` is None, and the env var `ARGILLA_API_URL` is not" - f" set, it will default to `{self.DEFAULT_API_URL}`." + f" set, it will default to `{self.DEFAULT_API_URL}`, which is the" + " default API URL in Argilla Quickstart." ), ) + api_url = self.DEFAULT_API_URL + if api_key is None and os.getenv("ARGILLA_API_KEY") is None: + self.DEFAULT_API_KEY = ( + "admin.apikey" + if parse(self.ARGILLA_VERSION) < parse("1.11.0") + else "owner.apikey" + ) + warnings.warn( ( "Since `api_key` is None, and the env var `ARGILLA_API_KEY` is not" - f" set, it will default to `{self.DEFAULT_API_KEY}`." + f" set, it will default to `{self.DEFAULT_API_KEY}`, which is the" + " default API key in Argilla Quickstart." ), ) + api_url = self.DEFAULT_API_URL # Connect to Argilla with the provided credentials, if applicable try: