-
Notifications
You must be signed in to change notification settings - Fork 4.6k
add Java sample skill (gRPC server) for gRPC operation runner (client) with a sample proto file #768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add Java sample skill (gRPC server) for gRPC operation runner (client) with a sample proto file #768
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
| target/ | ||
| !.mvn/wrapper/maven-wrapper.jar | ||
| !**/src/main/**/target/ | ||
| !**/src/test/**/target/ | ||
|
|
||
| ### IntelliJ IDEA ### | ||
| .idea | ||
| .idea/modules.xml | ||
| .idea/jarRepositories.xml | ||
| .idea/compiler.xml | ||
| .idea/libraries/ | ||
| *.iws | ||
| *.iml | ||
| *.ipr | ||
|
|
||
| ### Eclipse ### | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| .sts4-cache | ||
|
|
||
| ### NetBeans ### | ||
| /nbproject/private/ | ||
| /nbbuild/ | ||
| /dist/ | ||
| /nbdist/ | ||
| /.nb-gradle/ | ||
| build/ | ||
| !**/src/main/**/build/ | ||
| !**/src/test/**/build/ | ||
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| ### Mac OS ### | ||
| .DS_Store |
This file contains hidden or 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,23 @@ | ||
| # Java Reference Skill gRPC Server | ||
| This is a sample Java gRPC server that can be invoked via SK's gRPC client as a Native Skill/Function. The purpose of this project is to demonstrate how Polyglot skills can be supported using either REST or gRPC. | ||
|
|
||
| ## Prerequisites | ||
| * Java 17 | ||
| * Maven | ||
|
|
||
| ## Build | ||
| To build the project, run the following command: | ||
| ``` | ||
| mvn clean package | ||
| ``` | ||
| To generate the gRPC classes, run the following command: | ||
| ``` | ||
| mvn protobuf:compile | ||
| ``` | ||
|
|
||
| ## Run | ||
| To run the project, run the following command: | ||
| ``` | ||
| java -jar ./target/JavaReferenceSkill-1.0-SNAPSHOT-jar-with-dependencies.jar | ||
| ``` | ||
|
|
This file contains hidden or 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,109 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.microsoft.semantickernel.skills.random</groupId> | ||
| <artifactId>JavaReferenceSkill</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <grpc.version>1.54.0</grpc.version> | ||
| <javax.annotation-api.version>1.2</javax.annotation-api.version> | ||
| <os-maven-plugin.version>1.7.1</os-maven-plugin.version> | ||
| <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> | ||
| <protoc.version>3.22.2</protoc.version> | ||
| <mockito-core.version>5.2.0</mockito-core.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.grpc</groupId> | ||
| <artifactId>grpc-protobuf</artifactId> | ||
| <version>${grpc.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.grpc</groupId> | ||
| <artifactId>grpc-stub</artifactId> | ||
| <version>${grpc.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.grpc</groupId> | ||
| <artifactId>grpc-testing</artifactId> | ||
| <version>${grpc.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.grpc</groupId> | ||
| <artifactId>grpc-netty-shaded</artifactId> | ||
| <version>${grpc.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>${mockito-core.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>javax.annotation</groupId> | ||
| <artifactId>javax.annotation-api</artifactId> | ||
| <version>${javax.annotation-api.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <extensions> | ||
| <extension> | ||
| <groupId>kr.motd.maven</groupId> | ||
| <artifactId>os-maven-plugin</artifactId> | ||
| <version>${os-maven-plugin.version}</version> | ||
| </extension> | ||
| </extensions> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.xolstice.maven.plugins</groupId> | ||
| <artifactId>protobuf-maven-plugin</artifactId> | ||
| <version>${protobuf-maven-plugin.version}</version> | ||
| <configuration> | ||
| <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> | ||
| <pluginId>grpc-java</pluginId> | ||
| <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>compile</goal> | ||
| <goal>compile-custom</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-assembly-plugin</artifactId> | ||
| <configuration> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass>com.microsoft.semantickernel.skills.random.Main</mainClass> | ||
| </manifest> | ||
| </archive> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <id>make-assembly</id> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>single</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
28 changes: 28 additions & 0 deletions
28
...ava/JavaReferenceSkill/src/main/java/com/microsoft/semantickernel/skills/random/Main.java
This file contains hidden or 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,28 @@ | ||
| package com.microsoft.semantickernel.skills.random; | ||
|
|
||
| import io.grpc.Server; | ||
| import io.grpc.ServerBuilder; | ||
|
|
||
| import java.util.logging.Logger; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final int PORT = 50051; | ||
|
|
||
| public static void main(String[] args) { | ||
| Logger logger = java.util.logging.Logger.getLogger(Main.class.getName()); | ||
|
|
||
| Server server = ServerBuilder.forPort(PORT) | ||
| .addService(new RandomActivitySkill()).build(); | ||
|
|
||
| System.out.println("Starting server..."); | ||
| try { | ||
| server.start(); | ||
| System.out.println("gRPC Server for random activity started on port " + PORT); | ||
| server.awaitTermination(); | ||
| } catch (Exception e) { | ||
| logger.severe("Error with request: " + e.getMessage()); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...ceSkill/src/main/java/com/microsoft/semantickernel/skills/random/RandomActivitySkill.java
This file contains hidden or 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,42 @@ | ||
| package com.microsoft.semantickernel.skills.random; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import reference_skill.ActivityOuterClass; | ||
| import reference_skill.RandomActivitySkillGrpc; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class RandomActivitySkill extends RandomActivitySkillGrpc.RandomActivitySkillImplBase { | ||
|
|
||
| public static final String API_ACTIVITY_URL = "https://www.boredapi.com/api/activity"; | ||
|
|
||
| /** | ||
| * <pre> | ||
| * GetRandomActivity is an RPC method that retrieves a random activity from an API. | ||
| * </pre> | ||
| * | ||
| * @param request | ||
| * @param responseObserver | ||
| */ | ||
| @Override | ||
| public void getRandomActivity(ActivityOuterClass.GetRandomActivityRequest request, StreamObserver<ActivityOuterClass.GetRandomActivityResponse> responseObserver) { | ||
| Logger logger = java.util.logging.Logger.getLogger(this.getClass().getName()); | ||
| HttpClient httpClient = HttpClient.newHttpClient(); | ||
| HttpRequest httpRequest = HttpRequest.newBuilder() | ||
| .uri(URI.create(API_ACTIVITY_URL)) | ||
| .build(); | ||
| try { | ||
| CompletableFuture<HttpResponse<String>> response = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
| logger.info("Response: " + response.get().body()); | ||
| responseObserver.onNext(ActivityOuterClass.GetRandomActivityResponse.newBuilder().setActivity(response.get().body()).build()); | ||
| responseObserver.onCompleted(); | ||
| } catch (Exception e) { | ||
| logger.severe("Error with request: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
samples/java/JavaReferenceSkill/src/main/proto/activity.proto
This file contains hidden or 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,30 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package reference_skill; | ||
|
|
||
| // GetRandomActivityRequest is a message that contains input for the GetRandomActivity RPC method. | ||
| message GetRandomActivityRequest { | ||
| string input = 1; // Input is a hobby that is use to generate a random activity. | ||
| } | ||
|
|
||
| // GetRandomActivityResponse is a message that contains the activity returned by the GetRandomActivity RPC method. | ||
| message GetRandomActivityResponse { | ||
| string activity = 1; // Activity is a description of the random activity. | ||
| } | ||
|
|
||
| // RandomActivitySkill is a service that provides methods related to random activities. | ||
| service RandomActivitySkill { | ||
| // GetRandomActivity is an RPC method that retrieves a random activity from an API. | ||
| rpc GetRandomActivity (GetRandomActivityRequest) returns (GetRandomActivityResponse); | ||
| } | ||
|
|
||
| // Activity is a message that represents an activity with its various properties. | ||
| message Activity { | ||
| string activity = 1; // A description of the activity. | ||
| string type = 2; // The type or category of the activity. | ||
| int32 participants = 3; // The number of participants required for the activity. | ||
| double price = 4; // The cost associated with the activity, from 0 (free) to 1 (most expensive). | ||
| string link = 5; // A URL providing more information about the activity. | ||
| string key = 6; // A unique identifier for the activity. | ||
| float accessibility = 7; // The accessibility of the activity, from 0 (most accessible) to 1 (least accessible). | ||
| } |
51 changes: 51 additions & 0 deletions
51
...ill/src/test/java/com/microsoft/semantickernel/skills/random/RandomActivitySkillTest.java
This file contains hidden or 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,51 @@ | ||
| package com.microsoft.semantickernel.skills.random; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import io.grpc.testing.GrpcServerRule; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import reference_skill.ActivityOuterClass; | ||
| import reference_skill.RandomActivitySkillGrpc; | ||
|
|
||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class RandomActivitySkillTest { | ||
|
|
||
| @Rule | ||
| public GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); | ||
|
|
||
| private RandomActivitySkillGrpc.RandomActivitySkillBlockingStub blockingStub; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| grpcServerRule.getServiceRegistry().addService(new RandomActivitySkill()); | ||
| blockingStub = RandomActivitySkillGrpc.newBlockingStub(grpcServerRule.getChannel()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetRandomActivity() throws Exception { | ||
| HttpClient httpClient = mock(HttpClient.class); | ||
| HttpResponse<String> httpResponse = mock(HttpResponse.class); | ||
| CompletableFuture<HttpResponse<String>> responseFuture = CompletableFuture.completedFuture(httpResponse); | ||
|
|
||
| when(httpClient.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(responseFuture); | ||
| when(httpResponse.body()).thenReturn("{\"activity\":\"Test Activity\"}"); | ||
|
|
||
| RandomActivitySkill randomActivitySkill = new RandomActivitySkill() { | ||
| }; | ||
|
|
||
| ActivityOuterClass.GetRandomActivityRequest request = ActivityOuterClass.GetRandomActivityRequest.newBuilder().build(); | ||
| StreamObserver<ActivityOuterClass.GetRandomActivityResponse> responseObserver = mock(StreamObserver.class); | ||
| randomActivitySkill.getRandomActivity(request, responseObserver); | ||
|
|
||
| verify(responseObserver).onNext(any(ActivityOuterClass.GetRandomActivityResponse.class)); | ||
| verify(responseObserver).onCompleted(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.