Skip to content

Commit a9124bd

Browse files
ajac-zeroAnibaltylerhutcherson
authored
Add support for AzureOpenAI (#131)
**Objective** Added a new TextVectorizer for the AzureOpenAI API, which is based on the AzureOpenAI and AsyncAzureOpenAI classes from openai>=1.0.0. **Reason** Compatibility with Azure OpenAI is an important feature for developers that are building enterprise AI applications with Azure Cloud. Particularly for use cases where privacy is a concern, and data must stay within their cloud tenant. My team particularly wants to integrate Semantic Cache (with Azure Cache for Redis Enterprise). Since we rely on the Azure OpenAI API, I created this new vectorizer to add that functionality. --------- Co-authored-by: Anibal <a8065384@banorte.com> Co-authored-by: Tyler Hutcherson <tyler.hutcherson@redis.com>
1 parent 4f1dc6c commit a9124bd

File tree

5 files changed

+422
-2
lines changed

5 files changed

+422
-2
lines changed

conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def client(redis_url):
2121
def openai_key():
2222
return os.getenv("OPENAI_API_KEY")
2323

24+
@pytest.fixture
25+
def openai_version():
26+
return os.getenv("OPENAI_API_VERSION")
27+
28+
@pytest.fixture
29+
def azure_endpoint():
30+
return os.getenv("AZURE_OPENAI_ENDPOINT")
31+
2432
@pytest.fixture
2533
def cohere_key():
2634
return os.getenv("COHERE_API_KEY")

docs/user_guide/vectorizers_04.ipynb

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 2,
67+
"execution_count": 3,
6868
"metadata": {},
6969
"outputs": [],
7070
"source": [
@@ -176,6 +176,115 @@
176176
"print(\"Number of Embeddings:\", len(embeddings))\n"
177177
]
178178
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"### Azure OpenAI\n",
184+
"\n",
185+
"The ``AzureOpenAITextVectorizer`` is a variation of the OpenAI vectorizer that calls OpenAI models within Azure. If you've already installed ``openai``, then you're ready to use Azure OpenAI.\n",
186+
"\n",
187+
"The only practical difference between OpenAI and Azure OpenAI is the variables required to call the API."
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 4,
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"# additionally to the API Key, setup the API endpoint and version\n",
197+
"api_version = os.environ.get(\"OPENAI_API_VERSION\") or getpass.getpass(\"Enter your AzureOpenAI API version: \")\n",
198+
"azure_endpoint = os.environ.get(\"AZURE_OPENAI_ENDPOINT\") or getpass.getpass(\"Enter your AzureOpenAI API endpoint: \")"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": 5,
204+
"metadata": {},
205+
"outputs": [
206+
{
207+
"name": "stdout",
208+
"output_type": "stream",
209+
"text": [
210+
"Vector dimensions: 1536\n"
211+
]
212+
},
213+
{
214+
"data": {
215+
"text/plain": [
216+
"[-0.0010088568087667227,\n",
217+
" -0.003142790636047721,\n",
218+
" 0.0024922797456383705,\n",
219+
" -0.004522906616330147,\n",
220+
" -0.010369433090090752,\n",
221+
" 0.012739036232233047,\n",
222+
" -0.005365503951907158,\n",
223+
" -0.0029668458737432957,\n",
224+
" -0.007141091860830784,\n",
225+
" -0.03383301943540573]"
226+
]
227+
},
228+
"execution_count": 5,
229+
"metadata": {},
230+
"output_type": "execute_result"
231+
}
232+
],
233+
"source": [
234+
"from redisvl.utils.vectorize import AzureOpenAITextVectorizer\n",
235+
"\n",
236+
"# create a vectorizer\n",
237+
"az_oai = AzureOpenAITextVectorizer(\n",
238+
" model=\"text-embedding-ada-002\", # Must be your custom deployment name\n",
239+
" api_config={\n",
240+
" \"api_key\": api_key,\n",
241+
" \"api_version\": api_version,\n",
242+
" \"azure_endpoint\": azure_endpoint\n",
243+
" },\n",
244+
")\n",
245+
"\n",
246+
"test = az_oai.embed(\"This is a test sentence.\")\n",
247+
"print(\"Vector dimensions: \", len(test))\n",
248+
"test[:10]"
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": 6,
254+
"metadata": {},
255+
"outputs": [
256+
{
257+
"data": {
258+
"text/plain": [
259+
"[-0.017460526898503304,\n",
260+
" -6.895032856846228e-05,\n",
261+
" 0.0013909287517890334,\n",
262+
" -0.025688467547297478,\n",
263+
" -0.019813183695077896,\n",
264+
" 0.016087085008621216,\n",
265+
" -0.003729278687387705,\n",
266+
" 0.0009211922879330814,\n",
267+
" 0.006606514099985361,\n",
268+
" -0.025128915905952454]"
269+
]
270+
},
271+
"execution_count": 6,
272+
"metadata": {},
273+
"output_type": "execute_result"
274+
}
275+
],
276+
"source": [
277+
"# Just like OpenAI, AzureOpenAI supports batching embeddings and asynchronous requests.\n",
278+
"sentences = [\n",
279+
" \"That is a happy dog\",\n",
280+
" \"That is a happy person\",\n",
281+
" \"Today is a sunny day\"\n",
282+
"]\n",
283+
"\n",
284+
"embeddings = await az_oai.aembed_many(sentences)\n",
285+
"embeddings[0][:10]"
286+
]
287+
},
179288
{
180289
"cell_type": "markdown",
181290
"metadata": {},
@@ -547,7 +656,7 @@
547656
"name": "python",
548657
"nbconvert_exporter": "python",
549658
"pygments_lexer": "ipython3",
550-
"version": "3.9.12"
659+
"version": "3.11.5"
551660
},
552661
"orig_nbformat": 4,
553662
"vscode": {

redisvl/utils/vectorize/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from redisvl.utils.vectorize.base import BaseVectorizer
2+
from redisvl.utils.vectorize.text.azureopenai import AzureOpenAITextVectorizer
23
from redisvl.utils.vectorize.text.cohere import CohereTextVectorizer
34
from redisvl.utils.vectorize.text.huggingface import HFTextVectorizer
45
from redisvl.utils.vectorize.text.openai import OpenAITextVectorizer
@@ -10,4 +11,5 @@
1011
"HFTextVectorizer",
1112
"OpenAITextVectorizer",
1213
"VertexAITextVectorizer",
14+
"AzureOpenAITextVectorizer",
1315
]

0 commit comments

Comments
 (0)