5
5
6
6
import requests
7
7
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
9
9
from langchain_core .utils import convert_to_secret_str , get_from_dict_or_env , pre_init
10
10
from tenacity import (
11
11
before_sleep_log ,
@@ -45,25 +45,63 @@ def _embed_with_retry(*args: Any, **kwargs: Any) -> Any:
45
45
46
46
47
47
class MiniMaxEmbeddings (BaseModel , Embeddings ):
48
- """MiniMax's embedding service .
48
+ """MiniMax embedding model integration .
49
49
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:
53
70
54
- Example:
55
71
.. code-block:: python
56
72
57
73
from langchain_community.embeddings import MiniMaxEmbeddings
58
- embeddings = MiniMaxEmbeddings()
59
74
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
62
84
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 )
65
87
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
67
105
68
106
endpoint_url : str = "https://api.minimax.chat/v1/embeddings"
69
107
"""Endpoint URL to use."""
@@ -74,24 +112,27 @@ class MiniMaxEmbeddings(BaseModel, Embeddings):
74
112
embed_type_query : str = "query"
75
113
"""For embed_query"""
76
114
77
- minimax_group_id : Optional [str ] = None
115
+ minimax_group_id : Optional [str ] = Field ( default = None , alias = "group_id" )
78
116
"""Group ID for MiniMax API."""
79
- minimax_api_key : Optional [SecretStr ] = None
117
+ minimax_api_key : Optional [SecretStr ] = Field ( default = None , alias = "api_key" )
80
118
"""API Key for MiniMax API."""
81
119
82
120
class Config :
83
121
"""Configuration for this pydantic object."""
84
122
85
123
extra = Extra .forbid
124
+ allow_population_by_field_name = True
86
125
87
126
@pre_init
88
127
def validate_environment (cls , values : Dict ) -> Dict :
89
128
"""Validate that group id and api key exists in environment."""
90
129
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"
92
131
)
93
132
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
+ )
95
136
)
96
137
values ["minimax_group_id" ] = minimax_group_id
97
138
values ["minimax_api_key" ] = minimax_api_key
0 commit comments