chatgpt 是由 openai 开发的强大语言模型,可以用于生成类似人类的文本。openai api 允许开发人员访问该模型并在其自己的应用程序中使用。在本文中,我们将讨论如何使用 java spring framework 与 openai api 生成图像。
在开始之前,您需要在 openai 网站 "https://beta.openai.com/account/api-keys" 上注册 api 密钥。一旦拥有了 api 密钥,就可以开始向 api 发送请求。
要使用 java spring framework 与 openai api,请使用可以处理 http 请求的库。这方面比较常用的库是 spring resttemplate 库。resttemplate 是一种强大而灵活的库,可以轻松地发送 http 请求并处理响应。
首先,需要将 spring resttemplate 库添加到项目中。可以将以下依赖项添加到 build.gradle 文件:
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.openai'
version = '0.0.1-snapshot'
sourcecompatibility = '17'
repositories {
mavencentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testimplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
usejunitplatform()
}
接下来,创建一个处理 api 调用的类。可以使用下面的示例:
@component
public class openai {
private static final string openai_url = "https://api.openai.com/v1/images/generations";
private final string apikey = "<your-api-key";
private final resttemplate resttemplate = new resttemplate();
public string generateimages(string prompt, float temperature, int maxtokens, string stop, final int logprobs, final boolean echo) {
httpheaders headers = new httpheaders();
headers.setcontenttype(mediatype.application_json);
headers.set("authorization", "bearer " + apikey);
// we are including only some of the parameters to the json request
string requestjson = "{\"prompt\":\"" + prompt + "\",\"n\":" + n + "}";
httpentity < string > request = new httpentity < > (requestjson, headers);
responseentity < string > response = resttemplate.postforentity(openai_url, request, string.class);
return response.getbody();
}
}
这个类使用 spring resttemplate 库向 openai api 的生成图像端点发送 post 请求,包含给定的提示词和想要接收的图像数量。如果需要,还可以增加一些其他可选参数,可以在下面的链接中检查:https://beta.openai.com/docs/api-reference/images/create。
api 密钥已添加到请求头中,并以 json 的形式发送请求。然后解析响应以返回生成的图像 url。
现在,可以在代码中使用此类来使用 chatgpt 生成图像。以下是如何在 spring 控制器中使用 *generateimages* 方法的示例。
@restcontroller
public class openaicontroller {
@autowired
private final openai openai;
public openaicontroller(openai openai) {
this.openai = openai;
}
@postmapping("/generateimages")
public string generateimages(@requestbody generateimagesrequest request) {
return openai.generateimages(request.getprompt(), request.gettemperature(), request.getmaxtokens(), request.getstop(),
request.getlogprobs(), request.isecho(), request.getn());
}
}
上面的代码是 spring rest 控制器的示例,它使用请求正文中传递的参数调用 openai 类中的 generateimages 方法。它将 post 请求映射到 /generateimages 终点,并将生成的图像作为响应返回。
还可以为请求正文定义一个 pojo 类,例如:
public class generateimagesrequest {
private string prompt;
private float temperature;
private int maxtokens;
private string stop;
private int logprobs;
private boolean echo;
private int n;
// getters and setters
}
值得注意的是,生成的图像可能不完美,可能需要进一步审查和调整。我们已向模型添加了许多参数,但在向服务器发送 post 请求时,openai 实用程序类中并未使用它们。这是因为这些参数大多是可选的,有些甚至对于生成图像的端点(endpoint)无效。但是,对于其他端点,如“文本完成”等,这些是有效的。
测试 api 的时间到了
现在,我们已经准备好端点,可以启动服务器并使用以下 url 从 postman 或任何其他 api 测试工具发出 post 请求。
http://localhost:8080/generateimages
这是我的 api 测试截图和响应:

可以看到具有 prompt 和 n 值的 json 请求正文。n=3 表示我们将收到 5 张生成的图像作为响应。我们确实收到与刚刚发送的提示文本相关的 3 个图像 url。
现在可以复制这些 url 并将它们粘贴到浏览器中以查看实际图像。
图像生成使用 dall·e 模型。有许多其他模型可供使用,根据要求进行选择。不仅可以生成图像,还可以操纵它们。可以都尝试一下,看看哪个最合心意。使用下面的链接获取所有可用模型:https://api.openai.com/v1/models(将你的 api 密钥作为 bearer 令牌添加)。
本文提供了有关如何使用 java spring framework 与 openai api 生成 chatgpt 图像的详细步骤。希望本指南有助于在基于 spring 的项目中使用 chatgpt 的强大功能,使开发变得更轻松!
发表评论