Skip to content

Commit 455b116

Browse files
authored
Merge pull request #259576 from KarlErickson/karler-gm2552
Adding AI quick start content utilizing Spring AI.
2 parents e0b015c + 1cf79ad commit 455b116

13 files changed

+969
-40
lines changed

articles/ai-services/openai/chatgpt-quickstart.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ Use this article to get started using Azure OpenAI.
3636

3737
::: zone-end
3838

39-
4039
::: zone pivot="programming-language-java"
4140

4241
[!INCLUDE [Java quickstart](includes/chatgpt-java.md)]
4342

4443
::: zone-end
4544

45+
::: zone pivot="programming-language-spring"
46+
47+
[!INCLUDE [Spring quickstart](includes/chatgpt-spring.md)]
48+
49+
::: zone-end
50+
4651
::: zone pivot="programming-language-javascript"
4752

4853
[!INCLUDE [JavaScript quickstart](includes/chatgpt-javascript.md)]

articles/ai-services/openai/includes/chat-go.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
---
22
title: 'Quickstart: Use Azure OpenAI Service with the JavaScript SDK and the completions API'
3-
titleSuffix: Azure OpenAI
4-
description: Walkthrough on how to get started with Azure OpenAI and make your first completions call with the Go SDK.
3+
description: Walkthrough on how to get started with Azure OpenAI and make your first completions call with the Go SDK.
54
#services: cognitive-services
65
manager: nitinme
76
ms.service: azure-ai-openai
87
ms.topic: include
98
author: mrbullwinkle
109
ms.author: mbullwin
1110
ms.date: 08/30/2023
12-
keywords:
1311
---
1412

