asp.net 如何在azure openai dotnet web应用程序中应用流媒体?

lyr7nygr  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(128)

我想创建一个web API后端流openai完成响应。
如何将以下解决方案应用于控制器中的Web API操作?

var client = new OpenAIClient(nonAzureOpenAIApiKey, new OpenAIClientOptions());
var chatCompletionsOptions = new ChatCompletionsOptions()
{
    DeploymentName = "gpt-3.5-turbo", // Use DeploymentName for "model" with non-Azure clients
    Messages =
    {
        new ChatRequestSystemMessage("You are a helpful assistant. You will talk like a pirate."),
        new ChatRequestUserMessage("Can you help me?"),
        new ChatRequestAssistantMessage("Arrrr! Of course, me hearty! What can I do for ye?"),
        new ChatRequestUserMessage("What's the best way to train a parrot?"),
    }
};

await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
{
    if (chatUpdate.Role.HasValue)
    {
        Console.Write($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
    }
    if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
    {
        Console.Write(chatUpdate.ContentUpdate);
    }
}

字符串

uyhoqukh

uyhoqukh1#

您可以简单地将代码 Package 在控制器中

using Microsoft.AspNetCore.Mvc;
using OpenAI;
using OpenAI.Chat;
using System.Collections.Generic;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class ChatController : ControllerBase
{
    [HttpGet]
    public async Task<ActionResult<List<string>>> GetChatCompletions()
    {
        var client = new OpenAIClient(nonAzureOpenAIApiKey, new OpenAIClientOptions());
        var chatCompletionsOptions = new ChatCompletionsOptions()
        {
            DeploymentName = "gpt-3.5-turbo",
            Messages =
            {
                new ChatRequestSystemMessage("You are a helpful assistant. You will talk like a pirate."),
                new ChatRequestUserMessage("Can you help me?"),
                new ChatRequestAssistantMessage("Arrrr! Of course, me hearty! What can I do for ye?"),
                new ChatRequestUserMessage("What's the best way to train a parrot?"),
            }
        };

        var responses = new List<string>();

        await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
        {
            if (chatUpdate.Role.HasValue)
            {
                responses.Add($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
            }
            if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
            {
                responses.Add(chatUpdate.ContentUpdate);
            }
        }

        return Ok(responses);
    }
}

字符串
如果你不想硬编码消息并将其作为一个主体传递,那么你可以这样做

using Microsoft.AspNetCore.Mvc;
using OpenAI;
using OpenAI.Chat;
using System.Collections.Generic;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class ChatController : ControllerBase
{
    public class ChatRequest
    {
        public List<string> Messages { get; set; }
    }

    [HttpPost]
    public async Task<ActionResult<List<string>>> PostChatCompletions([FromBody] ChatRequest request)
    {
        var client = new OpenAIClient(nonAzureOpenAIApiKey, new OpenAIClientOptions());
        var chatCompletionsOptions = new ChatCompletionsOptions()
        {
            DeploymentName = "gpt-3.5-turbo",
            Messages = new List<ChatRequestMessage>()
        };

        foreach (var message in request.Messages)
        {
            chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(message));
        }

        var responses = new List<string>();

        await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
        {
            if (chatUpdate.Role.HasValue)
            {
                responses.Add($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
            }
            if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
            {
                responses.Add(chatUpdate.ContentUpdate);
            }
        }

        return Ok(responses);
    }
}


请记住,上述API的实现不支持流响应。它等待从OpenAI API接收到所有聊天完成,然后将它们一次性发送到客户端。
将从OpenAI API接收到的响应流传输到客户端需要不同的方法。这可以使用服务器发送事件(SSE)或类似技术来实现,但需要注意的是,并非所有客户端和网络环境都支持这些技术。
下面是一个简化的示例,说明如何使用ASP.NET Core中的服务器发送事件实现此功能:

[HttpPost]
public async Task PostChatCompletions([FromBody] ChatRequest request)
{
    var client = new OpenAIClient(nonAzureOpenAIApiKey, new OpenAIClientOptions());
    var chatCompletionsOptions = new ChatCompletionsOptions()
    {
        DeploymentName = "gpt-3.5-turbo",
        Messages = new List<ChatRequestMessage>()
    };

    foreach (var message in request.Messages)
    {
        chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(message));
    }

    Response.Headers.Add("Content-Type", "text/event-stream");

    await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
    {
        if (chatUpdate.Role.HasValue)
        {
            await Response.WriteAsync($"data: {chatUpdate.Role.Value.ToString().ToUpperInvariant()}: \n\n");
        }
        if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
        {
            await Response.WriteAsync($"data: {chatUpdate.ContentUpdate}\n\n");
        }
    }
}

相关问题