-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Java Examples web search * Add Search to AIScope * Add Search to AIScope * Refactor * Refactor * Add Chat * Refactor * Start java example gpt4all * Add flow collector * Add port to test * Finish java example gpt4all * Refactor * Add Publisher * Refactor * Refactor * Refactor * Refactor * Final Refactor * Final Refactor conversationId
- Loading branch information
Showing
8 changed files
with
134 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
examples/java/src/main/java/com/xebia/functional/xef/java/auto/gpt4all/Chat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.xebia.functional.xef.java.auto.gpt4all; | ||
|
||
import com.xebia.functional.gpt4all.GPT4All; | ||
import com.xebia.functional.gpt4all.Gpt4AllModel; | ||
import com.xebia.functional.xef.auto.PromptConfiguration; | ||
import com.xebia.functional.xef.java.auto.AIScope; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
public class Chat { | ||
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException { | ||
var userDir = System.getProperty("user.dir"); | ||
var path = userDir + "/models/gpt4all/ggml-replit-code-v1-3b.bin"; | ||
|
||
var supportedModels = Gpt4AllModel.Companion.getSupportedModels(); | ||
|
||
supportedModels.forEach(it -> { | ||
var url = (Objects.nonNull(it.getUrl())) ? " - " + it.getUrl() : ""; | ||
System.out.println("🤖 " + it.getName() + url); | ||
}); | ||
|
||
var url = "https://huggingface.co/nomic-ai/ggml-replit-code-v1-3b/resolve/main/ggml-replit-code-v1-3b.bin"; | ||
var modelPath = Path.of(path); | ||
var gpt4all = GPT4All.Companion.invoke(url, modelPath); | ||
|
||
System.out.println("🤖 GPT4All loaded: " + gpt4all); | ||
/** | ||
* Uses internally [HuggingFaceLocalEmbeddings] default of "sentence-transformers", "msmarco-distilbert-dot-v5" | ||
* to provide embeddings for docs in contextScope. | ||
*/ | ||
|
||
try (AIScope scope = new AIScope(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { | ||
|
||
System.out.println("🤖 Context loaded: " + scope.getExec().getContext()); | ||
|
||
System.out.println("\n🤖 Enter your question: "); | ||
|
||
while(true){ | ||
String line = br.readLine(); | ||
if (line.equals("exit")) break; | ||
|
||
var promptConfiguration = new PromptConfiguration.Companion.Builder().docsInContext(2).streamToStandardOut(true).build(); | ||
Publisher<String> answer = scope.promptStreaming(gpt4all, line, promptConfiguration); | ||
|
||
answer.subscribe(new Subscriber<String>() { | ||
StringBuilder answer = new StringBuilder(); | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
System.out.print("\n🤖 --> " + s); | ||
s.request(Long.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void onNext(String s) { | ||
answer.append(s); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
System.out.println(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
System.out.println("\n🤖 --> " + answer.toString()); | ||
System.out.println("\n🤖 --> Done"); | ||
System.out.println("\n🤖 Enter your question: "); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters