mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-20 19:41:23 +00:00
docs: Standardize MiniMaxEmbeddings (#24983)
- **Description:** Standardize MiniMaxEmbeddings - docs, the issue #24856 - model init arg names, the issue #20085
This commit is contained in:
parent
2c3e3dc6b1
commit
f5da0d6d87
@ -5,7 +5,7 @@ from typing import Any, Callable, Dict, List, Optional
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from langchain_core.embeddings import Embeddings
|
from langchain_core.embeddings import Embeddings
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Extra, SecretStr
|
from langchain_core.pydantic_v1 import BaseModel, Extra, Field, SecretStr
|
||||||
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
|
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
|
||||||
from tenacity import (
|
from tenacity import (
|
||||||
before_sleep_log,
|
before_sleep_log,
|
||||||
@ -45,25 +45,63 @@ def embed_with_retry(embeddings: MiniMaxEmbeddings, *args: Any, **kwargs: Any) -
|
|||||||
|
|
||||||
|
|
||||||
class MiniMaxEmbeddings(BaseModel, Embeddings):
|
class MiniMaxEmbeddings(BaseModel, Embeddings):
|
||||||
"""MiniMax's embedding service.
|
"""MiniMax embedding model integration.
|
||||||
|
|
||||||
|
Setup:
|
||||||
To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
|
To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
|
||||||
``MINIMAX_API_KEY`` set with your API token, or pass it as a named parameter to
|
``MINIMAX_API_KEY`` set with your API token.
|
||||||
the constructor.
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
export MINIMAX_API_KEY="your-api-key"
|
||||||
|
export MINIMAX_GROUP_ID="your-group-id"
|
||||||
|
|
||||||
|
Key init args — completion params:
|
||||||
|
model: Optional[str]
|
||||||
|
Name of ZhipuAI model to use.
|
||||||
|
api_key: Optional[str]
|
||||||
|
Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided.
|
||||||
|
group_id: Optional[str]
|
||||||
|
Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided.
|
||||||
|
|
||||||
|
See full list of supported init args and their descriptions in the params section.
|
||||||
|
|
||||||
|
Instantiate:
|
||||||
|
|
||||||
Example:
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from langchain_community.embeddings import MiniMaxEmbeddings
|
from langchain_community.embeddings import MiniMaxEmbeddings
|
||||||
embeddings = MiniMaxEmbeddings()
|
|
||||||
|
|
||||||
query_text = "This is a test query."
|
embed = MiniMaxEmbeddings(
|
||||||
query_result = embeddings.embed_query(query_text)
|
model="embo-01",
|
||||||
|
# api_key="...",
|
||||||
|
# group_id="...",
|
||||||
|
# other
|
||||||
|
)
|
||||||
|
|
||||||
document_text = "This is a test document."
|
Embed single text:
|
||||||
document_result = embeddings.embed_documents([document_text])
|
.. code-block:: python
|
||||||
|
|
||||||
"""
|
input_text = "The meaning of life is 42"
|
||||||
|
embed.embed_query(input_text)
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
[0.03016241, 0.03617699, 0.0017198119, -0.002061239, -0.00029994643, -0.0061320597, -0.0043635326, ...]
|
||||||
|
|
||||||
|
Embed multiple text:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
input_texts = ["This is a test query1.", "This is a test query2."]
|
||||||
|
embed.embed_documents(input_texts)
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
[
|
||||||
|
[-0.0021588828, -0.007608119, 0.029349545, -0.0038194496, 0.008031177, -0.004529633, -0.020150753, ...],
|
||||||
|
[ -0.00023150232, -0.011122423, 0.016930554, 0.0083089275, 0.012633711, 0.019683322, -0.005971041, ...]
|
||||||
|
]
|
||||||
|
""" # noqa: E501
|
||||||
|
|
||||||
endpoint_url: str = "https://api.minimax.chat/v1/embeddings"
|
endpoint_url: str = "https://api.minimax.chat/v1/embeddings"
|
||||||
"""Endpoint URL to use."""
|
"""Endpoint URL to use."""
|
||||||
@ -74,24 +112,27 @@ class MiniMaxEmbeddings(BaseModel, Embeddings):
|
|||||||
embed_type_query: str = "query"
|
embed_type_query: str = "query"
|
||||||
"""For embed_query"""
|
"""For embed_query"""
|
||||||
|
|
||||||
minimax_group_id: Optional[str] = None
|
minimax_group_id: Optional[str] = Field(default=None, alias="group_id")
|
||||||
"""Group ID for MiniMax API."""
|
"""Group ID for MiniMax API."""
|
||||||
minimax_api_key: Optional[SecretStr] = None
|
minimax_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
|
||||||
"""API Key for MiniMax API."""
|
"""API Key for MiniMax API."""
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""Configuration for this pydantic object."""
|
"""Configuration for this pydantic object."""
|
||||||
|
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
allow_population_by_field_name = True
|
||||||
|
|
||||||
@pre_init
|
@pre_init
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that group id and api key exists in environment."""
|
"""Validate that group id and api key exists in environment."""
|
||||||
minimax_group_id = get_from_dict_or_env(
|
minimax_group_id = get_from_dict_or_env(
|
||||||
values, "minimax_group_id", "MINIMAX_GROUP_ID"
|
values, ["minimax_group_id", "group_id"], "MINIMAX_GROUP_ID"
|
||||||
)
|
)
|
||||||
minimax_api_key = convert_to_secret_str(
|
minimax_api_key = convert_to_secret_str(
|
||||||
get_from_dict_or_env(values, "minimax_api_key", "MINIMAX_API_KEY")
|
get_from_dict_or_env(
|
||||||
|
values, ["minimax_api_key", "api_key"], "MINIMAX_API_KEY"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
values["minimax_group_id"] = minimax_group_id
|
values["minimax_group_id"] = minimax_group_id
|
||||||
values["minimax_api_key"] = minimax_api_key
|
values["minimax_api_key"] = minimax_api_key
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
from typing import cast
|
||||||
|
|
||||||
|
from langchain_core.pydantic_v1 import SecretStr
|
||||||
|
|
||||||
|
from langchain_community.embeddings import MiniMaxEmbeddings
|
||||||
|
|
||||||
|
|
||||||
|
def test_initialization_with_alias() -> None:
|
||||||
|
"""Test minimax embedding model initialization with alias."""
|
||||||
|
api_key = "your-api-key"
|
||||||
|
group_id = "your-group-id"
|
||||||
|
|
||||||
|
embeddings = MiniMaxEmbeddings( # type: ignore[arg-type, call-arg]
|
||||||
|
api_key=api_key, # type: ignore[arg-type]
|
||||||
|
group_id=group_id, # type: ignore[arg-type]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert cast(SecretStr, embeddings.minimax_api_key).get_secret_value() == api_key
|
||||||
|
assert embeddings.minimax_group_id == group_id
|
Loading…
Reference in New Issue
Block a user