lfx-cohere 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lfx_cohere/__init__.py +17 -0
- lfx_cohere/components/__init__.py +0 -0
- lfx_cohere/components/cohere/__init__.py +48 -0
- lfx_cohere/components/cohere/cohere_embeddings.py +80 -0
- lfx_cohere/components/cohere/cohere_models.py +45 -0
- lfx_cohere/components/cohere/cohere_rerank.py +51 -0
- lfx_cohere/extension.json +16 -0
- lfx_cohere-0.1.0.dist-info/METADATA +44 -0
- lfx_cohere-0.1.0.dist-info/RECORD +11 -0
- lfx_cohere-0.1.0.dist-info/WHEEL +4 -0
- lfx_cohere-0.1.0.dist-info/entry_points.txt +2 -0
lfx_cohere/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""lfx-cohere: Cohere bundle.
|
|
2
|
+
|
|
3
|
+
Distribution unit ``lfx-cohere``. At runtime Langflow's loader
|
|
4
|
+
discovers ``extension.json`` shipped alongside this ``__init__.py`` and
|
|
5
|
+
registers the bundle's components under the namespaced IDs
|
|
6
|
+
``ext:cohere:<Class>@official``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from lfx_cohere.components.cohere.cohere_embeddings import CohereEmbeddingsComponent
|
|
10
|
+
from lfx_cohere.components.cohere.cohere_models import CohereComponent
|
|
11
|
+
from lfx_cohere.components.cohere.cohere_rerank import CohereRerankComponent
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"CohereComponent",
|
|
15
|
+
"CohereEmbeddingsComponent",
|
|
16
|
+
"CohereRerankComponent",
|
|
17
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Lazy component re-exports for the ``cohere`` bundle.
|
|
2
|
+
|
|
3
|
+
Mirrors the pre-extraction layout of ``lfx.components.cohere`` so saved
|
|
4
|
+
flows that referenced the module-level class
|
|
5
|
+
(e.g. ``lfx.components.cohere.<Class>``) keep resolving via the
|
|
6
|
+
migration table after rewrite to
|
|
7
|
+
``lfx_cohere.components.cohere.<Class>``.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import TYPE_CHECKING, Any
|
|
13
|
+
|
|
14
|
+
from lfx.utils.lazy_import import import_mod
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from .cohere_embeddings import CohereEmbeddingsComponent
|
|
18
|
+
from .cohere_models import CohereComponent
|
|
19
|
+
from .cohere_rerank import CohereRerankComponent
|
|
20
|
+
|
|
21
|
+
_dynamic_imports = {
|
|
22
|
+
"CohereComponent": "cohere_models",
|
|
23
|
+
"CohereEmbeddingsComponent": "cohere_embeddings",
|
|
24
|
+
"CohereRerankComponent": "cohere_rerank",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"CohereComponent",
|
|
29
|
+
"CohereEmbeddingsComponent",
|
|
30
|
+
"CohereRerankComponent",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def __getattr__(attr_name: str) -> Any:
|
|
35
|
+
if attr_name not in _dynamic_imports:
|
|
36
|
+
msg = f"module {__name__!r} has no attribute {attr_name!r}"
|
|
37
|
+
raise AttributeError(msg)
|
|
38
|
+
try:
|
|
39
|
+
result = import_mod(attr_name, _dynamic_imports[attr_name], __spec__.parent)
|
|
40
|
+
except (ModuleNotFoundError, ImportError, AttributeError) as e:
|
|
41
|
+
msg = f"Could not import {attr_name!r} from {__name__!r}: {e}"
|
|
42
|
+
raise AttributeError(msg) from e
|
|
43
|
+
globals()[attr_name] = result
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def __dir__() -> list[str]:
|
|
48
|
+
return list(__all__)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
import cohere
|
|
4
|
+
from langchain_cohere import CohereEmbeddings
|
|
5
|
+
from lfx.base.models.model import LCModelComponent
|
|
6
|
+
from lfx.field_typing import Embeddings
|
|
7
|
+
from lfx.io import DropdownInput, FloatInput, IntInput, MessageTextInput, Output, SecretStrInput
|
|
8
|
+
|
|
9
|
+
HTTP_STATUS_OK = 200
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CohereEmbeddingsComponent(LCModelComponent):
|
|
13
|
+
display_name = "Cohere Embeddings"
|
|
14
|
+
description = "Generate embeddings using Cohere models."
|
|
15
|
+
icon = "Cohere"
|
|
16
|
+
name = "CohereEmbeddings"
|
|
17
|
+
|
|
18
|
+
inputs = [
|
|
19
|
+
SecretStrInput(name="api_key", display_name="Cohere API Key", required=True, real_time_refresh=True),
|
|
20
|
+
DropdownInput(
|
|
21
|
+
name="model_name",
|
|
22
|
+
display_name="Model",
|
|
23
|
+
advanced=False,
|
|
24
|
+
options=[
|
|
25
|
+
"embed-english-v2.0",
|
|
26
|
+
"embed-multilingual-v2.0",
|
|
27
|
+
"embed-english-light-v2.0",
|
|
28
|
+
"embed-multilingual-light-v2.0",
|
|
29
|
+
],
|
|
30
|
+
value="embed-english-v2.0",
|
|
31
|
+
refresh_button=True,
|
|
32
|
+
combobox=True,
|
|
33
|
+
),
|
|
34
|
+
MessageTextInput(name="truncate", display_name="Truncate", advanced=True),
|
|
35
|
+
IntInput(name="max_retries", display_name="Max Retries", value=3, advanced=True),
|
|
36
|
+
MessageTextInput(name="user_agent", display_name="User Agent", advanced=True, value="langchain"),
|
|
37
|
+
FloatInput(name="request_timeout", display_name="Request Timeout", advanced=True),
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
outputs = [
|
|
41
|
+
Output(display_name="Embeddings", name="embeddings", method="build_embeddings"),
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
def build_embeddings(self) -> Embeddings:
|
|
45
|
+
data = None
|
|
46
|
+
try:
|
|
47
|
+
data = CohereEmbeddings(
|
|
48
|
+
cohere_api_key=self.api_key,
|
|
49
|
+
model=self.model_name,
|
|
50
|
+
truncate=self.truncate,
|
|
51
|
+
max_retries=self.max_retries,
|
|
52
|
+
user_agent=self.user_agent,
|
|
53
|
+
request_timeout=self.request_timeout or None,
|
|
54
|
+
)
|
|
55
|
+
except Exception as e:
|
|
56
|
+
msg = (
|
|
57
|
+
"Unable to create Cohere Embeddings. ",
|
|
58
|
+
"Please verify the API key and model parameters, and try again.",
|
|
59
|
+
)
|
|
60
|
+
raise ValueError(msg) from e
|
|
61
|
+
# added status if not the return data would be serialised to create the status
|
|
62
|
+
return data
|
|
63
|
+
|
|
64
|
+
def get_model(self):
|
|
65
|
+
try:
|
|
66
|
+
co = cohere.ClientV2(self.api_key)
|
|
67
|
+
response = co.models.list(endpoint="embed")
|
|
68
|
+
models = response.models
|
|
69
|
+
return [model.name for model in models]
|
|
70
|
+
except Exception as e:
|
|
71
|
+
msg = f"Failed to fetch Cohere models. Error: {e}"
|
|
72
|
+
raise ValueError(msg) from e
|
|
73
|
+
|
|
74
|
+
async def update_build_config(self, build_config: dict, field_value: Any, field_name: str | None = None):
|
|
75
|
+
if field_name in {"model_name", "api_key"}:
|
|
76
|
+
if build_config.get("api_key", {}).get("value", None):
|
|
77
|
+
build_config["model_name"]["options"] = self.get_model()
|
|
78
|
+
else:
|
|
79
|
+
build_config["model_name"]["options"] = field_value
|
|
80
|
+
return build_config
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from langchain_cohere import ChatCohere
|
|
2
|
+
from lfx.base.models.model import LCModelComponent
|
|
3
|
+
from lfx.field_typing import LanguageModel
|
|
4
|
+
from lfx.field_typing.range_spec import RangeSpec
|
|
5
|
+
from lfx.io import SecretStrInput, SliderInput
|
|
6
|
+
from pydantic.v1 import SecretStr
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CohereComponent(LCModelComponent):
|
|
10
|
+
display_name = "Cohere Language Models"
|
|
11
|
+
description = "Generate text using Cohere LLMs."
|
|
12
|
+
documentation = "https://python.langchain.com/docs/integrations/llms/cohere/"
|
|
13
|
+
icon = "Cohere"
|
|
14
|
+
name = "CohereModel"
|
|
15
|
+
|
|
16
|
+
inputs = [
|
|
17
|
+
*LCModelComponent.get_base_inputs(),
|
|
18
|
+
SecretStrInput(
|
|
19
|
+
name="cohere_api_key",
|
|
20
|
+
display_name="Cohere API Key",
|
|
21
|
+
info="The Cohere API Key to use for the Cohere model.",
|
|
22
|
+
advanced=False,
|
|
23
|
+
value="COHERE_API_KEY",
|
|
24
|
+
required=True,
|
|
25
|
+
),
|
|
26
|
+
SliderInput(
|
|
27
|
+
name="temperature",
|
|
28
|
+
display_name="Temperature",
|
|
29
|
+
value=0.75,
|
|
30
|
+
range_spec=RangeSpec(min=0, max=2, step=0.01),
|
|
31
|
+
info="Controls randomness. Lower values are more deterministic, higher values are more creative.",
|
|
32
|
+
advanced=True,
|
|
33
|
+
),
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
def build_model(self) -> LanguageModel: # type: ignore[type-var]
|
|
37
|
+
cohere_api_key = self.cohere_api_key
|
|
38
|
+
temperature = self.temperature
|
|
39
|
+
|
|
40
|
+
api_key = SecretStr(cohere_api_key).get_secret_value() if cohere_api_key else None
|
|
41
|
+
|
|
42
|
+
return ChatCohere(
|
|
43
|
+
temperature=temperature or 0.75,
|
|
44
|
+
cohere_api_key=api_key,
|
|
45
|
+
)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from lfx.base.compressors.model import LCCompressorComponent
|
|
2
|
+
from lfx.field_typing import BaseDocumentCompressor
|
|
3
|
+
from lfx.inputs.inputs import SecretStrInput
|
|
4
|
+
from lfx.io import DropdownInput
|
|
5
|
+
from lfx.template.field.base import Output
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CohereRerankComponent(LCCompressorComponent):
|
|
9
|
+
display_name = "Cohere Rerank"
|
|
10
|
+
description = "Rerank documents using the Cohere API."
|
|
11
|
+
name = "CohereRerank"
|
|
12
|
+
icon = "Cohere"
|
|
13
|
+
|
|
14
|
+
inputs = [
|
|
15
|
+
*LCCompressorComponent.inputs,
|
|
16
|
+
SecretStrInput(
|
|
17
|
+
name="api_key",
|
|
18
|
+
display_name="Cohere API Key",
|
|
19
|
+
),
|
|
20
|
+
DropdownInput(
|
|
21
|
+
name="model",
|
|
22
|
+
display_name="Model",
|
|
23
|
+
options=[
|
|
24
|
+
"rerank-english-v3.0",
|
|
25
|
+
"rerank-multilingual-v3.0",
|
|
26
|
+
"rerank-english-v2.0",
|
|
27
|
+
"rerank-multilingual-v2.0",
|
|
28
|
+
],
|
|
29
|
+
value="rerank-english-v3.0",
|
|
30
|
+
),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
outputs = [
|
|
34
|
+
Output(
|
|
35
|
+
display_name="Reranked Documents",
|
|
36
|
+
name="reranked_documents",
|
|
37
|
+
method="compress_documents",
|
|
38
|
+
),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
def build_compressor(self) -> BaseDocumentCompressor: # type: ignore[type-var]
|
|
42
|
+
try:
|
|
43
|
+
from langchain_cohere import CohereRerank
|
|
44
|
+
except ImportError as e:
|
|
45
|
+
msg = "Please install langchain-cohere to use the Cohere model."
|
|
46
|
+
raise ImportError(msg) from e
|
|
47
|
+
return CohereRerank(
|
|
48
|
+
cohere_api_key=self.api_key,
|
|
49
|
+
model=self.model,
|
|
50
|
+
top_n=self.top_n,
|
|
51
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://schemas.langflow.org/extension/v1.json",
|
|
3
|
+
"id": "lfx-cohere",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"name": "Cohere",
|
|
6
|
+
"description": "Cohere component(s) as a standalone Langflow Extension Bundle.",
|
|
7
|
+
"lfx": {
|
|
8
|
+
"compat": ["1"]
|
|
9
|
+
},
|
|
10
|
+
"bundles": [
|
|
11
|
+
{
|
|
12
|
+
"name": "cohere",
|
|
13
|
+
"path": "components/cohere"
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lfx-cohere
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cohere component(s) as a standalone Langflow Extension Bundle.
|
|
5
|
+
Project-URL: Homepage, https://github.com/langflow-ai/langflow
|
|
6
|
+
Project-URL: Documentation, https://docs.langflow.org/extensions
|
|
7
|
+
Project-URL: Repository, https://github.com/langflow-ai/langflow
|
|
8
|
+
Author-email: Langflow <contact@langflow.org>
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: bundle,cohere,extension,langflow,lfx
|
|
11
|
+
Requires-Python: <3.15,>=3.10
|
|
12
|
+
Requires-Dist: langchain-cohere~=0.5.0
|
|
13
|
+
Requires-Dist: lfx<2.0.0,>=1.11.0.dev0
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# lfx-cohere
|
|
17
|
+
|
|
18
|
+
Cohere component(s) as a standalone Langflow Extension Bundle.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install lfx-cohere
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The bundle is registered automatically via the `langflow.extensions`
|
|
27
|
+
entry-point. After install, restart your Langflow server; the bundle's
|
|
28
|
+
components will appear in the palette under the `cohere` group with
|
|
29
|
+
the namespaced IDs `ext:cohere:<Class>@official`.
|
|
30
|
+
|
|
31
|
+
## Develop
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cd src/bundles/cohere
|
|
35
|
+
pip install -e .
|
|
36
|
+
lfx extension validate src/lfx_cohere
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Migration
|
|
40
|
+
|
|
41
|
+
Saved flows referencing the legacy class name(s) or the old import paths
|
|
42
|
+
under `lfx.components.cohere.*` are rewritten to the new namespaced
|
|
43
|
+
IDs by the migration table in
|
|
44
|
+
`src/lfx/src/lfx/extension/migration/migration_table.json`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
lfx_cohere/__init__.py,sha256=MczmAux1tSkNDyj5F8k_wNaZX9d0AWU2aPEizz-pxHI,597
|
|
2
|
+
lfx_cohere/extension.json,sha256=S4-cbGeBtBsOnyvxtRbiizcAtbGxqEV1rsK2kWrcCwA,339
|
|
3
|
+
lfx_cohere/components/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
lfx_cohere/components/cohere/__init__.py,sha256=chs8Z0J4iFw1_YGuamgXqa3KmvlLZL1ZBYiGHZ3oV-A,1449
|
|
5
|
+
lfx_cohere/components/cohere/cohere_embeddings.py,sha256=z1_t2epH_XuzdswW9_jeMw0hBqsHMeU2CdNjn3ycNag,3082
|
|
6
|
+
lfx_cohere/components/cohere/cohere_models.py,sha256=HDHfM5ghJ9w1QUn4rwGNV0SOaygAfX0iVll7XRS5Xwg,1562
|
|
7
|
+
lfx_cohere/components/cohere/cohere_rerank.py,sha256=qUoNEe6sjUnvkTHkCzwayBuLDoH957BBEgb-Qu_k9Yk,1554
|
|
8
|
+
lfx_cohere-0.1.0.dist-info/METADATA,sha256=JvctMF5B4v_cKkVqMubJJ1ezm1DHV0chO8mnV0_frKs,1307
|
|
9
|
+
lfx_cohere-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
10
|
+
lfx_cohere-0.1.0.dist-info/entry_points.txt,sha256=yEwxbwRG6SIkWSCREU5a8uBQQnlMfB3bt2FhSrydYjo,46
|
|
11
|
+
lfx_cohere-0.1.0.dist-info/RECORD,,
|