-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Ollama Examples, Structured Outputs, ChatRequestAPI, Tools (#135
- Loading branch information
LangChain4j
committed
Dec 22, 2024
1 parent
c6f16c0
commit c9379e7
Showing
6 changed files
with
202 additions
and
102 deletions.
There are no files selected for viewing
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
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
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
31 changes: 31 additions & 0 deletions
31
ollama-examples/src/main/java/utils/AbstractOllamaInfrastructure.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils; | ||
|
||
import static dev.langchain4j.internal.Utils.isNullOrEmpty; | ||
import static utils.OllamaImage.LLAMA_3_1; | ||
import static utils.OllamaImage.localOllamaImage; | ||
|
||
public class AbstractOllamaInfrastructure { | ||
|
||
public static final String OLLAMA_BASE_URL = System.getenv("OLLAMA_BASE_URL"); | ||
public static final String MODEL_NAME = LLAMA_3_1; | ||
|
||
public static LangChain4jOllamaContainer ollama; | ||
|
||
static { | ||
if (isNullOrEmpty(OLLAMA_BASE_URL)) { | ||
String localOllamaImage = localOllamaImage(MODEL_NAME); | ||
ollama = new LangChain4jOllamaContainer(OllamaImage.resolve(OllamaImage.OLLAMA_IMAGE, localOllamaImage)) | ||
.withModel(MODEL_NAME); | ||
ollama.start(); | ||
ollama.commitToImage(localOllamaImage); | ||
} | ||
} | ||
|
||
public static String ollamaBaseUrl(LangChain4jOllamaContainer ollama) { | ||
if (isNullOrEmpty(OLLAMA_BASE_URL)) { | ||
return ollama.getEndpoint(); | ||
} else { | ||
return OLLAMA_BASE_URL; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ollama-examples/src/main/java/utils/LangChain4jOllamaContainer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package utils; | ||
|
||
import com.github.dockerjava.api.command.InspectContainerResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.ollama.OllamaContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.io.IOException; | ||
|
||
public class LangChain4jOllamaContainer extends OllamaContainer { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LangChain4jOllamaContainer.class); | ||
|
||
private String model; | ||
|
||
public LangChain4jOllamaContainer(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
} | ||
|
||
public LangChain4jOllamaContainer withModel(String model) { | ||
this.model = model; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void containerIsStarted(InspectContainerResponse containerInfo) { | ||
if (this.model != null) { | ||
try { | ||
log.info("Start pulling the '{}' model ... would take several minutes ...", this.model); | ||
ExecResult r = execInContainer("ollama", "pull", this.model); | ||
log.info("Model pulling competed! {}", r); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException("Error pulling model", e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.