最近gpt大火,它除了聊天,其实还具备了绘画功能,很多人不知道怎么用,下面文章介绍下。
在下面的案例中,输入“请帮我画一只可爱的动物”,返回的图片如下:
使用前提条件:
1、科学 shang wang
2、已经注册了GPT账号,并且能够正常使用。(这方面网上教程很多,这里就不重复了)
3、会简单的java编程。
步骤:
1、注册成为开发者 获取API keys
- 访问路径为:https://platform.openai.com/account/api-keys
2)点击 create new secret key 按钮生成 api token。需要把token先拷贝到其他地方哦。如果你不慎忘记了,是没法找回的,只能重新生成一个。
2、编写简单java代码
maven依赖:
com.theokanning.openai-gpt3-java api 0.11.0 com.theokanning.openai-gpt3-java client 0.11.0 com.theokanning.openai-gpt3-java service 0.11.0
逻辑代码:
package com.samur.openai; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.image.ImageResult; public class Picturegenerator { private static final String token = "sk-7 你的token EEXmKGcyo"; public static void main(String[] args) { String information = "画一只可爱的小动物"; System.setProperty("java.net.useSystemProxies", "true"); // 代理*** ip和端口 System.setProperty("https.proxyHost", "127.0.0.1"); System.setProperty("https.proxyPort", "1080"); OpenAiService service = new OpenAiService(token); CreateImageRequest createImageRequest= CreateImageRequest.builder() //提示信息,字符串最大长度为 1000 个字符。 .prompt(information) //生成图像的像素 可选项为:256x256、512x512、1024x1024 .size("512x512") //响应格式,生成的图像返回的格式。必须是url或b64_json,默认为url,url将在一小时后过期。 .responseFormat("url") //要生成的图像数。必须介于 1 和 10 之间。 .n(1) .build(); ImageResult result=service.createImage(createImageRequest); String url=result.getData().get(0).getUrl(); System.out.println(url); } }
运行上述代码后,将返回图片的 url 链接地址,再把该链接地址使用浏览器打开即可。一副图片绘画完成!!