序
deepseek(深度求索) 最近可谓火爆的一塌糊涂,具体的介绍这里不再赘述,您可以各种搜索其信息,即使您不搜索,只要您拿起手机,各种关于 deepseek 的新闻、资讯也会扑面而来的推送到您面前。本人在闲暇之余也想了解一下其提供 api 的支持能力,也想试验一下 “嵌入式” 的应用体验。
打开官网,访问主页右上角的 api 开放平台,查看了一下 api 技术文档,果然不出所料,没有 c# 的调用示例,虽然语法调用都大同小异,但心中还是有些不爽,因此本文旨在提供相关的示例,仅供参考,希望对您有所帮助。根据目前的应用现状,本文提供了两种形式的调用方法:
1、原生官网 api 地址调用。
2、通过腾讯云知识引擎原子调用。(适合原生调用繁忙和失败的备用场景)
开发运行环境
操作系统: windows server 2019 datacenter
.net版本: .netframework4.7.2
开发工具:vs2019 c#
访问api的一个通用方法
创建webservice类,该类的getresponseresult 方法持续更新,主要根据 deepseek 对话补全的api文档,增加了httpwebrequest.accept 支持,同时增加了 get 访问请求的 webrequest.headrs 的支持。
更新后的代码如下:
public sealed class webservice
{
public string errormessage = "";
private static bool validsecurity(object sender, x509certificate certificate, x509chain chain, sslpolicyerrors sslpolicyerrors)
{
return true;
}
public string getresponseresult(string url, system.text.encoding encoding, string method, string postdata,string[] headers,string contenttype= "application/x-www-form-urlencoded",bool secvalid=true)
{
method = method.toupper();
if (secvalid == false)
{
servicepointmanager.servercertificatevalidationcallback = validsecurity;
}
system.net.servicepointmanager.securityprotocol = system.net.securityprotocoltype.tls | system.net.securityprotocoltype.tls11 | system.net.securityprotocoltype.tls12;
if (method == "get")
{
try
{
webrequest request2 = webrequest.create(@url);
request2.method = method;
webresponse response2 = request2.getresponse();
if (headers != null)
{
for (int i = 0; i < headers.getlength(0); i++)
{
if (headers[i].split(':').length < 2)
{
continue;
}
if (headers[i].split(':').length > 1)
{
if (headers[i].split(':')[0] == "content-type")
{
request2.contenttype = headers[i].split(':')[1];
continue;
}
}
request2.headers.add(headers[i]);
}
}
stream stream = response2.getresponsestream();
streamreader reader = new streamreader(stream, encoding);
string content2 = reader.readtoend();
return content2;
}
catch (exception ex)
{
errormessage = ex.message;
return "";
}
}
if (method == "post")
{
stream outstream = null;
stream instream = null;
streamreader sr = null;
httpwebresponse response = null;
httpwebrequest request = null;
byte[] data = encoding.getbytes(postdata);
// 准备请求...
try
{
// 设置参数
request = webrequest.create(url) as httpwebrequest;
cookiecontainer cookiecontainer = new cookiecontainer();
request.cookiecontainer = cookiecontainer;
request.allowautoredirect = true;
request.method = method;
request.timeout = 1000000;
request.contenttype = contenttype;
if (headers != null)
{
for (int i = 0; i < headers.getlength(0); i++)
{
if (headers[i].split(':').length < 2)
{
continue;
}
if (headers[i].split(':').length > 1)
{
if (headers[i].split(':')[0] == "host")
{
request.host = headers[i].split(':')[1];
continue;
}
else if (headers[i].split(':')[0] == "content-type")
{
request.contenttype = headers[i].split(':')[1];
continue;
}
else if (headers[i].split(':')[0] == "connection")
{
request.keepalive = headers[i].split(':')[1] == "close" ? false : true;
continue;
}
else if (headers[i].split(':')[0] == "accept")
{
request.accept = headers[i].split(':')[1];
continue;
}
}
request.headers.add(headers[i]);
}
}
request.contentlength = data.length;
outstream = request.getrequeststream();
outstream.write(data, 0, data.length);
outstream.close();
//发送请求并获取相应回应数据
response = request.getresponse() as httpwebresponse;
//直到request.getresponse()程序才开始向目标网页发送post请求
instream = response.getresponsestream();
sr = new streamreader(instream, encoding);
//返回结果网页(html)代码
string content = sr.readtoend();
sr.close();
sr.dispose();
return content;
}
catch (exception ex)
{
errormessage = ex.message;
return "";
}
}
errormessage = "不正确的方法类型。(目前仅支持get/post)";
return "";
}//get response result
}//class具体的参数说明和更新的日志可访问文章:
c#实现访问web api url提交数据并获取处理结果_c#教程_代码网
原生官网实现
申请 api key
访问官网 deepseek,如下:

