47
47
* Java Client for the Ollama API. <a href="https://ollama.ai/">https://ollama.ai</a>
48
48
*
49
49
* @author Christian Tzolov
50
+ * @author Thomas Vitale
50
51
* @since 0.8.0
51
52
*/
52
53
// @formatter:off
@@ -454,15 +455,20 @@ public Message build() {
454
455
/**
455
456
* Chat request object.
456
457
*
457
- * @param model The model to use for completion.
458
- * @param messages The list of messages to chat with.
459
- * @param stream Whether to stream the response.
460
- * @param format The format to return the response in. Currently, the only accepted
461
- * value is "json".
462
- * @param keepAlive The duration to keep the model loaded in ollama while idle.
463
- * @param options Additional model parameters. You can use the {@link OllamaOptions} builder
464
- * to create the options then {@link OllamaOptions#toMap()} to convert the options into a
465
- * map.
458
+ * @param model The model to use for completion. It should be a name familiar to Ollama from the <a href="https://ollama.com/library">Library</a>.
459
+ * @param messages The list of messages in the chat. This can be used to keep a chat memory.
460
+ * @param stream Whether to stream the response. If false, the response will be returned as a single response object rather than a stream of objects.
461
+ * @param format The format to return the response in. Currently, the only accepted value is "json".
462
+ * @param keepAlive Controls how long the model will stay loaded into memory following this request (default: 5m).
463
+ * @param tools List of tools the model has access to.
464
+ * @param options Model-specific options. For example, "temperature" can be set through this field, if the model supports it.
465
+ * You can use the {@link OllamaOptions} builder to create the options then {@link OllamaOptions#toMap()} to convert the options into a map.
466
+ *
467
+ * @see <a href=
468
+ * "https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion">Chat
469
+ * Completion API</a>
470
+ * @see <a href="https://github.com/ollama/ollama/blob/main/api/types.go">Ollama
471
+ * Types</a>
466
472
*/
467
473
@ JsonInclude (Include .NON_NULL )
468
474
public record ChatRequest (
@@ -471,9 +477,9 @@ public record ChatRequest(
471
477
@ JsonProperty ("stream" ) Boolean stream ,
472
478
@ JsonProperty ("format" ) String format ,
473
479
@ JsonProperty ("keep_alive" ) String keepAlive ,
474
- @ JsonProperty ("options " ) Map < String , Object > options ,
475
- @ JsonProperty ("tools " ) List < Tool > tools ) {
476
-
480
+ @ JsonProperty ("tools " ) List < Tool > tools ,
481
+ @ JsonProperty ("options " ) Map < String , Object > options
482
+ ) {
477
483
478
484
/**
479
485
* Represents a tool the model may call. Currently, only functions are supported as a tool.
@@ -544,8 +550,8 @@ public static class Builder {
544
550
private boolean stream = false ;
545
551
private String format ;
546
552
private String keepAlive ;
547
- private Map <String , Object > options = Map .of ();
548
553
private List <Tool > tools = List .of ();
554
+ private Map <String , Object > options = Map .of ();
549
555
550
556
public Builder (String model ) {
551
557
Assert .notNull (model , "The model can not be null." );
@@ -572,6 +578,11 @@ public Builder withKeepAlive(String keepAlive) {
572
578
return this ;
573
579
}
574
580
581
+ public Builder withTools (List <Tool > tools ) {
582
+ this .tools = tools ;
583
+ return this ;
584
+ }
585
+
575
586
public Builder withOptions (Map <String , Object > options ) {
576
587
Objects .requireNonNull (options , "The options can not be null." );
577
588
@@ -585,33 +596,30 @@ public Builder withOptions(OllamaOptions options) {
585
596
return this ;
586
597
}
587
598
588
- public Builder withTools (List <Tool > tools ) {
589
- this .tools = tools ;
590
- return this ;
591
- }
592
-
593
599
public ChatRequest build () {
594
- return new ChatRequest (model , messages , stream , format , keepAlive , options , tools );
600
+ return new ChatRequest (model , messages , stream , format , keepAlive , tools , options );
595
601
}
596
602
}
597
603
}
598
604
599
605
/**
600
606
* Ollama chat response object.
601
607
*
602
- * @param model The model name used for completion .
603
- * @param createdAt When the request was made .
608
+ * @param model The model used for generating the response .
609
+ * @param createdAt The timestamp of the response generation .
604
610
* @param message The response {@link Message} with {@link Message.Role#ASSISTANT}.
611
+ * @param doneReason The reason the model stopped generating text.
605
612
* @param done Whether this is the final response. For streaming response only the
606
613
* last message is marked as done. If true, this response may be followed by another
607
614
* response with the following, additional fields: context, prompt_eval_count,
608
615
* prompt_eval_duration, eval_count, eval_duration.
609
616
* @param totalDuration Time spent generating the response.
610
617
* @param loadDuration Time spent loading the model.
611
- * @param promptEvalCount number of tokens in the prompt.(*)
612
- * @param promptEvalDuration time spent evaluating the prompt.
613
- * @param evalCount number of tokens in the response.
614
- * @param evalDuration time spent generating the response.
618
+ * @param promptEvalCount Number of tokens in the prompt.
619
+ * @param promptEvalDuration Time spent evaluating the prompt.
620
+ * @param evalCount Number of tokens in the response.
621
+ * @param evalDuration Time spent generating the response.
622
+ *
615
623
* @see <a href=
616
624
* "https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion">Chat
617
625
* Completion API</a>
@@ -623,13 +631,15 @@ public record ChatResponse(
623
631
@ JsonProperty ("model" ) String model ,
624
632
@ JsonProperty ("created_at" ) Instant createdAt ,
625
633
@ JsonProperty ("message" ) Message message ,
634
+ @ JsonProperty ("done_reason" ) String doneReason ,
626
635
@ JsonProperty ("done" ) Boolean done ,
627
636
@ JsonProperty ("total_duration" ) Duration totalDuration ,
628
637
@ JsonProperty ("load_duration" ) Duration loadDuration ,
629
638
@ JsonProperty ("prompt_eval_count" ) Integer promptEvalCount ,
630
639
@ JsonProperty ("prompt_eval_duration" ) Duration promptEvalDuration ,
631
640
@ JsonProperty ("eval_count" ) Integer evalCount ,
632
- @ JsonProperty ("eval_duration" ) Duration evalDuration ) {
641
+ @ JsonProperty ("eval_duration" ) Duration evalDuration
642
+ ) {
633
643
}
634
644
635
645
/**
0 commit comments