1513
[Source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/ai/azopenai) | [Package (Go)](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai)| [Samples](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai#pkg-examples)
@@ -18,7 +16,7 @@ keywords:
1816

1917
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2018
- Access granted to the Azure OpenAI service in the desired Azure subscription.
21-
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true).
19+
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true).
2220
- [Go 1.21.0](https://go.dev/dl/) or higher installed locally.
2321
- An Azure OpenAI Service resource with the `gpt-35-turbo` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
2422

@@ -30,7 +28,7 @@ keywords:
3028

3129
## Create a sample application
3230

33-
Create a new file named chat_completions.go. Copy the following code into the chat_completions.go file.
31+
Create a new file named *chat_completions.go*. Copy the following code into the *chat_completions.go* file.
3432

3533
```go
3634
package main
@@ -122,9 +120,6 @@ Next run:
122120

123121
```cmd
124122
go mod tidy
125-
```
126-
127-
```cmd
128123
go run chat_completions.go
129124
```
130125

@@ -143,4 +138,4 @@ If you want to clean up and remove an Azure OpenAI resource, you can delete the
143138

144139
## Next steps
145140

146-
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)
141+
For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)

articles/ai-services/openai/includes/chatgpt-java.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ keywords:
3636

3737
Create a new Gradle project.
3838

39-
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
39+
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
4040

4141
```console
4242
mkdir myapp && cd myapp

articles/ai-services/openai/includes/chatgpt-powershell.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,20 @@ Create and assign persistent environment variables for your key and endpoint.
4545

4646
```powershell-interactive
4747
$Env:AZURE_OPENAI_KEY = 'YOUR_KEY_VALUE'
48-
```
49-
50-
```powershell-interactive
5148
$Env:AZURE_OPENAI_ENDPOINT = 'YOUR_ENDPOINT'
5249
```
5350

54-
# [Command Line](#tab/command-line)
51+
# [Command line](#tab/command-line)
5552

56-
```CMD
53+
```cmd
5754
setx AZURE_OPENAI_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
58-
```
59-
60-
```CMD
6155
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
6256
```
6357

6458
# [Bash](#tab/bash)
6559

66-
```Bash
60+
```bash
6761
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
68-
```
69-
70-
```Bash
7162
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
7263
```
7364

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
---
2+
services: cognitive-services
3+
manager: nitinme
4+
ms.service: azure-ai-openai
5+
ms.topic: include
6+
author: mrbullwinkle # external contributor: gm2552
7+
ms.author: mbullwin
8+
ms.date: 11/27/2023
9+
---
10+
11+
[Source code](https://github.com/spring-projects-experimental/spring-ai) | [Artifacts (Maven)](https://repo.spring.io/ui/native/snapshot/org/springframework/experimental/ai/spring-ai-openai-spring-boot-starter/0.7.0-SNAPSHOT) | [Sample](https://github.com/rd-1-2022/ai-azure-openai-prompt-roles)
12+
13+
## Prerequisites
14+
15+
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
16+
- Access granted to the Azure OpenAI service in the desired Azure subscription.
17+
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true).
18+
- The current version of the [Java Development Kit (JDK)](https://www.microsoft.com/openjdk)
19+
- The [Spring Boot CLI tool](https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing.cli)
20+
- An Azure OpenAI Service resource with the `gpt-35-turbo` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
21+
22+
> [!div class="nextstepaction"]
23+
> [I ran into an issue with the prerequisites.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=SPRING&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Prerequisites)
24+
25+
## Set up
26+
27+
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
28+
29+
[!INCLUDE [environment-variables](spring-environment-variables.md)]
30+
31+
> [!div class="nextstepaction"]
32+
> [I ran into an issue with the setup.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=SPRING&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Set-up-the-environment)
33+
34+
## Create a new Spring application
35+
36+
Create a new Spring project.
37+
38+
In a Bash window, create a new directory for your app, and navigate to it.
39+
40+
```bash
41+
mkdir ai-chat-demo && cd ai-chat-demo
42+
```
43+
44+
Run the `spring init` command from your working directory. This command creates a standard directory structure for your Spring project including the main Java class source file and the *pom.xml* file used for managing Maven based projects.
45+
46+
```bash
47+
spring init -a ai-chat-demo -n AIChat --force --build maven -x
48+
```
49+
50+
The generated files and folders resemble the following structure:
51+
52+
```
53+
ai-chat-demo/
54+
|-- pom.xml
55+
|-- mvn
56+
|-- mvn.cmd
57+
|-- HELP.md
58+
|-- src/
59+
|-- main/
60+
| |-- resources/
61+
| | |-- application.properties
62+
| |-- java/
63+
| |-- com/
64+
| |-- example/
65+
| |-- aichatdemo/
66+
| |-- AiChatApplication.java
67+
|-- test/
68+
|-- java/
69+
|-- com/
70+
|-- example/
71+
|-- aichatdemo/
72+
|-- AiChatApplicationTests.java
73+
```
74+
75+
## Edit Spring application
76+
77+
1. Edit the *pom.xml* file.
78+
79+
From the root of the project directory, open the *pom.xml* file in your preferred editor or IDE and overwrite the file with the following content:
80+
81+
```xml
82+
<?xml version="1.0" encoding="UTF-8"?>
83+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
84+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
85+
<modelVersion>4.0.0</modelVersion>
86+
<parent>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-parent</artifactId>
89+
<version>3.2.0</version>
90+
<relativePath/> <!-- lookup parent from repository -->
91+
</parent>
92+
<groupId>com.example</groupId>
93+
<artifactId>ai-chat-demo</artifactId>
94+
<version>0.0.1-SNAPSHOT</version>
95+
<name>AIChat</name>
96+
<description>Demo project for Spring Boot</description>
97+
<properties>
98+
<java.version>17</java.version>
99+
</properties>
100+
<dependencies>
101+
<dependency>
102+
<groupId>org.springframework.boot</groupId>
103+
<artifactId>spring-boot-starter</artifactId>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.springframework.experimental.ai</groupId>
107+
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
108+
<version>0.7.0-SNAPSHOT</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.springframework.boot</groupId>
112+
<artifactId>spring-boot-starter-test</artifactId>
113+
<scope>test</scope>
114+
</dependency>
115+
</dependencies>
116+
<build>
117+
<plugins>
118+
<plugin>
119+
<groupId>org.springframework.boot</groupId>
120+
<artifactId>spring-boot-maven-plugin</artifactId>
121+
</plugin>
122+
</plugins>
123+
</build>
124+
<repositories>
125+
<repository>
126+
<id>spring-snapshots</id>
127+
<name>Spring Snapshots</name>
128+
<url>https://repo.spring.io/snapshot</url>
129+
<releases>
130+
<enabled>false</enabled>
131+
</releases>
132+
</repository>
133+
</repositories>
134+
</project>
135+
```
136+
137+
1. From the *src/main/java/com/example/aichatdemo* folder, open *AiChatApplication.java* in your preferred editor or IDE and paste in the following code:
138+
139+
```java
140+
package com.example.aichatdemo;
141+
142+
import java.util.ArrayList;
143+
import java.util.List;
144+
145+
import org.springframework.ai.client.AiClient;
146+
import org.springframework.ai.prompt.Prompt;
147+
import org.springframework.ai.prompt.messages.ChatMessage;
148+
import org.springframework.ai.prompt.messages.Message;
149+
import org.springframework.ai.prompt.messages.MessageType;
150+
import org.springframework.beans.factory.annotation.Autowired;
151+
import org.springframework.boot.CommandLineRunner;
152+
import org.springframework.boot.SpringApplication;
153+
import org.springframework.boot.autoconfigure.SpringBootApplication;
154+
155+
@SpringBootApplication
156+
public class AiChatApplication implements CommandLineRunner
157+
{
158+
private static final String ROLE_INFO_KEY = "role";
159+
160+
@Autowired
161+
private AiClient aiClient;
162+
163+
public static void main(String[] args) {
164+
SpringApplication.run(AiChatApplication.class, args);
165+
}
166+
167+
@Override
168+
public void run(String... args) throws Exception
169+
{
170+
System.out.println(String.format("Sending chat prompts to AI service. One moment please...\r\n"));
171+
172+
final List<Message> msgs = new ArrayList<>();
173+
174+
msgs.add(new ChatMessage(MessageType.SYSTEM, "You are a helpful assistant"));
175+
msgs.add(new ChatMessage(MessageType.USER, "Does Azure OpenAI support customer managed keys?"));
176+
msgs.add(new ChatMessage(MessageType.ASSISTANT, "Yes, customer managed keys are supported by Azure OpenAI?"));
177+
msgs.add(new ChatMessage(MessageType.USER, "Do other Azure AI services support this too?"));
178+
179+
final var resps = aiClient.generate(new Prompt(msgs));
180+
181+
System.out.println(String.format("Prompt created %d generated response(s).", resps.getGenerations().size()));
182+
183+
resps.getGenerations().stream()
184+
.forEach(gen -> {
185+
final var role = gen.getInfo().getOrDefault(ROLE_INFO_KEY, MessageType.ASSISTANT.getValue());
186+
187+
System.out.println(String.format("Generated respose from \"%s\": %s", role, gen.getText()));
188+
});
189+
}
190+
191+
}
192+
```
193+
194+
> [!IMPORTANT]
195+
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
196+
197+
1. Navigate back to the project root folder, and run the app by using the following command:
198+
199+
```bash
200+
./mvnw spring-boot:run
201+
```
202+
203+
## Output
204+
205+
```output
206+
. ____ _ __ _ _
207+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
208+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
209+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
210+
' |____| .__|_| |_|_| |_\__, | / / / /
211+
=========|_|==============|___/=/_/_/_/
212+
:: Spring Boot :: (v3.1.5)
213+
214+
2023-11-07T13:31:10.884-06:00 INFO 6248 --- [ main] c.example.aichatdemo.AiChatApplication : No active profile set, falling back to 1 default profile: "default"
215+
2023-11-07T13:31:11.595-06:00 INFO 6248 --- [ main] c.example.aichatdemo.AiChatApplication : Started AiChatApplication in 0.994 seconds (process running for 1.28)
216+
Sending chat prompts to AI service. One moment please...
217+
218+
Prompt created 1 generated response(s).
219+
Generated respose from "assistant": Yes, other Azure AI services also support customer managed keys. Azure AI Services, Azure Machine Learning, and other AI services in Azure provide options for customers to manage and control their encryption keys. This allows customers to have greater control over their data and security.
220+
```
221+
222+
> [!div class="nextstepaction"]
223+
> [I ran into an issue when running the code sample.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=SPRING&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Create-application)
224+
225+
## Clean up resources
226+
227+
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models.
228+
229+
- [Portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources)
230+
- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources)
231+
232+
## Next steps
233+
234+
For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)

articles/ai-services/openai/includes/environment-variables.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,23 @@ Create and assign persistent environment variables for your key and endpoint.
1616

1717
# [Command Line](#tab/command-line)
1818

19-
```CMD
19+
```cmd
2020
setx AZURE_OPENAI_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
21-
```
22-
23-
```CMD
2421
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
2522
```
2623

2724
# [PowerShell](#tab/powershell)
2825

2926
```powershell
3027
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
31-
```
32-
33-
```powershell
3428
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
3529
```
3630

3731
# [Bash](#tab/bash)
3832

39-
```Bash
40-
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
33+
```bash
34+
export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE"
35+
export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE"
4136
```
4237

43-
```Bash
44-
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
45-
```
46-
---
38+
---

0 commit comments

Comments
 (0)