如图使用您的手机号注册一个帐户,然后再点击右上角 “api 开放平台” 链接申请 api key。
点击如下图:

访问左侧 api keys 功能菜单,点击 “创建 api key” 按钮,按提示输入名称等点击确认即可生成 key 值,请务必妥善存储,这是调用 api 的关键认证信息值。
调用实现
创建 deepseek 类,类说明如下表:
| 序号 | 成员名称 | 成员类型 | 类型 | 说明 |
|---|---|---|---|---|
| 1 | apiurl | 属性 | string | 访问的api路径 |
| 2 | apikey | 属性 | string | 申请的 api key 值 |
| 3 | model | 属性 | string | 使用的模型名称 |
| 4 | errormessage | 属性 | string | 反馈的异常信息 |
| 5 | resultjson | 属性 | string | 得到的json结果信息 |
| 6 | chat(string say) | 方法 | void | 调用原生对话api,参数为问题内容, 方法会写入 errormessage和resultjson属性值 |
| 7 | tc_chat(string say) | 方法 | void | 调用腾讯云封装对话api,参数为问题内容, 方法会写入 errormessage和resultjson属性值 |
类实现代码如下:
public class deepseek
{
public string apiurl { get; set; }
public string apikey { get; set; }
public string model { get; set; }
public string errormessage = "";
public string resultjson = "";
public deepseek(string apikey = "")
{
apikey = apikey;
}
public void chat(string say)
{
apiurl = "https://api.deepseek.com/chat/completions";
model = "deepseek-chat";
webservice ws = new webservice();
string[] headers = new string[3];
headers[0] = "content-type:application/json";
headers[1] = "accept:application/json";
headers[2] = "authorization:bearer " + apikey + "";
var readydata = new
{
model = model,
messages = new[]{
new {role="user",content=say}
}
};
string postdata = newtonsoft.json.jsonconvert.serializeobject(readydata);
errormessage = "";
resultjson = "";
string rs = ws.getresponseresult(apiurl, encoding.utf8, "post", postdata, headers);
errormessage = ws.errormessage;
resultjson = rs;
}
public void tc_chat(string say)
{
apiurl = "https://api.lkeap.cloud.tencent.com/v1/chat/completions";
model = "deepseek-r1";
webservice ws = new webservice();
string[] headers = new string[3];
headers[0] = "content-type:application/json";
headers[1] = "accept:application/json";
headers[2] = "authorization:bearer " + apikey + "";
var readydata = new
{
model = model,
messages = new[]{
new {role="user",content=say}
}
};
string postdata = newtonsoft.json.jsonconvert.serializeobject(readydata);
errormessage = "";
resultjson = "";
string rs = ws.getresponseresult(apiurl, encoding.utf8, "post", postdata, headers);
errormessage = ws.errormessage;
resultjson = rs;
}
}调用示例
示例代码如下:
string ak = ""; //您申请的 api key
deepseek dp = new deepseek(ak);
dp.chat("你好!");
string debug = string.format("errormessage:{0}\r\nresultjson:{1}", dp.errormessage, dp.resultjson);腾讯云知识引擎原子调用
申请 api key
访问产品官网 https://console.cloud.tencent.com/lkeap,登录成功如下:

如图选择左侧“立即接入”菜单功能,选择 使用 openai sdk方式接入,点击“创建 api key”按钮,按提示操作即可创建,创建成功如下图:

如图选择“apk key 管理”,即可查看已经成功创建的 key 列表,点击“查看”链接可以复制键值,如下图中操作步骤。

调用示例
在原生实现章节中已经实现了方法调用编写,这里仅展示调用示例,代码如下:
string ak = ""; //您申请的 api key
deepseek dp = new deepseek(ak);
dp.tc_chat("你好!");
string debug = string.format("errormessage:{0}\r\nresultjson:{1}", dp.errormessage, dp.resultjson);调用方法的区别在于调用了 tc_chat 方法,其它无需改变代码。
小结
以上就是c#调用deepseek api的两种实现方案的详细内容,更多关于c#调用deepseek api的资料请关注代码网其它相关文章!
发表评论