Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Java: Assistants #6009

Draft
wants to merge 1 commit into
base: java-v1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/**
* Represents an OpenAI Assistant object.
*/
public interface Assistant {

/**
* Get the identifier of the assistant.
*
* @return The identifier of the assistant.
*/
String getId();

/**
* Get the object type of the assistant, which is always {@code assistant}.
*
* @return The object type of the assistant.
*/
default String getObjectType() { return "assistant"; };

/**
* Get the Unix timestamp for when the assistant was created.
*
* @return The Unix timestamp for when the assistant was created.
*/
OffsetDateTime getCreatedAt();

/**
* Get the name of the assistant.
*
* @return The name of the assistant.
*/
@Nullable
String getName();

/**
* Get the description of the assistant.
*
* @return The description of the assistant.
*/
@Nullable
String getDescription();

/**
* Get the ID of the model used by the assistant.
*
* @return The ID of the model used by the assistant.
*/
String getModel();

/**
* Get the system instructions used by the assistant.
*
* @return The system instructions used by the assistant.
*/
@Nullable
String getInstructions();

/**
* Get the list of tools enabled on the assistant.
*
* @return The list of tools enabled on the assistant.
*/
List<Tool> getTools();

/**
* Get the set of resources used by the assistant's tools.
*
* @return The set of resources used by the assistant's tools.
*/
@Nullable
Map<ToolType, ToolResource> getToolResources();

/**
* Get the metadata attached to the assistant.
*
* @return The metadata attached to the assistant.
*/
Map<String, String> getMetadata();

/**
* Get the sampling temperature used by the assistant.
*
* @return The sampling temperature used by the assistant.
*/
@Nullable
Double getTemperature();

/**
* Get the top p value used by the assistant.
*
* @return The top p value used by the assistant.
*/
@Nullable
Double getTopP();

/**
* Get the response format used by the assistant.
*
* @return The response format used by the assistant.
*/
String getResponseFormat();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import com.microsoft.semantickernel.services.chatcompletion.AuthorRole;

/**
* Represents a message within a Thread.
*/
public interface AssistantMessage {


/**
* The identifier, which can be referenced in API endpoints.
* @return string
*/
String id();

/**
* The object type, which is always thread.message.
* @return string
*/
String getObject();

/**
* The Unix timestamp (in seconds) for when the message was created.
* @return integer
*/
int getCreatedAt();

/**
* The thread ID that this message belongs to.
* @return string
*/
String getThreadId();

/**
* The status of the message, which can be either in_progress, incomplete, or completed.
* @return string
*/
String getStatus();

/**
* On an incomplete message, details about why the message is incomplete.
* @return object or null
*/
@Nullable Object getIncompleteDetails();

/**
* The Unix timestamp (in seconds UTC) for when the message was completed.
* @return epoch time in UTC, or null
*/
@Nullable Long getCompletedAt();

/**
* The Unix timestamp (in seconds in UTC) for when the message was marked as incomplete.
* @return epoch time in UTC, or null
*/
@Nullable Long getIncompleteAt();

/**
* The entity that produced the message. One of user or assistant.
* @return string
*/
String getRole();

/**
* The content of the message in array of text and/or images.
* @return array
*/
List<String> getContent();

/**
* If applicable, the ID of the assistant that authored this message.
* @return string or null
*/
@Nullable String getAssistantId();

/**
* The ID of the run associated with the creation of this message. Value is null when messages are created manually using the create message or create thread endpoints.
* @return string or null
*/
@Nullable String getRunId();

/**
* A list of file IDs that the assistant should use. Useful for tools like retrieval and code_interpreter that can access files. A maximum of 10 files can be attached to a message.
* @return array
*/
List<String> getFileIds();

/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
* @return map
*/
Map<String, String> getMetadata();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AssistantMetadata implements Map<String, String> {

private final Map<String, String> metadata;

public AssistantMetadata() {
this.metadata = new HashMap<>();
}

@Override
public int size() {
return metadata.size();
}

@Override
public boolean isEmpty() {
return metadata.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return metadata.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return metadata.containsValue(value);
}

@Override
public String get(Object key) {
return metadata.get(key);
}

@Override
public String put(String key, String value) {
if (key.length() > 64 || value.length() > 512) {
throw new IllegalArgumentException("Key must be 64 characters or less and value must be 512 characters or less");
}
if (metadata.size() >= 16) {
throw new IllegalArgumentException("Metadata can only contain 16 key-value pairs");
}
return metadata.put(key, value);
}

@Override
public String remove(Object key) {
return metadata.remove(key);
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {
m.entrySet().forEach(entry -> put(entry.getKey(), entry.getValue()));
}

@Override
public void clear() {
metadata.clear();
}

@Override
public Set<String> keySet() {
return metadata.keySet();
}

@Override
public Collection<String> values() {
return metadata.values();
}

@Override
public Set<Entry<String, String>> entrySet() {
return metadata.entrySet();
}

@Override
public boolean equals(Object o) {
return metadata.equals(o);
}

@Override
public int hashCode() {
return metadata.hashCode();
}

@Override
public String toString() {
return metadata.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

import javax.annotation.Nullable;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.aiservices.openai.OpenAiService;
/**
* Represents an OpenAI Assistant.
*/
public class AssistantService extends OpenAiService {


public AssistantService(
OpenAIAsyncClient client,
@Nullable String serviceId,
String modelId) {
super(client, serviceId, modelId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

/**
* Represents a {@code code_interperter} tool definition.
*/
public interface CodeInterpreterTool extends Tool {

@Override
default ToolType getType() {
return ToolType.CODE_INTERPRETER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.microsoft.semantickernel.aiservices.openai.assistants;

import java.util.List;

/** Represents a {@code code_interpreter} type of {@code tool_resource} */
public interface CodeInterpreterToolResource extends ToolResource {

@Override
default ToolType getType() {
return ToolType.CODE_INTERPRETER;
}

/**
* A list of file IDs made available to the {@code code_interpreter} tool.
* @return A list of file IDs.
*/
List<String> getFileIds();
}