Skip to content

很高兴,你们提供了这个软件包,我在第一次尝试就成功了,尽管介绍并不是很好 #37

Description

@nanaminato

我添加我的示例,以便于其他用户可以进行参考

public class AlibabaChatHandler: IChatHandler
{
    public async Task Chat(NoModelChatBody chatBody, ModelKey modelKey, HttpResponse response)
    {
        var dScopeClient = new DashScopeClient(modelKey.SupplierKey!.ApiKey!, new HttpClient());
        var request = new CompletionRequest();
        request.Model = modelKey.Model!;
        request.Input = ParseInput(chatBody);
        request.Parameters = ParseParameters(chatBody);
        await foreach (var res in dScopeClient.GenerationStreamAsync(request))
        {
            await response.WriteAsync(res.Output.Choices![0].Message.Content);
        }

        await response.CompleteAsync();
    }

    private static CompletionInput ParseInput(NoModelChatBody chatBody)
    {
        var ms = new CompletionInput();
        ms.Messages = new List<Message>();
        foreach (var message in chatBody.Messages!)
        {
            ms.Messages.Add(new ()
            {
                Content = message.Content!,
                Role = message.Role!
            });
        }

        return ms;
    }
    private static CompletionParameters ParseParameters(NoModelChatBody chatBody)
    {
        return new CompletionParameters()
        {
            TopP = chatBody.TopP.HasValue? (float)chatBody.TopP : null,
            Temperature = chatBody.Temperature.HasValue?(float)chatBody.Temperature:null,
            ResultFormat = "message",
            IncrementalOutput = true
        };
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions