赞
踩
langchain4j版本用的是0.27.1,LLM大模型使用OpenAi
本文主要介绍聊天对话、流式对话、文生图三种模式的使用,只需要两步就可以实现和LLM交互
<?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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>langChain_demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <langchain4j.version>0.27.1</langchain4j.version> </properties> <dependencies> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> <version>${langchain4j.version}</version> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> <version>${langchain4j.version}</version> </dependency> </dependencies> </project>
apikey为demo时,将url转成代理地址http://langchain4j.dev/demo/openai/v1,非demo时,则url为https://api.openai.com/v1。
简单聊天场景可以使用demo测试,更高级的场景如文生图等需要自己申请的openAi的令牌key。
后续示例中的常量OPEN_AI_BASE_URL 为 https://api.openai.com/v1,OPEN_AI_API_KEY需要设置自己的key
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.builder()
.apiKey("demo")
.build();
String result = model.generate("你是谁");
System.out.println(result);
}
上面ChatLanguageModel模型,大模型会一下输出所有信息;如果需要像打字机一样,一个字一个字输出,则可以使用StreamingChatLanguageModel模型
public static void main(String[] args) { StreamingChatLanguageModel model = OpenAiStreamingChatModel.builder() .baseUrl(OPEN_AI_BASE_URL) .apiKey(OPEN_AI_API_KEY) .build(); model.generate("你好 我是小橘", new StreamingResponseHandler<AiMessage>() { @Override public void onNext(String token) { System.out.println(token); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new RuntimeException(e); } } @Override public void onError(Throwable throwable) { } }); }
大模型可以根据你的信息生成对应的图片,下面链接就是生成的橘猫图片url;响应除了可以拿到url外,也提供下Base64的编码的文件字符串
https://oaidalleapiprodscus.blob.core.windows.net/private/org-ZLTuOuStHhQibWNJKgkpotO2/user-TFhEhsgVpt9L4c1u6Smt9gIu/img-Qb8hXLdi3NQDnhxO1DKu7NH6.png?st=2024-05-17T06%3A58%3A23Z&se=2024-05-17T08%3A58%3A23Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-05-16T13%3A52%3A40Z&ske=2024-05-17T13%3A52%3A40Z&sks=b&skv=2021-08-06&sig=yHC60xphZw1Zu94VKPnWx0yK0Ggq/BzUsspwQKFTAC8%3D
public static void main(String[] args) {
ImageModel imageModel = OpenAiImageModel.builder()
.baseUrl(OPEN_AI_BASE_URL)
.apiKey(OPEN_AI_API_KEY)
.build();
Response<Image> response = imageModel.generate("橘猫");
System.out.println(response.content().url());
}
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。