Skip to content

Commit 8e7ca13

Browse files
Add detailed Javadoc comments to Llama classes for improved documentation
1 parent 6ce4b09 commit 8e7ca13

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ public <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Contex
220220
}
221221
}
222222

223+
/**
224+
* Declare a field that is an array of objects or null. Used to avoid calling the consumer when used with
225+
* {@link #optionalConstructorArg()} or {@link #constructorArg()}.
226+
* @param consumer Consumer that will be passed as is to the {@link #declareField(BiConsumer, ContextParser, ParseField, ValueType)}.
227+
* @param objectParser Parser that will parse the objects in the array, checking for nulls.
228+
* @param field Field to declare.
229+
*/
223230
@Override
224231
public <T> void declareObjectArrayOrNull(
225232
BiConsumer<Value, List<T>> consumer,

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/llama/action/LlamaActionCreator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232
import static org.elasticsearch.core.Strings.format;
3333
import static org.elasticsearch.xpack.inference.common.Truncator.truncate;
3434

35+
/**
36+
* Creates actions for Llama inference requests, handling both embeddings and completions.
37+
* This class implements the {@link LlamaActionVisitor} interface to provide specific action creation methods.
38+
*/
3539
public class LlamaActionCreator implements LlamaActionVisitor {
3640

3741
private static final String FAILED_TO_SEND_REQUEST_ERROR_MESSAGE = "Failed to send Llama %s request from inference entity id [%s]";
38-
public static final String COMPLETION_ERROR_PREFIX = "Llama completions";
42+
private static final String COMPLETION_ERROR_PREFIX = "Llama completions";
3943
private static final String USER_ROLE = "user";
4044

4145
private static final ResponseHandler EMBEDDINGS_HANDLER = new LlamaEmbeddingsResponseHandler(
@@ -50,6 +54,12 @@ public class LlamaActionCreator implements LlamaActionVisitor {
5054
private final Sender sender;
5155
private final ServiceComponents serviceComponents;
5256

57+
/**
58+
* Constructs a new LlamaActionCreator with the specified sender and service components.
59+
*
60+
* @param sender the sender to use for executing actions
61+
* @param serviceComponents the service components providing necessary services
62+
*/
5363
public LlamaActionCreator(Sender sender, ServiceComponents serviceComponents) {
5464
this.sender = Objects.requireNonNull(sender);
5565
this.serviceComponents = Objects.requireNonNull(serviceComponents);
@@ -87,6 +97,13 @@ public ExecutableAction create(LlamaChatCompletionModel model) {
8797
return new SingleInputSenderExecutableAction(sender, manager, errorMessage, COMPLETION_ERROR_PREFIX);
8898
}
8999

100+
/**
101+
* Builds an error message for failed requests.
102+
*
103+
* @param requestType the type of request that failed
104+
* @param inferenceId the inference entity ID associated with the request
105+
* @return a formatted error message
106+
*/
90107
public static String buildErrorMessage(TaskType requestType, String inferenceId) {
91108
return format(FAILED_TO_SEND_REQUEST_ERROR_MESSAGE, requestType.toString(), inferenceId);
92109
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/llama/action/LlamaActionVisitor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@
1111
import org.elasticsearch.xpack.inference.services.llama.completion.LlamaChatCompletionModel;
1212
import org.elasticsearch.xpack.inference.services.llama.embeddings.LlamaEmbeddingsModel;
1313

14+
/**
15+
* Visitor interface for creating executable actions for Llama inference models.
16+
* This interface defines methods to create actions for both embeddings and chat completion models.
17+
*/
1418
public interface LlamaActionVisitor {
19+
/**
20+
* Creates an executable action for the given Llama embeddings model.
21+
*
22+
* @param model the Llama embeddings model
23+
* @return an executable action for the embeddings model
24+
*/
1525
ExecutableAction create(LlamaEmbeddingsModel model);
1626

27+
/**
28+
* Creates an executable action for the given Llama chat completion model.
29+
*
30+
* @param model the Llama chat completion model
31+
* @return an executable action for the chat completion model
32+
*/
1733
ExecutableAction create(LlamaChatCompletionModel model);
1834
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/llama/completion/LlamaChatCompletionModel.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@
2020

2121
import java.util.Map;
2222

23+
/**
24+
* Represents a Llama chat completion model for inference.
25+
* This class extends the LlamaModel and provides specific configurations and settings for chat completion tasks.
26+
*/
2327
public class LlamaChatCompletionModel extends LlamaModel {
2428

29+
/**
30+
* Constructor for creating a LlamaChatCompletionModel with specified parameters.
31+
* @param inferenceEntityId the unique identifier for the inference entity
32+
* @param taskType the type of task this model is designed for
33+
* @param service the name of the inference service
34+
* @param serviceSettings the settings for the inference service, specific to chat completion
35+
* @param secrets the secret settings for the model, such as API keys or tokens
36+
* @param context the context for parsing configuration settings
37+
*/
2538
public LlamaChatCompletionModel(
2639
String inferenceEntityId,
2740
TaskType taskType,
@@ -39,6 +52,14 @@ public LlamaChatCompletionModel(
3952
);
4053
}
4154

55+
/**
56+
* Constructor for creating a LlamaChatCompletionModel with specified parameters.
57+
* @param inferenceEntityId the unique identifier for the inference entity
58+
* @param taskType the type of task this model is designed for
59+
* @param service the name of the inference service
60+
* @param serviceSettings the settings for the inference service, specific to chat completion
61+
* @param secrets the secret settings for the model, such as API keys or tokens
62+
*/
4263
public LlamaChatCompletionModel(
4364
String inferenceEntityId,
4465
TaskType taskType,
@@ -53,6 +74,14 @@ public LlamaChatCompletionModel(
5374
setPropertiesFromServiceSettings(serviceSettings);
5475
}
5576

77+
/**
78+
* Factory method to create a LlamaChatCompletionModel with overridden model settings based on the request.
79+
* If the request does not specify a model, the original model is returned.
80+
*
81+
* @param model the original LlamaChatCompletionModel
82+
* @param request the UnifiedCompletionRequest containing potential overrides
83+
* @return a new LlamaChatCompletionModel with overridden settings or the original model if no overrides are specified
84+
*/
5685
public static LlamaChatCompletionModel of(LlamaChatCompletionModel model, UnifiedCompletionRequest request) {
5786
if (request.model() == null) {
5887
// If no model id is specified in the request, return the original model
@@ -81,11 +110,22 @@ private void setPropertiesFromServiceSettings(LlamaChatCompletionServiceSettings
81110
this.rateLimitSettings = serviceSettings.rateLimitSettings();
82111
}
83112

113+
/**
114+
* Returns the service settings specific to Llama chat completion.
115+
*
116+
* @return the LlamaChatCompletionServiceSettings associated with this model
117+
*/
84118
@Override
85119
public LlamaChatCompletionServiceSettings getServiceSettings() {
86120
return (LlamaChatCompletionServiceSettings) super.getServiceSettings();
87121
}
88122

123+
/**
124+
* Accepts a visitor that creates an executable action for this Llama chat completion model.
125+
*
126+
* @param creator the visitor that creates the executable action
127+
* @return an ExecutableAction representing this model
128+
*/
89129
public ExecutableAction accept(LlamaActionVisitor creator) {
90130
return creator.create(this);
91131
}

0 commit comments

Comments
 (0)