Skip to content

Commit

Permalink
Merge branch 'llm' of https://github.com/MyRobotLab/myrobotlab into d…
Browse files Browse the repository at this point in the history
…evelop
  • Loading branch information
supertick committed May 21, 2024
2 parents f19767d + ec1342f commit 7695bff
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 69 deletions.
116 changes: 53 additions & 63 deletions src/main/java/org/myrobotlab/service/LLM.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,77 +93,64 @@ public void clearInputs() {

public String createChatCompletionPayload(String model, String systemContent, String userContent, int n, float temperature, int maxTokens) {
try {
// Create the map to hold the request parameters
LinkedHashMap<String, Object> requestPayload = new LinkedHashMap<>();

// Add model to the map
requestPayload.put("model", model);

// Create the messages array
LinkedHashMap<String, Object> systemMessage = new LinkedHashMap<>();
systemMessage.put("role", "system");

// Get the current date
LocalDate currentDate = LocalDate.now();
// Get the current time
LocalTime currentTime = LocalTime.now();
// Get the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();

// Format the date as a string
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = currentDate.format(dateFormatter);

// Format the time as a string in 12-hour format with AM/PM
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a");
String timeString = currentTime.format(timeFormatter);

// Format the full date with day of the week
DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofPattern("EEEE MMMM d'th' yyyy h:mm a");
String fullDateString = currentDateTime.format(fullDateFormatter);

inputs.put("Date", dateString);
inputs.put("Time", timeString);
inputs.put("DateTime", fullDateString);

for (String inputKey : inputs.keySet()) {
Object objectValue = inputs.get(inputKey);
if (objectValue != null) {
systemContent = systemContent.replace(String.format("{{%s}}", inputKey), objectValue.toString());
// Create the map to hold the request parameters
LinkedHashMap<String, Object> requestPayload = new LinkedHashMap<>();
requestPayload.put("model", model);

// Create and format date and time strings
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a");
DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofPattern("EEEE MMMM d'th' yyyy h:mm a");

inputs.put("Date", currentDateTime.format(dateFormatter));
inputs.put("Time", currentDateTime.format(timeFormatter));
inputs.put("DateTime", currentDateTime.format(fullDateFormatter));

// Replace placeholders in system content
for (Map.Entry<String, Object> entry : inputs.entrySet()) {
if (entry.getValue() != null) {
systemContent = systemContent.replace(String.format("{{%s}}", entry.getKey()), entry.getValue().toString());
}
}
}
systemMessage.put("content", systemContent);

if (config.maxHistory == 0) {
// history is disabled
userMessages.clear();
} else if (userMessages.size() > config.maxHistory) {
userMessages.remove(0);
}
LinkedHashMap<String, Object> userMessage = new LinkedHashMap<>();
userMessage.put("role", "user");
userMessage.put("content", userContent);
userMessages.add(userMessage);

List<LinkedHashMap<String,Object>> allMessages = new ArrayList<>();
allMessages.add(systemMessage);
allMessages.addAll(userMessages);
// Create system message
LinkedHashMap<String, Object> systemMessage = new LinkedHashMap<>();
systemMessage.put("role", "system");
systemMessage.put("content", systemContent);

// Handle message history
LinkedHashMap<String, Object> userMessage = new LinkedHashMap<>();
userMessage.put("role", "user");
userMessage.put("content", userContent);
userMessages.add(userMessage);

if (config.maxHistory > 0) {
while (userMessages.size() > config.maxHistory) {
userMessages.remove(0);
}
} else {
userMessages.clear();
}

// make history
requestPayload.put("messages", allMessages);
// Combine messages
List<LinkedHashMap<String, Object>> allMessages = new ArrayList<>();
allMessages.add(systemMessage);
allMessages.addAll(userMessages);
requestPayload.put("messages", allMessages);

// Add n, temperature, and max_tokens to the map
requestPayload.put("n", n);
requestPayload.put("temperature", temperature);
requestPayload.put("max_tokens", maxTokens);
// Add other parameters
requestPayload.put("n", n);
requestPayload.put("temperature", temperature);
requestPayload.put("max_tokens", maxTokens);

return CodecUtils.toJson(requestPayload);
return CodecUtils.toJson(requestPayload);

} catch (Exception e) {
error(e);
return null;
error(e);
return null;
}
}
}

public LinkedHashMap<String, Object> createFunctionDefinition(String name, String description, LinkedHashMap<String, Object> parameters) {
LinkedHashMap<String, Object> functionDefinition = new LinkedHashMap<>();
Expand Down Expand Up @@ -211,6 +198,9 @@ public Response getResponse(String text) {
log.info("curl {} -d '{}'", config.url, json);

String msg = http.postJson(config.password, config.url, json);
log.error("url: {}", config.url);
log.error("json: {}", json);
System.out.print(json);

Map<String, Object> payload = CodecUtils.fromJson(msg, new StaticType<>() {
});
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/myrobotlab/service/config/LLMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public class LLMConfig extends ServiceConfig {

public int maxTokens = 256;
public float temperature = 0.7f;
/**
* number of history items to keep when re-submitting
*/
public int history = 5;
// public String url = "https://api.openai.com/v1/chat/completions";
// http://localhost:11434/v1/chat/completions
public String url = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
<td>Temperature is how creative the responses should be.</td>
</tr>
<tr>
<td>History</td>
<td><input class="form-control" ng-model="service.config.history" /></td>
<td>Max History</td>
<td><input class="form-control" ng-model="service.config.maxHistory" /></td>
<td>The number of responses saved for context.</td>
</tr>
<tr>
Expand Down

0 comments on commit 7695bff

Please sign in to comment.