Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain_cohere: add cohere as a partner package #19049

Merged
merged 39 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a5e63e2
Add cohere partner package from template
billytrend-cohere Mar 13, 2024
12a434a
Copy over integrations
billytrend-cohere Mar 13, 2024
7df44ff
Make mypy pass for 5.0.0a9
billytrend-cohere Mar 13, 2024
c552920
Lint fix and format
billytrend-cohere Mar 14, 2024
21d2001
Fix imports
billytrend-cohere Mar 14, 2024
2e80ad7
Format
billytrend-cohere Mar 14, 2024
103e969
include enforce_stop_tokens
billytrend-cohere Mar 14, 2024
f326c72
Fix circular
billytrend-cohere Mar 14, 2024
cbee44e
4mat
billytrend-cohere Mar 14, 2024
c727bae
Merge branch 'master' into billy/add-cohere-partner
billytrend-cohere Mar 20, 2024
bbe6ae4
Merge changes from https://github.com/langchain-ai/langchain/pull/19084
billytrend-cohere Mar 20, 2024
4510739
Fix tests
billytrend-cohere Mar 20, 2024
da917c4
Fix imports
billytrend-cohere Mar 20, 2024
d278f30
Fix
billytrend-cohere Mar 20, 2024
aaeb486
Format
billytrend-cohere Mar 20, 2024
7ffc748
Dummy for unit tests
billytrend-cohere Mar 20, 2024
4bca787
Revert root dep
billytrend-cohere Mar 20, 2024
831d0c4
Move rerank
billytrend-cohere Mar 20, 2024
89ed4ba
Deprecate
billytrend-cohere Mar 20, 2024
f826e0a
Fix deprecated
billytrend-cohere Mar 20, 2024
edf4471
cohere==5.1.1
billytrend-cohere Mar 25, 2024
01f36b1
Merge branch 'master' into billy/add-cohere-partner
billytrend-cohere Mar 25, 2024
267149d
Cohere API parameters default to server side defaults
harry-cohere Mar 25, 2024
82a502e
fix lint
harry-cohere Mar 25, 2024
bf5f7c6
add truncate back to
harry-cohere Mar 25, 2024
b5c8dac
Fix docs
harry-cohere Mar 25, 2024
77b2704
fix lint
harry-cohere Mar 25, 2024
a2d2ad1
Merge pull request #1 from billytrend-cohere/harry/change-client-defa…
harry-cohere Mar 25, 2024
3426d65
Delete libs/partners/cohere/tests/integration_tests/test_llms.py
billytrend-cohere Mar 25, 2024
2d91752
Update __init__.py
billytrend-cohere Mar 25, 2024
3d90fa8
Update test_imports.py
billytrend-cohere Mar 25, 2024
a1bcf1c
Update imports
billytrend-cohere Mar 25, 2024
b06c30f
update existing pip install -U at top of notebooks
harry-cohere Mar 25, 2024
4f83ffb
Merge remote-tracking branch 'origin/master' into billy/add-cohere-pa…
efriis Mar 25, 2024
f864c4b
cr
efriis Mar 25, 2024
ba1bfcc
cr
efriis Mar 25, 2024
fd98e1a
cr
efriis Mar 25, 2024
b345324
cr
efriis Mar 25, 2024
1a87008
cr
efriis Mar 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/integrations/chat/cohere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"The integration lives in the `langchain-community` package. We also need to install the `cohere` package itself. We can install these with:\n",
"\n",
"```bash\n",
"pip install -U langchain-community cohere\n",
"pip install -U langchain-community langchain-cohere\n",
"```\n",
"\n",
"We'll also need to get a [Cohere API key](https://cohere.com/) and set the `COHERE_API_KEY` environment variable:"
Expand Down Expand Up @@ -89,7 +89,7 @@
},
"outputs": [],
"source": [
"from langchain_community.chat_models import ChatCohere\n",
"from langchain_cohere import ChatCohere\n",
"from langchain_core.messages import HumanMessage"
]
},
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/integrations/llms/cohere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"The integration lives in the `langchain-community` package. We also need to install the `cohere` package itself. We can install these with:\n",
"\n",
"```bash\n",
"pip install -U langchain-community cohere\n",
"pip install -U langchain-community langchain-cohere\n",
"```\n",
"\n",
"We'll also need to get a [Cohere API key](https://cohere.com/) and set the `COHERE_API_KEY` environment variable:"
Expand All @@ -39,7 +39,7 @@
},
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
" ········\n"
Expand Down Expand Up @@ -91,7 +91,7 @@
},
"outputs": [],
"source": [
"from langchain_community.llms import Cohere\n",
"from langchain_cohere import Cohere\n",
"from langchain_core.messages import HumanMessage"
]
},
Expand Down Expand Up @@ -255,7 +255,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/integrations/retrievers/cohere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"outputs": [],
"source": [
"from langchain_community.chat_models import ChatCohere\n",
"from langchain_cohere import ChatCohere\n",
"from langchain_community.retrievers import CohereRagRetriever\n",
"from langchain_core.documents import Document"
]
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/integrations/text_embedding/cohere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.embeddings import CohereEmbeddings"
"from langchain_cohere import CohereEmbeddings"
]
},
{
Expand Down
4 changes: 4 additions & 0 deletions libs/community/langchain_community/chat_models/cohere.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
Expand Down Expand Up @@ -94,6 +95,9 @@ def get_cohere_chat_request(
return {k: v for k, v in req.items() if v is not None}


@deprecated(
since="0.0.30", removal="0.2.0", alternative_import="langchain_cohere.ChatCohere"
)
class ChatCohere(BaseChatModel, BaseCohere):
"""`Cohere` chat large language models.

Expand Down
6 changes: 6 additions & 0 deletions libs/community/langchain_community/embeddings/cohere.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from typing import Any, Dict, List, Optional

from langchain_core._api.deprecation import deprecated
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
from langchain_core.utils import get_from_dict_or_env

from langchain_community.llms.cohere import _create_retry_decorator


@deprecated(
since="0.0.30",
removal="0.2.0",
alternative_import="langchain_cohere.CohereEmbeddings",
)
class CohereEmbeddings(BaseModel, Embeddings):
"""Cohere embedding models.

Expand Down
7 changes: 7 additions & 0 deletions libs/community/langchain_community/llms/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import Any, Callable, Dict, List, Optional

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
Expand Down Expand Up @@ -69,6 +70,9 @@ async def _completion_with_retry(**kwargs: Any) -> Any:
return _completion_with_retry(**kwargs)


@deprecated(
since="0.0.30", removal="0.2.0", alternative_import="langchain_cohere.BaseCohere"
)
class BaseCohere(Serializable):
"""Base class for Cohere models."""

Expand Down Expand Up @@ -117,6 +121,9 @@ def validate_environment(cls, values: Dict) -> Dict:
return values


@deprecated(
since="0.1.14", removal="0.2.0", alternative_import="langchain_cohere.Cohere"
)
class Cohere(LLM, BaseCohere):
"""Cohere large language models.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING, Any, Dict, List

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import (
AsyncCallbackManagerForRetrieverRun,
CallbackManagerForRetrieverRun,
Expand Down Expand Up @@ -40,6 +41,11 @@ def _get_docs(response: Any) -> List[Document]:
return docs


@deprecated(
since="0.0.30",
removal="0.2.0",
alternative_import="langchain_cohere.CohereRagRetriever",
)
class CohereRagRetriever(BaseRetriever):
"""Cohere Chat API with RAG."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import deepcopy
from typing import Any, Dict, List, Optional, Sequence, Union

from langchain_core._api.deprecation import deprecated
from langchain_core.documents import Document
from langchain_core.pydantic_v1 import Extra, root_validator

Expand All @@ -11,6 +12,9 @@
from langchain.utils import get_from_dict_or_env


@deprecated(
since="0.0.30", removal="0.2.0", alternative_import="langchain_cohere.CohereRerank"
)
class CohereRerank(BaseDocumentCompressor):
"""Document compressor that uses `Cohere Rerank API`."""

Expand Down
1 change: 1 addition & 0 deletions libs/partners/cohere/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
21 changes: 21 additions & 0 deletions libs/partners/cohere/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 LangChain, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions libs/partners/cohere/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.PHONY: all format lint test tests integration_tests docker_tests help extended_tests

# Default target executed when no arguments are given to make.
all: help

# Define a variable for the test file path.
TEST_FILE ?= tests/unit_tests/
integration_test integration_tests: TEST_FILE=tests/integration_tests/

test tests integration_test integration_tests:
poetry run pytest $(TEST_FILE)


######################
# LINTING AND FORMATTING
######################

# Define a variable for Python and notebook files.
PYTHON_FILES=.
MYPY_CACHE=.mypy_cache
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/partners/cohere --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
lint_package: PYTHON_FILES=langchain_cohere
lint_tests: PYTHON_FILES=tests
lint_tests: MYPY_CACHE=.mypy_cache_test

lint lint_diff lint_package lint_tests:
poetry run ruff .
poetry run ruff format $(PYTHON_FILES) --diff
poetry run ruff --select I $(PYTHON_FILES)
mkdir $(MYPY_CACHE); poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)

format format_diff:
poetry run ruff format $(PYTHON_FILES)
poetry run ruff --select I --fix $(PYTHON_FILES)

spell_check:
poetry run codespell --toml pyproject.toml

spell_fix:
poetry run codespell --toml pyproject.toml -w

check_imports: $(shell find langchain_cohere -name '*.py')
poetry run python ./scripts/check_imports.py $^

######################
# HELP
######################

help:
@echo '----'
@echo 'check_imports - check imports'
@echo 'format - run code formatters'
@echo 'lint - run linters'
@echo 'test - run unit tests'
@echo 'tests - run unit tests'
@echo 'test TEST_FILE=<test_file> - run all tests in file'
1 change: 1 addition & 0 deletions libs/partners/cohere/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# langchain-cohere
12 changes: 12 additions & 0 deletions libs/partners/cohere/langchain_cohere/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from langchain_cohere.chat_models import ChatCohere
from langchain_cohere.embeddings import CohereEmbeddings
from langchain_cohere.rag_retrievers import CohereRagRetriever
from langchain_cohere.rerank import CohereRerank

__all__ = [
"ChatCohere",
"CohereVectorStore",
"CohereEmbeddings",
"CohereRagRetriever",
"CohereRerank",
]
Loading
Loading