แชร์ผ่าน


ใช้ Azure OpenAI ใน Fabric กับ REST API (ตัวอย่าง)

สำคัญ

คุณลักษณะนี้อยู่ในตัวอย่าง

เอกสารนี้แสดงตัวอย่างวิธีการใช้ Azure OpenAI ใน Fabric โดยใช้ REST API

การเริ่มต้น

from synapse.ml.fabric.service_discovery import get_fabric_env_config
from synapse.ml.fabric.token_utils import TokenUtils

fabric_env_config = get_fabric_env_config().fabric_env_config
auth_header_value = TokenUtils().get_openai_auth_header()

auth_headers = {
    "Authorization": auth_header_value,
    "Content-Type": "application/json"
}

แชท

GPT-4.1 และ GPT-4.1-mini เป็นโมเดลภาษาที่ปรับให้เหมาะกับอินเทอร์เฟซการสนทนา ใช้ GPT-5 สําหรับความสามารถในการให้เหตุผล

import requests

def print_chat_result(messages, response_code, response):
    print("=" * 90)
    print("| OpenAI Input    |")
    for msg in messages:
        if msg["role"] == "system":
            print("[System]", msg["content"])
        elif msg["role"] == "user":
            print("Q:", msg["content"])
        else:
            print("A:", msg["content"])
    print("-" * 90)
    print("| Response Status |", response_code)
    print("-" * 90)
    print("| OpenAI Output   |")
    if response.status_code == 200:
        print(response.json()["choices"][0]["message"]["content"])
    else:
        print(response.content)
    print("=" * 90)

deployment_name = "gpt-4.1"

openai_url = (
    f"{fabric_env_config.ml_workload_endpoint}cognitive/openai/openai/deployments/"
    f"{deployment_name}/chat/completions?api-version=2024-02-15-preview"
)

payload = {
    "messages": [
        {"role": "system", "content": "You are an AI assistant that helps people find information."},
        {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"}
    ]
}

response = requests.post(openai_url, headers=auth_headers, json=payload)
print_chat_result(payload["messages"], response.status_code, response)

เอาท์พุท

==========================================================================================
| OpenAI Input    |
[System] You are an AI assistant that helps people find information.
Q: Does Azure OpenAI support customer managed keys?
------------------------------------------------------------------------------------------
| Response Status | 200
------------------------------------------------------------------------------------------
| OpenAI Output   |
Yes, **Azure OpenAI Service** supports **customer managed keys (CMK)** for encrypting your data at rest. This allows organizations to control and manage their own encryption keys using **Azure Key Vault**. By integrating with Azure Key Vault, you can bring your own keys (BYOK) to have full control over the encryption of data stored and processed by Azure OpenAI.

**Reference:**
- [Azure OpenAI encryption documentation - Microsoft Learn](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/security#encryption)
- [Azure OpenAI Security - Microsoft Learn](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/security)

**Key features:**
- Data at rest is encrypted by default with a service-managed key.
- You can specify your own customer managed key (CMK) in Azure Key Vault for additional control.
- Supported for both Standard and Enterprise Azure OpenAI deployments.

**Summary:**  
You can use customer managed keys with Azure OpenAI for enhanced security and regulatory compliance.
==========================================================================================

การฝัง

การฝังเป็นรูปแบบการแสดงข้อมูลพิเศษที่แบบจําลองการเรียนรู้ของเครื่องและอัลกอริทึมสามารถใช้งานได้อย่างง่ายดาย ซึ่งประกอบด้วยความหมายเชิงความหมายที่สมบูรณ์ของข้อความ ซึ่งแสดงโดยเวกเตอร์ของตัวเลขจุดทศนิยมลอยตัว ระยะห่างระหว่างสองการฝังในพื้นที่เวกเตอร์นั้นเกี่ยวข้องกับความคล้ายคลึงกันเชิงความหมายระหว่างอินพุตเดิมสองตัว ตัวอย่างเช่น หากข้อความสองข้อความมีความคล้ายคลึงกัน ตัวแทนเวกเตอร์ของข้อความควรคล้ายกัน

หากต้องการเข้าถึงจุดสิ้นสุดการฝัง Azure OpenAI ใน Fabric คุณสามารถส่งคําขอ API โดยใช้รูปแบบต่อไปนี้:

POST <url_prefix>/openai/deployments/<deployment_name>/embeddings?api-version=2024-02-01

deployment_nameอาจเป็นtext-embedding-ada-002

from synapse.ml.fabric.service_discovery import get_fabric_env_config
from synapse.ml.fabric.token_utils import TokenUtils
import requests


fabric_env_config = get_fabric_env_config().fabric_env_config

auth_header_value = TokenUtils().get_openai_auth_header()
auth_headers = {
    "Authorization": auth_header_value,
    "Content-Type": "application/json"
}

def print_embedding_result(prompts, response_code, response):
    print("=" * 90)
    print("| OpenAI Input    |\n\t" + "\n\t".join(prompts))
    print("-" * 90)
    print("| Response Status |", response_code)
    print("-" * 90)
    print("| OpenAI Output   |")
    if response_code == 200:
        for data in response.json()['data']:
            print("\t[" + ", ".join([f"{n:.8f}" for n in data["embedding"][:10]]) + ", ... ]")
    else:
        print(response.content)
    print("=" * 90)

deployment_name = "text-embedding-ada-002"
openai_url = (
    f"{fabric_env_config.ml_workload_endpoint}cognitive/openai/openai/deployments/"
    f"{deployment_name}/embeddings?api-version=2024-02-15-preview"
)

payload = {
    "input": [
        "empty prompt, need to fill in the content before the request",
        "Once upon a time"
    ]
}

response = requests.post(openai_url, headers=auth_headers, json=payload)
print_embedding_result(payload["input"], response.status_code, response)

เอาท์พุท

==========================================================================================
| OpenAI Input    |
	empty prompt, need to fill in the content before the request
	Once upon a time
------------------------------------------------------------------------------------------
| Response Status | 200
------------------------------------------------------------------------------------------
| OpenAI Output   |
	[-0.00263638, -0.00441368, -0.01702866, 0.00410065, -0.03052361, 0.01894856, -0.01293149, -0.01421838, -0.03505902, -0.01835033, ... ]
	[0.02133885, -0.02018847, -0.00464259, -0.01151640, -0.01114348, 0.00194205, 0.00229917, -0.01371602, 0.00857094, -0.01467678, ... ]
==========================================================================================