Open langchain4j opened 1 day ago
Regarding https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=37: did you consider using high-level AI Services? They integrate nicely into Spring Boot application, here are few examples:
hi @langchain4j , sure! Nice to be in touch. Regarding entities, I was referring to this: https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=32 I know it's also possible to do Structured Outputs in LangChain4J as shown here, but I find it complicated.
Hope that helps :)
@michaelisvy thank you for taking the time to provide feedback, I really appreciate! Would you mind elaborating a bit further? I'd love to better understand which specific aspects you found complicated.
There are currently 2 ways to implement this in LC4J:
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
@AiService
public interface ActorFilmsGenerator {
ActorFilms generate(String query);
}
@RestController
public class ActorFilmsController {
private final ActorFilmsGenerator actorFilmsGenerator;
public ActorFilmsController(ActorFilmsGenerator actorFilmsGenerator) {
this.actorFilmsGenerator = actorFilmsGenerator;
}
@GetMapping("/generateActorFilms")
public ActorFilms generateActorFilms() {
return actorFilmsGenerator.generate("Generate the 10 most popular movies starring Bruce Willis");
}
}
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
@RestController
public class ActorFilmsController {
private final ChatLanguageModel chatModel;
public ActorFilmsController(ChatLanguageModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/generateActorFilms")
public String generateActorFilms() {
ChatRequest chatRequest = ChatRequest.builder()
.messages(UserMessage.from("Generate the 10 most popular movies starring Bruce Willis"))
.responseFormat(ResponseFormat.builder()
.type(JSON)
.jsonSchema(JsonSchemas.jsonSchemaFrom(ActorFilms.class).get())
.build())
.build();
ChatResponse chatResponse = chatModel.chat(chatRequest);
return chatResponse.aiMessage().text();
}
}
understood thanks! I had missed it from the documentation, I'll add it to my sample application.
By the way, one small other thing which I thought was missing: I didn't find a way to load an image from th classpath. See here: https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=33
Cheers, Michael.
@michaelisvy sure, this can be done as well:
@Service
class ImageService {
@Value("classpath:images/scientist.jpg")
private Resource imageResourceScientist;
private final ChatLanguageModel chatModel;
public ImageService(ChatLanguageModel chatModel) {
this.chatModel = chatModel;
}
public String describeScientist() throws IOException {
UserMessage userMessage = UserMessage.from(
TextContent.from("can you describe this person? And what is written on top of his head?"),
ImageContent.from(encodeInBase64(imageResourceScientist), "image/jpg")
);
ChatResponse chatResponse = chatModel.chat(ChatRequest.builder().messages(userMessage).build());
return chatResponse.aiMessage().text();
}
private String encodeInBase64(Resource resource) throws IOException {
return Base64.getEncoder().encodeToString(resource.getContentAsByteArray());
}
}
Thank you for pointing this out, I will add more examples to the documentation!
Hi @michaelisvy, sorry for pinging you here - I couldn’t find another way to reach out. I hope you don’t mind.
I came across https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=20 and wanted to get some feedback, especially on the "Support for Entities" category. Could you please share what problems you encountered there?
Thank you!