forked from Mirror/ollama4j
added Prompt Builder
This commit is contained in:
parent
7470ebe846
commit
e2b29b6a07
8
Makefile
8
Makefile
@ -16,4 +16,10 @@ build-docs:
|
|||||||
npm i --prefix docs && npm run build --prefix docs
|
npm i --prefix docs && npm run build --prefix docs
|
||||||
|
|
||||||
start-docs:
|
start-docs:
|
||||||
npm i --prefix docs && npm run start --prefix docs
|
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
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
sidebar_position: 5
|
sidebar_position: 6
|
||||||
---
|
---
|
||||||
|
|
||||||
# Generate Embeddings
|
# Generate Embeddings
|
||||||
|
73
docs/docs/apis-ask/prompt-builder.md
Normal file
73
docs/docs/apis-ask/prompt-builder.md
Normal file
@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
@ -21,6 +21,7 @@ public class OllamaModelType {
|
|||||||
public static final String VICUNA = "vicuna";
|
public static final String VICUNA = "vicuna";
|
||||||
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
||||||
public static final String PHIND_CODELLAMA = "phind-codellama";
|
public static final String PHIND_CODELLAMA = "phind-codellama";
|
||||||
|
public static final String PHI = "phi";
|
||||||
public static final String ZEPHYR = "zephyr";
|
public static final String ZEPHYR = "zephyr";
|
||||||
public static final String WIZARDCODER = "wizardcoder";
|
public static final String WIZARDCODER = "wizardcoder";
|
||||||
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
||||||
|
@ -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.
|
||||||
|
*
|
||||||
|
* <p>Example usage:
|
||||||
|
*
|
||||||
|
* <pre>{@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);
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user