Skip to content

Commit

Permalink
address code review/walkthrough comments
Browse files Browse the repository at this point in the history
Signed-off-by: HenryL27 <[email protected]>
  • Loading branch information
HenryL27 committed Aug 28, 2023
1 parent 58b6bcf commit bd0ff99
Show file tree
Hide file tree
Showing 42 changed files with 423 additions and 1,063 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,27 @@ public class ActionConstants {
public final static String NEXT_TOKEN_FIELD = "next_token";
/** name of input field in all requests */
public final static String INPUT_FIELD = "input";
/** name of prompt field in all requests */
public final static String PROMPT_FIELD = "prompt";
/** name of AI response field in all respopnses */
public final static String AI_RESPONSE_FIELD = "response";
/** name of agent field in all requests */
public final static String AI_AGENT_FIELD = "agent";
/** name of interaction attributes field in all requests */
public final static String INTER_ATTRIBUTES_FIELD = "attributes";
/** name of origin field in all requests */
public final static String RESPONSE_ORIGIN_FIELD = "origin";
/** name of success field in all requests */
public final static String SUCCESS_FIELD = "success";

/** path for create conversation */
public final static String CREATE_CONVERSATION_PATH = "/_plugins/_ml/conversational/memory";
public final static String CREATE_CONVERSATION_REST_PATH = "/_plugins/_ml/memory/conversation";
/** path for list conversations */
public final static String LIST_CONVERSATIONS_PATH = "/_plugins/_ml/conversational/memory";
public final static String GET_CONVERSATIONS_REST_PATH = "/_plugins/_ml/memory/conversation";
/** path for put interaction */
public final static String CREATE_INTERACTION_PATH = "/_plugins/_ml/conversational/memory/{conversation_id}";
public final static String CREATE_INTERACTION_REST_PATH = "/_plugins/_ml/memory/conversation/{conversation_id}";
/** path for get interactions */
public final static String GET_INTERACTIONS_PATH = "/_plugins/_ml/conversational/memory/{conversation_id}";
public final static String GET_INTERACTIONS_REST_PATH = "/_plugins/_ml/memory/conversation/{conversation_id}";
/** path for delete conversation */
public final static String DELETE_CONVERSATION_PATH = "/_plugins/_ml/conversational/memory/{conversation_id}";
public final static String DELETE_CONVERSATION_REST_PATH = "/_plugins/_ml/memory/conversation/{conversation_id}";

/** default max results returned by get operations */
public final static int DEFAULT_MAX_RESULTS = 10;

/** default username for reporting security errors if no or malformed username */
public final static String DEFAULT_USERNAME_FOR_ERRORS = "BAD_USER";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;

import org.opensearch.action.index.IndexRequest;
import org.opensearch.core.common.io.stream.StreamInput;
Expand All @@ -36,17 +37,13 @@
* Class for holding conversational metadata
*/
@AllArgsConstructor
public final class ConversationMeta implements Writeable, ToXContentObject {
public class ConversationMeta implements Writeable, ToXContentObject {

@Getter
private String id;
@Getter
private Instant createdTime;
@Getter
private Instant lastHitTime;
@Getter
private int numInteractions;
@Getter
private String name;
@Getter
private String user;
Expand All @@ -69,11 +66,9 @@ public static ConversationMeta fromSearchHit(SearchHit hit) {
*/
public static ConversationMeta fromMap(String id, Map<String, Object> docFields) {
Instant created = Instant.parse((String) docFields.get(ConversationalIndexConstants.META_CREATED_FIELD));
Instant lastHit = Instant.parse((String) docFields.get(ConversationalIndexConstants.META_ENDED_FIELD));
int numInteractions = (int) docFields.get(ConversationalIndexConstants.META_LENGTH_FIELD);
String name = (String) docFields.get(ConversationalIndexConstants.META_NAME_FIELD);
String user = (String) docFields.get(ConversationalIndexConstants.USER_FIELD);
return new ConversationMeta(id, created, lastHit, numInteractions, name, user);
return new ConversationMeta(id, created, name, user);
}

/**
Expand All @@ -86,34 +81,19 @@ public static ConversationMeta fromMap(String id, Map<String, Object> docFields)
public static ConversationMeta fromStream(StreamInput in) throws IOException {
String id = in.readString();
Instant created = in.readInstant();
Instant lastHit = in.readInstant();
int numInteractions = in.readInt();
String name = in.readString();
String user = in.readOptionalString();
return new ConversationMeta(id, created, lastHit, numInteractions, name, user);
return new ConversationMeta(id, created, name, user);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeInstant(createdTime);
out.writeInstant(lastHitTime);
out.writeInt(numInteractions);
out.writeString(name);
out.writeOptionalString(user);
}

/**
* Hit this conversationMeta at this time, increasing the converation length
* @param hitTime the Instant when the new interaction was created
* @return this conversationMeta object (fields updated)
*/
public ConversationMeta hit(Instant hitTime) {
this.lastHitTime = hitTime;
this.numInteractions++;
return this;
}


/**
* Convert this conversationMeta object into an IndexRequest so it can be indexed
Expand All @@ -124,8 +104,6 @@ public IndexRequest toIndexRequest(String index) {
IndexRequest request = new IndexRequest(index);
return request.id(this.id).source(
ConversationalIndexConstants.META_CREATED_FIELD, this.createdTime,
ConversationalIndexConstants.META_ENDED_FIELD, this.lastHitTime,
ConversationalIndexConstants.META_LENGTH_FIELD, this.numInteractions,
ConversationalIndexConstants.META_NAME_FIELD, this.name
);
}
Expand All @@ -134,9 +112,7 @@ public IndexRequest toIndexRequest(String index) {
public String toString() {
return "{id=" + id
+ ", name=" + name
+ ", length=" + numInteractions
+ ", created=" + createdTime.toString()
+ ", lastHit=" + lastHitTime.toString()
+ ", user=" + user
+ "}";
}
Expand All @@ -146,8 +122,6 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContentObject.Para
builder.startObject();
builder.field(ActionConstants.CONVERSATION_ID_FIELD, this.id);
builder.field(ConversationalIndexConstants.META_CREATED_FIELD, this.createdTime);
builder.field(ConversationalIndexConstants.META_ENDED_FIELD, this.lastHitTime);
builder.field(ConversationalIndexConstants.META_LENGTH_FIELD, this.numInteractions);
builder.field(ConversationalIndexConstants.META_NAME_FIELD, this.name);
if(this.user != null) {
builder.field(ConversationalIndexConstants.USER_FIELD, this.user);
Expand All @@ -161,18 +135,11 @@ public boolean equals(Object other) {
if(!(other instanceof ConversationMeta)) {
return false;
}
ConversationMeta otherconversation = (ConversationMeta) other;
if(! otherconversation.id.equals(this.id)) {
return false;
} if(! (otherconversation.user == this.user || otherconversation.user.equals(this.user))) {
return false;
} if(! otherconversation.createdTime.equals(this.createdTime)) {
return false;
} if(! otherconversation.lastHitTime.equals(this.lastHitTime)) {
return false;
} if(otherconversation.numInteractions != this.numInteractions) {
return false;
} return otherconversation.name == this.name || otherconversation.name.equals(this.name);
ConversationMeta otherConversation = (ConversationMeta) other;
return Objects.equals(this.id, otherConversation.id) &&
Objects.equals(this.user, otherConversation.user) &&
Objects.equals(this.createdTime, otherConversation.createdTime) &&
Objects.equals(this.name, otherConversation.name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
* Class containing a bunch of constant defining how the conversational indices are formatted
*/
public class ConversationalIndexConstants {
/** Version of the meta index schema */
public final static Integer META_INDEX_SCHEMA_VERSION = 1;
/** Name of the conversational metadata index */
public final static String META_INDEX_NAME = ".conversational-meta";
/** Name of the metadata field for initial timestamp */
public final static String META_CREATED_FIELD = "create_time";
/** Name of the metadata field for most recent timestamp */
public final static String META_ENDED_FIELD = "last_interaction_time";
/** Name of the metadata field for number of interactions */
public final static String META_LENGTH_FIELD = "num_interactions";
/** Name of the metadata field for name of the conversation */
public final static String META_NAME_FIELD = "name";
/** Name of the owning user field in all indices */
public final static String USER_FIELD = "user";
/** Mappings for the conversational metadata index */
public final static String META_MAPPING = "{\n"
+ " \"_meta\": {\n"
+ " \"schema_version\": " + META_INDEX_SCHEMA_VERSION + "\n"
+ " },\n"
+ " \"properties\": {\n"
+ " \""
+ META_NAME_FIELD
Expand All @@ -43,35 +44,30 @@ public class ConversationalIndexConstants {
+ META_CREATED_FIELD
+ "\": {\"type\": \"date\", \"format\": \"strict_date_optional_time||epoch_millis\"},\n"
+ " \""
+ META_ENDED_FIELD
+ "\": {\"type\": \"date\", \"format\": \"strict_date_optional_time||epoch_millis\"},\n"
+ " \""
+ META_LENGTH_FIELD
+ "\": {\"type\": \"integer\"},\n"
+ " \""
+ USER_FIELD
+ "\": {\"type\": \"keyword\"}\n"
+ " }\n"
+ "}";

/** Name of the conversational interactions index */
public final static String INTERACTIONS_INDEX_NAME = ".conversational-interactions";
/** Name of the interaction field for the conversation Id */
public final static String INTERACTIONS_CONVERSATION_ID_FIELD = "conversation_id";
/** Name of the interaction field for the human input */
public final static String INTERACTIONS_INPUT_FIELD = "input";
/** Name of the interaction field for the prompt template */
public final static String INTERACTIONS_PROMPT_FIELD = "prompt";
/** Name of the interaction field for the AI response */
public final static String INTERACTIONS_RESPONSE_FIELD = "response";
/** Name of the interaction field for the GAI Agent */
public final static String INTERACTIONS_AGENT_FIELD = "agent";
/** Name of the interaction field for the timestamp */
public final static String INTERACTIONS_TIMESTAMP_FIELD = "timestamp";
/** Name of the interaction field for any excess metadata */
public final static String INTERACTIONS_METADATA_FIELD = "metadata";
/** Mappings for the interactions index */
public final static String INTERACTIONS_MAPPINGS = "{\n"
/** Version of the interactions index schema */
public final static Integer INTERACTIONS_INDEX_SCHEMA_VERSION = 1;
/** Name of the conversational interactions index */
public final static String INTERACTIONS_INDEX_NAME = ".conversational-interactions";
/** Name of the interaction field for the conversation Id */
public final static String INTERACTIONS_CONVERSATION_ID_FIELD = "conversation_id";
/** Name of the interaction field for the human input */
public final static String INTERACTIONS_INPUT_FIELD = "input";
/** Name of the interaction field for the AI response */
public final static String INTERACTIONS_RESPONSE_FIELD = "response";
/** Name of the interaction field for the response's origin */
public final static String INTERACTIONS_ORIGIN_FIELD = "origin";
/** Name of the interaction field for the timestamp */
public final static String INTERACTIONS_TIMESTAMP_FIELD = "timestamp";
/** Mappings for the interactions index */
public final static String INTERACTIONS_MAPPINGS = "{\n"
+ " \"_meta\": {\n"
+ " \"schema_version\": " + INTERACTIONS_INDEX_SCHEMA_VERSION + "\n"
+ " },\n"
+ " \"properties\": {\n"
+ " \""
+ INTERACTIONS_CONVERSATION_ID_FIELD
Expand All @@ -83,18 +79,12 @@ public class ConversationalIndexConstants {
+ INTERACTIONS_INPUT_FIELD
+ "\": {\"type\": \"text\"},\n"
+ " \""
+ INTERACTIONS_PROMPT_FIELD
+ "\": {\"type\": \"text\"},\n"
+ " \""
+ INTERACTIONS_RESPONSE_FIELD
+ "\": {\"type\": \"text\"},\n"
+ " \""
+ INTERACTIONS_AGENT_FIELD
+ INTERACTIONS_ORIGIN_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ INTERACTIONS_METADATA_FIELD
+ "\": {\"type\": \"text\"},\n"
+ " \""
+ USER_FIELD
+ "\": {\"type\": \"keyword\"}\n"
+ " }\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ public class Interaction implements Writeable, ToXContentObject {
@Getter
private String input;
@Getter
private String prompt;
@Getter
private String response;
@Getter
private String agent;
@Getter
private String metadata;
private String origin;

/**
* Creates an Interaction object from a map of fields in the OS index
Expand All @@ -66,11 +62,9 @@ public static Interaction fromMap(String id, Map<String, Object> fields) {
Instant timestamp = Instant.parse((String) fields.get(ConversationalIndexConstants.INTERACTIONS_TIMESTAMP_FIELD));
String conversationId = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_CONVERSATION_ID_FIELD);
String input = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_INPUT_FIELD);
String prompt = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_PROMPT_FIELD);
String response = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_RESPONSE_FIELD);
String agent = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_AGENT_FIELD);
String metadata = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_METADATA_FIELD);
return new Interaction(id, timestamp, conversationId, input, prompt, response, agent, metadata);
String agent = (String) fields.get(ConversationalIndexConstants.INTERACTIONS_ORIGIN_FIELD);
return new Interaction(id, timestamp, conversationId, input, response, agent);
}

/**
Expand All @@ -84,7 +78,7 @@ public static Interaction fromSearchHit(SearchHit hit) {
}

/**
* Creates a new Interaction onject from a stream
* Creates a new Interaction object from a stream
* @param in stream to read from; assumes Interactions.writeTo was called on it
* @return a new Interaction
* @throws IOException if can't read or the stream isn't pointing to an intraction or something
Expand All @@ -94,11 +88,9 @@ public static Interaction fromStream(StreamInput in) throws IOException {
Instant timestamp = in.readInstant();
String conversationId = in.readString();
String input = in.readString();
String prompt = in.readString();
String response = in.readString();
String agent = in.readString();
String metadata = in.readOptionalString();
return new Interaction(id, timestamp, conversationId, input, prompt, response, agent, metadata);
String origin = in.readString();
return new Interaction(id, timestamp, conversationId, input, response, origin);
}


Expand All @@ -108,10 +100,8 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeInstant(timestamp);
out.writeString(conversationId);
out.writeString(input);
out.writeString(prompt);
out.writeString(response);
out.writeString(agent);
out.writeOptionalString(metadata);
out.writeString(origin);
}

@Override
Expand All @@ -121,12 +111,8 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContentObject.Para
builder.field(ActionConstants.RESPONSE_INTERACTION_ID_FIELD, id);
builder.field(ConversationalIndexConstants.INTERACTIONS_TIMESTAMP_FIELD, timestamp);
builder.field(ConversationalIndexConstants.INTERACTIONS_INPUT_FIELD, input);
builder.field(ConversationalIndexConstants.INTERACTIONS_PROMPT_FIELD, prompt);
builder.field(ConversationalIndexConstants.INTERACTIONS_RESPONSE_FIELD, response);
builder.field(ConversationalIndexConstants.INTERACTIONS_AGENT_FIELD, agent);
if(metadata != null) {
builder.field(ActionConstants.INTER_ATTRIBUTES_FIELD, metadata);
}
builder.field(ConversationalIndexConstants.INTERACTIONS_ORIGIN_FIELD, origin);
builder.endObject();
return builder;
}
Expand All @@ -139,10 +125,8 @@ public boolean equals(Object other) {
((Interaction) other).conversationId.equals(this.conversationId) &&
((Interaction) other).timestamp.equals(this.timestamp) &&
((Interaction) other).input.equals(this.input) &&
((Interaction) other).prompt.equals(this.prompt) &&
((Interaction) other).response.equals(this.response) &&
((Interaction) other).agent.equals(this.agent) &&
((Interaction) other).metadata.equals(this.metadata)
((Interaction) other).origin.equals(this.origin)
);
}

Expand All @@ -152,11 +136,9 @@ public String toString() {
+ "id=" + id
+ ",cid=" + conversationId
+ ",timestamp=" + timestamp
+ ",agent=" + agent
+ ",prompt=" + prompt
+ ",origin=" + origin
+ ",input=" + input
+ ",response=" + response
+ ",metadata=" + metadata
+ "}";
}

Expand Down
Loading

0 comments on commit bd0ff99

Please sign in to comment.