-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from milderhc/sk-java-hello-world
Add sk-java-hello-world
- Loading branch information
Showing
11 changed files
with
266 additions
and
0 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
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 | ||
``` |
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,6 @@ | ||
client.openai.key="" | ||
client.openai.organizationid="" | ||
|
||
client.azureopenai.key="" | ||
client.azureopenai.endpoint="" | ||
client.azureopenai.deploymentname="" |
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,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> |
39 changes: 39 additions & 0 deletions
39
sk-java-hello-world/src/main/java/com/microsoft/semantickernel/helloworld/Main.java
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,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()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
sk-java-hello-world/src/main/resources/skills/FunSkill/Excuses/config.json
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,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": "" | ||
} | ||
] | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sk-java-hello-world/src/main/resources/skills/FunSkill/Excuses/skprompt.txt
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,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}} |
26 changes: 26 additions & 0 deletions
26
sk-java-hello-world/src/main/resources/skills/FunSkill/Joke/config.json
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,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": "" | ||
} | ||
] | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
sk-java-hello-world/src/main/resources/skills/FunSkill/Joke/skprompt.txt
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,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}} | ||
+++++ |
26 changes: 26 additions & 0 deletions
26
sk-java-hello-world/src/main/resources/skills/FunSkill/Limerick/config.json
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,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": "" | ||
} | ||
] | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sk-java-hello-world/src/main/resources/skills/FunSkill/Limerick/skprompt.txt
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,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. |