-
Notifications
You must be signed in to change notification settings - Fork 43
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
10 changed files
with
316 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"String userHomeDir = System.getProperty(\"user.home\");\n", | ||
"String localRespoUrl = \"file://\" + userHomeDir + \"/.m2/repository/\";\n", | ||
"String langchain4jVersion = \"0.34.0\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\u001b[0mRepository \u001b[1m\u001b[32mlocal\u001b[0m url: \u001b[1m\u001b[32mfile:///Users/bsorrentino/.m2/repository/\u001b[0m added.\n", | ||
"\u001b[0mRepositories count: 5\n", | ||
"\u001b[0mname: \u001b[1m\u001b[32mcentral \u001b[0murl: \u001b[1m\u001b[32mhttps://repo.maven.apache.org/maven2/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n", | ||
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mjcenter \u001b[0murl: \u001b[1m\u001b[32mhttps://jcenter.bintray.com/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n", | ||
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mjboss \u001b[0murl: \u001b[1m\u001b[32mhttps://repository.jboss.org/nexus/content/repositories/releases/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n", | ||
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32matlassian \u001b[0murl: \u001b[1m\u001b[32mhttps://packages.atlassian.com/maven/public \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n", | ||
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mlocal \u001b[0murl: \u001b[1m\u001b[32mfile:///Users/bsorrentino/.m2/repository/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mtrue \u001b[0mupdate:\u001b[32malways \n", | ||
"\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%dependency /add-repo local \\{localRespoUrl} release|never snapshot|always\n", | ||
"%dependency /list-repos" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Adding dependency \u001b[0m\u001b[1m\u001b[32morg.slf4j:slf4j-jdk14:2.0.9\n", | ||
"\u001b[0mAdding dependency \u001b[0m\u001b[1m\u001b[32morg.bsc.langgraph4j:langgraph4j-core-jdk8:1.0-SNAPSHOT\n", | ||
"\u001b[0mAdding dependency \u001b[0m\u001b[1m\u001b[32mdev.langchain4j:langchain4j:0.34.0\n", | ||
"\u001b[0mAdding dependency \u001b[0m\u001b[1m\u001b[32mdev.langchain4j:langchain4j-open-ai:0.34.0\n", | ||
"\u001b[0mSolving dependencies\n", | ||
"Resolved artifacts count: 26\n", | ||
"Add to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/slf4j/slf4j-jdk14/2.0.9/slf4j-jdk14-2.0.9.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j/langgraph4j-core-jdk8/1.0-SNAPSHOT/langgraph4j-core-jdk8-1.0-SNAPSHOT.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/async/async-generator-jdk8/2.0.1/async-generator-jdk8-2.0.1.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j/0.34.0/langchain4j-0.34.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-core/0.34.0/langchain4j-core-0.34.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apache/opennlp/opennlp-tools/1.9.4/opennlp-tools-1.9.4.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jsoup/jsoup/1.16.1/jsoup-1.16.1.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-open-ai/0.34.0/langchain4j-open-ai-0.34.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/ai4j/openai4j/0.20.0/openai4j-0.20.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/retrofit2/retrofit/2.9.0/retrofit-2.9.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/retrofit2/converter-jackson/2.9.0/converter-jackson-2.9.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-databind/2.17.2/jackson-databind-2.17.2.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-annotations/2.17.2/jackson-annotations-2.17.2.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-core/2.17.2/jackson-core-2.17.2.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okhttp3/okhttp/4.12.0/okhttp-4.12.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okio/okio/3.6.0/okio-3.6.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okio/okio-jvm/3.6.0/okio-jvm-3.6.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okhttp3/okhttp-sse/4.12.0/okhttp-sse-4.12.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.10/kotlin-stdlib-jdk8-1.9.10.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib/1.9.10/kotlin-stdlib-1.9.10.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.10/kotlin-stdlib-common-1.9.10.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/annotations/13.0/annotations-13.0.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.10/kotlin-stdlib-jdk7-1.9.10.jar\u001b[0m\n", | ||
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/knuddels/jtokkit/1.1.0/jtokkit-1.1.0.jar\u001b[0m\n", | ||
"\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%dependency /add org.slf4j:slf4j-jdk14:2.0.9\n", | ||
"%dependency /add org.bsc.langgraph4j:langgraph4j-core-jdk8:1.0-SNAPSHOT\n", | ||
"%dependency /add dev.langchain4j:langchain4j:\\{langchain4jVersion}\n", | ||
"%dependency /add dev.langchain4j:langchain4j-open-ai:\\{langchain4jVersion}\n", | ||
"\n", | ||
"%dependency /resolve" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Initialize Logger" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-09-24 17:14:57 INFO REPL.$JShell$12 do_it$ LET'S START\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"var lm = java.util.logging.LogManager.getLogManager();\n", | ||
"lm.checkAccess(); \n", | ||
"try( var file = new java.io.FileInputStream(\"./logging.properties\")) {\n", | ||
" lm.readConfiguration( file );\n", | ||
"}\n", | ||
"\n", | ||
"var log = org.slf4j.LoggerFactory.getLogger(\"AdaptiveRag\");\n", | ||
"log.info( \"LET'S START\" );\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Test Issue [#32](https://github.com/bsorrentino/langgraph4j/issues/32)\n", | ||
"\n", | ||
"Issue concerns a problem on `AdaptiveRag` implementation referred to `AnswerGrader` task" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import dev.langchain4j.model.chat.ChatLanguageModel;\n", | ||
"import dev.langchain4j.model.input.Prompt;\n", | ||
"import dev.langchain4j.model.input.structured.StructuredPrompt;\n", | ||
"import dev.langchain4j.model.input.structured.StructuredPromptProcessor;\n", | ||
"import dev.langchain4j.model.openai.OpenAiChatModel;\n", | ||
"import dev.langchain4j.model.output.structured.Description;\n", | ||
"import dev.langchain4j.service.AiServices;\n", | ||
"import dev.langchain4j.service.SystemMessage;\n", | ||
"import java.time.Duration;\n", | ||
"import java.util.function.Function;\n", | ||
"\n", | ||
"\n", | ||
"public class AnswerGrader implements Function<AnswerGrader.Arguments,AnswerGrader.Score> {\n", | ||
"\n", | ||
" static final String MODELS[] = { \"gpt-3.5-turbo-0125\", \"gpt-4o-mini\" };\n", | ||
"\n", | ||
" /**\n", | ||
" * Binary score to assess answer addresses question.\n", | ||
" */\n", | ||
" public static class Score {\n", | ||
"\n", | ||
" @Description(\"Answer addresses the question, 'yes' or 'no'\")\n", | ||
" public String binaryScore;\n", | ||
"\n", | ||
" @Override\n", | ||
" public String toString() {\n", | ||
" return \"Score: \" + binaryScore;\n", | ||
" }\n", | ||
" }\n", | ||
"\n", | ||
" @StructuredPrompt(\"\"\"\n", | ||
"User question: \n", | ||
"\n", | ||
"{{question}}\n", | ||
"\n", | ||
"LLM generation: \n", | ||
"\n", | ||
"{{generation}}\n", | ||
"\"\"\")\n", | ||
" record Arguments(String question, String generation) {\n", | ||
" }\n", | ||
"\n", | ||
" interface Service {\n", | ||
"\n", | ||
" @SystemMessage(\"\"\"\n", | ||
"You are a grader assessing whether an answer addresses and/or resolves a question. \n", | ||
"\n", | ||
"Give a binary score 'yes' or 'no'. Yes, means that the answer resolves the question otherwise return 'no'\n", | ||
" \"\"\")\n", | ||
" Score invoke(String userMessage);\n", | ||
" }\n", | ||
"\n", | ||
" String openApiKey;\n", | ||
"\n", | ||
" @Override\n", | ||
" public Score apply(Arguments args) {\n", | ||
" ChatLanguageModel chatLanguageModel = OpenAiChatModel.builder()\n", | ||
" .apiKey( System.getenv(\"OPENAI_API_KEY\") )\n", | ||
" .modelName( MODELS[1] )\n", | ||
" .timeout(Duration.ofMinutes(2))\n", | ||
" .logRequests(true)\n", | ||
" .logResponses(true)\n", | ||
" .maxRetries(2)\n", | ||
" .temperature(0.0)\n", | ||
" .maxTokens(2000)\n", | ||
" .build();\n", | ||
"\n", | ||
"\n", | ||
" Service service = AiServices.create(Service.class, chatLanguageModel);\n", | ||
"\n", | ||
" Prompt prompt = StructuredPromptProcessor.toPrompt(args);\n", | ||
"\n", | ||
" log.trace( \"prompt: {}\", prompt.text() );\n", | ||
" \n", | ||
" return service.invoke(prompt.text());\n", | ||
" }\n", | ||
"\n", | ||
"}\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-09-24 17:14:58 FINEST REPL.$JShell$23$AnswerGrader apply prompt: User question:\n", | ||
"\n", | ||
"does it concern the Mathematics ?\n", | ||
"\n", | ||
"LLM generation:\n", | ||
"\n", | ||
"text\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"Score: no" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"var grader = new AnswerGrader();\n", | ||
"\n", | ||
"var args = new AnswerGrader.Arguments( \"does it concern the Mathematics ?\", \"text\" );\n", | ||
"grader.apply( args )" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Java (rjk 2.1.0)", | ||
"language": "java", | ||
"name": "rapaio-jupyter-kernel" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "java", | ||
"file_extension": ".jshell", | ||
"mimetype": "text/x-java-source", | ||
"name": "java", | ||
"nbconvert_exporter": "script", | ||
"pygments_lexer": "java", | ||
"version": "22.0.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
Oops, something went wrong.