From e3d28358d179271955c3c3c56cac4f1a30f70a5b Mon Sep 17 00:00:00 2001 From: "David L. Qiu" Date: Fri, 21 Jun 2024 07:23:04 -0700 Subject: [PATCH 1/3] fix Cohere models by depending on langchain_cohere --- .../jupyter_ai_magics/__init__.py | 2 - .../jupyter_ai_magics/embedding_providers.py | 19 --------- .../partner_providers/cohere.py | 40 +++++++++++++++++++ .../jupyter_ai_magics/providers.py | 20 ---------- packages/jupyter-ai-magics/pyproject.toml | 12 +++--- 5 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/__init__.py b/packages/jupyter-ai-magics/jupyter_ai_magics/__init__.py index 7c609a606..d6ca65135 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/__init__.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/__init__.py @@ -4,7 +4,6 @@ from .embedding_providers import ( BaseEmbeddingsProvider, BedrockEmbeddingsProvider, - CohereEmbeddingsProvider, GPT4AllEmbeddingsProvider, HfHubEmbeddingsProvider, QianfanEmbeddingsEndpointProvider, @@ -22,7 +21,6 @@ BaseProvider, BedrockChatProvider, BedrockProvider, - CohereProvider, GPT4AllProvider, HfHubProvider, QianfanProvider, diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py index 3dea152e7..9f3bde6e9 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py @@ -10,10 +10,8 @@ from langchain.pydantic_v1 import BaseModel, Extra from langchain_community.embeddings import ( BedrockEmbeddings, - CohereEmbeddings, GPT4AllEmbeddings, HuggingFaceHubEmbeddings, - OpenAIEmbeddings, QianfanEmbeddingsEndpoint, ) @@ -68,23 +66,6 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs, **model_kwargs) -class CohereEmbeddingsProvider(BaseEmbeddingsProvider, CohereEmbeddings): - id = "cohere" - name = "Cohere" - models = [ - "embed-english-v2.0", - "embed-english-light-v2.0", - "embed-multilingual-v2.0", - "embed-english-v3.0", - "embed-english-light-v3.0", - "embed-multilingual-v3.0", - "embed-multilingual-light-v3.0", - ] - model_id_key = "model" - pypi_package_deps = ["cohere"] - auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY") - - class HfHubEmbeddingsProvider(BaseEmbeddingsProvider, HuggingFaceHubEmbeddings): id = "huggingface_hub" name = "Hugging Face Hub" diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py b/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py new file mode 100644 index 000000000..3286e8e35 --- /dev/null +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py @@ -0,0 +1,40 @@ +from langchain_cohere import ChatCohere, CohereEmbeddings + +from ..embedding_providers import BaseEmbeddingsProvider +from ..providers import BaseProvider, EnvAuthStrategy + + +class CohereProvider(BaseProvider, ChatCohere): + id = "cohere" + name = "Cohere" + # https://docs.cohere.com/docs/models + # note: This provider uses the Chat endpoint instead of the Generate + # endpoint, which is now deprecated. + models = [ + "command", + "command-nightly", + "command-light", + "command-light-nightly", + "command-r-plus", + "command-r", + ] + model_id_key = "model" + pypi_package_deps = ["cohere"] + auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY") + + +class CohereEmbeddingsProvider(BaseEmbeddingsProvider, CohereEmbeddings): + id = "cohere" + name = "Cohere" + models = [ + "embed-english-v2.0", + "embed-english-light-v2.0", + "embed-multilingual-v2.0", + "embed-english-v3.0", + "embed-english-light-v3.0", + "embed-multilingual-v3.0", + "embed-multilingual-light-v3.0", + ] + model_id_key = "model" + pypi_package_deps = ["cohere"] + auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY") diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py index 4cd39650b..c38569e66 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py @@ -546,26 +546,6 @@ def is_api_key_exc(cls, e: Exception): return False -class CohereProvider(BaseProvider, Cohere): - id = "cohere" - name = "Cohere" - # Source: https://docs.cohere.com/reference/generate; https://docs.cohere.com/docs/models - models = [ - "command", - "command-nightly", - "command-light", - "command-light-nightly", - "command-r-plus", - "command-r", - ] - model_id_key = "model" - pypi_package_deps = ["cohere"] - auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY") - - async def _acall(self, *args, **kwargs) -> Coroutine[Any, Any, str]: - return await self._call_in_executor(*args, **kwargs) - - class GPT4AllProvider(BaseProvider, GPT4All): def __init__(self, **kwargs): model = kwargs.get("model_id") diff --git a/packages/jupyter-ai-magics/pyproject.toml b/packages/jupyter-ai-magics/pyproject.toml index 941d17ba6..5e3791402 100644 --- a/packages/jupyter-ai-magics/pyproject.toml +++ b/packages/jupyter-ai-magics/pyproject.toml @@ -36,15 +36,15 @@ test = ["coverage", "pytest", "pytest-asyncio", "pytest-cov"] all = [ "ai21", - "cohere>4.40,<5", "gpt4all", "huggingface_hub", "ipywidgets", "langchain_anthropic", - "langchain-mistralai", + "langchain_cohere", + "langchain_google_genai", + "langchain_mistralai", "langchain_nvidia_ai_endpoints", - "langchain-google-genai", - "langchain-openai", + "langchain_openai", "pillow", "boto3", "qianfan", @@ -55,7 +55,7 @@ all = [ ai21 = "jupyter_ai_magics:AI21Provider" anthropic = "jupyter_ai_magics.partner_providers.anthropic:AnthropicProvider" anthropic-chat = "jupyter_ai_magics.partner_providers.anthropic:ChatAnthropicProvider" -cohere = "jupyter_ai_magics:CohereProvider" +cohere = "jupyter_ai_magics.partner_providers.cohere:CohereProvider" gpt4all = "jupyter_ai_magics:GPT4AllProvider" huggingface_hub = "jupyter_ai_magics:HfHubProvider" openai = "jupyter_ai_magics.partner_providers.openai:OpenAIProvider" @@ -72,7 +72,7 @@ mistralai = "jupyter_ai_magics.partner_providers.mistralai:MistralAIProvider" [project.entry-points."jupyter_ai.embeddings_model_providers"] bedrock = "jupyter_ai_magics:BedrockEmbeddingsProvider" -cohere = "jupyter_ai_magics:CohereEmbeddingsProvider" +cohere = "jupyter_ai_magics.partner_providers.cohere:CohereEmbeddingsProvider" mistralai = "jupyter_ai_magics.partner_providers.mistralai:MistralAIEmbeddingsProvider" gpt4all = "jupyter_ai_magics:GPT4AllEmbeddingsProvider" huggingface_hub = "jupyter_ai_magics:HfHubEmbeddingsProvider" From 1b50b623649df0a04a9858ac944d280c20630542 Mon Sep 17 00:00:00 2001 From: "David L. Qiu" Date: Fri, 21 Jun 2024 07:25:19 -0700 Subject: [PATCH 2/3] remove Cohere import --- packages/jupyter-ai-magics/jupyter_ai_magics/providers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py index c38569e66..0dd02b343 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py @@ -36,7 +36,6 @@ from langchain_community.llms import ( AI21, Bedrock, - Cohere, GPT4All, HuggingFaceEndpoint, SagemakerEndpoint, From ebf3dc012d7966a48d652e88dd7f1ddbce6dfe15 Mon Sep 17 00:00:00 2001 From: "David L. Qiu" Date: Fri, 21 Jun 2024 07:29:28 -0700 Subject: [PATCH 3/3] update Cohere provider dependencies in docs --- docs/source/users/index.md | 2 +- .../jupyter_ai_magics/partner_providers/cohere.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/users/index.md b/docs/source/users/index.md index 7fc2af6cd..4b339f13c 100644 --- a/docs/source/users/index.md +++ b/docs/source/users/index.md @@ -160,7 +160,7 @@ Jupyter AI supports the following model providers: | Anthropic (chat) | `anthropic-chat` | `ANTHROPIC_API_KEY` | `langchain-anthropic` | | Bedrock | `bedrock` | N/A | `boto3` | | Bedrock (chat) | `bedrock-chat` | N/A | `boto3` | -| Cohere | `cohere` | `COHERE_API_KEY` | `cohere` | +| Cohere | `cohere` | `COHERE_API_KEY` | `langchain_cohere` | | ERNIE-Bot | `qianfan` | `QIANFAN_AK`, `QIANFAN_SK` | `qianfan` | | Gemini | `gemini` | `GOOGLE_API_KEY` | `langchain-google-genai` | | GPT4All | `gpt4all` | N/A | `gpt4all` | diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py b/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py index 3286e8e35..d9b4a9e7b 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/cohere.py @@ -19,7 +19,7 @@ class CohereProvider(BaseProvider, ChatCohere): "command-r", ] model_id_key = "model" - pypi_package_deps = ["cohere"] + pypi_package_deps = ["langchain_cohere"] auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY") @@ -36,5 +36,5 @@ class CohereEmbeddingsProvider(BaseEmbeddingsProvider, CohereEmbeddings): "embed-multilingual-light-v3.0", ] model_id_key = "model" - pypi_package_deps = ["cohere"] + pypi_package_deps = ["langchain_cohere"] auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY")