Große Auswahl an Modelle: → Link

image.png

Ich kann mir einfach ein Model aussuchen:

image.png

Option A

In meinem Windows-PC kann ich mich einloggen mit az login. Mehr dazu siehe Azure CLI.

Im Script verwende ich das Model folgendermaßen:

from langchain_openai import AzureChatOpenAI
from azure.identity import DefaultAzureCredential
import os
from dotenv import load_dotenv  # Add this import

# Load environment variables from .env file
load_dotenv()  # Add this line

# Get the Azure Credential
credential = DefaultAzureCredential(exclude_managed_identity_credential=True, exclude_shared_token_cache_credential=True)

# Set the API type to `azure_ad`
os.environ["OPENAI_API_TYPE"] = "azure_ad"
# Set the API_KEY to the token from the Azure credential
os.environ["AZURE_OPENAI_API_KEY"] = credential.get_token("<https://cognitiveservices.azure.com/.default>").token

llm = AzureChatOpenAI(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
    openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
    streaming=True
)

messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg = ai_msg.content
print(f"AI Response: {ai_msg}")

Das .env file sieht so aus:

AZURE_OPENAI_ENDPOINT="<https://ai-openai-prd-swecen-001.openai.azure.com/>"
AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini-stable"
AZURE_OPENAI_API_VERSION="2024-06-01"

Option B (nicht getestet)