Mistral

MistralEmbeddingClient 使用 Mistral AI 模型生成文字嵌入。 可用於向量索引、語意搜尋、叢集或其他需要 Agent Framework 嵌入客戶端的應用程式。

該供應商目前僅提供嵌入;它沒有提供 Agent Framework 聊天客戶端。

安裝套件

pip install agent-framework-mistral --pre

Configuration

MISTRAL_API_KEY="<api-key>"
MISTRAL_EMBEDDING_MODEL="mistral-embed"
# Optional compatible endpoint:
MISTRAL_SERVER_URL="<server-url>"

產生內嵌

建立客戶端並呼叫 get_embeddings()

async def basic_embedding_example() -> None:
    """Generate embeddings for a list of texts."""
    print("=== Basic Embedding Generation ===")

    # 1. Create the embedding client using environment-based configuration.
    client = MistralEmbeddingClient()

    # 2. Generate embeddings for multiple texts.
    texts = ["Hello, world!", "How are you?", "Agent Framework with Mistral AI"]
    try:
        result = await client.get_embeddings(texts)

        # 3. Print the generated vectors and usage metadata.
        print(f"Generated {len(result)} embeddings")
        for i, embedding in enumerate(result):
            print(f"  Text {i + 1}: dimensions={embedding.dimensions}, vector={embedding.vector[:5]}...")

        if result.usage:
            print(
                f"  Usage: {result.usage['input_token_count']} input tokens, "
                f"{result.usage['total_token_count']} total tokens"
            )
    finally:
        await client.close()


async def embedding_with_options_example() -> None:
    """Generate embeddings with custom dimensions."""
    print("\n=== Embedding with Custom Dimensions ===")

    from agent_framework.mistral import MistralEmbeddingOptions

    # Only some models support a custom output dimension (e.g. codestral-embed; mistral-embed does not).
    client = MistralEmbeddingClient(model="codestral-embed")

    options: MistralEmbeddingOptions = {"dimensions": 256}

用來 MistralEmbeddingOptions 請求支援的輸出維度。 你也可以設定 MISTRAL_SERVER_URL 應用程式何時使用相容的客製化端點。

Important

Mistral AI 是第三方系統。 在傳送應用程式資料前,請先審查其服務條款、資料處理、區域邊界、模型授權及使用成本。

工具

工具不適用,因為這個套件目前提供的是嵌入客戶端,而不是 Agent Framework 聊天客戶端。

下一步