当前位置: 代码网 > it编程>编程语言>C# > 使用C# 调用deepseek api接口实现正常访问的过程

使用C# 调用deepseek api接口实现正常访问的过程

2025年02月13日 C# 我要评论
先上图,结果如图先去官方网站充值api费用,默认对应的c#代码封装public class deepseekhelper { private static readonly httpclien

先上图,结果如图

先去官方网站充值api费用,默认

对应的c#代码封装

public class deepseekhelper
 {
     private static readonly httpclient client = new httpclient();
     private const string apiendpoint = "https://api.deepseek.com/v1/chat/completions";
     private static readonly string apikey = "你的apikey";
     public static async task<string> calldeepseekapi(string userquestion)
     {
         servicepointmanager.securityprotocol = securityprotocoltype.tls12;
         try
         {
             // 设置请求头
             client.defaultrequestheaders.clear();
             client.defaultrequestheaders.add("authorization", $"bearer {apikey}");
             client.defaultrequestheaders.add("accept", "application/json");
             // 构建请求体
             var requestbody = new
             {
                 model = "deepseek-reasoner", // 根据实际模型调整
                 messages = new[]
                 {
                 new
                 {
                     role = "user",
                     content = userquestion
                 }
             },
                 temperature = 0.7
             };
             // 序列化请求体
             //var jsoncontent = jsonserializer.serialize(requestbody);
             var jsoncontent = jsonconvert.serializeobject(requestbody);
             var content = new stringcontent(jsoncontent, encoding.utf8, "application/json");
             // 发送请求
             var response = await client.postasync(apiendpoint, content);
             // 处理响应
             if (!response.issuccessstatuscode)
             {
                 throw new exception($"api请求失败: {response.statuscode}");
             }
             var responsecontent = await response.content.readasstringasync();
             var resultmodel = jsonconvert.deserializeobject<deepseekresponse>(responsecontent);
             if (resultmodel != null && resultmodel.choices.count > 0)
                 return resultmodel.choices[0].message.content;
             return responsecontent;
         }
         catch (exception ex)
         {
             // 处理异常
             return $"调用api时发生错误: {ex.message}";
         }
     }
 }
 public class deepseekresponse
 {
     public string id { get; set; }
     public string object { get; set; }
     public long created { get; set; }
     public string model { get; set; }
     public list<choice> choices { get; set; }
     public usage usage { get; set; }
     public string systemfingerprint { get; set; }
     // 重写tostring方法以便更好地显示对象信息
     public override string tostring()
     {
         return $"deepseekresponse(id={id}, object={object}, created={created}, model={model}, choices={string.join(", ", choices)}, usage={usage}, systemfingerprint={systemfingerprint})";
     }
 }
 public class choice
 {
     public int index { get; set; }
     public message message { get; set; }
     // 其他choice相关的属性...
     // 重写tostring方法以便更好地显示choice信息(这里仅展示index和message作为示例)
     public override string tostring()
     {
         return $"choice(index={index}, message={message})";
     }
 }
 public class message
 {
     public string role { get; set; }
     public string content { get; set; }
     // 其他message相关的属性...
 }
 public class usage
 {
     public int prompttokens { get; set; }
     public int completiontokens { get; set; }
     public int totaltokens { get; set; }
     // 其他usage相关的属性,包括嵌套的字典等,可以根据需要添加
 }

调用示例

private async void sendbutton_click(object sender, routedeventargs e)
   {
       string requesttext = requesttextbox.text;
       if (!string.isnullorwhitespace(requesttext))
       {
           try
           {
               string responsedata = await deepseekhelper.calldeepseekapi(requesttext); // await new deepseekapi().calldeepseekapi(chatrequest);
               responsetextbox.text = responsedata;
           }
           catch (exception ex)
           {
               responsetextbox.text = $"error: {ex.message}";
           }
       }
       else
       {
           messagebox.show("please enter a request.");
       }
   }

常见问题:

1发送请求时出错 innerexception = {"请求被中止: 未能创建 ssl/tls 安全通道。"}

指定使用tls1.2加密协议,添加如下代码

 servicepointmanager.securityprotocol = securityprotocoltype.tls12;

2 收到内容为空白

服务器繁忙,请把deepseek-chat模型切换到deepseek-reasoner 试试,或者换个时间再次尝试.

模型区别如下.代码中默认使用了reasoner模型,俗称满血版.

默认账户赠送10元余额.

到此这篇关于使用c# 调用deepseek api接口,来实现正常访问的文章就介绍到这了,更多相关c# 调用deepseek api内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com