Skip to content

Commit

Permalink
Add PDFDocument.java
Browse files Browse the repository at this point in the history
  • Loading branch information
adam47deg committed Jul 11, 2023
1 parent 6f4b917 commit 1cb87b0
Showing 1 changed file with 53 additions and 0 deletions.
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;
}
}
}

0 comments on commit 1cb87b0

Please sign in to comment.