C#调用DeepSeek API的方法详解
作者:上位机李工
DeepSeek(深度求索) 最近可谓火爆的一塌糊涂,具体的介绍这里不再赘述,本文为大家介绍了在C#中调用DeepSeek API的方法,希望对大家有所帮助
一、官方网站
DeepSeek 官网:https://www.deepseek.com/
DeepSeek API 官网文档:https://api-docs.deepseek.com/zh-cn/
二、DeepSeek测试
DeepSeek三大适用模式:基础模型(V3)、深度思考(R1)、联网搜索。
基础模型(V3)
深度思考(R1)
联网搜索
三、C#调用DeepSeek API
核心代码
//引用 using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; //请求 class Program { private static readonly string apiKey = "your_deepseek_api_key"; // 替换为你的 API 密钥 private static readonly string apiUrl = "https://api.deepseek.com/v1/endpoint"; // 替换为 API 的 URL static async Task Main(string[] args) { // 构造请求数据 var requestData = new { prompt = "Hello, DeepSeek!", // 示例请求参数 max_tokens = 50 }; // 调用 API var response = await CallDeepSeekAPI(requestData); // 输出结果 Console.WriteLine("API 响应:"); Console.WriteLine(response); } static async Task<string> CallDeepSeekAPI(object requestData) { using (var client = new HttpClient()) { // 设置请求头 client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); client.DefaultRequestHeaders.Add("Accept", "application/json"); // 将请求数据序列化为 JSON var jsonContent = JsonConvert.SerializeObject(requestData); var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送 POST 请求 var response = await client.PostAsync(apiUrl, httpContent); // 检查响应状态 if (response.IsSuccessStatusCode) { // 读取响应内容 var responseJson = await response.Content.ReadAsStringAsync(); return responseJson; } else { // 处理错误 var errorResponse = await response.Content.ReadAsStringAsync(); throw new Exception($"API 调用失败: {response.StatusCode}\n{errorResponse}"); } } } }
四、服务状态
由于国际原因,API服务不是很稳定,所以如果在调用其API接口如果无法及时响应,可以看下目前API服务状态
以上就是C#调用DeepSeek API的方法详解的详细内容,更多关于C#调用DeepSeek API的资料请关注脚本之家其它相关文章!