本章将分享如何与技术人员相结合,方便自己使用。
deepseek官网:https://www.deepseek.com。
选择“开始对话”需要注册#xff0c;但更引人注目的句子,让我一个普通的研发人员感受到来自世界的压力。
选择“API开放平台”提示维修(这只是暂时的,中国人战胜小人只是时间问题)
回到正题我们使用.net 6.技术对接,调用API实现助手,一个简单的技术对接工具是通过控制台命令编写的。
预计效果如下:
直接打开控制台对话无需打开浏览器、app等应用程序。
如果你不知道如何开发但也希望使用这个工具,可以关注我后续API网站恢复后,我完成了工具编写和#xff0c;通过公共文章提供下载链接。
在编写完整的代码之前,需要事先解释几件事:
1、要实现上述效果,需要将控制台程序添加到环境变量,通过“操作”可以直接打开,否则,需要输入较长的程序路径。
2、需要在DeepSeek官网申请自己的apikey,在自己的key替换代码中使用“your_api_key_here”。
3、每月1000次API请求或100小时Deepseek的计算时间,超过免费限额后的费用通常按每千次请求或每小时计算。例如,每千次收费0.01或每小时计算时间0.01或每小时计算时间0.10。
4、在调用Deepseekapi时,可以使用连续问题模式,实现原理:与之对话的内容(含:您发送的,它的回复)全部作为参数发送给它,它将逻辑地整理和反馈你需要的信息,并根据上下文的所有内容进行反馈。
5、“运行”中输入的“s-art"您的应用程序名称,如:s-art.exe。
该代码解决了上述序号1的环境变量配置问题和序号4的连续问题,但是,首次启动需要使用管理员身份运行它将自动检测环境变量配置是否存在,并增加配置。
using System;using System.IO;using System.Net.Http;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;class Program{ static async Task Main(string[] args) { // 获取当前程序的路径 string programPath = System.Reflection.Assembly.GetExecutingAssembly().Location; string programDirectory = Path.GetDirectoryName(programPath); // 检查是否已配置 PATH 环境变量 if (!IsProgramInPath(programDirectory)) { Console.WriteLine("未配置程序 PATH 环境变量自动配置..."); AddProgramToPath(programDirectory); Console.WriteLine("#xff01配置完成;请重启命令行或终端,以使更改生效。IsProgramInPath(programDirectory)) { Console.WriteLine("未配置程序 PATH 环境变量自动配置..."); AddProgramToPath(programDirectory); Console.WriteLine("#xff01配置完成;请重启命令行或终端,以使更改生效。"); } else { Console.WriteLine("已经配置了程序 PATH 环境变量。"); } // 调用 API 接口 Console.WriteLine("开始调用 API 接口..."); await CallApiAsync(); Console.WriteLine("按任意键退出..."); Console.ReadKey(); } // 检查程序路径是否已经过去 PATH 环境变量中 static bool IsProgramInPath(string programDirectory) { string pathVariable = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) ??? ""; return pathVariable.Contains(programDirectory); } // 添加程序路径 PATH 环境变量 static void AddProgramToPath(string programDirectory) { try { // 获取当前的 PATH 环境变量 string pathVariable = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) ?? ""; // 如果 PATH 不包括程序路径,则添加 if (!pathVariable.Contains(programDirectory)) { pathVariable += ";" + programDirectory; Environment.SetEnvironmentVariable("PATH", pathVariable, EnvironmentVariableTarget.Machine); Console.WriteLine("添加到程序路径中 PATH 环境变量。"); } } catch (UnauthorizedAccessException) { Console.WriteLine("错误:需要管理员的权限才能修改 PATH 环境变量。"); } catch (Exception ex) { Console.WriteLine("发生错误:" + ex.Message); } } // 调用 API 接口 static async Task CallApiAsync() { // API的URL string apiUrl = "https://api.deepseek.com/v1/chat/completions"; // 您的API密钥 string apiKey = "your_api_key_here"; // 创建HTTPClient实例 using (HttpClient client = new HttpClient()) { // 设置请求头包括API密钥 client.DefaultRequestHeaders.Add("Authorization", $"Bearer { apiKey}"); // 创建请求内容(JSON格式) var requestBody = new { model = "deepseek-chat", messages = new[] { new { role = "user", content = "你好" } } }; string jsonContent = JsonConvert.SerializeObject(requestBody); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送POST请求 HttpResponseMessage response = await client.PostAsync(apiUrl, content); // 检查响应状态 if (response.IsSuccessStatusCode) { // 读取响应内容 string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine("API 响应: " + responseBody); } else { Console.WriteLine("错误: " + response.StatusCode); } } }}。