大型語言模型(LLMS)正在改變包括軟件開發在內的各個領域。 他們理解和生成文本(和其他數據類型)的能力可以從文本提示中實現代碼建議,更正甚至生成。本文探討了基於Java的解決方案jlama 庫,用於將LLM集成到Java生態系統中。 Jlama提供靈活性,可作為命令行接口(CLI)或項目的依賴性(例如,通過pom.xml
)。我們將通過將其集成到spring boot應用程序來演示其功能。
>先決條件和突出顯示
由於使用Java Vector API,Jlama需要Jlama 20或更高的Java 20或更高。 現有的
langchain
用戶可以將其與Jlama集成,利用Langchain的工具進行簡化的LLM交互。
這個示例項目具有兩個通過提示與LLMS交互的兩個端點:>
langchain和jlama結合了端點。
此端點直接利用Jlama根據用戶提示生成響應。
定義了所需的模型。如果不是本地可用的,它將自動下載到指定的目錄。 創建了提示上下文,Jlama生成了響應。 >
蘭鍊和jlama端點
這個端點使用蘭鏈,減少了Jlama交互所需的代碼。
Langchain通過直接在構建器中定義模型和參數來簡化實現。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @PostMapping( "/jlama" )
public ResponseEntity<ChatPromptResponse> chatJlama(@RequestBody ChatPromptRequest request) {
PromptContext context;
if (abstractModel.promptSupport().isPresent()) {
context = abstractModel.promptSupport()
.get()
.builder()
.addSystemMessage( "You are a helpful chatbot providing concise answers." )
.addUserMessage(request.prompt())
.build();
} else {
context = PromptContext.of(request.prompt());
}
System.out.println( "Prompt: " + context.getPrompt() + "\n" );
Generator.Response response = abstractModel
.generate(UUID.randomUUID(), context, 0.0f, 256, (s, f) -> {});
System.out.println(response.responseText);
return ResponseEntity.ok( new ChatPromptResponse(response.responseText));
}
|
登入後複製
鏈接和引用
1 2 3 4 5 6 7 8 9 | String model = "tjake/Llama-3.2-1B-Instruct-JQ4" ;
String workingDirectory = "./models" ;
File localModelPath = new Downloader(workingDirectory, model).huggingFaceModel();
ModelSupport.loadModel(localModelPath, DType.F32, DType.I8);
|
登入後複製
這個項目的靈感來自Isidro教授在Soujava的演講。 [鏈接到演示文稿(如果有的話,請替換為實際鏈接)]
>
有用的文檔:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @PostMapping( "/langchain" )
public ResponseEntity<Object> chatLangChain(@RequestBody ChatPromptRequest request) {
var model = JlamaChatModel.builder()
.modelName( "meta-llama/Llama-3.2-1B" )
.temperature(0.7f)
.build();
var promptResponse = model.generate(
SystemMessage.from( "You are a helpful chatbot providing the shortest possible response." ),
UserMessage.from(request.prompt()))
.content()
.text();
System.out.println( "\n" + promptResponse + "\n" );
return ResponseEntity.ok(promptResponse);
}
|
登入後複製
GitHub上的Jlama [鏈接到Jlama GitHub(替換為實際鏈接)]>
> langchain [鏈接到Langchain文檔(替換為實際鏈接)]>
結論
Jlama和Langchain提供了將LLM集成到Java應用程序中的有力方法。本文演示瞭如何與Spring Boot配置和使用這些工具來創建有效的文本提示處理端點。
您是否在Java項目中與LLMS合作?在評論中分享您的經驗和見解!
以上是使用Spring Boot和Langchain探索JLAMA圖書館的詳細內容。更多資訊請關注PHP中文網其他相關文章!