Skip to content

Instantly share code, notes, and snippets.

@ThomasVitale
Last active October 31, 2024 10:37
Show Gist options
  • Save ThomasVitale/6a4e468636a5a15ff8fbe5966a63761a to your computer and use it in GitHub Desktop.
Save ThomasVitale/6a4e468636a5a15ff8fbe5966a63761a to your computer and use it in GitHub Desktop.
Java 23 + Spring AI Ollama + JBang
//usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 23
//PREVIEW
//REPOS mavencentral,https://repo.spring.io/milestone
//DEPS org.springframework.boot:spring-boot-starter:3.3.5
//DEPS org.springframework.ai:spring-ai-ollama-spring-boot-starter:1.0.0-M3
//JAVA_OPTIONS -Dspring.main.web-application-type=none
//JAVA_OPTIONS -Dspring.ai.ollama.init.pull-model-strategy=always
//JAVA_OPTIONS -Dspring.ai.ollama.chat.options.model=llama3.2:1b
package com.thomasvitale.ai;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner chatClient(ChatClient.Builder chatClientBuilder) {
return _ -> {
var response = chatClientBuilder.build()
.prompt("What is the capital of Denmark?")
.call()
.content();
System.out.println(response);
};
}
}
@ThomasVitale
Copy link
Author

Pre-requisites: Ollama is up and running.

Run with JBang:

jbang Application.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment