diff --git a/Makefile b/Makefile index 694d980..4d89692 100644 --- a/Makefile +++ b/Makefile @@ -16,4 +16,10 @@ build-docs: npm i --prefix docs && npm run build --prefix docs start-docs: - npm i --prefix docs && npm run start --prefix docs \ No newline at end of file + npm i --prefix docs && npm run start --prefix docs + +start-cpu: + docker run -it -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama + +start-gpu: + docker run -it --gpus=all -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama \ No newline at end of file diff --git a/docs/docs/apis-ask/generate-embeddings.md b/docs/docs/apis-ask/generate-embeddings.md index b163ef6..1da45e1 100644 --- a/docs/docs/apis-ask/generate-embeddings.md +++ b/docs/docs/apis-ask/generate-embeddings.md @@ -1,5 +1,5 @@ --- -sidebar_position: 5 +sidebar_position: 6 --- # Generate Embeddings diff --git a/docs/docs/apis-ask/prompt-builder.md b/docs/docs/apis-ask/prompt-builder.md new file mode 100644 index 0000000..d23fc15 --- /dev/null +++ b/docs/docs/apis-ask/prompt-builder.md @@ -0,0 +1,73 @@ +--- +sidebar_position: 5 +--- + +# Prompt Builder + +This is designed for prompt engineering. It allows you to easily build the prompt text for zero-shot, one-shot, few-shot +inferences. + +```java + +import io.github.amithkoujalgi.ollama4j.core.OllamaAPI; +import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult; +import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType; +import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder; + +public class AskPhi { + public static void main(String[] args) throws Exception { + + String host = "http://localhost:11434/"; + OllamaAPI ollamaAPI = new OllamaAPI(host); + ollamaAPI.setRequestTimeoutSeconds(10); + + String model = OllamaModelType.PHI; + + PromptBuilder promptBuilder = + new PromptBuilder() + .addLine("You are an expert coder and understand different programming languages.") + .addLine("Given a question, answer ONLY with code.") + .addLine("Produce clean, formatted and indented code in markdown format.") + .addLine( + "DO NOT include ANY extra text apart from code. Follow this instruction very strictly!") + .addLine("If there's any additional information you want to add, use comments within code.") + .addLine("Answer only in the programming language that has been asked for.") + .addSeparator() + .addLine("Example: Sum 2 numbers in Python") + .addLine("Answer:") + .addLine("```python") + .addLine("def sum(num1: int, num2: int) -> int:") + .addLine(" return num1 + num2") + .addLine("```") + .addSeparator() + .add("How do I read a file in Go and print its contents to stdout?"); + + OllamaResult response = ollamaAPI.ask(model, promptBuilder.build()); + System.out.println(response.getResponse()); + } +} +``` + +You will get a response similar to: + +```go +package main + +import ( + "fmt" + "io/ioutil" +) + +func readFile(fileName string) { + file, err := ioutil.ReadFile(fileName) + if err != nil { + fmt.Fprintln(os.Stderr, "Error reading file:", err.Error()) + return + } + + f, _ := ioutil.ReadFile("file.txt") + if f != nil { + fmt.Println(f.String()) + } +} +``` \ No newline at end of file diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java index 31ad412..96bcc43 100644 --- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java @@ -21,6 +21,7 @@ public class OllamaModelType { public static final String VICUNA = "vicuna"; public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored"; public static final String PHIND_CODELLAMA = "phind-codellama"; + public static final String PHI = "phi"; public static final String ZEPHYR = "zephyr"; public static final String WIZARDCODER = "wizardcoder"; public static final String MISTRAL_OPENORCA = "mistral-openorca"; diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java new file mode 100644 index 0000000..be487d9 --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java @@ -0,0 +1,69 @@ +package io.github.amithkoujalgi.ollama4j.core.utils; + +/** + * The {@code PromptBuilder} class is used to construct prompt texts for language models (LLMs). It + * provides methods for adding text, adding lines, adding separators, and building the final prompt. + * + *
Example usage: + * + *
{@code + * PromptBuilder promptBuilder = new PromptBuilder(); + * promptBuilder.add("This is a sample prompt for language models.") + * .addLine("You can add lines to provide context.") + * .addSeparator() + * .add("Feel free to customize as needed."); + * String finalPrompt = promptBuilder.build(); + * System.out.println(finalPrompt); + * }+ */ +public class PromptBuilder { + + private final StringBuilder prompt; + + /** Constructs a new {@code PromptBuilder} with an empty prompt. */ + public PromptBuilder() { + this.prompt = new StringBuilder(); + } + + /** + * Appends the specified text to the prompt. + * + * @param text the text to be added to the prompt + * @return a reference to this {@code PromptBuilder} instance for method chaining + */ + public PromptBuilder add(String text) { + prompt.append(text); + return this; + } + + /** + * Appends the specified text followed by a newline character to the prompt. + * + * @param text the text to be added as a line to the prompt + * @return a reference to this {@code PromptBuilder} instance for method chaining + */ + public PromptBuilder addLine(String text) { + prompt.append(text).append("\n"); + return this; + } + + /** + * Appends a separator line to the prompt. The separator is a newline followed by a line of + * dashes. + * + * @return a reference to this {@code PromptBuilder} instance for method chaining + */ + public PromptBuilder addSeparator() { + prompt.append("\n--------------------------------------------------\n"); + return this; + } + + /** + * Builds and returns the final prompt as a string. + * + * @return the final prompt as a string + */ + public String build() { + return prompt.toString(); + } +}