Skip to content

Commit c82a6b1

Browse files
maang-holgamurraft
authored andcommitted
docs: Standardize MiniMaxEmbeddings (langchain-ai#24983)
- **Description:** Standardize MiniMaxEmbeddings - docs, the issue langchain-ai#24856 - model init arg names, the issue langchain-ai#20085
1 parent f6b7cba commit c82a6b1

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

libs/community/langchain_community/embeddings/minimax.py

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import requests
77
from langchain_core.embeddings import Embeddings
8-
from langchain_core.pydantic_v1 import BaseModel, Extra, SecretStr
8+
from langchain_core.pydantic_v1 import BaseModel, Extra, Field, SecretStr
99
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
1010
from tenacity import (
1111
before_sleep_log,
@@ -45,25 +45,63 @@ def _embed_with_retry(*args: Any, **kwargs: Any) -> Any:
4545

4646

4747
class MiniMaxEmbeddings(BaseModel, Embeddings):
48-
"""MiniMax's embedding service.
48+
"""MiniMax embedding model integration.
4949
50-
To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
51-
``MINIMAX_API_KEY`` set with your API token, or pass it as a named parameter to
52-
the constructor.
50+
Setup:
51+
To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
52+
``MINIMAX_API_KEY`` set with your API token.
53+
54+
.. code-block:: bash
55+
56+
export MINIMAX_API_KEY="your-api-key"
57+
export MINIMAX_GROUP_ID="your-group-id"
58+
59+
Key init args — completion params:
60+
model: Optional[str]
61+
Name of ZhipuAI model to use.
62+
api_key: Optional[str]
63+
Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided.
64+
group_id: Optional[str]
65+
Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided.
66+
67+
See full list of supported init args and their descriptions in the params section.
68+
69+
Instantiate:
5370
54-
Example:
5571
.. code-block:: python
5672
5773
from langchain_community.embeddings import MiniMaxEmbeddings
58-
embeddings = MiniMaxEmbeddings()
5974
60-
query_text = "This is a test query."
61-
query_result = embeddings.embed_query(query_text)
75+
embed = MiniMaxEmbeddings(
76+
model="embo-01",
77+
# api_key="...",
78+
# group_id="...",
79+
# other
80+
)
81+
82+
Embed single text:
83+
.. code-block:: python
6284
63-
document_text = "This is a test document."
64-
document_result = embeddings.embed_documents([document_text])
85+
input_text = "The meaning of life is 42"
86+
embed.embed_query(input_text)
6587
66-
"""
88+
.. code-block:: python
89+
90+
[0.03016241, 0.03617699, 0.0017198119, -0.002061239, -0.00029994643, -0.0061320597, -0.0043635326, ...]
91+
92+
Embed multiple text:
93+
.. code-block:: python
94+
95+
input_texts = ["This is a test query1.", "This is a test query2."]
96+
embed.embed_documents(input_texts)
97+
98+
.. code-block:: python
99+
100+
[
101+
[-0.0021588828, -0.007608119, 0.029349545, -0.0038194496, 0.008031177, -0.004529633, -0.020150753, ...],
102+
[ -0.00023150232, -0.011122423, 0.016930554, 0.0083089275, 0.012633711, 0.019683322, -0.005971041, ...]
103+
]
104+
""" # noqa: E501
67105

68106
endpoint_url: str = "https://api.minimax.chat/v1/embeddings"
69107
"""Endpoint URL to use."""
@@ -74,24 +112,27 @@ class MiniMaxEmbeddings(BaseModel, Embeddings):
74112
embed_type_query: str = "query"
75113
"""For embed_query"""
76114

77-
minimax_group_id: Optional[str] = None
115+
minimax_group_id: Optional[str] = Field(default=None, alias="group_id")
78116
"""Group ID for MiniMax API."""
79-
minimax_api_key: Optional[SecretStr] = None
117+
minimax_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
80118
"""API Key for MiniMax API."""
81119

82120
class Config:
83121
"""Configuration for this pydantic object."""
84122

85123
extra = Extra.forbid
124+
allow_population_by_field_name = True
86125

87126
@pre_init
88127
def validate_environment(cls, values: Dict) -> Dict:
89128
"""Validate that group id and api key exists in environment."""
90129
minimax_group_id = get_from_dict_or_env(
91-
values, "minimax_group_id", "MINIMAX_GROUP_ID"
130+
values, ["minimax_group_id", "group_id"], "MINIMAX_GROUP_ID"
92131
)
93132
minimax_api_key = convert_to_secret_str(
94-
get_from_dict_or_env(values, "minimax_api_key", "MINIMAX_API_KEY")
133+
get_from_dict_or_env(
134+
values, ["minimax_api_key", "api_key"], "MINIMAX_API_KEY"
135+
)
95136
)
96137
values["minimax_group_id"] = minimax_group_id
97138
values["minimax_api_key"] = minimax_api_key
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import cast
2+
3+
from langchain_core.pydantic_v1 import SecretStr
4+
5+
from langchain_community.embeddings import MiniMaxEmbeddings
6+
7+
8+
def test_initialization_with_alias() -> None:
9+
"""Test minimax embedding model initialization with alias."""
10+
api_key = "your-api-key"
11+
group_id = "your-group-id"
12+
13+
embeddings = MiniMaxEmbeddings( # type: ignore[arg-type, call-arg]
14+
api_key=api_key, # type: ignore[arg-type]
15+
group_id=group_id, # type: ignore[arg-type]
16+
)
17+
18+
assert cast(SecretStr, embeddings.minimax_api_key).get_secret_value() == api_key
19+
assert embeddings.minimax_group_id == group_id

0 commit comments

Comments
 (0)