Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Standardize MiniMaxEmbeddings #24983

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions libs/community/langchain_community/embeddings/minimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import requests
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 tenacity import (
before_sleep_log,
Expand Down Expand Up @@ -45,25 +45,63 @@ def _embed_with_retry(*args: Any, **kwargs: Any) -> Any:


class MiniMaxEmbeddings(BaseModel, Embeddings):
"""MiniMax's embedding service.
"""MiniMax embedding model integration.

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
the constructor.
Setup:
To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
``MINIMAX_API_KEY`` set with your API token.

.. 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

from langchain_community.embeddings import MiniMaxEmbeddings
embeddings = MiniMaxEmbeddings()

query_text = "This is a test query."
query_result = embeddings.embed_query(query_text)
embed = MiniMaxEmbeddings(
model="embo-01",
# api_key="...",
# group_id="...",
# other
)

Embed single text:
.. code-block:: python

document_text = "This is a test document."
document_result = embeddings.embed_documents([document_text])
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 to use."""
Expand All @@ -74,24 +112,27 @@ class MiniMaxEmbeddings(BaseModel, Embeddings):
embed_type_query: str = "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."""
minimax_api_key: Optional[SecretStr] = None
minimax_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
"""API Key for MiniMax API."""

class Config:
"""Configuration for this pydantic object."""

extra = Extra.forbid
allow_population_by_field_name = True

@pre_init
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that group id and api key exists in environment."""
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(
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_api_key"] = minimax_api_key
Expand Down
19 changes: 19 additions & 0 deletions libs/community/tests/integration_tests/embeddings/test_minimax.py
Original file line number Diff line number Diff line change
@@ -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