随笔 - 53  文章 - 1  评论 - 407  阅读 - 15万
  2025年2月7日

先上图,结果如图

 

 

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

 

对应的C#代码封装

  1  public class DeepSeekHelper
  2  {
  3      private static readonly HttpClient client = new HttpClient();
  4      private const string ApiEndpoint = "https://api.deepseek.com/v1/chat/completions";
  5      private static readonly string apiKey = "你的apikey";
  6      public static async Task<string> CallDeepSeekAPI(string userQuestion)
  7      {
  8          ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  9          try
 10          {
 11              // 设置请求头
 12              client.DefaultRequestHeaders.Clear();
 13              client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
 14              client.DefaultRequestHeaders.Add("Accept", "application/json");
 15 
 16              // 构建请求体
 17              var requestBody = new
 18              {
 19                  model = "deepseek-reasoner", // 根据实际模型调整
 20                  messages = new[]
 21                  {
 22                  new
 23                  {
 24                      role = "user",
 25                      content = userQuestion
 26                  }
 27              },
 28                  temperature = 0.7
 29              };
 30 
 31              // 序列化请求体
 32              //var jsonContent = JsonSerializer.Serialize(requestBody);
 33              var jsonContent = JsonConvert.SerializeObject(requestBody);
 34              var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
 35 
 36              // 发送请求
 37              var response = await client.PostAsync(ApiEndpoint, content);
 38 
 39              // 处理响应
 40              if (!response.IsSuccessStatusCode)
 41              {
 42                  throw new Exception($"API请求失败: {response.StatusCode}");
 43              }
 44 
 45              var responseContent = await response.Content.ReadAsStringAsync();
 46              var resultModel = JsonConvert.DeserializeObject<DeepSeekResponse>(responseContent);
 47              if (resultModel != null && resultModel.Choices.Count > 0)
 48                  return resultModel.Choices[0].Message.Content;
 49              return responseContent;
 50          }
 51          catch (Exception ex)
 52          {
 53              // 处理异常
 54              return $"调用API时发生错误: {ex.Message}";
 55          }
 56      }
 57  }
 58 
 59 
 60 
 61  public class DeepSeekResponse
 62  {
 63      public string Id { get; set; }
 64      public string Object { get; set; }
 65      public long Created { get; set; }
 66      public string Model { get; set; }
 67      public List<Choice> Choices { get; set; }
 68      public Usage Usage { get; set; }
 69      public string SystemFingerprint { get; set; }
 70 
 71      // 重写ToString方法以便更好地显示对象信息
 72      public override string ToString()
 73      {
 74          return $"DeepSeekResponse(Id={Id}, Object={Object}, Created={Created}, Model={Model}, Choices={string.Join(", ", Choices)}, Usage={Usage}, SystemFingerprint={SystemFingerprint})";
 75      }
 76  }
 77 
 78  public class Choice
 79  {
 80      public int Index { get; set; }
 81      public Message Message { get; set; }
 82      // 其他Choice相关的属性...
 83 
 84      // 重写ToString方法以便更好地显示Choice信息(这里仅展示Index和Message作为示例)
 85      public override string ToString()
 86      {
 87          return $"Choice(Index={Index}, Message={Message})";
 88      }
 89  }
 90 
 91  public class Message
 92  {
 93      public string Role { get; set; }
 94      public string Content { get; set; }
 95      // 其他Message相关的属性...
 96  }
 97 
 98  public class Usage
 99  {
100      public int PromptTokens { get; set; }
101      public int CompletionTokens { get; set; }
102      public int TotalTokens { get; set; }
103      // 其他Usage相关的属性,包括嵌套的字典等,可以根据需要添加
104  }

 

调用示例

   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元余额.

 

 

posted @ 2025-02-07 14:05 火星大能猫 阅读(1551) 评论(1) 推荐(10) 编辑
  2024年10月31日
摘要: 取消勾选 enable JavaScript source map 阅读全文
posted @ 2024-10-31 14:57 火星大能猫 阅读(76) 评论(0) 推荐(0) 编辑
  2024年8月2日
摘要: 1 安装ftp软件 yum install vsftpd* -y 2 配置服务启动 #启用 firewalld 服务 systemctl start vsftpd.service #开启vsftpd服务 这条命令设置 firewalld 服务在系统启动时自动启动。systemctl enable 命 阅读全文
posted @ 2024-08-02 11:35 火星大能猫 阅读(68) 评论(0) 推荐(0) 编辑
  2024年7月10日
摘要: .Net Core WebApi项目 Development模式下报错 System.InvalidOperationException:“Cannot resolve scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[I 阅读全文
posted @ 2024-07-10 11:33 火星大能猫 阅读(86) 评论(0) 推荐(0) 编辑
  2024年6月19日
摘要: 废话不多说(主要文笔比较差),直接上代码 一个简单的demo,如下 <img :src="orginalImgSrc" style="display: none;" crossOrigin="Anonymous" @load="imgLoaded"> vue代码 imgLoaded(e) { deb 阅读全文
posted @ 2024-06-19 16:56 火星大能猫 阅读(392) 评论(1) 推荐(0) 编辑
  2024年5月11日
摘要: 效果如图 实现了绘图,自适应缩放 核心代码如下 <Window.InputBindings> <KeyBinding Key="Z" Modifiers="Ctrl" Command="{Binding UndoCommand}" /> </Window.InputBindings> <i:Inte 阅读全文
posted @ 2024-05-11 13:47 火星大能猫 阅读(1527) 评论(0) 推荐(0) 编辑
  2024年4月12日
摘要: 自定义泛型用户控件: <UserControl x:Class="ClassLibrary1.UcEumCmb" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic 阅读全文
posted @ 2024-04-12 15:29 火星大能猫 阅读(106) 评论(0) 推荐(1) 编辑
  2023年11月29日
摘要: 首先是表结构,部分字段脱敏已删除 CREATE TABLE `log_device_heart` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `device_number` varchar(255) CHARACTER SET utf8mb4 COLLA 阅读全文
posted @ 2023-11-29 15:50 火星大能猫 阅读(325) 评论(0) 推荐(0) 编辑
  2023年11月17日
摘要: public static String ToChineseYearAndMonth(this DateTime dt) { string[] chineseNumbers = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; // 将年份和 阅读全文
posted @ 2023-11-17 11:22 火星大能猫 阅读(203) 评论(0) 推荐(0) 编辑
  2023年7月24日
摘要: .net core 7.0+centos 7.0 cetnos目前运行在hyper V虚拟机里 虚拟机部署的注意事项 1 需要配置网络环境, 确保在同一局域网下 如果sftp无法连接 ctrl+shift+f2切换到命令行,开启22端口 iptables -I INPUT -p tcp --dpor 阅读全文
posted @ 2023-07-24 13:35 火星大能猫 阅读(2365) 评论(9) 推荐(1) 编辑
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示