前言 智谱 AI 大模型提供了多种模型的接口调用,并且其中glm-4-flash还提供免费调用。
这里就简单体验一下
官方的体验入口
https://open.bigmodel.cn/console/trialcenter
接口
https://open.bigmodel.cn/dev/api/normal-model/glm-4
SDK
https://open.bigmodel.cn/dev/api/libraries
模型列表
https://open.bigmodel.cn/dev/howuse/model
免费服务
https://open.bigmodel.cn/dev/activities/freebie
.Net SDK https://github.com/MetaGLM/zhipuai-sdk-csharp-v4.git
创建你的项目z_chat_ai文件夹,并进入
1 2 git clone https://github.com/MetaGLM/zhipuai-sdk-csharp-v4.git dotnet new console -o ZChatAi
引用sdk
1 2 3 cd ZChatAi dotnet add reference ../zhipuai-sdk-csharp-v4/ZhipuApi.csproj dotnet add package System.Text.Json
调用示例
Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 using System.Text.Json;using ZhipuApi;using ZhipuApi.Models.RequestModels;namespace ZChatAi ;class Program { static void Main (string [] args ) { var clientV4 = new ClientV4("API_KEY" ); var response = clientV4.chat.Completion( new TextRequestBase() .SetModel("glm-4-flash" ) .SetMessages(new [] { new MessageItem("user" , "使用Java写一段冒泡排序?" ) }) .SetTemperature(0.7 ) .SetTopP(0.7 ) ); JsonSerializerOptions DEFAULT_SERIALIZER_OPTION = new JsonSerializerOptions { WriteIndented = true }; Console.WriteLine(JsonSerializer.Serialize(response, DEFAULT_SERIALIZER_OPTION)); } }
运行
流式调用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 using System.Text.Json;using ZhipuApi;using ZhipuApi.Models.RequestModels;namespace ZChatAi ;class Program { static void Main (string [] args ) { var clientV4 = new ClientV4("API_KEY" ); var responseIterator = clientV4.chat.Stream( new TextRequestBase() .SetModel("glm-4-flash" ) .SetMessages(new [] { new MessageItem("user" , "1+1等于多少" ), new MessageItem("assistant" , "1+1等于2。" ), new MessageItem("user" , "使用Java写一个冒泡排序?" ), }) .SetTemperature(0.7 ) .SetTopP(0.7 ) ); foreach (var response in responseIterator) { Console.Write(response.choices[0 ].delta.content); } } }
WPF 创建WPF项目
添加依赖
1 2 3 dotnet add package System.Text.Json dotnet add package System.IdentityModel.Tokens.Jwt dotnet add package Newtonsoft.Json
也就是
1 2 3 <PackageReference Include ="Newtonsoft.Json" Version ="13.0.3" /> <PackageReference Include ="System.IdentityModel.Tokens.Jwt" Version ="8.1.2" /> <PackageReference Include ="System.Text.Json" Version ="8.0.5" />
添加WebView2
1 dotnet add package Microsoft.Web.WebView2 --version 1.0.2792.45
添加JS
1 <script src ="https://js.cybozu.cn/markedjs/v0.3.5/marked.min.js" > </script >
解析Markdown
1 2 3 4 let htmlStr = marked.parse ("## 你好" )document .getElementById ('body' ).innerHTML = htmlStr