Skip to main content

LlamaIndex Llms Integration: Azure Openai

Installation

%pip install llama-index-llms-azure-openai
!pip install llama-index

Prerequisites

Follow this to setup your Azure account: Setup Azure account

Set the environment variables

OPENAI_API_VERSION = "2023-07-01-preview"
AZURE_OPENAI_ENDPOINT = "https://YOUR_RESOURCE_NAME.openai.azure.com/"
OPENAI_API_KEY = "<your-api-key>"

import os

os.environ["OPENAI_API_KEY"] = "<your-api-key>"
os.environ[
    "AZURE_OPENAI_ENDPOINT"
] = "https://<your-resource-name>.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"

# Use your LLM
from llama_index.llms.azure_openai import AzureOpenAI

# Unlike normal OpenAI, you need to pass an engine argument in addition to model.
# The engine is the name of your model deployment you selected in Azure OpenAI Studio.

llm = AzureOpenAI(
    engine="simon-llm", model="gpt-35-turbo-16k", temperature=0.0
)

# Alternatively, you can also skip setting environment variables, and pass the parameters in directly via constructor.
llm = AzureOpenAI(
    engine="my-custom-llm",
    model="gpt-35-turbo-16k",
    temperature=0.0,
    azure_endpoint="https://<your-resource-name>.openai.azure.com/",
    api_key="<your-api-key>",
    api_version="2023-07-01-preview",
)

# Use the complete endpoint for text completion
response = llm.complete("The sky is a beautiful blue and")
print(response)

# Expected Output:
# the sun is shining brightly. Fluffy white clouds float lazily across the sky,
# creating a picturesque scene. The vibrant blue color of the sky brings a sense
# of calm and tranquility...

Streaming completion

response = llm.stream_complete("The sky is a beautiful blue and")
for r in response:
    print(r.delta, end="")

# Expected Output (Stream):
# the sun is shining brightly. Fluffy white clouds float lazily across the sky,
# creating a picturesque scene. The vibrant blue color of the sky brings a sense
# of calm and tranquility...

# Use the chat endpoint for conversation
from llama_index.core.llms import ChatMessage

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality."
    ),
    ChatMessage(role="user", content="Hello"),
]

response = llm.chat(messages)
print(response)

# Expected Output:
# assistant: Ahoy there, matey! How be ye on this fine day? I be Captain Jolly Roger,
# the most colorful pirate ye ever did lay eyes on! What brings ye to me ship?

Streaming chat

response = llm.stream_chat(messages)
for r in response:
    print(r.delta, end="")

# Expected Output (Stream):
# Ahoy there, matey! How be ye on this fine day? I be Captain Jolly Roger,
# the most colorful pirate ye ever did lay eyes on! What brings ye to me ship?

# Rather than adding the same parameters to each chat or completion call,
# you can set them at a per-instance level with additional_kwargs.
llm = AzureOpenAI(
    engine="simon-llm",
    model="gpt-35-turbo-16k",
    temperature=0.0,
    additional_kwargs={"user": "your_user_id"},
)

LLM Implementation example

https://docs.llamaindex.ai/en/stable/examples/llm/azure_openai/

Metadata

Release files for basejump-llama-index-llms-azure-openai 0.3.2.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for basejump-llama-index-llms-azure-openai 0.3.2.post1
File Size Uploaded
basejump_llama_index_llms_azure_openai-0.3.2.post1.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for basejump-llama-index-llms-azure-openai 0.3.2.post1
File Interpreter ABI Platform
basejump_llama_index_llms_azure_openai-0.3.2.post1-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / basejump_llama_index_llms_azure_openai-0.3.2.post1.tar.gz

Download URL basejump_llama_index_llms_azure_openai-0.3.2.post1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8d68b5266f8b6b460486810aac1b792ec86fd0d0e3e8d828d44a08bf5ace78a6
BLAKE2b-256 checksum
How to use checksums
feb382e2955e52e9596ac4ae88d2b565d8a9ad1acbfe112d4ccef05bd76d05d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / basejump_llama_index_llms_azure_openai-0.3.2.post1-py3-none-any.whl

Download URL basejump_llama_index_llms_azure_openai-0.3.2.post1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e2f221caa68db511f0a21348b193db41d28e2472d40a082e9a5cfa5ea9a9077
BLAKE2b-256 checksum
How to use checksums
3d6afce18de8a612574cc23b9bf9601c19e9429cc105f1808d0bb112c9398326
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.3.2.post1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page