diff --git a/libs/partners/cohere/README.md b/libs/partners/cohere/README.md new file mode 100644 index 00000000000..a80d7c9ea5e --- /dev/null +++ b/libs/partners/cohere/README.md @@ -0,0 +1,39 @@ +# LangChain Cohere + +This package contains the Cohere integrations for LangChain. + +## Installation + +```bash +pip install langchain-cohere +``` + +## Usage + +The `langchain-cohere` package provides integrations for Cohere's language models and embeddings. + +### Chat Models + +```python +from langchain_cohere import ChatCohere + +chat = ChatCohere(model="command-r-plus") +``` + +### Embeddings + +```python +from langchain_cohere import CohereEmbeddings + +embeddings = CohereEmbeddings(model="embed-english-v3.0") +``` + +### Rerank + +```python +from langchain_cohere import CohereRerank + +rerank = CohereRerank(model="rerank-english-v3.0") +``` + +For more details, visit the [LangChain Cohere repository](https://github.com/langchain-ai/langchain-cohere). \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/__init__.py b/libs/partners/cohere/langchain_cohere/__init__.py new file mode 100644 index 00000000000..af81b80f8ec --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/__init__.py @@ -0,0 +1,22 @@ +"""Cohere integration package for LangChain. + +This package contains the Cohere integrations for LangChain. +For the full implementation, please install: pip install langchain-cohere + +This is a placeholder package to enable API documentation generation. +""" + +# Import from local modules for documentation generation +from .chat_models import ChatCohere +from .common import CohereCitation +from .embeddings import CohereEmbeddings +from .rag_retrievers import CohereRagRetriever +from .rerank import CohereRerank + +__all__ = [ + "CohereCitation", + "ChatCohere", + "CohereEmbeddings", + "CohereRagRetriever", + "CohereRerank", +] \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/chat_models.py b/libs/partners/cohere/langchain_cohere/chat_models.py new file mode 100644 index 00000000000..9a49238ce72 --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/chat_models.py @@ -0,0 +1,63 @@ +"""Chat model for Cohere.""" + +from typing import Any, Dict, List, Optional, Union + +try: + from langchain_core.language_models.chat_models import BaseChatModel + from langchain_core.messages import BaseMessage +except ImportError: + # Fallback for when langchain_core is not available + class BaseChatModel: + pass + class BaseMessage: + pass + + +class ChatCohere(BaseChatModel): + """Cohere chat model. + + To use this, you need to install the ``langchain-cohere`` package. + + .. code-block:: bash + + pip install langchain-cohere + + Example: + .. code-block:: python + + from langchain_cohere import ChatCohere + + chat = ChatCohere(model="command-r-plus") + messages = [("human", "Hello, how are you?")] + response = chat.invoke(messages) + + """ + + model: str = "command-r-plus" + """Model name to use.""" + + cohere_api_key: Optional[str] = None + """Cohere API key. If not provided, will read from environment variable COHERE_API_KEY.""" + + temperature: float = 0.0 + """Temperature for sampling.""" + + max_tokens: Optional[int] = None + """Maximum number of tokens to generate.""" + + def _generate( + self, + messages: List[BaseMessage], + stop: Optional[List[str]] = None, + run_manager: Optional[Any] = None, + **kwargs: Any, + ) -> Any: + """Generate chat completion.""" + raise NotImplementedError( + "This is a placeholder class. Install langchain-cohere to use: pip install langchain-cohere" + ) + + @property + def _llm_type(self) -> str: + """Return type of chat model.""" + return "cohere-chat" \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/common.py b/libs/partners/cohere/langchain_cohere/common.py new file mode 100644 index 00000000000..c031b54fed8 --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/common.py @@ -0,0 +1,53 @@ +"""Common utilities and types for Cohere integration.""" + +from typing import Any, Dict, Optional + + +class CohereCitation: + """Represents a citation from Cohere model responses. + + To use this, you need to install the ``langchain-cohere`` package. + + .. code-block:: bash + + pip install langchain-cohere + + Example: + .. code-block:: python + + from langchain_cohere import CohereCitation + + citation = CohereCitation( + start=0, + end=10, + text="cited text", + document_ids=["doc1"] + ) + + """ + + start: int + """Start index of the citation in the generated text.""" + + end: int + """End index of the citation in the generated text.""" + + text: str + """The cited text.""" + + document_ids: list[str] + """List of document IDs that support this citation.""" + + def __init__( + self, + start: int, + end: int, + text: str, + document_ids: list[str], + **kwargs: Any, + ) -> None: + """Initialize citation.""" + self.start = start + self.end = end + self.text = text + self.document_ids = document_ids \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/embeddings.py b/libs/partners/cohere/langchain_cohere/embeddings.py new file mode 100644 index 00000000000..c5966cb3602 --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/embeddings.py @@ -0,0 +1,48 @@ +"""Cohere embeddings.""" + +from typing import List, Optional + +try: + from langchain_core.embeddings import Embeddings +except ImportError: + # Fallback for when langchain_core is not available + class Embeddings: + pass + + +class CohereEmbeddings(Embeddings): + """Cohere embeddings. + + To use this, you need to install the ``langchain-cohere`` package. + + .. code-block:: bash + + pip install langchain-cohere + + Example: + .. code-block:: python + + from langchain_cohere import CohereEmbeddings + + embeddings = CohereEmbeddings(model="embed-english-v3.0") + text_embeddings = embeddings.embed_documents(["Hello world"]) + + """ + + model: str = "embed-english-v3.0" + """Model name to use.""" + + cohere_api_key: Optional[str] = None + """Cohere API key. If not provided, will read from environment variable COHERE_API_KEY.""" + + def embed_documents(self, texts: List[str]) -> List[List[float]]: + """Embed search docs.""" + raise NotImplementedError( + "This is a placeholder class. Install langchain-cohere to use: pip install langchain-cohere" + ) + + def embed_query(self, text: str) -> List[float]: + """Embed query text.""" + raise NotImplementedError( + "This is a placeholder class. Install langchain-cohere to use: pip install langchain-cohere" + ) \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/py.typed b/libs/partners/cohere/langchain_cohere/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libs/partners/cohere/langchain_cohere/rag_retrievers.py b/libs/partners/cohere/langchain_cohere/rag_retrievers.py new file mode 100644 index 00000000000..200ca6f67f1 --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/rag_retrievers.py @@ -0,0 +1,47 @@ +"""Cohere RAG retriever.""" + +from typing import Any, List, Optional + +try: + from langchain_core.documents import Document + from langchain_core.retrievers import BaseRetriever +except ImportError: + # Fallback for when langchain_core is not available + class Document: + pass + class BaseRetriever: + pass + + +class CohereRagRetriever(BaseRetriever): + """Cohere RAG retriever. + + To use this, you need to install the ``langchain-cohere`` package. + + .. code-block:: bash + + pip install langchain-cohere + + Example: + .. code-block:: python + + from langchain_cohere import CohereRagRetriever + + retriever = CohereRagRetriever() + docs = retriever.get_relevant_documents("query") + + """ + + cohere_api_key: Optional[str] = None + """Cohere API key. If not provided, will read from environment variable COHERE_API_KEY.""" + + def _get_relevant_documents( + self, + query: str, + *, + run_manager: Optional[Any] = None, + ) -> List[Document]: + """Get documents relevant to a query.""" + raise NotImplementedError( + "This is a placeholder class. Install langchain-cohere to use: pip install langchain-cohere" + ) \ No newline at end of file diff --git a/libs/partners/cohere/langchain_cohere/rerank.py b/libs/partners/cohere/langchain_cohere/rerank.py new file mode 100644 index 00000000000..4990bc25d7e --- /dev/null +++ b/libs/partners/cohere/langchain_cohere/rerank.py @@ -0,0 +1,51 @@ +"""Cohere rerank functionality.""" + +from typing import Any, List, Optional, Sequence + +try: + from langchain_core.documents import Document +except ImportError: + # Fallback for when langchain_core is not available + class Document: + pass + + +class CohereRerank: + """Cohere rerank model. + + To use this, you need to install the ``langchain-cohere`` package. + + .. code-block:: bash + + pip install langchain-cohere + + Example: + .. code-block:: python + + from langchain_cohere import CohereRerank + + rerank = CohereRerank(model="rerank-english-v3.0") + docs = [Document(page_content="doc1"), Document(page_content="doc2")] + reranked = rerank.rerank(docs, "query") + + """ + + model: str = "rerank-english-v3.0" + """Model name to use.""" + + cohere_api_key: Optional[str] = None + """Cohere API key. If not provided, will read from environment variable COHERE_API_KEY.""" + + top_n: int = 10 + """Number of documents to return.""" + + def rerank( + self, + documents: Sequence[Document], + query: str, + **kwargs: Any, + ) -> List[Document]: + """Rerank documents based on query relevance.""" + raise NotImplementedError( + "This is a placeholder class. Install langchain-cohere to use: pip install langchain-cohere" + ) \ No newline at end of file diff --git a/libs/partners/cohere/pyproject.toml b/libs/partners/cohere/pyproject.toml new file mode 100644 index 00000000000..00558780f20 --- /dev/null +++ b/libs/partners/cohere/pyproject.toml @@ -0,0 +1,93 @@ +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[project] +name = "langchain-cohere" +version = "0.4.4" +description = "An integration package connecting Cohere and LangChain" +readme = "README.md" +license = {text = "MIT"} +dependencies = [ + "langchain-core>=0.3.27", + "cohere>=5.12.0,<6.0", + "langchain-community>=0.3.0", + "pydantic>=2,<3", +] +authors = [ + {name = "LangChain", email = "hello@langchain.com"} +] + +[project.urls] +"Source Code" = "https://github.com/langchain-ai/langchain-cohere" +"Homepage" = "https://github.com/langchain-ai/langchain-cohere" + +[tool.poetry] +name = "langchain-cohere" +version = "0.4.4" +description = "An integration package connecting Cohere and LangChain" +authors = [] +readme = "README.md" +repository = "https://github.com/langchain-ai/langchain-cohere" +license = "MIT" + +[tool.poetry.urls] +"Source Code" = "https://github.com/langchain-ai/langchain-cohere" + +[tool.poetry.dependencies] +python = ">=3.9,<4.0" +langchain-core = "^0.3.27" +cohere = ">=5.12.0,<6.0" +langchain-community = { version = "^0.3.0"} +pydantic = ">=2,<3" + +[tool.poetry.group.test] +optional = true + +[tool.poetry.group.test.dependencies] +pytest = "^8.2.2" +freezegun = "^1.2.2" +pytest-mock = "^3.10.0" +syrupy = "^4.0.2" +pytest-watcher = "^0.3.4" +pytest-asyncio = "^0.21.1" +langchain-tests = "0.3.1" + +[tool.poetry.group.codespell] +optional = true + +[tool.poetry.group.codespell.dependencies] +codespell = "^2.2.0" + +[tool.poetry.group.lint] +optional = true + +[tool.poetry.group.lint.dependencies] +ruff = "^0.1.5" + +[tool.poetry.group.typing.dependencies] +mypy = "^0.991" + +[tool.ruff] +select = [ + "E", # pycodestyle + "F", # pyflakes + "I", # isort +] + +fixable = ["ALL"] + +[tool.mypy] +disallow_untyped_defs = true + +[tool.coverage.run] +omit = ["tests/*"] + +[tool.pytest.ini_options] +addopts = "--snapshot-warn-unused --strict-markers --strict-config --durations=5" +markers = [ + "requires: mark tests as requiring a specific library", + "asyncio: mark tests as requiring asyncio", + "compile: mark placeholder test used to compile integration tests without running them", +] +asyncio_mode = "auto" \ No newline at end of file