Fork me on GitHub

Semantic Kernel:接入azure中的deepseek-r1

  SemanticKernel已经支持deepseek-r1了,官方的Blog地址是https://devblogs.microsoft.com/semantic-kernel/using-deepseek-models-in-semantic-kernel,同时给出了接入的Demo,遗憾的是deepseek不支持API的充值,没有办法测试。

  办法总比困难多,正好微软现在支持在Azure部署deepseek-r1了,具体部署方法官方有具体的方式方法,见下面的url:https://azure.microsoft.com/en-us/blog/deepseek-r1-is-now-available-on-azure-ai-foundry-and-github/

  与此同时,Azure上的给Demo如下:

复制代码
var apiKey = File.ReadAllText("C:/gpt/deepseekkey.txt");
var handler = new HttpClientHandler()
    {
        ClientCertificateOptions = ClientCertificateOption.Manual,
        ServerCertificateCustomValidationCallback =
                (httpRequestMessage, cert, cetChain, policyErrors) => { return true; }
    };
using (var client = new HttpClient(handler))
{
    var requestBody = @"{
              ""messages"": [                 
                {
                  ""role"": ""user"",
                  ""content"": ""你是谁?""
                }
              ],
              ""max_tokens"": 2048
            }";

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
    client.BaseAddress = new Uri("https://DeepSeek-R1-iwztj.eastus2.models.ai.azure.com/chat/completions");

    var content = new StringContent(requestBody);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    HttpResponseMessage response = await client.PostAsync("", content);

    if (response.IsSuccessStatusCode)
    {
        string result = await response.Content.ReadAsStringAsync();
        var ent = System.Text.Json.JsonSerializer.Deserialize<ChatCompletionResponse>(result, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
        foreach (var choice in ent.Choices)
        {
            Console.WriteLine("Result: {0}", choice.Message.Content);
        }
    }
    else
    {
        Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));
        Console.WriteLine(response.Headers.ToString());

        Console.WriteLine("错误");
    }
}

public class ChatCompletionResponse
{
    public List<Choice> Choices { get; set; }
    public long Created { get; set; }
    public string Id { get; set; }
    public string Model { get; set; }
    public string Object { get; set; }
    public List<PromptFilterResult> PromptFilterResults { get; set; }
    public Usage Usage { get; set; }
}

public class Choice
{
    public ContentFilterResults ContentFilterResults { get; set; }
    public string FinishReason { get; set; }
    public int Index { get; set; }
    public Message Message { get; set; }
}

public class ContentFilterResults
{
    public FilterStatus Hate { get; set; }
    public FilterStatus SelfHarm { get; set; }
    public FilterStatus Sexual { get; set; }
    public FilterStatus Violence { get; set; }
}

public class FilterStatus
{
    public bool Filtered { get; set; }
    public string Severity { get; set; }
}

public class Message
{
    public string Content { get; set; }
    public string ReasoningContent { get; set; }
    public string Role { get; set; }
    public object ToolCalls { get; set; }
}

public class PromptFilterResult
{
    public int PromptIndex { get; set; }
    public ContentFilterResults ContentFilterResults { get; set; }
}

public class Usage
{
    public int CompletionTokens { get; set; }
    public int PromptTokens { get; set; }
    public object PromptTokensDetails { get; set; }
    public int TotalTokens { get; set; }
}
复制代码

结果如下:

 换成SK的对接方式如下:

复制代码
var apiKey = File.ReadAllText("C:/gpt/deepseekkey.txt");
var chatCompletionService = new OpenAIChatCompletionService(
    endpoint: new Uri("https://DeepSeek-R1-iwztj.eastus2.models.ai.azure.com/"),
    apiKey: apiKey,
    modelId: "deepseek-chat"
);
var chatHistory = new ChatHistory();
chatHistory.AddUserMessage("你好,你是谁?");
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);
Console.WriteLine(reply);
复制代码

结果如下:

   文章来源微信公众号

  想要更快更方便的了解相关知识,可以关注微信公众号 

posted @   桂素伟  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示