.net平台使用SDK快速对接各大语言模型

.net平台使用SDK快速对接各大语言模型

1.项目介绍

最近,在做GPT项目时,发现各个平台的接口规范和参数不同,需要根据不同平台和模型写多种接口,比较麻烦,不想Python那样有丰富和方便的AI环境, 如果c#有一个SDK可以方便调用各种平台模型就好了,这是AllInAI.Sharp.API萌芽的起源。

于是我开发了这个SDK,包名:[AllInAI.Sharp.API](https://github.com/raokun/AllInAI.Sharp.API.Sample)。

AllInAI.Sharp.API一款调用各大平台语言模型的SDK,能帮助使用者快速对接各大模型。已整合OpenAI,chatGLM,文心千帆,同义千问,stable-diffusion 等 支持设置反向代理,支持流式接口 AllInAI SDK 在聊天和图片接口中整合统一的入参和出参。方便调用。

SDK已开源在nuget上,欢迎使用。

目前sdk使用.net7.如需其他版本支持请在项目中提issue。

2.已完成模型

  • OpenAI
  • chatGLM
  • 文心千帆
  • 同义千问
  • stable-diffusion

3.接口分类

目前分为两类

  • ChatService 聊天服务
  • ImgService 图片服务

4.使用范例

使用示例可参考示例项目:AllInAI.Sharp.API](https://github.com/raokun/AllInAI.Sharp.API.Sample

1.设置基础配置:

key -- 模型的秘钥key
BaseUrl -- 代理地址
AIType -- 模型类型,对应枚举Enums.AITypeEnum

2.调用接口

1.chat

AuthOption authOption = new AuthOption() { Key = "sk-***", BaseUrl = "https://api.openai.com", AIType = Enums.AITypeEnum.OpenAi };

ChatService chatService = new ChatService(authOption);
CompletionReq completionReq = new CompletionReq();
List<MessageDto> messages = new List<MessageDto>();
messages.Add(new MessageDto() { Role = "user", Content = "Hello!" });
completionReq.Model = "gpt-3.5-turbo";
completionReq.Messages = messages;
CompletionRes completionRes = await chatService.Completion(completionReq);

2.image

AuthOption authOption = new AuthOption() {BaseUrl = "http://43.134.164.127:77", AIType = Enums.AITypeEnum.SD };
ImgService imgService = new ImgService(authOption);
Txt2ImgReq imgReq = new Txt2ImgReq();
imgReq.Steps = 20;
imgReq.Size = "1024x1024";
imgReq.N = 1;
imgReq.Prompt = "kitty";
imgReq.ResponseFormat = "b64_json";
ImgRes imgRes = await imgService.Txt2Img(imgReq);

5.项目中的应用

AllInAI.Sharp.API 已在 TerraMours_Gpt 中广泛使用。TerraMours_Gpt 项目是一个多模型的AI应用和管理系统,支持多语言模型聊天、基于chatgpt和SD的多模型图片生成等功能。管理端实现数据看板、聊天记录管理,角色管理,图片记录管理、用户管理、系统配置等。

目前已支持配置api的模型:OpenAI,chatGLM,文心千帆,同义千问,stable-diffusion。

项目支持基于docker的快速部署搭建。个人搭建AI应用和管理系统只需一段docker-compose命令。

docker-compose命令

image-20231205102937675

阅读如遇样式问题,请前往个人博客浏览: https://www.raokun.top

拥抱ChatGPT:https://first.terramours.site

posted @ 2023-12-05 11:01  raokun  阅读(217)  评论(1编辑  收藏  举报