Skip to content
Merged

Llm #116

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PatchCat
Submodule PatchCat updated 1 files
+23 −2 src/PatchCatGin.py
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation 'org.junit.platform:junit-platform-engine'
implementation 'org.junit.vintage:junit-vintage-engine'
compileOnly 'org.apiguardian:apiguardian-api:1.1.2'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
//implementation group: 'junit', name: 'junit', version: '4.13.1'
//implementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2'
implementation 'junit:junit:4.13.2'
Expand Down Expand Up @@ -76,7 +77,7 @@ dependencies {
implementation 'ch.qos.logback:logback-classic:1.2.3'

// LLM: ollama4j
implementation 'io.github.amithkoujalgi:ollama4j:1.0.44'
implementation 'io.github.ollama4j:ollama4j:1.1.6'

// https://mvnrepository.com/artifact/com.github.spullara.cli-parser/cli-parser
implementation group: 'com.github.spullara.cli-parser', name: 'cli-parser', version: '1.1.5'
Expand Down Expand Up @@ -213,3 +214,4 @@ tasks.test {
systemProperty "gin.jar", "${buildDir}/gin.jar"
}


4 changes: 2 additions & 2 deletions src/main/java/gin/edit/llm/LLMConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gin.edit.llm;

import org.checkerframework.checker.units.qual.s;
//import org.checkerframework.checker.units.qual.s;

import dev.langchain4j.model.openai.OpenAiModelName;
import gin.edit.llm.PromptTemplate.PromptTag;
Expand Down Expand Up @@ -94,4 +94,4 @@ public static PromptTemplate getDefaultPromptTemplate() {



}
}
5 changes: 3 additions & 2 deletions src/main/java/gin/edit/llm/LLMReplaceStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.utils.Log;

import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
//import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;

import gin.SourceFile;
import gin.SourceFileTree;
Expand Down Expand Up @@ -142,7 +142,8 @@ public List<SourceFile> applyMultiple(SourceFile sourceFile, int count, Map<Prom
if ("OpenAI".equalsIgnoreCase(LLMConfig.modelType)) {
llmQuery = new OpenAILLMQuery();
} else {
llmQuery = new Ollama4jLLMQuery("http://localhost:11434", LLMConfig.modelType);
String host = "http://localhost:11434";
llmQuery = new Ollama4jLLMQuery(host, LLMConfig.modelType);
}

// TODO here, could call sourceFile.getSource() to provide whole class for context...
Expand Down
58 changes: 42 additions & 16 deletions src/main/java/gin/edit/llm/Ollama4jLLMQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,74 @@

import java.io.IOException;

import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType;
import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
import io.github.ollama4j.Ollama;
import io.github.ollama4j.utils.OptionsBuilder;
import io.github.ollama4j.models.response.OllamaResult;
import io.github.ollama4j.exceptions.OllamaException;
import io.github.ollama4j.models.generate.OllamaGenerateRequest;

//import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
//import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
//import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType;
//import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
//import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
//import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;

import gin.edit.llm.LLMQuery;

public class Ollama4jLLMQuery implements LLMQuery {
private OllamaAPI ollamaAPI;
private Ollama ollamaAPI;
private String modelType;

private static final String OLLAMA_SERVER = System.getenv("OLLAMA_SERVER");
private static final String OLLAMA_API_KEY = System.getenv("OLLAMA_API_KEY");

// c'tor
public Ollama4jLLMQuery(String ollamaServerHost, String modelType) {
this.modelType = modelType;

this.ollamaAPI = new OllamaAPI(ollamaServerHost);
if (OLLAMA_SERVER == null || OLLAMA_SERVER.isBlank() || OLLAMA_API_KEY == null || OLLAMA_API_KEY.isBlank()) {
this.ollamaAPI = new Ollama(ollamaServerHost);
} else {
this.ollamaAPI = new Ollama(OLLAMA_SERVER);
this.ollamaAPI.setBearerAuth(OLLAMA_API_KEY);
}
ollamaAPI.setRequestTimeoutSeconds(LLMConfig.timeoutInSeconds);
ollamaAPI.setVerbose(true);
//ollamaAPI.setVerbose(true);
}

@Override
@Override
public boolean testServerReachable() {
return ollamaAPI.ping();
boolean ret = false;
try {
ret = ollamaAPI.ping();
} catch (OllamaException e) {
// handle the exception
e.printStackTrace();
}
return ret;
}

@Override
public String chatLLM(String prompt) {
try {
// code that might throw OllamaBaseException
OllamaResult result =
ollamaAPI.ask(modelType, prompt, new OptionsBuilder().build());
OllamaGenerateRequest req = OllamaGenerateRequest.builder()
.withModel(this.modelType).withPrompt(prompt)
// .withContext("You are a Java Optimization Engine that creates a valid faster code")
.build();
OllamaResult result = ollamaAPI.generate(req, null);
//ollamaAPI.ask(modelType, prompt, new OptionsBuilder().build());
return result.getResponse();
} catch (OllamaBaseException e) {
} catch (OllamaException e) {
// handle the exception
e.printStackTrace();
} catch (IOException | InterruptedException e) {
//} catch (IOException | InterruptedException e) {
// handle the IOException
e.printStackTrace();
// e.printStackTrace();
}
return "";
}
}


6 changes: 5 additions & 1 deletion src/main/java/gin/util/LocalSearchSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ protected void search(TargetMethod method, Patch origPatch) {
ProcessBuilder builder = new ProcessBuilder(
"python3",
"../gin/PatchCat/src/PatchCatGin.py",
"--diff-text", lastReplacement.toString(),
// KEM left the original code, but I tried with giving PatchCat both before and after, we can comapre performance later
//"--diff-text", lastReplacement.toString(),
// We likely want a flag here to select between the two options!
"--A-text", lastReplacement.toString(),
"--B-text", origPatch.toString(),
"--vectorizer-path", "../gin/PatchCat/src/running-model/vectorizer.pkl",
"--model-path", "../gin/PatchCat/src/running-model/model.pkl"
);
Expand Down
Loading