using Microsoft.SemanticKernel;
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddAzureOpenAIChatCompletion(
deploymentName: "NAME_OF_YOUR_DEPLOYMENT",
apiKey: "YOUR_API_KEY",
endpoint: "YOUR_AZURE_ENDPOINT",
modelId: "gpt-4", // Optional name of the underlying model if the deployment name doesn't match the model name
serviceId: "YOUR_SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
Kernel kernel = kernelBuilder.Build();
using Microsoft.SemanticKernel;
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: "gpt-4",
apiKey: "YOUR_API_KEY",
orgId: "YOUR_ORG_ID", // Optional
serviceId: "YOUR_SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
Kernel kernel = kernelBuilder.Build();
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOllamaChatCompletion(
modelId: "NAME_OF_MODEL", // E.g. "phi3" if phi3 was downloaded as described above.
endpoint: new Uri("YOUR_ENDPOINT"), // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
Kernel kernel = kernelBuilder.Build();
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddBedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime, // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
Kernel kernel = kernelBuilder.Build();
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddBedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime, // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
Kernel kernel = kernelBuilder.Build();
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOnnxRuntimeGenAIChatCompletion(
modelId: "NAME_OF_MODEL", // E.g. phi-3
modelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
jsonSerializerOptions: customJsonSerializerOptions // Optional; for providing custom serialization settings for e.g. function argument / result serialization and parsing.
);
Kernel kernel = kernelBuilder.Build();
對於支援 OpenAI 聊天完成 API 的其他 AI 服務提供者(例如 LLM Studio),您可以使用下列程式代碼重複使用現有的 OpenAI 聊天完成連接器。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0010
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: "NAME_OF_MODEL",
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Used to point to your service
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
Kernel kernel = kernelBuilder.Build();
使用相依性插入
如果您使用相依性插入,您可能會想要將 AI 服務直接新增至服務提供者。 如果您想要建立 AI 服務的單一專案,並在暫時性核心中重複使用它們,這會很有説明。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAzureOpenAIChatCompletion(
deploymentName: "NAME_OF_YOUR_DEPLOYMENT",
apiKey: "YOUR_API_KEY",
endpoint: "YOUR_AZURE_ENDPOINT",
modelId: "gpt-4", // Optional name of the underlying model if the deployment name doesn't match the model name
serviceId: "YOUR_SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddOpenAIChatCompletion(
modelId: "gpt-4",
apiKey: "YOUR_API_KEY",
orgId: "YOUR_ORG_ID", // Optional; for OpenAI deployment
serviceId: "YOUR_SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddOllamaChatCompletion(
modelId: "NAME_OF_MODEL", // E.g. "phi3" if phi3 was downloaded as described above.
endpoint: new Uri("YOUR_ENDPOINT"), // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddBedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime, // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddBedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime, // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddOnnxRuntimeGenAIChatCompletion(
modelId: "NAME_OF_MODEL", // E.g. phi-3
modelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
jsonSerializerOptions: customJsonSerializerOptions // Optional; for providing custom serialization settings for e.g. function argument / result serialization and parsing.
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
對於支援 OpenAI 聊天完成 API 的其他 AI 服務提供者(例如 LLM Studio),您可以使用下列程式代碼重複使用現有的 OpenAI 聊天完成連接器。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0010
builder.Services.AddOpenAIChatCompletion(
modelId: "NAME_OF_MODEL",
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Used to point to your service
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
AzureOpenAIChatCompletionService chatCompletionService = new (
deploymentName: "NAME_OF_YOUR_DEPLOYMENT",
apiKey: "YOUR_API_KEY",
endpoint: "YOUR_AZURE_ENDPOINT",
modelId: "gpt-4", // Optional name of the underlying model if the deployment name doesn't match the model name
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
using Microsoft.SemanticKernel.Connectors.OpenAI;
OpenAIChatCompletionService chatCompletionService = new (
modelId: "gpt-4",
apiKey: "YOUR_API_KEY",
organization: "YOUR_ORG_ID", // Optional
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
using Microsoft.SemanticKernel.Connectors.HuggingFace;
#pragma warning disable SKEXP0070
HuggingFaceChatCompletionService chatCompletionService = new (
model: "NAME_OF_MODEL",
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT") // Optional
);
重要
Azure AI 推斷聊天完成連接器目前為實驗性。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Connectors.AzureAIInference;
#pragma warning disable SKEXP0070
AzureAIInferenceChatCompletionService chatCompletionService = new (
modelId: "YOUR_MODEL_ID",
apiKey: "YOUR_API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Used to point to your service
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
using Microsoft.SemanticKernel.ChatCompletion;
using OllamaSharp;
#pragma warning disable SKEXP0070
using var ollamaClient = new OllamaApiClient(
uriString: "YOUR_ENDPOINT" // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
defaultModel: "NAME_OF_MODEL" // E.g. "phi3" if phi3 was downloaded as described above.
);
IChatCompletionService chatCompletionService = ollamaClient.AsChatCompletionService();
using Microsoft.SemanticKernel.Connectors.Amazon;
#pragma warning disable SKEXP0070
BedrockChatCompletionService chatCompletionService = new BedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
);
using Microsoft.SemanticKernel.Connectors.Amazon;
#pragma warning disable SKEXP0070
BedrockChatCompletionService chatCompletionService = new BedrockChatCompletionService(
modelId: "NAME_OF_MODEL",
bedrockRuntime: amazonBedrockRuntime // Optional; An instance of IAmazonBedrockRuntime, used to communicate with Azure Bedrock.
);
using Microsoft.SemanticKernel.Connectors.Onnx;
#pragma warning disable SKEXP0070
OnnxRuntimeGenAIChatCompletionService chatCompletionService = new OnnxRuntimeGenAIChatCompletionService(
modelId: "NAME_OF_MODEL", // E.g. phi-3
modelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32
jsonSerializerOptions: customJsonSerializerOptions // Optional; for providing custom serialization settings for e.g. function argument / result serialization and parsing.
);
對於支援 OpenAI 聊天完成 API 的其他 AI 服務提供者(例如 LLM Studio),您可以使用下列程式代碼重複使用現有的 OpenAI 聊天完成連接器。
using Microsoft.SemanticKernel.Connectors.OpenAI;
#pragma warning disable SKEXP0010
OpenAIChatCompletionService chatCompletionService = new (
modelId: "gpt-4",
apiKey: "YOUR_API_KEY",
organization: "YOUR_ORG_ID", // Optional
endpoint: new Uri("YOUR_ENDPOINT"), // Used to point to your service
httpClient: new HttpClient() // Optional; if not provided, the HttpClient from the kernel will be used
);
若要建立聊天完成服務,您必須匯入必要的模組,並建立服務的實例。 以下是為每個 AI 服務提供者建立聊天完成服務的步驟。
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
chat_completion_service = AzureChatCompletion(
deployment_name="my-deployment",
api_key="my-api-key",
endpoint="my-api-endpoint", # Used to point to your service
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
# You can do the following if you have set the necessary environment variables or created a .env file
chat_completion_service = AzureChatCompletion(service_id="my-service-id")
注意
AzureChatCompletion 服務也支援 Microsoft Entra 驗證。 如果您沒有提供 API 金鑰,服務會嘗試使用 Entra 令牌進行驗證。
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
chat_completion_service = OpenAIChatCompletion(
ai_model_id="my-deployment",
api_key="my-api-key",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
# You can do the following if you have set the necessary environment variables or created a .env file
chat_completion_service = OpenAIChatCompletion(service_id="my-service-id")
from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion
chat_completion_service = AzureAIInferenceChatCompletion(
ai_model_id="my-deployment",
api_key="my-api-key",
endpoint="my-api-endpoint", # Used to point to your service
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
# You can do the following if you have set the necessary environment variables or created a .env file
chat_completion_service = AzureAIInferenceChatCompletion(ai_model_id="my-deployment", service_id="my-service-id")
# You can also use an Azure OpenAI deployment with the Azure AI Inference service
from azure.ai.inference.aio import ChatCompletionsClient
from azure.identity.aio import DefaultAzureCredential
chat_completion_service = AzureAIInferenceChatCompletion(
ai_model_id="my-deployment",
client=ChatCompletionsClient(
endpoint=f"{str(endpoint).strip('/')}/openai/deployments/{deployment_name}",
credential=DefaultAzureCredential(),
credential_scopes=["https://cognitiveservices.azure.com/.default"],
),
)
注意
AzureAIInferenceChatCompletion 服務也支援 Microsoft Entra 驗證。 如果您沒有提供 API 金鑰,服務會嘗試使用 Entra 令牌進行驗證。
from semantic_kernel.connectors.ai.anthropic import AnthropicChatCompletion
chat_completion_service = AnthropicChatCompletion(
chat_model_id="model-id",
api_key="my-api-key",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
from semantic_kernel.connectors.ai.bedrock import BedrockChatCompletion
chat_completion_service = BedrockChatCompletion(
model_id="model-id",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
from semantic_kernel.connectors.ai.google.google_ai import GoogleAIChatCompletion
chat_completion_service = GoogleAIChatCompletion(
gemini_model_id="model-id",
api_key="my-api-key",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
提示
用戶可以透過Google AI Studio或Google頂點平臺存取Google的Gemini模型。 請遵循此 指南 來設定您的環境。
from semantic_kernel.connectors.ai.google.vertex_ai import VertexAIChatCompletion
chat_completion_service = VertexAIChatCompletion(
project_id="my-project-id",
gemini_model_id="model-id",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
提示
用戶可以透過Google AI Studio或Google頂點平臺存取Google的Gemini模型。 請遵循此 指南 來設定您的環境。
from semantic_kernel.connectors.ai.mistral_ai import MistralAIChatCompletion
chat_completion_service = MistralAIChatCompletion(
ai_model_id="model-id",
api_key="my-api-key",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
from semantic_kernel.connectors.ai.ollama import OllamaChatCompletion
chat_completion_service = OllamaChatCompletion(
ai_model_id="model-id",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
提示
請在這裡深入瞭解 Ollama,並從 下載必要的軟體。
from semantic_kernel.connectors.ai.onnx import OnnxGenAIChatCompletion
chat_completion_service = OnnxGenAIChatCompletion(
template="phi3v",
ai_model_path="model-path",
service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel
)
from semantic_kernel import Kernel
# Initialize the kernel
kernel = Kernel()
# Add the chat completion service created above to the kernel
kernel.add_service(chat_completion_service)
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.services.chatcompletion.ChatCompletionService;
// Create the client
OpenAIAsyncClient client = new OpenAIClientBuilder()
.credential(azureOpenAIClientCredentials)
.endpoint(azureOpenAIClientEndpoint)
.buildAsyncClient();
// Create the chat completion service
ChatCompletionService openAIChatCompletion = OpenAIChatCompletion.builder()
.withOpenAIAsyncClient(client)
.withModelId(modelId)
.build();
// Initialize the kernel
Kernel kernel = Kernel.builder()
.withAIService(ChatCompletionService.class, openAIChatCompletion)
.build();
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.services.chatcompletion.ChatCompletionService;
// Create the client
OpenAIAsyncClient client = new OpenAIClientBuilder()
.credential(openAIClientCredentials)
.buildAsyncClient();
// Create the chat completion service
ChatCompletionService openAIChatCompletion = OpenAIChatCompletion.builder()
.withOpenAIAsyncClient(client)
.withModelId(modelId)
.build();
// Initialize the kernel
Kernel kernel = Kernel.builder()
.withAIService(ChatCompletionService.class, openAIChatCompletion)
.build();
擷取聊天完成服務
將聊天完成服務新增至核心之後,您可以使用 get 服務方法來擷取它們。 以下是如何從核心擷取聊天完成服務的範例。
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
# Retrieve the chat completion service by type
chat_completion_service = kernel.get_service(type=ChatCompletionClientBase)
# Retrieve the chat completion service by id
chat_completion_service = kernel.get_service(service_id="my-service-id")
# Retrieve the default inference settings
execution_settings = kernel.get_prompt_execution_settings_from_service_id("my-service-id")
ChatHistory history = [];
history.AddUserMessage("Hello, how are you?");
var response = await chatCompletionService.GetChatMessageContentAsync(
history,
kernel: kernel
);
chat_history = ChatHistory()
chat_history.add_user_message("Hello, how are you?")
response = await chat_completion.get_chat_message_content(
chat_history=history,
settings=execution_settings,
)
ChatHistory history = new ChatHistory();
history.addUserMessage("Hello, how are you?");
InvocationContext optionalInvocationContext = null;
List<ChatMessageContent<?>> response = chatCompletionService.getChatMessageContentsAsync(
history,
kernel,
optionalInvocationContext
);
串流聊天完成
若要使用串流聊天完成,您可以使用下列程式代碼從 AI 代理程式產生回應。
ChatHistory history = [];
history.AddUserMessage("Hello, how are you?");
var response = chatCompletionService.GetStreamingChatMessageContentsAsync(
chatHistory: history,
kernel: kernel
);
await foreach (var chunk in response)
{
Console.Write(chunk);
}
chat_history = ChatHistory()
chat_history.add_user_message("Hello, how are you?")
response = chat_completion.get_streaming_chat_message_content(
chat_history=history,
settings=execution_settings,
)
async for chunk in response:
print(chunk, end="")
注意
Java 的語意核心不支援串流回應模型。
下一步
既然您已將聊天完成服務新增至語意核心專案,您就可以開始建立與 AI 代理程式的交談。 若要深入瞭解如何使用聊天完成服務,請參閱下列文章: