Created
April 2, 2024 08:45
-
-
Save glaforge/48d2b1217d4af1e8d650e8cffadcf5bd to your computer and use it in GitHub Desktop.
Conditionally create the derived container if it hasn't been already created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gemini.workshop; | |
import dev.langchain4j.model.chat.ChatLanguageModel; | |
import dev.langchain4j.model.ollama.OllamaChatModel; | |
import org.testcontainers.ollama.OllamaContainer; | |
import org.testcontainers.utility.DockerImageName; | |
import java.io.IOException; | |
public class CallGemma { | |
private static final String TC_OLLAMA_GEMMA_2_B = "tc-ollama-gemma-2b"; | |
private static class CreateGemmaOllamaContainer { | |
public static void main(String[] args) throws IOException, InterruptedException { | |
// Creating an Ollama container with Gemma 2B, only once | |
try (OllamaContainer ollama = new OllamaContainer("ollama/ollama:0.1.26")) { | |
ollama.start(); | |
ollama.execInContainer("ollama", "pull", "gemma:2b"); | |
ollama.commitToImage(TC_OLLAMA_GEMMA_2_B); | |
} | |
} | |
} | |
public static void main(String[] args) throws IOException, InterruptedException { | |
try ( | |
// Image substitution to use our new Gemma-based Ollama image instead of the default Ollama image | |
OllamaContainer ollama = new OllamaContainer( | |
DockerImageName.parse(TC_OLLAMA_GEMMA_2_B).asCompatibleSubstituteFor("ollama/ollama"))) { | |
ollama.start(); | |
ChatLanguageModel model = OllamaChatModel.builder() | |
.baseUrl(String.format("http://%s:%d", ollama.getHost(), ollama.getFirstMappedPort())) | |
.modelName("gemma:2b") | |
.build(); | |
String response = model.generate("Why is the sky blue?"); | |
System.out.println(response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment