diff --git a/graalpy/graalpy-openai-starter/README.md b/graalpy/graalpy-openai-starter/README.md index 3a758246..d03b19e6 100644 --- a/graalpy/graalpy-openai-starter/README.md +++ b/graalpy/graalpy-openai-starter/README.md @@ -46,8 +46,8 @@ To execute the main method, run: The [App.java](src/main/java/com/example/App.java) embeds a `create_chat_completion()` function based on [the official usage example](https://github.com/openai/openai-python?tab=readme-ov-file#usage). The Java text block that contains the Python code is annotated with `// language=python`, which triggers a [language injection in IntelliJ IDEA](https://www.jetbrains.com/help/idea/using-language-injections.html). -The `create_chat_completion()` function is returned when the Python code is evaluated. -Afterward, the Python function is called with `userInput` from Java, and the result is mapped to the `ChatCompletion` interface. +The `create_chat_completion()` function is returned when the Python code is evaluated, and mapped to `CreateChatCompletionFunction`, a `FunctionalInterface` from `String` to `ChatCompletion`. +Afterward, the Python function is called with `userInput` from Java. The interfaces `ChatCompletion`, `Choice`, and `ChatComplectionMessage` are ported from [the official API](https://github.com/openai/openai-python/blob/main/api.md), and only contain the functionality exercised by the example code. These interfaces can be extended appropriately when needed. diff --git a/graalpy/graalpy-openai-starter/src/main/java/com/example/App.java b/graalpy/graalpy-openai-starter/src/main/java/com/example/App.java index 4cb123b9..17dcac2b 100644 --- a/graalpy/graalpy-openai-starter/src/main/java/com/example/App.java +++ b/graalpy/graalpy-openai-starter/src/main/java/com/example/App.java @@ -8,17 +8,16 @@ import org.graalvm.polyglot.Context; import org.graalvm.polyglot.PolyglotException; -import org.graalvm.polyglot.Value; import org.graalvm.python.embedding.utils.GraalPyResources; import java.util.List; public class App { - public static void main(String[] args) { + public static void main(String[] args) { String userInput = args.length > 0 ? args[0] : "Say this is a test"; try (Context context = GraalPyResources.createContext()) { - Value createChatCompletion = context.eval("python", + CreateChatCompletionFunction createChatCompletion = context.eval("python", // language=python """ import os @@ -39,10 +38,10 @@ def create_chat_completion(user_input): ], model="gpt-3.5-turbo", ) - + create_chat_completion - """); - ChatCompletion chatCompletion = createChatCompletion.execute(userInput).as(ChatCompletion.class); + """).as(CreateChatCompletionFunction.class); + ChatCompletion chatCompletion = createChatCompletion.apply(userInput); for (Choice choice : chatCompletion.choices()) { System.out.println(choice.message().content()); } @@ -51,15 +50,20 @@ def create_chat_completion(user_input): } } + @FunctionalInterface + public interface CreateChatCompletionFunction { + ChatCompletion apply(String choice); + } + public interface ChatCompletion { - List choices(); + List choices(); } public interface Choice { ChatCompletionMessage message(); } - public interface ChatCompletionMessage { + public interface ChatCompletionMessage { String content(); } }