.NET Core C#系列之 Semantic Kernel 使用案例(AI翻译)

B站影视 2024-12-03 10:31 2

摘要:上一篇,我们讲述了 Semantic Kernel入门课程,(点击跳转)本次将写一个案例,让大家更多的了解Semantic Kernel如何落地项目。

上一篇,我们讲述了 Semantic Kernel入门课程,(点击跳转)本次将写一个案例,让大家更多的了解Semantic Kernel如何落地项目。

需求,在外面日常使用的翻译中,经常会遇到翻译一些md文档的时候将我们的一些特殊的字符也进行翻译,所以在翻译md相关的文档的时候我们就会使用AI来进行翻译,下面我们通过一个案例来了解Semantic Kernel的大文本翻译功能和一些高级的用法。 Exe net8.0 enable enable 创建OpenAIHttpClientHandler.cs文件public class OpenAIHttpClientHandler : HttpClientHandler{ private readonly string _uri; public OpenAIHttpClientHandler(string uri) => _uri = uri.TrimEnd('/'); protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { UriBuilder uriBuilder; if (request.RequestUri?.LocalPath == "/v1/chat/completions") { uriBuilder = new UriBuilder(_uri + "/v1/chat/completions"); request.RequestUri = uriBuilder.Uri; } else if (request.RequestUri?.LocalPath == "/v1/embeddings") { uriBuilder = new UriBuilder(_uri + "/v1/embeddings"); } return await base.SendAsync(request, cancellationtoken); }}打开Program.csusing System.Text;using ConsoleApp1;using Microsoft.SemanticKernel;using Microsoft.SemanticKernel.ChatCompletion;using Microsoft.SemanticKernel.Text;var kernel = Kernel.CreateBuilder .AddOpenAIChatCompletion( modelId: "gpt-3.5-turbo-0125", apiKey: "这里填写在https://api.token-ai.cn/创建的令牌", httpClient: new HttpClient(new OpenAIHttpClientHandler("https://api.token-ai.cn/"))) .Build;const string prompt = @"- Expertise: 双向翻译- Language Pairs: 中文 英文- Description: 你是一个中英文翻译专家,将用户输入的中文翻译成英文,或将用户输入的英文翻译成中文。对于非中文内容,它将提供中文翻译结果。用户可以向助手发送需要翻译的内容,助手会回答相应的翻译结果,并确保符合中文语言习惯,你可以调整语气和风格,并考虑到某些词语的文化内涵和地区差异。同时作为翻译家,需将原文翻译成具有信达雅标准的译文。""信"" 即忠实于原文的内容与意图;""达"" 意味着译文应通顺易懂,表达清晰;""雅"" 则追求译文的文化审美和语言的优美。目标是创作出既忠于原作精神,又符合目标语言文化和读者审美的翻译。";// 文件读取一个大文件var text = File.ReadAllText("data.md");#pragma warning disable SKEXP0050 // by designvar values = TextChunker.SplitPlainTextLines(text, 500);Console.WriteLine("分割后的文本数量:" + values.Count);var str = new StringBuilder;var chat = kernel.GetRequiredService;foreach (var item in values){ var chatHistory = new ChatHistory; chatHistory.AddSystemMessage(prompt); chatHistory.AddUserMessage(item); Console.WriteLine("翻译文字数量:" + item.Length); var chatMessageContents = await chat.GetChatMessageContentsAsync(chatHistory); str.AppendLine(chatMessageContents.First.Content);}Console.WriteLine(str.ToString);在这里我们使用到了TextChunker.SplitPlainTextLines这个实验性的工具,这个是由Semantic Kernel提供的一个工具,用来将文本进行分割,并且根据我们传递的token的数量进行分割,这样我们就可以将大文本进行分割,然后进行翻译,因为直接传递大文件会导致超出模型的MaxToken,并且大模型的ResponseMaxToken也不能完全翻译所有的文本,所以我们需要将文本进行分割,但是也不能直接将文本进行分割,因为文本的分割会导致文本的意思不完整,所以我们需要根据文本的意思进行分割,这样我们就可以保证文本的意思不会被破坏,您也可以在Prompt然后运行项目,您会看到输出结果为:通过将我们的文本分割成了4个文本,然后我们将这4个文本进行翻译,最后我们将这4个文本的翻译结果进行拼接,这样我们就可以将大文本进行翻译。分割后的文本数量:4翻译文字数量:1572翻译文字数量:1495翻译文字数量:1459翻译文字数量:1503# Deploying Chat Copilot as a Web Application Service on AzureIn this operation guide, we will provide the steps to deploy the semantic core to Azure as a Web Application Service. Deploying the semantic core as an Azure web service provides developers with a significant way to leverage Azure computing and other services (such as Azure Cognitive Services for responsible AI and vectorized databases).## PrerequisitesThe Chat Copilot deployment uses Azure Active Directory for user authentication and to secure access to the backend web services. The following steps will guide you through the necessary configurations to ensure smooth access to your deployment.### Application Registration (Authentication)You will need two Azure Active Directory (AAD) [app registrations](https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app) — one for the front-end web application and another for the backend API.> Note: If needed, you can also use other account types to allow multi-tenant and personal Microsoft accounts to use your application. This may result in more users, leading to higher costs.#### Front-end App Registration- Choose `Single-page application (SPA)` as the platform type and set the Redirect URI to `http://localhost:3000`- Choose `{YOUR TENANT} only - Single tenant` as the Supported Account Types.- Record the `Application (client) ID` from the Azure portal. This is the value of `YOUR_FRONTEND_CLIENT_ID` mentioned below.#### Backend App Registration- Do not set a Redirect URI- Choose `{YOUR TENANT} only - Single tenant` as the Supported Account Types.- Record the `Application (client) ID` from the Azure portal. This is the value of `YOUR_BACKEND_CLIENT_ID` mentioned below.#### Linking Front-end to BackendIn the backend app registration:- Expose an API - Select `Expose an API` from the menu - Add an `Application ID URI`- Add a Scope - Set `Scope name` to `access_as_user` - Set `Who can consent` to `Admins and users`- Add a client application - For `Client ID`, enter the ID of the front-end application (client) - Ensure the checkbox is checked under `Authorized scopes`In the front-end app registration:- Select `API permissions` from the menu- Add Permissions - Select the `APIs my organization uses` tab - Choose the backend app registration - Select the `access_as_user` permission## ConsiderationsYou can use one of the deployment options to deploy according to your use case and preferences. When choosing a deployment option, keep in mind the following considerations:1. Azure currently limits the number of Azure OpenAI resources per region per subscription to 3. Not every region offers Azure OpenAI (please refer to this [availability map](https://azure.microsoft.com/explore/global-infrastructure/products-by-region/?products=cognitive-services)). With this limitation in mind, you may want to consider using the same Azure OpenAI instance for multiple deployments of deploying the Semantic Kernel to Azure.2. F1 and D1 application service SKUs (namely, free and shared SKUs) do not support this deployment.3. Ensure that you have sufficient permissions to create resources in the target subscription.4. When accessing the deployment through the web frontend, make sure to include your frontend URL in the CORS settings of the deployment as an allowed origin. Otherwise, the web browser will reject JavaScript calls to your deployment.## Deployment Options[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://aka.ms/sk-deploy-existing-azureopenai-portal)PowerShell Script ([PowerShell File](https://aka.ms/sk-deploy-existing-azureopenai-powershell))Bash Script ([Bash File](https://aka.ms/sk-deploy-existing-azureopenai-bash))## Script ParametersBelow are examples of running the PowerShell and Bash scripts. Refer to each script file for a full list of available parameters and usage.### **PowerShell**- Create a new Azure OpenAI resourcePowerShell./deploy-azure.ps1 -Subscription {YOUR_SUBSCRIPTION_ID} -DeploymentName {YOUR_DEPLOYMENT_NAME} -AIService {AzureOpenAI or OpenAI} -AIApiKey {YOUR_AI_KEY} -AIEndpoint {YOUR_AZURE_OPENAI_ENDPOINT} -BackendClientId {YOUR_BACKEND_APPLICATION_ID} -FrontendClientId {YOUR_FRONTEND_APPLICATION_ID} -TenantId {YOUR_TENANT_ID}- To use an existing Azure OpenAI resource, set `-AIService` to `AzureOpenAI` and include `-AIApiKey` and `-AIEndpoint`.- To deploy a new Azure OpenAI resource, set `-AIService` to `AzureOpenAI` and omit `-AIApiKey` and `-AIEndpoint`.- To use an OpenAI account, set `-AIService` to `OpenAI` and include `-AIApiKey`.### **Bash**Bash```bashchmod +x ./deploy-azure.sh./deploy-azure.sh --subscription {YOUR_SUBSCRIPTION_ID} --deployment-name {YOUR_DEPLOYMENT_NAME} --ai-service {AzureOpenAI or OpenAI} --ai-service-key {YOUR_AI_KEY} --ai-endpoint {YOUR_AZURE_OPENAI_ENDPOINT} --client-id {YOUR_BACKEND_APPLICATION_ID} --frontend-client-id {YOUR_FRONTEND_APPLICATION_ID} --tenant-id {YOUR_TENANT_ID}- To use the existing Azure OpenAI resources, set `--ai-service` to `AzureOpenAI`, and include `--ai-service-key` and `--ai-endpoint`.- To deploy new Azure OpenAI resources, set `--ai-service` to `AzureOpenAI`, and omit `--ai-service-key` and `--ai-endpoint`.- To use OpenAI account, set `--ai-service` to `OpenAI` and include `--ai-service-key`.### Azure Portal TemplateIf you choose to use Azure Portal as the deployment method, you will need to review and update the template form to create resources. Here is a list of items that you need to review and update:1. Subscription: Decide which Azure subscription to use. This will be used to host the resource group for the Semantic Core Network application.2. Resource Group: The resource group where the deployment will go into. Creating a new resource group helps in isolating resources, especially if you are still actively developing.3. Region: Select the geographical region for the deployment. Note: Azure OpenAI is not available in all regions, currently limited to three instances per subscription.4. Name: Used to identify the application.5. App Service SKU: Choose the pricing tier based on your usage. Click [here](https://azure.microsoft.com/pricing/details/app-service/windows/) for more information on Azure App Service plans.6. Package URI: No need to change this unless you want to deploy a custom version of the semantic core. (For more information on publishing your own Semantic Core Web App Service version, see [this page](https://learn.microsoft.com/zh-cn/semantic-kernel/chat-copilot/publish-changes-to-azure))7. Finish, Embed, and Schedule Models: These default to the appropriate model based on your current use case - either Azure OpenAI or OpenAI. You can update these models as needed.8. Endpoint: Only applicable when using Azure OpenAI, it is the Azure OpenAI endpoint to use.9. API Key: Input the API key for the Azure OpenAI or OpenAI instance you want to use.10. Web API Client ID: The Application (client) ID associated with your backend application registration.11. Azure AD Tenant ID: The Azure AD tenant used for authenticating users. For single-tenant applications (recommended), this will match the tenant ID of your backend app registration.12. Azure AD Instance: This is the Azure cloud instance used to authenticate users. Default value is https://login.microsoftonline.com/. If you are using sovereign clouds, you will need to update this value. 13. CosmosDB: Whether to deploy CosmosDB resources for chat storage. Otherwise, volatile storage will be used.14. Speech Service: Whether to deploy an instance of Azure Speech Service for speech-to-text input.## What Resources Are Deployed?Below are key resources created in the resource group when deploying Semantic Core to Azure as a Web App Service:1. Azure Web App Service: Hosting the Semantic Core.2. Application Insights: Application logs and debugging.3. Azure Cosmos DB: Used for chat storage (optional).4. Qdrant Vector Database (in a container): Used for embedding storage (optional).5. Azure Speech Service: Used for speech-to-text (optional).## Deployment VerificationTo ensure that the Web application service is running, please visit https://YOUR_INSTANCE_NAME.azurewebsites.net/healthz. To obtain the URL of the instance, go to the deployed resource group (click the "Go to resource group" button, which is located at the end of the deployment, if using the "Deploy to Azure" button). Then click on the resource ending with "-webapi".This will take you to the overview page of the Web service. The URL of your instance is the value next to the "Default domain" field.## Configuration Changes and Monitoring DeploymentsAfter deployment, you can make configuration changes in the "Settings" section of the Semantic Kernel Web App service page in the Azure portal. Scroll down within the same panel to the "Monitoring" section, where you can access various ways to monitor deployments.Additionally, the "Diagnostics and solving problems" item is located near the top of the panel and can provide critical insights into some issues that your deployment may encounter.## How to Clean Up ResourcesWhen you want to clean up the resources for this deployment, use the Azure portal or run the following [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/) command:PowerShellaz group delete --name YOUR_RESOURCE_GROUP## Deploying Chat Copilot to AzureML as an Online EndpointEnhance your Semantic Kernel application by deploying it as an online endpoint in AzureML to help manage real-time inference workloads.### Prerequisites- Azure Machine Learning workspace - [Create a new workspace](https://learn.microsoft.com/zh-cn/azure/machine-learning/concept-workspace#create-a-workspace)- Get the OpenAI API key or create an [Azure OpenAI deployment](https://learn.microsoft.com/zh-cn/azure/ai-services/openai/how-to/create-resource?pivots=web-portal)### Follow the Sample NotebookIn the [azureml-examples github repo](https://github.com/Azure/azureml-examples/blob/main/sdk/python/endpoints/online/llm/semantic-kernel/1_semantic_http_server.ipynb), populate the sample notebook with your AzureML resources. Executing the notebook will deploy a sample Semantic Kernel application to an online endpoint in your AzureML workspace. The sample employs a custom container to deploy a Flask web server that exposes the API endpoint of the Semantic Kernel application for inference.## Next Steps> Learn [how to make changes to your Semantic Kernel web app](https://learn.microsoft.com/zh-cn/semantic-kernel/chat-copilot/publish-changes-to-azure), such as adding new skills.> If you haven't already, star the GitHub repository and join the Semantic Kernel community! [Star the Semantic Kernel repository](https://github.com/microsoft/semantic-kernel)

来源:opendotnet

相关推荐