基于ChatGPT的API的C#接入研究
<p>今年开年,最火的莫过于ChatGPT的相关讨论,这个提供了非常强大的AI处理,并且整个平台也提供了很多对应的API进行接入的处理,使得我们可以在各种程序上无缝接入AI的后端处理,从而实现智能AI的各种应用。ChatGPT的API可以在前端,以及一些后端进行API的接入,本篇随笔主要介绍基于ChatGPT的API的C#接入研究。</p><h3>1、ChatGPT的介绍</h3>
<p>ChatGPT(全名:Chat Generative Pre-trained Transformer),美国OpenAI研发的聊天机器人程序 ,于2022年11月30日发布。ChatGPT是人工智能技术驱动的自然语言处理工具,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,真正像人类一样来聊天交流,甚至能完成撰写邮件、视频脚本、文案、翻译、代码,写论文 等任务。</p>
<p>ChatGPT是美国人工智能研究实验室OpenAI新推出的一种人工智能技术驱动的自然语言处理工具,使用了Transformer神经网络架构,也是GPT-3.5架构,这是一种用于处理序列数据的模型,拥有语言理解和文本生成能力,尤其是它会通过连接大量的语料库来训练模型,这些语料库包含了真实世界中的对话,使得ChatGPT具备上知天文下知地理,还能根据聊天的上下文进行互动的能力,做到与真正人类几乎无异的聊天场景进行交流。ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。</p>
<p>ChatGPT 的官网地址如下:https://platform.openai.com/ ,如果我们需要使用它的对话处理以及强大的API能力,需要注册才能使用,至于如何注册以及接收验证码的操作,请百度一下一下即可,这里忽略,只是介绍它的功能以及如何介入使用。</p>
<p>ChatGPT 可以做很多不同类型的工作,常规的问答聊天,编写各种语言的应用代码,编写论文、摘要等,以及图片处理,翻译等等,如下是它们的一些功能分类和介绍。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220120059763-956480061.png" alt="" width="605" height="524" loading="lazy"></p>
<p>如我们可以在对话中测试其聊天/应答功能。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220120807866-1457074913.png" alt="" width="725" height="874" loading="lazy"></p>
<p> 我们也可以让它编写一段操作代码,如下所示。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220120714862-1416608073.png" alt="" width="901" height="989" loading="lazy"></p>
<p> 如果我们要了解ChatGPT平台的API介绍,可以参考 https://platform.openai.com/docs/api-reference/introduction ,其大概分类也是很常规的功能应用相关的,如下是它的列表介绍。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220121023283-1522117011.png" alt="" width="193" height="414" loading="lazy"> </p>
<p>我们要学会如何使用API的话,需要了解它的相关模型概念,以及对各种处理的定义。</p>
<h3>2、基于ChatGPT的API的C#接入</h3>
<p>从上面的API介绍中,我们可以看到,API平台没有针对不同的语义给出不同的实现方式,只是给出了一个通用的调用方式,主要是基于输入参数,响应的数据格式的参考,具体的不同语言的应用如C#的接入,需要根据实际的格式进行对接。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220121456372-1634547098.png" alt="" width="853" height="835" loading="lazy"> </p>
<p> 主要的代码如下所示。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> RestSharp;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> Gpt3APIExample
{
</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Program
{
</span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> OpenAI API地址</span>
<span style="color: rgba(0, 0, 255, 1)">string</span> apiUrl = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://api.openai.com/v1/engines/gpt-3/jobs</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> OpenAI API密钥</span>
<span style="color: rgba(0, 0, 255, 1)">string</span> apiKey = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">YOUR_API_KEY</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个RestClient对象</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> client = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RestClient(apiUrl);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个RestRequest对象</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> request = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RestRequest(Method.POST);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在请求头中添加API密钥</span>
request.AddHeader(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Authorization</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Bearer </span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> apiKey);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加请求内容</span>
request.AddJsonBody(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)">
{
model </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text-davinci-002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
prompt </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">What is the capital of France?</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
max_tokens </span>= <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">,
n </span>= <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">,
stop </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
temperature </span>= <span style="color: rgba(128, 0, 128, 1)">0.5</span><span style="color: rgba(0, 0, 0, 1)">,
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送请求并获取响应</span>
IRestResponse response =<span style="color: rgba(0, 0, 0, 1)"> client.Execute(request);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示响应内容</span>
<span style="color: rgba(0, 0, 0, 1)"> Console.WriteLine(response.Content);
Console.ReadLine();
}
}
}</span></pre>
</div>
<p>这个代码主要就是针对输入信息和输出的内容进行简单的包装,使用 RestSharp 的一个Http类库进行访问的包装。</p>
<p>而为了更好的使用ChatGPT的API处理,我们可以使用更高级一点的类库,如下面介绍的两个开源C#包装ChatGPT的类库:</p>
<p>https://github.com/OkGoDoIt/OpenAI-API-dotnet</p>
<p>https://github.com/RageAgainstThePixel/OpenAI-DotNet </p>
<p>我觉得他们都是很不错的,都是基于ChatGPT的API格式进行了一定程度的面向对象的包装,使得我们基于C#开发起来更方便。</p>
<p>例如我们基于 https://github.com/RageAgainstThePixel/OpenAI-DotNet </p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220122125559-405745146.png" alt="" width="509" height="87" loading="lazy"></p>
<p> 来查看使用代码,它的介绍也比较详细,基本上覆盖到了各个方面。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220122322196-1930178532.png" alt="" width="315" height="552" loading="lazy"></p>
<p> 我们可以模仿来生成自己的代码即可,如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 基于OpenAI-DotNet的使用
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Test2
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">async</span><span style="color: rgba(0, 0, 0, 1)"> Task ExcuteCode()
{
</span><span style="color: rgba(0, 0, 255, 1)">var</span> api = <span style="color: rgba(0, 0, 255, 1)">new</span> OpenAIClient(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OpenAIAuthentication(Constants.ApiKey));
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result = <span style="color: rgba(0, 0, 255, 1)">await</span> api.CompletionsEndpoint.CreateCompletionAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">读取图片文件的展示到窗体的C#代码</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(128, 0, 128, 1)">1000</span>, <span style="color: rgba(128, 0, 128, 1)">0.1</span><span style="color: rgba(0, 0, 0, 1)">);
Log.Information(result.ToJson());
}</span></pre>
</div>
<p>运行效果,可以看到输出的结果。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220122532025-1290019111.png" alt="" width="943" height="995" loading="lazy"></p>
<p> 对于图片的生成和处理,我们也可以按照类似的API进行调用接口,如下是一段寻找图片的代码。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">var</span> results = <span style="color: rgba(0, 0, 255, 1)">await</span> api.ImagesEndPoint.GenerateImageAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国布达拉宫的全景图片</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">, ImageSize.Large);
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> results)
{
Log.Information(item);
}</span></pre>
</div>
<p>为了验证图片的AI处理,我们生成两张正方形的图片,图片右下角扣掉一块空白的图片,图片格式使用PNG格式,然后调用如下代码进行替换处理。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">var</span> imageAssetPath = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">images/休息区.png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> maskAssetPath = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">images/休息区2.png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> imageResults = <span style="color: rgba(0, 0, 255, 1)">await</span> api.ImagesEndPoint.CreateImageEditAsync(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">阳光明媚的室内休息区,<span style="color: rgba(255, 0, 0, 1)"><strong>有一个鸭子在池塘中</strong></span></span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">, ImageSize.Small);
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> imageResults)
{
Log.Information(item);
}</span></pre>
</div>
<p> <img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220125152878-845717819.png" alt="" width="621" height="485" loading="lazy"></p>
<p> </p>
<p> 生成后的图片效果如下所示。</p>
<p><img src="https://img2023.cnblogs.com/blog/8867/202302/8867-20230220124948233-1926696419.png" alt="" width="686" height="512" loading="lazy"></p>
<p> 生成的空白地方,填入一个所需要的区域,红色框是我加上去醒目的。</p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 98%; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee;">
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt>
<span style="color: #000000"><span class="Apple-tab-span" style="white-space: pre"></span>
专注于代码生成工具、.Net/Python 框架架构及软件开发,以及各种Vue.js的前端技术应用。著有Winform开发框架/混合式开发框架、微信开发框架、Bootstrap开发框架、ABP开发框架、SqlSugar开发框架、Python开发框架等框架产品。
<br> 转载请注明出处:撰写人:伍华聪 http://www.iqidi.com <br> </span></div><br><br>
来源:https://www.cnblogs.com/wuhuacong/p/17136952.html
頁:
[1]