Skip to content

Commit

Permalink
Use FunctionalInterface for Python function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 1, 2024
1 parent 36f6f96 commit 04ce064
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions graalpy/graalpy-openai-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
20 changes: 12 additions & 8 deletions graalpy/graalpy-openai-starter/src/main/java/com/example/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -51,15 +50,20 @@ def create_chat_completion(user_input):
}
}

@FunctionalInterface
public interface CreateChatCompletionFunction {
ChatCompletion apply(String choice);
}

public interface ChatCompletion {
List<Choice> choices();
List<Choice> choices();
}

public interface Choice {
ChatCompletionMessage message();
}

public interface ChatCompletionMessage {
public interface ChatCompletionMessage {
String content();
}
}

0 comments on commit 04ce064

Please sign in to comment.