-
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.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
examples/java/src/main/java/com/xebia/functional/xef/java/auto/PDFDocument.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,53 @@ | ||
package com.xebia.functional.xef.java.auto; | ||
|
||
import com.xebia.functional.tokenizer.ModelType; | ||
import com.xebia.functional.xef.textsplitters.TextSplitter; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static com.xebia.functional.xef.textsplitters.TokenTextSplitterKt.TokenTextSplitter; | ||
|
||
public class PDFDocument { | ||
|
||
public static class AIResponse { | ||
public String answer; | ||
public String source; | ||
} | ||
|
||
private static final String pdfUrl = "https://people.cs.ksu.edu/~schmidt/705a/Scala/Programming-in-Scala.pdf"; | ||
private static final BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
private static CompletableFuture<Void> askQuestion(AIScope scope) { | ||
System.out.println("Enter your question: "); | ||
|
||
String line = readLine(); | ||
if (line == null) { | ||
return CompletableFuture.completedFuture(null); | ||
} else { | ||
scope.prompt(line, AIResponse.class) | ||
.thenAccept((aiRes) -> System.out.println(aiRes.answer + "\n---\n" + | ||
aiRes.source + "\n---\n")); | ||
|
||
return askQuestion(scope); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
TextSplitter textSplitter = TokenTextSplitter(ModelType.getDEFAULT_SPLITTER_MODEL(), 100, 50); | ||
try (AIScope scope = new AIScope()) { | ||
scope.contextScope(scope.pdf(pdfUrl, textSplitter).get(), PDFDocument::askQuestion).get(); | ||
} | ||
} | ||
|
||
private static String readLine() { | ||
try { | ||
return sysin.readLine(); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
} |