先上图,结果如图

先去官方网站充值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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论