使用 語意核心 與 Microsoft Foundry(經典版)開發應用程式

僅適用於:Foundry(經典)入口。 這篇文章無法在新的 Foundry 入口網站中提供。 了解更多關於新入口網站的資訊。

本文中的連結可能會開啟新版 Microsoft Foundry 文件的內容,而非您目前正在瀏覽的 Foundry(經典版)文件。

在本文中,您將學習如何使用 語意核心,搭配從 Microsoft Foundry 入口網站中的模型目錄部署的模型。

重要

Azure AI Inference beta SDK 已被棄用,將於 2026 年 8 月 26 日正式退休。 切換到普遍可用的 OpenAI/v1 API ,搭配穩定的 OpenAI SDK。 依照 遷移指南 切換到 OpenAI/v1,使用 SDK 搭配你偏好的程式語言。

先決條件

  • 一個有有效訂閱的 Azure 帳號。 如果你還沒有,請建立一個free Azure帳號,其中包含免費試用訂閱

  • 在 Foundry 平台中創建專案所述的 Foundry 專案。

  • 一個已部署的模型,支援 Azure AI 模型推論 API。 本文使用部署方式 Mistral-Large 。 你可以用任何型號。 如果要在 LlamaIndex 中使用嵌入功能,你需要嵌入模型,例如 cohere-embed-v3-multilingual

  • 安裝Python 3.10或更新版本,包含 pip。

  • 語意核心 已安裝。 你可以使用以下指令:

    pip install semantic-kernel
    
  • 本文使用模型推理 API,請安裝相關的 Azure 相依關係。 你可以使用以下指令:

    pip install semantic-kernel[azure]
    

設定環境

要使用部署在 Foundry 入口網站的語言模型,你需要端點和憑證才能連接到你的專案。 請依照以下步驟取得模特兒所需的資訊:

提示

因為你可以在 Microsoft Foundry 入口網站中自訂左側窗格,所以你可能會看到與這些步驟中顯示的不同物品。 如果你找不到你想要的,選擇 ⋯⋯更多選項 在左側窗格底部。

  1. 登入 Microsoft Foundry

  2. 如果模型尚未開啟,請打開該模型部署的專案。

  3. Models + endpoints ,依照前置條件選擇你部署的模型。

  4. 複製端點的網址和金鑰。

    提示

    如果你部署的模型支援 Microsoft Entra ID,就不需要金鑰。

此範例同時使用了端點 URL 與金鑰的環境變數:

export AZURE_AI_INFERENCE_ENDPOINT="<your-model-endpoint-goes-here>"
export AZURE_AI_INFERENCE_API_KEY="<your-key-goes-here>"

在你設定端點和金鑰後,建立一個客戶端來連接端點:

from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion

chat_completion_service = AzureAIInferenceChatCompletion(ai_model_id="<deployment-name>")

提示

客戶端會自動讀取環境變數 AZURE_AI_INFERENCE_ENDPOINTAZURE_AI_INFERENCE_API_KEY 連接到模型。 你可以直接用 endpoint 建構子上的 and api_key 參數,將端點和金鑰傳給客戶端。

或者,如果你的端點支援 Microsoft Entra ID,你可以使用以下程式碼建立客戶端:

export AZURE_AI_INFERENCE_ENDPOINT="<your-model-endpoint-goes-here>"
from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion

chat_completion_service = AzureAIInferenceChatCompletion(ai_model_id="<deployment-name>")

如果你使用 Microsoft Entra ID,請確保端點已部署使用該認證方法,且你擁有呼叫該方法所需的權限。

Azure OpenAI 模型

如果你使用的是 Azure OpenAI 模型,請使用以下程式碼來建立客戶端:

from azure.ai.inference.aio import ChatCompletionsClient
from azure.identity.aio import DefaultAzureCredential

from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion

chat_completion_service = AzureAIInferenceChatCompletion(
    ai_model_id="<deployment-name>",
    client=ChatCompletionsClient(
        endpoint=f"{str(<your-azure-open-ai-endpoint>).strip('/')}/openai/deployments/{<deployment_name>}",
        credential=DefaultAzureCredential(),
        credential_scopes=["https://cognitiveservices.azure.com/.default"],
    ),
)

推論參數

你可以透過以下 AzureAIInferenceChatPromptExecutionSettings 類別來配置如何進行推論:

from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatPromptExecutionSettings

execution_settings = AzureAIInferenceChatPromptExecutionSettings(
    max_tokens=100,
    temperature=0.5,
    top_p=0.9,
    # extra_parameters={...},    # model-specific parameters
)

呼叫服務

首先,打電話給聊天完成服務,並提供簡單的聊天紀錄:

提示

語意核心 是一個非同步函式庫,所以你需要使用 asyncio 函式庫來執行程式碼。

import asyncio

async def main():
    ...

if __name__ == "__main__":
    asyncio.run(main())
from semantic_kernel.contents.chat_history import ChatHistory

chat_history = ChatHistory()
chat_history.add_user_message("Hello, how are you?")

response = await chat_completion_service.get_chat_message_content(
    chat_history=chat_history,
    settings=execution_settings,
)
print(response)

或者,您也可以串流服務的回應內容:

chat_history = ChatHistory()
chat_history.add_user_message("Hello, how are you?")

response = chat_completion_service.get_streaming_chat_message_content(
    chat_history=chat_history,
    settings=execution_settings,
)

chunks = []
async for chunk in response:
    chunks.append(chunk)
    print(chunk, end="")

full_response = sum(chunks[1:], chunks[0])

建立一場長期的對話

你可以透過使用循環來建立一段長時間的對話:

while True:
    response = await chat_completion_service.get_chat_message_content(
        chat_history=chat_history,
        settings=execution_settings,
    )
    print(response)
    chat_history.add_message(response)
    chat_history.add_user_message(user_input = input("User:> "))

如果你正在串流回應,可以使用以下代碼:

while True:
    response = chat_completion_service.get_streaming_chat_message_content(
        chat_history=chat_history,
        settings=execution_settings,
    )

    chunks = []
    async for chunk in response:
        chunks.append(chunk)
        print(chunk, end="")

    full_response = sum(chunks[1:], chunks[0])
    chat_history.add_message(full_response)
    chat_history.add_user_message(user_input = input("User:> "))

使用嵌入模型

配置環境的方式與前述步驟類似,但請使用以下 AzureAIInferenceEmbeddings 類別:

from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceTextEmbedding

embedding_generation_service = AzureAIInferenceTextEmbedding(ai_model_id="<deployment-name>")

以下程式碼說明如何從服務取得嵌入:

embeddings = await embedding_generation_service.generate_embeddings(
    texts=["My favorite color is blue.", "I love to eat pizza."],
)

for embedding in embeddings:
    print(embedding)