Files
semantic-kernel/java/samples/sample-code
David Grieve bcf87d1afb Java: Move sk-v1-proposal java source to semantic-kernel (#4008)
### TODO
- [x] Update pom files. This may include disabling the build of existing
modules.
- [x] Get a clean compile. `./mvnw clean install -Prelease,with-samples
-Dmaven.test.skip=true` works
- [x] Where does the assistants package belong? ~~For now, I just put it
in with 06-Assistants~~ Resolution: move to
/plugins/semantickernel-plugin-core
- [x] I'm not sure `aiservices-*` belong under `extensions` Resolution:
make an aiservices module and put them there
- [x] I'm not sure the bing plugin belongs under `plugins\web`
Resolution: yeah, that works for now

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
We were working from two repos. This will make Java v1 development
easier for all.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Took compiling code from sk-v1-proposal/java and moved it to
semantic-kernel/java.
Modified pom files as needed. 
Modifications to code as needed to get it to compile. 

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
2023-12-08 10:25:34 -05:00
..

Java Samples

TL;DR

Run with:

# AZURE:
OPENAI_CLIENT_TYPE=AZURE_OPEN_AI \
AZURE_OPEN_AI_KEY="my-key" \
AZURE_OPEN_AI_ENDPOINT="endpoint url" \
../../mvnw clean package exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

# OPENAI:
OPENAI_CLIENT_TYPE=OPENAI \
OPEN_AI_KEY="my-key" \
OPEN_AI_ORGANIZATION_ID="organisation id" \
../../mvnw clean package exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

Compile

These samples can be compiled via:

 ../../mvnw clean package

They can then be run by:

../../mvnw exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

Configuration

By default, the samples will use the Open AI client, but you can also use the Azure Open AI client.

Open AI client type

You can define the provider of Open AI (openai.com or Azure), this can be done by setting the OPENAI_CLIENT_TYPE property or environment variable to either OPENAI or AZURE_OPEN_AI. By default, the samples will use the Open AI client.

OPENAI_CLIENT_TYPE=OPEN_AI ../../mvnw exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

OR

 ../../mvnw exec:java -DOPENAI_CLIENT_TYPE=AZURE_OPEN_AI -Dsample=Example04_CombineLLMPromptsAndNativeCode

Client Settings

The samples search for the client settings in the following order:

  1. Properties file whose location is defined by the CONF_PROPERTIES property or environment variable.
  2. System properties defined on the command line.
  3. Environment variables.
  4. Properties file at java/samples/conf.properties.
  5. Properties file at ~/.sk/conf.properties.

Properties File

You can set the location of a properties file, by setting the CONF_PROPERTIES property or environment variable, ie:

CONF_PROPERTIES=my.properties \
OPENAI_CLIENT_TYPE=OPEN_AI \
../../mvnw exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

OR

../../mvnw exec:java \
-DCONF_PROPERTIES=my.properties \
-DOPENAI_CLIENT_TYPE=AZURE_OPEN_AI \
-Dsample=Example04_CombineLLMPromptsAndNativeCode

A properties file looks as follows:

# If using openai.com
client.openai.key:"my-key"
client.openai.organizationid:"my-org-id"
# if using Azure Open AI
client.azureopenai.key:"my-key"
client.azureopenai.endpoint:"url of azure openai endpoint"
client.azureopenai.deploymentname:"deployment name"

System Properties

As an alternative to providing the key/endpoint properties via a file, you can set them directly via system properties, ie:

# OpenAI
../../mvnw exec:java \
-DOPENAI_CLIENT_TYPE=AZURE_OPEN_AI \
-Dclient.openai.key="my-key" \
-Dclient.openai.organizationid="my-org-id" \
-Dsample=Example04_CombineLLMPromptsAndNativeCode

# Azure
../../mvnw exec:java \
-DOPENAI_CLIENT_TYPE=AZURE_OPEN_AI \
-Dclient.azureopenai.key="my-key" \
-Dclient.azureopenai.endpoint="url of azure openai endpoint" \
-Dsample=Example04_CombineLLMPromptsAndNativeCode

Environment variables

Alternative to properties, you can set environment variables as follows:

# AZURE:
OPENAI_CLIENT_TYPE=AZURE_OPEN_AI \
AZURE_OPEN_AI_KEY="my-key" \
AZURE_OPEN_AI_ENDPOINT="endpoint url" \
../../mvnw clean package exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

# OPENAI:
OPENAI_CLIENT_TYPE=OPEN_AI \
OPEN_AI_KEY="my-key" \
OPEN_AI_ORGANIZATION_ID="organisation id" \
../../mvnw clean package exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode

Managed Identity and Azure CLI Credentials (Azure only)

The credentialtype setting (i.e client.openai.credentialtype):

  • managed_identity: will attempt to obtain credentials via Azure's managed identity.
  • azure_cli: will attempt to obtain credentials via Azure's CLI, to use this you must have logged in to the az client for instance by running az login.

To use either of these settings, the azure-identity dependency must be available to the application. Additionally, you must have the "Cognitive Services User" role assigned to your identity.