OpenAI Dall-E-客户端(VB.NET源码)
有关如何在 VB.NET 中询问 Dall-E 的概述
本文展示了如何 VB.NET OpenAI 成像模型 Dall-E-2 生成 Web 请求,作为基于 Web 以及 NodeJS 和 Python 解决方案的替代方案。
该代码以 JSON 格式向 OpenAI 图像生成终端节点发送请求,编写一个标准请求,以使用 Dall-E-2 模型和标准质量大小 512x512 生成 1 个图像;对 JSON 格式的响应进行反序列化,并提取图像 URL 并显示在 Picturebox 中 使用代码该代码由一个长过程组成,用于管理发送请求和获取响应;启用两个小过程来管理 Prompt TextBox 并将图像保存到本地磁盘。 该代码使用以下导入: - Imports System.Net
- Imports System.IO
- Imports System.Configuration
- Imports System.Security.Cryptography
- Imports System.Net.SecurityProtocolType
- Imports System.Threading.Tasks
- Imports System.ComponentModel
复制代码专用表单包含: - 提示的文本框
- OpenAI API 密钥的文本框
- 用于显示图像 URL 的 pictureBox
- 用于发送请求的按钮
- 用于保存图像的按钮
- 显示活动日志的 Textbox(一种控制台)
该事件触发了所有的魔法!btnSend_Click 第一个操作是设置请求并将其 JSONify 化: - logTxt.Text = "Sending request..." & vbCrLf
- System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
- Dim apiEndpoint As String = "https://api.openai.com/v1/images/generations"
- Dim apiKey As String = ApiKeyTextBox.Text
- Dim prompt As String = PromptTextBox.Text
- Dim model As String = "dall-e-2"
- Dim numberofimages As String = "1"
- Dim imagesize As String = "512x512"
- ' Creation of HTTP request
- Dim request As HttpWebRequest = WebRequest.Create(apiEndpoint)
- request.Method = "POST"
- request.ContentType = "application/json"
- request.Headers.Add("Authorization", "Bearer " & apiKey)
- ' Creation of JSON request
- Dim data As String = "{"
- data += " ""model"": """ & model & ""","
- data += " ""prompt"": """ & prompt & ""","
- data += " ""n"": 1,"
- data += " ""size"": """ & imagesize & """"
- data += "}"
- logTxt.Text += "Request grammar: " & data.ToString & vbCrLf
- ' Write body of request
- Using streamWriter As New StreamWriter(request.GetRequestStream())
- streamWriter.Write(data)
- End Using
复制代码第二步是收集响应并反序列化它。 - Dim response As HttpWebResponse = Nothing
- Try
- response = CType(request.GetResponse(), HttpWebResponse)
- 'Getting JSON format
- Dim responseStream As Stream = response.GetResponseStream()
- Dim reader As New StreamReader(responseStream)
- Dim jsonResponse As String = reader.ReadToEnd()
- logTxt.Text += "Receiving response..." & vbCrLf
- reader.Close()
- responseStream.Close()
- 'Deserializing JSON response
- Dim oJavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
- Dim oJson As Hashtable = oJavaScriptSerializer.Deserialize(Of Hashtable)(jsonResponse)
- logTxt.Text += "Response received: " & jsonResponse.ToString & vbCrLf
复制代码完成后,我们可以拦截图像 url 并将其显示在 :PictureBox - 'Extracting the image URL
- Dim imageUrl As String = oJson("data")(0)("url").ToString()
- ' Visualizing the image
- Dim imageClient As New WebClient()
- Dim imageData As Byte() = imageClient.DownloadData(imageUrl)
- Using ms As New MemoryStream(imageData)
- PictureBox1.Image = Image.FromStream(ms)
- End Using
- saveBtn.Visible = True
复制代码需要完成一些简单的错误处理: - Catch ex As WebException
- 'simple error handling
- MessageBox.Show("Web error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Finally
- If response IsNot Nothing Then
- response.Close()
- End If
- End Try
复制代码为了完整起见,下面报告了将图像保存到本地光盘的例程: - If PictureBox1.Image IsNot Nothing Then
- Dim saveDialog As New SaveFileDialog()
- saveDialog.Filter = "JPG Image|*.jpg"
- saveDialog.Title = "Save Image"
- If saveDialog.ShowDialog() = DialogResult.OK Then
- PictureBox1.Image.Save(saveDialog.FileName, Imaging.ImageFormat.Jpeg)
- MessageBox.Show("Imge saved correctly!", "Save Image", MessageBoxButtons.OK, MessageBoxIcon.Information)
- End If
- Else
- MessageBox.Show("Image could not be saved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End If
复制代码
兴趣点对代码的主要兴趣是通过 VB.NET 询问 OpenAI API,而不必依赖 Python 或 NodeJS,这是此类操作中使用的两种主要技术,也是 OpenAI 在其文档中支持的唯一两种技术。 VB.NET 仍然允许桌面开发人员与现代技术进行交互,尤其是在不依赖第三方库的情况下。 提取码下载:
|