Skip to content

Commit

Permalink
Merge pull request #84 from milderhc/sk-java-hello-world
Browse files Browse the repository at this point in the history
Add sk-java-hello-world
  • Loading branch information
markwallace-microsoft authored Dec 2, 2023
2 parents 047f893 + a3f9003 commit 4746653
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Alternatively, you can use the [Semantic Kernel Tools](https://marketplace.visua
- [C# Azure ChatGPT Plugin](sk-csharp-chatgpt-plugin): The Joke ChatGPT Plugin starter using C# Azure Functions for the Semantic Kernel.
- [Python Hello World](sk-python-hello-world): The Hello World Python console application starter for the Semantic Kernel.
- [Python Azure Functions](sk-python-azure-functions): The Hello World Python Azure Functions starter for the Semantic Kernel.
- [Typescript Console Chat](sk-typescript-console-chat): A console application based chat starter for the Semantic Kernel.
- [Java Hello World](sk-java-hello-world): The Hello World Java console application starter for the Semantic Kernel.
- [Python Azure Functions ChatGPT Plugin](sk-python-azure-functions-chatgpt-plugin): The Joke ChatGPT Plugin starter using Python Azure Functions for the Semantic Kernel.
- [Python Flask ChatGPT Plugin](sk-python-flask-chatgpt-plugin): The Joke ChatGPT Plugin starter using Python Flask for the Semantic Kernel.
- [Typescript Console Chat](sk-typescript-console-chat): A console application based chat starter for the Semantic Kernel.
Expand Down
41 changes: 41 additions & 0 deletions sk-java-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Semantic Kernel Java Hello World Starter

The `sk-java-hello-world` console application demonstrates how to execute a semantic function.

## Prerequisites

- [Java](https://learn.microsoft.com/java/openjdk/download) 11 or above.
- [Maven](https://maven.apache.org/download.cgi)

## Configuring the starter

The starter can be configured with a `conf.properties` file in the project which holds api keys and other secrets and configurations.

Make sure you have an
[Open AI API Key](https://openai.com/api/) or
[Azure Open AI service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api).

Copy the `example.conf.properties` file to a new file named `conf.properties`. Then, copy those keys into the `conf.properties` file.

If you are using Open AI:

```
client.openai.key=""
client.openai.organizationid=""
```

Or, if you are using Azure Open AI:

```
client.azureopenai.key=""
client.azureopenai.endpoint=""
client.azureopenai.deploymentname=""
```

## Running the starter

Run the starter using maven:

```
mvn compile exec:java
```
6 changes: 6 additions & 0 deletions sk-java-hello-world/example.conf.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
client.openai.key=""
client.openai.organizationid=""

client.azureopenai.key=""
client.azureopenai.endpoint=""
client.azureopenai.deploymentname=""
57 changes: 57 additions & 0 deletions sk-java-hello-world/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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</groupId>
<artifactId>sk-java-hello-world</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-bom</artifactId>
<version>0.2.11-alpha</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.microsoft.semantickernel.helloworld.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
</properties>

<dependencies>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-api</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-connectors-ai-openai</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-core</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-settings-loader</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.microsoft.semantickernel.helloworld;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
import reactor.core.publisher.Mono;

public class Main {
public static void main(String[] args) throws ConfigurationException {
// Configure OpenAI client. Load settings from conf.properties
OpenAIAsyncClient client = OpenAIClientProvider.getClient();

// Build Kernel with Text Completion service
Kernel kernel = SKBuilders.kernel()
.withDefaultAIService(SKBuilders.textCompletion()
.withOpenAIClient(client)
.withModelId("text-davinci-003")
.build())
.build();

// Import semantic skill
kernel.importSkillFromResources("skills", "FunSkill", "Joke");

CompletionSKFunction joke = (CompletionSKFunction) kernel.getFunction("FunSkill", "Joke");
// Set input variable for Joke function
SKContext context = SKBuilders.context().build()
.setVariable("input", "Time travel to dinosaur age")
.setVariable("style", "Wacky");

// Invoke function and get result
Mono<SKContext> result = joke.invokeAsync(context);

System.out.println(result.block().getResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"schema": 1,
"description": "Turn a scenario into a creative or humorous excuse to send your boss",
"type": "completion",
"completion": {
"max_tokens": 60,
"temperature": 0.5,
"top_p": 0,
"presence_penalty": 0,
"frequency_penalty": 0
},
"input": {
"parameters": [
{
"name": "input",
"description": "The event to generate an excuse for",
"defaultValue": ""
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Generate a creative reason or excuse for the given event. Be creative and be funny. Let your imagination run wild.

Event:I am running late.
Excuse:I was being held ransom by giraffe gangsters.

Event:{{$input}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"schema": 1,
"description": "Generate a funny joke",
"type": "completion",
"completion": {
"max_tokens": 1000,
"temperature": 0.9,
"top_p": 0,
"presence_penalty": 0,
"frequency_penalty": 0
},
"input": {
"parameters": [
{
"name": "input",
"description": "The subject of the joke",
"defaultValue": ""
},
{
"name": "style",
"description": "The style of the joke",
"defaultValue": ""
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW

JOKE MUST BE:
- G RATED
- WORKPLACE/FAMILY SAFE
NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY

BE CREATIVE AND FUNNY. I WANT TO LAUGH.
+++++
STYLE:
{{$style}}
+++++
TOPIC:
{{$input}}
+++++
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"schema": 1,
"description": "Generate a funny limerick about a person",
"type": "completion",
"completion": {
"max_tokens": 100,
"temperature": 0.7,
"top_p": 0,
"presence_penalty": 0,
"frequency_penalty": 0
},
"input": {
"parameters": [
{
"name": "name",
"description": "The name of the person who the Limerick is about",
"defaultValue": ""
},
{
"name": "input",
"description": "The theme of the Limerick",
"defaultValue": ""
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
There was a young woman named Bright,
Whose speed was much faster than light.
She set out one day,
In a relative way,
And returned on the previous night.

There was an odd fellow named Gus,
When traveling he made such a fuss.
He was banned from the train,
Not allowed on a plane,
And now travels only by bus.

There once was a man from Tibet,
Who couldn't find a cigarette
So he smoked all his socks,
and got chicken-pox,
and had to go to the vet.

There once was a boy named Dan,
who wanted to fry in a pan.
He tried and he tried,
and eventually died,
that weird little boy named Dan.

Now write a very funny limerick about {{$name}}.
{{$input}}
Invent new facts about their life. Must be funny.

0 comments on commit 4746653

Please sign in to comment.