赞
踩
最近国产的Kimi免费了,咱们肯定得大大支持,查看了Api文档,发现没有.net版本对应的Api案例,所以以下针对一个API进行展示如何在.net中连接,其他Api接口使用方式相似。
Api官网地址:API Reference – Moonshot AI 开放平台
我们需要到以下地址拿到Api Key用于调用Api ,只需要手机号登录完成,然后点击API Key管理新建一个保存好。
由于其并未提供.net版本的SDK,所以我们通过CURL方式实现,本质就是发送Http请求。
Request Structure:
- //url & authorization
- struct MoonshotConfiguration
- {
- public string Url { get; set; }
- public string Authorization { get; set; }
- }
-
- //playload
- class RequestMessageMoon
- {
- public string model { get; set; }
- public double temperature { get; set; }
-
- public List<MoonMessage> messages { get; set; }
- }
-
- //层级封装
- struct MoonMessage
- {
- public string role { get; set; }
- public string content { get; set; }
- }
- //role
- public enum RoleEnum
- {
- system,
- user,
- assistant
- }
Respnse structure:
- public class ChoicesItem
- {
- /// <summary>
- ///
- /// </summary>
- public int index { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public Message message { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public string finish_reason { get; set; }
- }
-
- public class Usage
- {
- /// <summary>
- ///
- /// </summary>
- public int prompt_tokens { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public int completion_tokens { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public int total_tokens { get; set; }
- }
-
- public class Root
- {
- /// <summary>
- ///
- /// </summary>
- public string id { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public string @object { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public int created { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public string model { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public List<ChoicesItem> choices { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public Usage usage { get; set; }
- }
- /// <summary>
- /// 聊天功能
- /// </summary>
- /// <param name="requestBody"></param>
- /// <returns></returns>
- public async Task<string> Chat(string requestBody)
- {
- if (string.IsNullOrEmpty(requestBody))
- return "Please enter effective information to seek.";
-
- using var client = new HttpClient();
-
- //config这种建议写在appsetting里面,这里只是做展示,所以写在一起
- var config = new MoonshotConfiguration
- {
- Url = "https://api.moonshot.cn/v1/chat/completions",
- Authorization = "Bearer 你的API key"
- };
-
- client.DefaultRequestHeaders.Add("Authorization", config.Authorization);
-
- var moonMessages = new List<MoonMessage>();
- moonMessages.Add(new MoonMessage
- {
- role = RoleEnum.user.ToString(),
- content = requestBody ?? ""
- });
- var requestMessageMoon = new RequestMessageMoon()
- {
- model = "moonshot-v1-8k",
- temperature = 0.3,
- messages = moonMessages
- };
- var playLoad = JsonConvert.SerializeObject(requestMessageMoon);
- try
- {
- var content = new StringContent(playLoad, Encoding.UTF8, "application/json");
- var responseMessage = await client.PostAsync(config.Url, content);
-
- if (responseMessage.IsSuccessStatusCode)
- {
- var cRoot = JsonConvert.DeserializeObject<Root>(await responseMessage.Content.ReadAsStringAsync());
- return cRoot?.choices.FirstOrDefault().message.content;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("这里自己可以对异常信息进行处理,比如日志记录,重试等等...");
- }
-
- return "Server is being upgrade ,please try it later...";
- }
我们可以看到成功的调用它的Api,并且在content 上返回了结果,有关其他的返回参数信息可以到Api文档中查看。
以上就是一个简单的连接Kimi Api参考,如果想自己搭建平台,建议可以优化HttpClient,以提高效率,希望对你有帮助。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。