Initial commit
This commit is contained in:
49
langchain4j-ai-image/pom.xml
Normal file
49
langchain4j-ai-image/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iwe3</groupId>
|
||||
<artifactId>langchain4j-ai-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>langchain4j-ai-image</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!--DashScope (Qwen)接入阿里云百炼平台
|
||||
https://docs.langchain4j.dev/integrations/language-models/dashscope
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--导入低阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
<!--导入高阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ImageApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ImageApplication.class,args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.community.model.dashscope.WanxImageModel;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LLMConfig
|
||||
{
|
||||
@Bean
|
||||
public ChatModel ImageModel() {
|
||||
return OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
//qwen-vl-max 是一个多模态大模型,支持图片和文本的结合输入,适用于视觉-语言任务。
|
||||
.modelName("qwen-vl-max")
|
||||
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description: 测试通义万象来实现图片生成,
|
||||
* 知识出处,https://help.aliyun.com/zh/model-studio/text-to-image
|
||||
*/
|
||||
@Bean
|
||||
public WanxImageModel wanxImageModel()
|
||||
{
|
||||
return WanxImageModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.modelName("wanx2.1-t2i-turbo") //图片生成 https://help.aliyun.com/zh/model-studio/text-to-image
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.iwe3.langchain4j.controller;
|
||||
|
||||
import dev.langchain4j.data.message.ImageContent;
|
||||
import dev.langchain4j.data.message.TextContent;
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class ImageModelController
|
||||
{
|
||||
@Autowired
|
||||
private ChatModel ImageModel;
|
||||
|
||||
@Value("classpath:static/imgs/1.png")
|
||||
private Resource resource;//import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* 通过Base64编码将图片转化为字符串
|
||||
* 结合ImageContent和TextContent形成UserMessage一起发送到模型进行处理。
|
||||
*
|
||||
*测试地址:http://localhost:9005/lc4j/image/call
|
||||
*/
|
||||
@GetMapping(value = "/lc4j/image/call")
|
||||
public String readImageContent() throws IOException
|
||||
{
|
||||
String result = null;
|
||||
|
||||
//第一步,图片转码:通过Base64编码将图片转化为字符串
|
||||
byte[] byteArray = resource.getContentAsByteArray();
|
||||
String base64Data = Base64.getEncoder().encodeToString(byteArray);
|
||||
|
||||
//第二步,提示词指定:结合ImageContent和TextContent一起发送到模型进行处理。
|
||||
UserMessage userMessage = UserMessage.from(
|
||||
TextContent.from("她是谁?"),
|
||||
ImageContent.from(base64Data, "image/jpg")
|
||||
);
|
||||
//第三步,API调用:使用OpenAiChatModel来构建请求,并通过chat()方法调用模型。
|
||||
//请求内容包括文本提示和图片,模型会根据输入返回分析结果。
|
||||
ChatResponse chatResponse = ImageModel.chat(userMessage);
|
||||
|
||||
//第四步,解析与输出:从ChatResponse中获取AI大模型的回复,打印出处理后的结果。
|
||||
result = chatResponse.aiMessage().text();
|
||||
|
||||
//后台打印
|
||||
System.out.println(result);
|
||||
|
||||
//返回前台
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.iwe3.langchain4j.controller;
|
||||
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
|
||||
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
|
||||
import com.alibaba.dashscope.exception.ApiException;
|
||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||
import com.alibaba.dashscope.utils.JsonUtils;
|
||||
import dev.langchain4j.community.model.dashscope.WanxImageModel;
|
||||
import dev.langchain4j.data.image.Image;
|
||||
import dev.langchain4j.model.output.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class WanxImageModelController
|
||||
{
|
||||
@Autowired
|
||||
private WanxImageModel wanxImageModel;
|
||||
|
||||
// http://localhost:9005/lc4j/image/create
|
||||
@GetMapping(value = "/lc4j/image/create")
|
||||
public String createImageContent() throws IOException
|
||||
{
|
||||
System.out.println(wanxImageModel);
|
||||
var imageResponse = wanxImageModel.generate("美女");
|
||||
System.out.println(imageResponse.content().url());
|
||||
return imageResponse.content().url().toString();
|
||||
|
||||
}
|
||||
|
||||
// http://localhost:9005/lc4j/image/create2
|
||||
@GetMapping(value = "/lc4j/image/create2")
|
||||
public String createImageContent2() throws IOException
|
||||
{
|
||||
|
||||
var prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头," +
|
||||
"民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。";
|
||||
var param =
|
||||
ImageSynthesisParam.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.model(ImageSynthesis.Models.WANX_V1)
|
||||
.prompt(prompt)
|
||||
.style("<watercolor>")
|
||||
.n(1)
|
||||
.size("1024*1024")
|
||||
.build();
|
||||
|
||||
var imageSynthesis = new ImageSynthesis();
|
||||
ImageSynthesisResult result = null;
|
||||
|
||||
try {
|
||||
System.out.println("---sync call, please wait a moment----");
|
||||
|
||||
result = imageSynthesis.call(param);
|
||||
} catch (ApiException | NoApiKeyException e){
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
System.out.println(JsonUtils.toJson(result));
|
||||
|
||||
return JsonUtils.toJson(result);
|
||||
}
|
||||
}
|
||||
5
langchain4j-ai-image/src/main/resources/application.yml
Normal file
5
langchain4j-ai-image/src/main/resources/application.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
server:
|
||||
port: 9005
|
||||
spring:
|
||||
application:
|
||||
name: langchain4j-ai-image
|
||||
BIN
langchain4j-ai-image/src/main/resources/static/imgs/1.png
Normal file
BIN
langchain4j-ai-image/src/main/resources/static/imgs/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 250 KiB |
BIN
langchain4j-ai-image/src/main/resources/static/imgs/2.png
Normal file
BIN
langchain4j-ai-image/src/main/resources/static/imgs/2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 492 KiB |
Reference in New Issue
Block a user