Skip to content

Commit

Permalink
Merge pull request #39 from frazik-main/34-use-amplifygpt-to-update-r…
Browse files Browse the repository at this point in the history
…eadmemd

34 use amplifygpt to update readmemd
  • Loading branch information
MatKollar authored Sep 11, 2023
2 parents ac49c2a + 4e908a8 commit e5582c5
Show file tree
Hide file tree
Showing 15 changed files with 646 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export OPENAI_API_KEY=<your-openai-api-key>
- Java 1.8+
- [An OpenAI API Key](https://platform.openai.com/account/api-keys)
- Windows/Linux/Mac
- Google API key
- Google API key (optional)

## Want to contribute?

Expand Down
440 changes: 440 additions & 0 deletions chat-logs/examples.ExampleSearch/prompt.log

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions chat-logs/examples.ExampleSearch/summary.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

--prompt--
{"role": "user", "content": """""" Using the above text, please answer the following question: "best cars" -- if the question cannot be answered using the text, please summarize the text."}
--response--
The text does not provide any information about "best cars."
--prompt--
{"role": "user", "content": """Best Cars for 2023 and 2024 | U.S. News Cars New Cars New Cars for Sale Research Cars Best Price Program New Car Rankings Car Deals This Month Compare Cars Calculate Car Payment Used Cars Used Cars for Sale Certified Pre-Owned Cars for Sale Used Car Rankings Used Car Deals This Month Compare Used Cars Calculate Used Car Payment Car Rankings New Car Rankings Used Car Rankings Best Cars for the Money Best Cars for Families Best Vehicle Brands Cars for Sale New Cars for Sale Used Cars for Sale Certified Pre-Owned Cars for Sale Electric Vehicles Advice Advice Features Car Insurance Loans & Leasing Motorcycles ATVs UTVs Best Car Deals New Car Deals Used Car Deals Lease Car Deals Sell Your Car Car Selling Advice Get Instant Cash Offer AcuraAlfa RomeoAston MartinAudiBMWBuickCadillacChevroletChryslerDodgeFerrariFIATFordGenesisGMCHondaHUMMERHyundaiInfinitiIsuzuJaguarJeepKiaLamborghiniLand RoverLexusLincolnLotusMaseratiMazdaMercedes-BenzMercuryMINIMitsubishiNissanPontiacPorscheRamSaabSaturnScionSmartSubaruSuzukiTeslaToyotaVolkswagenVolvoMake Model Year Sign In Cars Rankings Cars Best Cars for 2023 and 2024 Find the best cars then compare models and read our car reviews, which provide the consensus opinion from the automotive press. You can also view new car prices in your area. Read how we rank new cars New Car Rankings Used Car Rankings Best Cars for the Money Best Car for Families Best Vehicle Brands New Car Rankings Used Car Rankings Best Cars for the Money Best Car for Families Best Vehicle Brands Cars SUVs Trucks Vans Hybrids Electric Small Cars Small Cars #1 2023 Honda Civic Type R #2 2024 Acura Integra #3 2023 Honda Civic See full rankings Luxury Small Cars #1 2024 Acura Integra #2 2023 Audi A4 #2 2023 Mercedes-Benz C-Class See full rankings Advertisement Hatchbacks #1 2023 Honda Civic Type R #2 2023 Honda Civic #3 2023 Mazda Mazda3 See full rankings Compact Cars #1 2023 Honda Civic #2 2023 Mazda Mazda3 #3 2023 Hyundai Elantra See full rankings Subcompact Cars #1 2024 MINI Cooper #2 2023 Kia Rio #3 2023 Nissan Versa See full rankings Advertisement Midsize Cars Midsize Cars #1 2023 Honda Accord #2 2023 Kia K5 #3 2023 Toyota Camry See full rankings Luxury Midsize Cars #1 2023 Mercedes-Benz E-Class #2 2023 BMW 5-Series #3 2023 Genesis G80 See full rankings Large Cars Large Cars #1 2023 Dodge Charger #2 2023 Nissan Maxima #3 2023 Chrysler 300 See full rankings Luxury Large Cars #1 2023 Genesis G90 #2 2023 Audi A7 #3 2023 Mercedes-Benz S-Class See full rankings Advertisement Sports Cars Sports Cars #1 2023 Subaru BRZ #2 2024 Toyota GR86 #3 2024 Ford Mustang See full rankings Luxury Sports Cars #1 2023 Chevrolet Corvette #2 2023 Audi R8 #3 2023 Porsche 911 See full rankings Advertisement Performance Cars Sport Compact Cars #1 2023 Honda Civic Type R #2 2023 Hyundai Elantra N #3 2023 Volkswagen GTI See full rankings Small Sport Sedans #1 2024 Acura Integra Type S #2 2023 Cadillac CT4-V Blackwing #3 2023 Audi S5 See full rankings Midsize Sport Sedans #1 2023 Cadillac CT5-V Blackwing #2 2023 Audi S6 #2 2023 BMW M5 See full rankings Large Sport Sedans #1 2023 Audi S8 #2 2023 Audi RS 7 #3 2023 Audi S7 See full rankings Convertibles Convertibles #1 2024 Ford Mustang #1 2023 Mazda MX-5 Miata #3 2024 Chevrolet Camaro See full rankings Luxury Convertibles #1 2023 Chevrolet Corvette #2 2023 Porsche 911 #2 2023 Porsche Boxster See full rankings Wagons Wagons #1 2023 Audi A6 Allroad #2 2023 Volvo V60 #3 2023 Audi A4 Allroad See full rankings Hybrid Cars Hybrid Cars #1 2023 Honda Accord Hybrid #1 2023 Hyundai Sonata Hybrid #3 2023 Toyota Camry Hybrid See all Hybrid Cars Advertisement Advertisement Browse Cars Small Cars Midsize Cars Large Cars SUVs Crossovers Trucks Hybrids Vans Electric Luxury Sports Cars Convertibles Browse all models Advertisement Advertisement New Cars New Cars for Sale Research Cars Best Price Program New Car Rankings Car Deals This Month Compare Cars Calculate Car Payment Used Cars Used Cars for Sale Certified Pre-Owned Cars for Sale Used Car Rankings Insurance Cheapest Car Insurance Companies Best Car Insurance Companies Car Rankings New Car Rankings Used Car Rankings Best Cars for the Money Best Cars for Families Best Vehicle Brands Advice Best Car Deals New Car Deals Used Car Deals Lease Car Deals How We Rank New Cars Used Cars Cars for Sale New Cars for Sale Used Cars for Sale Certified Pre-Owned Cars for Sale Stock photography by izmostock. © 1986 - 2023 Autodata, Inc. dba Chrome Data New Cars Used Cars Insurance Car Rankings Advice Best Car Deals How We Rank Cars for Sale Stock photography by izmostock. © 1986 - 2023 Autodata, Inc. dba Chrome Data About U.S. News Contact Press Advertise Newsletters Jobs Site Map Store Copyright 2023 © U.S. News & World Report L.P.Terms & Conditions/Privacy Policy and Cookie Notice/US State Privacy Notice/Your Privacy Choices California Consumer Privacy Act (CCPA) Opt-Out Icon""" Using the above text, please answer the following question: "best cars" -- if the question cannot be answered using the text, please summarize the text."}
--response--
According to the provided text, the "best cars" for 2023 and 2024 are listed in various categories such as small cars, luxury small cars, hatchbacks, compact cars, subcompact cars, midsize cars, luxury midsize cars, large cars, luxury large cars, sports cars, luxury sports cars, performance cars, convertibles, wagons, and hybrid cars. The rankings include popular models like the Honda Civic, Acura Integra, Audi A4, Mercedes-Benz E-Class, Dodge Charger, Genesis G90, Subaru BRZ, Chevrolet Corvette, Honda Accord Hybrid, and many more.
--prompt--
{"role": "user", "content": """""" Using the above text, please answer the following question: "best cars" -- if the question cannot be answered using the text, please summarize the text."}
--response--
The provided text does not mention anything about "best cars" or provide any relevant information regarding that topic.
--prompt--
{"role": "user", "content": """Best Cars for 2023 and 2024 | U.S. News Cars New Cars New Cars for Sale Research Cars Best Price Program New Car Rankings Car Deals This Month Compare Cars Calculate Car Payment Used Cars Used Cars for Sale Certified Pre-Owned Cars for Sale Used Car Rankings Used Car Deals This Month Compare Used Cars Calculate Used Car Payment Car Rankings New Car Rankings Used Car Rankings Best Cars for the Money Best Cars for Families Best Vehicle Brands Cars for Sale New Cars for Sale Used Cars for Sale Certified Pre-Owned Cars for Sale Electric Vehicles Advice Advice Features Car Insurance Loans & Leasing Motorcycles ATVs UTVs Best Car Deals New Car Deals Used Car Deals Lease Car Deals Sell Your Car Car Selling Advice Get Instant Cash Offer AcuraAlfa RomeoAston MartinAudiBMWBuickCadillacChevroletChryslerDodgeFerrariFIATFordGenesisGMCHondaHUMMERHyundaiInfinitiIsuzuJaguarJeepKiaLamborghiniLand RoverLexusLincolnLotusMaseratiMazdaMercedes-BenzMercuryMINIMitsubishiNissanPontiacPorscheRamSaabSaturnScionSmartSubaruSuzukiTeslaToyotaVolkswagenVolvoMake Model Year Sign In Cars Rankings Cars Best Cars for 2023 and 2024 Find the best cars then compare models and read our car reviews, which provide the consensus opinion from the automotive press. You can also view new car prices in your area. Read how we rank new cars New Car Rankings Used Car Rankings Best Cars for the Money Best Car for Families Best Vehicle Brands New Car Rankings Used Car Rankings Best Cars for the Money Best Car for Families Best Vehicle Brands Cars SUVs Trucks Vans Hybrids Electric Small Cars Small Cars #1 2023 Honda Civic Type R #2 2024 Acura Integra #3 2023 Honda Civic See full rankings Luxury Small Cars #1 2024 Acura Integra #2 2023 Audi A4 #2 2023 Mercedes-Benz C-Class See full rankings Advertisement Hatchbacks #1 2023 Honda Civic Type R #2 2023 Honda Civic #3 2023 Mazda Mazda3 See full rankings Compact Cars #1 2023 Honda Civic #2 2023 Mazda Mazda3 #3 2023 Hyundai Elantra See full rankings Subcompact Cars #1 2024 MINI Cooper #2 2023 Kia Rio #3 2023 Nissan Versa See full rankings Advertisement Midsize Cars Midsize Cars #1 2023 Honda Accord #2 2023 Kia K5 #3 2023 Toyota Camry See full rankings Luxury Midsize Cars #1 2023 Mercedes-Benz E-Class #2 2023 BMW 5-Series #3 2023 Genesis G80 See full rankings Large Cars Large Cars #1 2023 Dodge Charger #2 2023 Nissan Maxima #3 2023 Chrysler 300 See full rankings Luxury Large Cars #1 2023 Genesis G90 #2 2023 Audi A7 #3 2023 Mercedes-Benz S-Class See full rankings Advertisement Sports Cars Sports Cars #1 2023 Subaru BRZ #2 2024 Toyota GR86 #3 2024 Ford Mustang See full rankings Luxury Sports Cars #1 2023 Chevrolet Corvette #2 2023 Audi R8 #3 2023 Porsche 911 See full rankings Advertisement Performance Cars Sport Compact Cars #1 2023 Honda Civic Type R #2 2023 Hyundai Elantra N #3 2023 Volkswagen GTI See full rankings Small Sport Sedans #1 2024 Acura Integra Type S #2 2023 Cadillac CT4-V Blackwing #3 2023 Audi S5 See full rankings Midsize Sport Sedans #1 2023 Cadillac CT5-V Blackwing #2 2023 Audi S6 #2 2023 BMW M5 See full rankings Large Sport Sedans #1 2023 Audi S8 #2 2023 Audi RS 7 #3 2023 Audi S7 See full rankings Convertibles Convertibles #1 2024 Ford Mustang #1 2023 Mazda MX-5 Miata #3 2024 Chevrolet Camaro See full rankings Luxury Convertibles #1 2023 Chevrolet Corvette #2 2023 Porsche 911 #2 2023 Porsche Boxster See full rankings Wagons Wagons #1 2023 Audi A6 Allroad #2 2023 Volvo V60 #3 2023 Audi A4 Allroad See full rankings Hybrid Cars Hybrid Cars #1 2023 Honda Accord Hybrid #1 2023 Hyundai Sonata Hybrid #3 2023 Toyota Camry Hybrid See all Hybrid Cars Advertisement Advertisement Browse Cars Small Cars Midsize Cars Large Cars SUVs Crossovers Trucks Hybrids Vans Electric Luxury Sports Cars Convertibles Browse all models Advertisement Advertisement New Cars New Cars for Sale Research Cars Best Price Program New Car Rankings Car Deals This Month Compare Cars Calculate Car Payment Used Cars Used Cars for Sale Certified Pre-Owned Cars for Sale Used Car Rankings Insurance Cheapest Car Insurance Companies Best Car Insurance Companies Car Rankings New Car Rankings Used Car Rankings Best Cars for the Money Best Cars for Families Best Vehicle Brands Advice Best Car Deals New Car Deals Used Car Deals Lease Car Deals How We Rank New Cars Used Cars Cars for Sale New Cars for Sale Used Cars for Sale Certified Pre-Owned Cars for Sale Stock photography by izmostock. © 1986 - 2023 Autodata, Inc. dba Chrome Data New Cars Used Cars Insurance Car Rankings Advice Best Car Deals How We Rank Cars for Sale Stock photography by izmostock. © 1986 - 2023 Autodata, Inc. dba Chrome Data About U.S. News Contact Press Advertise Newsletters Jobs Site Map Store Copyright 2023 © U.S. News & World Report L.P.Terms & Conditions/Privacy Policy and Cookie Notice/US State Privacy Notice/Your Privacy Choices California Consumer Privacy Act (CCPA) Opt-Out Icon""" Using the above text, please answer the following question: "best cars" -- if the question cannot be answered using the text, please summarize the text."}
--response--
According to the provided text, the best cars for 2023 and 2024 are listed in different categories such as small cars, luxury small cars, hatchbacks, compact cars, subcompact cars, midsize cars, luxury midsize cars, large cars, luxury large cars, sports cars, luxury sports cars, performance cars, convertibles, wagons, hybrid cars, and electric cars. The text provides rankings for each category and mentions specific models that are considered the best in their respective categories.
2 changes: 0 additions & 2 deletions examples/src/main/java/examples/ExampleSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public static void main(String[] args) {
goals.add("There will be no user assistance. Terminate once writing both files is complete.");

Agent agent = new Agent(name, description, goals, "gpt-3.5-turbo");

agent.cli();

}

}
10 changes: 4 additions & 6 deletions lib/src/main/java/com/frazik/instructgpt/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.frazik.instructgpt.auto.Cli;
import com.frazik.instructgpt.embedding.OpenAIEmbeddingProvider;
import com.frazik.instructgpt.memory.LocalMemory;
import com.frazik.instructgpt.models.Model;
import com.frazik.instructgpt.models.OpenAIModel;
import com.frazik.instructgpt.prompts.Prompt;
import com.frazik.instructgpt.prompts.PromptHistory;
Expand All @@ -30,7 +30,7 @@ public class Agent {
private final List<Tool> tools;
private Map<String, Object> stagingTool;
private JsonNode stagingResponse;
private final OpenAIModel openAIModel;
private final Model openAIModel;
private static final ObjectMapper mapper = new ObjectMapper();
public Agent(String name, String description, List<String> goals, String model) {
this.history = new PromptHistory();
Expand Down Expand Up @@ -180,11 +180,9 @@ public Response chat(String message, boolean runTool) {
this.stagingResponse = null;
}
List<Map<String, String>> fullPrompt = this.getFullPrompt(message);
int tokenCount = openAIModel.countTokens(fullPrompt);
int tokenLimit = openAIModel.getTokenLimit();
String resp = openAIModel.chat(fullPrompt, tokenLimit - tokenCount);
String resp = openAIModel.chat(fullPrompt);
System.out.println("=========");
System.out.println(resp);
System.out.println("RESPONSE FINISHED");
System.out.println("=========");
this.history.addNewPrompt("user", message);
this.history.addNewPrompt("assistant", resp);
Expand Down
14 changes: 7 additions & 7 deletions lib/src/main/java/com/frazik/instructgpt/Summarizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
Adapted from Auto-GPT (https://github.com/Significant-Gravitas/Auto-GPT)
*/

import com.frazik.instructgpt.models.Model;
import com.frazik.instructgpt.models.OpenAIModel;

import java.util.ArrayList;
import java.util.List;

public class Summarizer {
private String model;
private OpenAIModel openAIModel;
private Model model;

public Summarizer(String model) {
this.model = model;
this.model = new OpenAIModel(model);
}

public String summarize(String text, String query) {
List<String> summaries = new ArrayList<>();
for (String chunk : chunkText(text)) {
summaries.add(chat(new String[]{prompt(chunk, query)}, 300));
summaries.add(chat(new String[]{prompt(chunk, query)}));
}
String summary = String.join("\n", summaries);
while (summary.length() > Math.pow(2, 13)) {
summaries = new ArrayList<>();
for (String chunk : chunkText(summary)) {
summaries.add(chat(new String[]{prompt(chunk, query)}, 300));
summaries.add(chat(new String[]{prompt(chunk, query)}));
}
summary = String.join("\n", summaries);
}
Expand Down Expand Up @@ -64,8 +64,8 @@ private String prompt(String text, String query) {
+ "\" -- if the question cannot be answered using the text, please summarize the text.\"}";
}

private String chat(String[] prompts, int maxTokens) {
return openAIModel.chat(prompts);
private String chat(String[] prompts) {
return model.summarize(prompts);
}
}

23 changes: 11 additions & 12 deletions lib/src/main/java/com/frazik/instructgpt/auto/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ public void run() {
}
if (resp.hasCommand()) {
System.out.println("Agent wants to execute the following command: \n" + resp.getCommand());
while (true) {
String yn = "";
do {
System.out.print("(Y/N)? ");
String yn = scanner.nextLine().toLowerCase().trim();
if ("y".equals(yn) || "n".equals(yn)) {
if ("y".equals(yn)) {
resp = agent.chat("GENERATE NEXT COMMAND JSON", true);
} else {
System.out.print("Enter feedback (Why not execute the command?): ");
String feedback = scanner.nextLine();
resp = agent.chat(feedback, false);
}
break;
}
yn = scanner.nextLine().toLowerCase().trim();
} while (!"y".equals(yn) && !"n".equals(yn));
if ("y".equals(yn)) {
resp = agent.chat("GENERATE NEXT COMMAND JSON", true);
} else {
System.out.print("Enter feedback (Why not execute the command?): ");
String feedback = scanner.nextLine();
resp = agent.chat(feedback, false);
}
continue;
}
System.out.print("Enter message: ");
String inp = scanner.nextLine();
Expand Down
70 changes: 70 additions & 0 deletions lib/src/main/java/com/frazik/instructgpt/logging/ChatLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.frazik.instructgpt.logging;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@SuppressWarnings("ResultOfMethodCallIgnored")
@Slf4j
public class ChatLogger {

private final File currentFolder;
private final File promptFile;
private final File summaryFile;
public ChatLogger() {
this.currentFolder = new File("chat-logs/" + System.getProperty("sun.java.command"));
this.promptFile = new File(currentFolder, "prompt.log");
this.summaryFile = new File(currentFolder, "summary.log");
if (promptFile.exists()) {
promptFile.delete();
}
if (summaryFile.exists()) {
summaryFile.delete();
}
if (!currentFolder.exists()) {
currentFolder.mkdirs();
}
}
public void write(List<Map<String, String>> fullPrompt, String response) {
try {
FileUtils.write(promptFile, "\n--prompt--\n", "UTF-8", true);
for (Map<String, String> prompt : fullPrompt) {
for (Map.Entry<String, String> entry : prompt.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
FileUtils.write(promptFile, key + ": " + value + "\n", "UTF-8", true);
}
}
FileUtils.write(promptFile, "\n--response--\n", "UTF-8", true);
FileUtils.write(promptFile, response, "UTF-8", true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void write(String[] prompts, String response) {
try {
FileUtils.write(summaryFile, "\n--prompt--\n", "UTF-8", true);
for (String prompt : prompts) {
FileUtils.write(summaryFile, prompt, "UTF-8", true);
}
FileUtils.write(summaryFile, "\n--response--\n", "UTF-8", true);
// Write response to response.log
FileUtils.write(summaryFile, response, "UTF-8", true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private File getAndInitialize(String x) {
File promptFile = new File(currentFolder, x + ".log");
if (promptFile.exists()) {
promptFile.delete();
}
return promptFile;
}

}
Loading

0 comments on commit e5582c5

Please sign in to comment.