{ "cells": [ { "cell_type": "raw", "id": "0aed0743", "metadata": {}, "source": [ "---\n", "keywords: [AzureOpenAIEmbeddings]\n", "---" ] }, { "cell_type": "markdown", "id": "c3852491", "metadata": {}, "source": [ "# Azure OpenAI\n", "\n", "Let's load the Azure OpenAI Embedding class with environment variables set to indicate to use Azure endpoints." ] }, { "cell_type": "code", "execution_count": 1, "id": "8a6ed30d-806f-4800-b5fd-d04126be9060", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n", "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://.openai.azure.com/\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "20179bc7-3f71-4909-be12-d38bce009b18", "metadata": {}, "outputs": [], "source": [ "from langchain_openai import AzureOpenAIEmbeddings\n", "\n", "embeddings = AzureOpenAIEmbeddings(\n", " azure_deployment=\"\",\n", " openai_api_version=\"2023-05-15\",\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "id": "f8cb9dca-738b-450f-9986-5c3efd3c6eb3", "metadata": {}, "outputs": [], "source": [ "text = \"this is a test document\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "0fae0295-b117-4a5a-8b98-500c79306551", "metadata": {}, "outputs": [], "source": [ "query_result = embeddings.embed_query(text)" ] }, { "cell_type": "code", "execution_count": 5, "id": "65a01ddd-0bbf-444f-a87f-93af25ef902c", "metadata": {}, "outputs": [], "source": [ "doc_result = embeddings.embed_documents([text])" ] }, { "cell_type": "code", "execution_count": 6, "id": "45771052-68ca-4e03-9c4f-a0c7796d9442", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-0.012222584727053133,\n", " 0.0072103982392216145,\n", " -0.014818063280923775,\n", " -0.026444746872933557,\n", " -0.0034330499700826883]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc_result[0][:5]" ] }, { "cell_type": "markdown", "id": "e66ec1f2-6768-4ee5-84bf-a2d76adc20c8", "metadata": {}, "source": [ "## [Legacy] When using `openai<1`" ] }, { "cell_type": "code", "execution_count": null, "id": "1b40f827", "metadata": {}, "outputs": [], "source": [ "# set the environment variables needed for openai package to know to reach out to azure\n", "import os\n", "\n", "os.environ[\"OPENAI_API_TYPE\"] = \"azure\"\n", "os.environ[\"OPENAI_API_BASE\"] = \"https://