Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feat add generate builder #127

Merged
merged 6 commits into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -171,6 +171,50 @@ Then you can use the Model classes to run models:
}
```

Or you can use a **Flow API**:

```java
public void sample() throws IOException {
String model = "tjake/Llama-3.2-1B-Instruct-JQ4";
String workingDirectory = "./models";

String prompt = "What is the best season to plant avocados?";

// Downloads the model or just returns the local path if it's already downloaded
File localModelPath = new Downloader(workingDirectory, model).huggingFaceModel();

// Loads the quantized model and specified use of quantized memory
AbstractModel m = ModelSupport.loadModel(localModelPath, DType.F32, DType.I8);

PromptContext ctx;
// Checks if the model supports chat prompting and adds prompt in the expected format for this model
if (m.promptSupport().isPresent()) {
ctx = m.promptSupport()
.get()
.builder()
.addSystemMessage("You are a helpful chatbot who writes short responses.")
.addUserMessage(prompt)
.build();
} else {
ctx = PromptContext.of(prompt);
}

System.out.println("Prompt: " + ctx.getPrompt() + "\n");
// Generates a response to the prompt and prints it
// The api allows for streaming or non-streaming responses
// The response is generated with a temperature of 0.7 and a max token length of 256
Generator.Response r = m.generateBuilder()
.session(UUID.randomUUID()) //By default, UUID.randomUUID()
.promptContext(ctx) // Required or use prompt(String text)
.ntokens(256) //By default, 256
.temperature(0.0f) //By default, 0.0f
.onTokenWithTimings((s, aFloat) -> {}) //By default, (s, aFloat) -> {}, nothing
.generate();

System.out.println(r.responseText);
}
```

## ⭐ Give us a Star!

If you like or are using this project to build your own, please give us a star. It's a free way to show your support.
Original file line number Diff line number Diff line change
@@ -149,6 +149,10 @@ default Response generate(
return generate(session, promptContext, temperature, ntokens, (s, aFloat) -> {});
}

default GenerateBuilder generateBuilder() {
return new GenerateBuilder(this);
}

enum PoolingType {
MODEL, // Use the model's pooling layers
AVG,
@@ -179,4 +183,67 @@ default Map<String, Float> classify(String input, PoolingType poolingType) {
Tokenizer getTokenizer();

Optional<PromptSupport> promptSupport();

class GenerateBuilder {
private UUID session = UUID.randomUUID();
private PromptContext promptContext;
private float temperature = 0.0f;
private int ntokens = 256;
private BiConsumer<String, Float> onTokenWithTimings = (s, aFloat) -> {};
private final Generator generator;

public GenerateBuilder(Generator generator) {
this.generator = generator;
}

public GenerateBuilder session(UUID session) {
this.session = session;

return this;
}

public GenerateBuilder session(String session) {
this.session = UUID.fromString(session);

return this;
}

public GenerateBuilder promptContext(PromptContext promptContext) {
this.promptContext = promptContext;

return this;
}

public GenerateBuilder prompt(String prompt) {
this.promptContext = PromptContext.of(prompt);

return this;
}

public GenerateBuilder temperature(float temperature) {
this.temperature = temperature;

return this;
}

public GenerateBuilder ntokens(int ntokens) {
this.ntokens = ntokens;

return this;
}

public GenerateBuilder onTokenWithTimings(BiConsumer<String, Float> onTokenWithTimings) {
this.onTokenWithTimings = onTokenWithTimings;

return this;
}

public Response generate() {
if (promptContext == null) {
throw new IllegalArgumentException("promptContext cannot be null");
}

return generator.generate(session, promptContext, temperature, ntokens, onTokenWithTimings);
}
}
}