动手点关注
干货不迷路
当前LLM应用集成开发工具链支持语言以Python为主,LangChain便是其中代表,这对于Web时代占霸主地位的Java开发者来讲十分不友好,生态差距明显。虽然,开发者可以转语言,投入Python怀抱,但很多时候对于普通开发者以及大量既有Java应用来讲并不容易,让人有一种Java开发者会被时代抛弃的错觉。 微软作为LLM应用开发全栈布局最稳健的的巨头,在模型层和OpenAI(ChatGPT),Meta(llama2)等企业形成了战略合作,在应用开发层面不仅集成了主流的LangChain,还自主开发了Semantic-Kernel,该框架不仅支持Python,还支持dotNet,笔者在前些日子也爆料微软在紧锣密鼓开发Java版本《爆料:微软大模型开发包Semantic Kernel (SK)疑似增加java版本,java开发者有福了》。这一消息最近也坐实了,微软官方博客已经撰文预热Java版本,感兴趣的朋友可以一看究竟:https://devblogs.microsoft.com/semantic-kernel/introducing-semantic-kernel-for-java/。
但对于Java开发者来讲,微软技术栈有一种天然的陌生感,加上它的一些领域概念和设计实现,与LangChain有一定的差异,因此,未来Semantic-Kernel在Java社区的表现还有待观察。不过,前段时间笔者注意到一个有潜力的项目LangChain4J(https://github.com/langchain4j/langchain4j),从名字就能看到它的定位比较明确,就是要做java版本的LangChain。
经过一个月左右的持续跟踪,笔者发现该项目成长很快,目前项目最新版本0.16.0,虽然,点赞量仅有144个,6个贡献者,兼容Java 8和Springboot 2,3,从资源投入上比不上S emantic-Kernel ,开发到现在不过一个多月,但更新频繁(一周一个版本),并且设计思想和领域概念与LangChain相似,对于那些熟悉LangChain的java开发者比较友好。
项目目标:
简化Java 应用集成LLM能力的复杂度。
设计理念与功能特性:
1.简单一致的抽象层,上层开发者无需关注大模型,向量数据库等底层实现。
大量的候选底层实现,开发者可以按需选择。
3 .LLM开发的相关特性 :
1)多样数据摄取能力 2)智能agent 3)prompt模版,以便获得模型的高质量输出 4)Memory,支持多轮对话context管理 5)结构化的输出,可以将大模型输出转换成java POJO类 6)AI service抽象,将复杂AI行为封装成简单API供上层使用。 7)Chain,常见的高阶功能模版封装,简化上层实现,如问答chain。 8)引导改进,模型输入输出的适应性调整,保证输入输出的无害。 从功能目标来看,虽然和Python版LangChain有一些差距,但整体看还是比较完整的,覆盖了LLM应用开发的大部分需求,并且有AI services这样的特有设计,更加贴合业务开发需要。
快速上手:
1.添加依赖
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>0.16.0</version>
</dependency>
2.配置OpenAI/HuggingFace APIKey(暂无法直接使用本地LLM):
String apiKey = System.getenv("OPENAI\_API\_KEY");
3.创建模型实例
OpenAiChatModel model = OpenAiChatModel.withApiKey(apiKey);
AiMessage answer = model.sendUserMessage("Hello world!");
System.out.println(answer.text()); // Hello! How can I assist you today?
AIservice封装示例: 1.简单声明一个AIService:
interface Assistant {
String chat(String userMessage);
}
Assistant assistant = AiServices.create(Assistant.class, model);
String answer = assistant.chat("Hello");
System.out.println(answer); // Hello! How can I assist you today?
2.把LLM作为一个分类器示例:
enum Sentiment {
POSITIVE, NEUTRAL, NEGATIVE;
}
interface SentimentAnalyzer {
@UserMessage("Analyze sentiment of {{it}}")
Sentiment analyzeSentimentOf(String text);
@UserMessage("Does {{it}} have a positive sentiment?")
boolean isPositive(String text);
}
SentimentAnalyzer sentimentAnalyzer = AiServices.create(SentimentAnalyzer.class, model);
Sentiment sentiment = sentimentAnalyzer.analyzeSentimentOf("It is good!");
// POSITIVE
boolean positive = sentimentAnalyzer.isPositive("It is bad!");
// false
3.结构化输入输出:
class Person {
private String firstName;
private String lastName;
private LocalDate birthDate;
public String toString() {...}
}
interface PersonExtractor {
@UserMessage("Extract information about a person from {{it}}")
Person extractPersonFrom(String text);
}
PersonExtractor extractor = AiServices.create(PersonExtractor.class, model);
String text = "In 1968, amidst the fading echoes of Independence Day, "
+ "a child named John arrived under the calm evening sky. "
+ "This newborn, bearing the surname Doe, marked the start of a new journey.";
Person person = extractor.extractPersonFrom(text);
// Person { firstName = "John", lastName = "Doe", birthDate = 1968-07-04 }
4.prompt模版支持mustache语法:
interface Translator {
@SystemMessage("You are a professional translator into {{language}}")
@UserMessage("Translate the following text: {{text}}")
String translate(@V("text") String text, @V("language") String language);
}
Translator translator = AiServices.create(Translator.class, model);
String translation = translator.translate("Hello, how are you?", "Italian");
// Ciao, come stai?
更多例子:https://github.com/langchain4j/langchain4j-examples/tree/main/other-examples/src/main/java
当前已经实现的能力:
小结:
客观讲,虽然该项目定位很好,也在快速迭代,但就笔者来看,很多功能尚不够完备,如本地模型,一些向量数据库支持等,还需要大量工作才能达到生产可用状态。想要使用的开发者要做好和它一起成熟的准备, 不过还是希望大家可以尝试试用,并且做代码贡献,让这个项目能够更快成长。
阅读至此了,分享、点赞、在看三选一吧🙏