{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "7ee94c2f", "metadata": {}, "outputs": [], "source": [ "# Copyright (c) 2026 Microsoft Corporation.\n", "# Licensed under the MIT License." ] }, { "cell_type": "markdown", "id": "a9696211", "metadata": {}, "source": [ "## Basic embedding example\n", "\n", "This examples demonstrates how to generate text embeddings using the GraphRAG LLM library with Azure OpenAI's embedding service. It loads API credentials from environment variables, creates a ModelConfig for the Azure embedding model and configures authentication to use either API key or Azure Managed Identity. The script then creates an embedding client and processes a batch of two text strings (\"Hello world\" and \"How are you?\") to generate their vector embeddings." ] }, { "cell_type": "code", "execution_count": null, "id": "7ed37af8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.0021342115942388773, -0.049084946513175964, 0.020961761474609375]\n", "[0.02755456231534481, -0.026555174961686134, -0.027031073346734047]\n" ] } ], "source": [ "import os\n", "\n", "from graphrag_llm.config.model_config import ModelConfig\n", "from graphrag_llm.config.types import AuthMethod\n", "from graphrag_llm.embedding import LLMEmbedding, create_embedding\n", "from graphrag_llm.types import LLMEmbeddingResponse\n", "\n", "api_key = os.getenv(\"GRAPHRAG_API_KEY\")\n", "api_base = os.getenv(\"GRAPHRAG_API_BASE\")\n", "\n", "embedding_config = ModelConfig(\n", " model_provider=\"azure\",\n", " model=os.getenv(\"GRAPHRAG_EMBEDDING_MODEL\", \"text-embedding-3-small\"),\n", " azure_deployment_name=os.getenv(\n", " \"GRAPHRAG_LLM_EMBEDDING_MODEL\", \"text-embedding-3-small\"\n", " ),\n", " api_base=api_base,\n", " api_version=os.getenv(\"GRAPHRAG_API_VERSION\", \"2025-04-01-preview\"),\n", " api_key=api_key,\n", " auth_method=AuthMethod.AzureManagedIdentity if not api_key else AuthMethod.ApiKey,\n", ")\n", "\n", "llm_embedding: LLMEmbedding = create_embedding(embedding_config)\n", "\n", "embeddings_batch: LLMEmbeddingResponse = llm_embedding.embedding(\n", " input=[\"Hello world\", \"How are you?\"]\n", ")\n", "for data in embeddings_batch.data:\n", " print(data.embedding[0:3])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }