Skip to content

Commit d20a0a1

Browse files
authored
Merge pull request #2801 from KarlErickson/karler-danvega
Updating AI Completion Quick Start based on the updated demo code
2 parents 5244210 + 1ae5e36 commit d20a0a1

File tree

1 file changed

+55
-60
lines changed
  • articles/ai-services/openai/includes

1 file changed

+55
-60
lines changed

articles/ai-services/openai/includes/spring.md

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: mbullwin
88
ms.date: 11/27/2023
99
---
1010

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-helloworld)
11+
[Source code](https://github.com/spring-projects/spring-ai) | [Artifacts (Maven)](https://repo.spring.io/ui/native/snapshot/org/springframework/experimental/ai/spring-ai-openai-spring-boot-starter/1.0.0-SNAPSHOT/) | [Sample](https://github.com/Azure-Samples/spring-ai-samples/tree/main/ai-completion-demo)
1212

1313
## Prerequisites
1414

@@ -17,7 +17,6 @@ ms.date: 11/27/2023
1717
- The [Spring Boot CLI tool](https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing.cli)
1818
- 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).
1919

20-
2120
## Set up
2221

2322
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
@@ -59,10 +58,10 @@ ai-completion-demo/
5958
| |-- AiCompletionApplication.java
6059
|-- test/
6160
|-- java/
62-
|-- com/
63-
|-- example/
64-
|-- aicompletiondemo/
65-
|-- AiCompletionApplicationTests.java
61+
|-- com/
62+
|-- example/
63+
|-- aicompletiondemo/
64+
|-- AiCompletionApplicationTests.java
6665
```
6766

6867
## Edit the Spring application
@@ -74,12 +73,12 @@ ai-completion-demo/
7473
```xml
7574
<?xml version="1.0" encoding="UTF-8"?>
7675
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
77-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
76+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
7877
<modelVersion>4.0.0</modelVersion>
7978
<parent>
8079
<groupId>org.springframework.boot</groupId>
8180
<artifactId>spring-boot-starter-parent</artifactId>
82-
<version>3.2.0</version>
81+
<version>3.3.4</version>
8382
<relativePath/> <!-- lookup parent from repository -->
8483
</parent>
8584
<groupId>com.example</groupId>
@@ -89,23 +88,36 @@ ai-completion-demo/
8988
<description>Demo project for Spring Boot</description>
9089
<properties>
9190
<java.version>17</java.version>
91+
<spring-ai.version>1.0.0-M5</spring-ai.version>
9292
</properties>
9393
<dependencies>
9494
<dependency>
9595
<groupId>org.springframework.boot</groupId>
9696
<artifactId>spring-boot-starter</artifactId>
9797
</dependency>
9898
<dependency>
99-
<groupId>org.springframework.experimental.ai</groupId>
99+
<groupId>org.springframework.ai</groupId>
100100
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
101-
<version>0.7.0-SNAPSHOT</version>
102101
</dependency>
103102
<dependency>
104103
<groupId>org.springframework.boot</groupId>
105104
<artifactId>spring-boot-starter-test</artifactId>
106105
<scope>test</scope>
107106
</dependency>
108107
</dependencies>
108+
109+
<dependencyManagement>
110+
<dependencies>
111+
<dependency>
112+
<groupId>org.springframework.ai</groupId>
113+
<artifactId>spring-ai-bom</artifactId>
114+
<version>${spring-ai.version}</version>
115+
<type>pom</type>
116+
<scope>import</scope>
117+
</dependency>
118+
</dependencies>
119+
</dependencyManagement>
120+
109121
<build>
110122
<plugins>
111123
<plugin>
@@ -116,12 +128,12 @@ ai-completion-demo/
116128
</build>
117129
<repositories>
118130
<repository>
119-
<id>spring-snapshots</id>
120-
<name>Spring Snapshots</name>
121-
<url>https://repo.spring.io/snapshot</url>
122-
<releases>
131+
<id>spring-milestones</id>
132+
<name>Spring Milestones</name>
133+
<url>https://repo.spring.io/milestone</url>
134+
<snapshots>
123135
<enabled>false</enabled>
124-
</releases>
136+
</snapshots>
125137
</repository>
126138
</repositories>
127139
</project>
@@ -132,51 +144,35 @@ ai-completion-demo/
132144
```java
133145
package com.example.aicompletiondemo;
134146

135-
import java.util.Collections;
136-
import java.util.List;
137-
138-
import org.springframework.ai.client.AiClient;
139-
import org.springframework.ai.prompt.Prompt;
140-
import org.springframework.ai.prompt.messages.Message;
141-
import org.springframework.ai.prompt.messages.MessageType;
142-
import org.springframework.ai.prompt.messages.UserMessage;
143-
import org.springframework.beans.factory.annotation.Autowired;
147+
import org.slf4j.Logger;
148+
import org.slf4j.LoggerFactory;
149+
import org.springframework.ai.chat.client.ChatClient;
144150
import org.springframework.boot.CommandLineRunner;
145151
import org.springframework.boot.SpringApplication;
146152
import org.springframework.boot.autoconfigure.SpringBootApplication;
153+
import org.springframework.context.annotation.Bean;
147154

148155
@SpringBootApplication
149-
public class AiCompletionApplication implements CommandLineRunner
150-
{
151-
private static final String ROLE_INFO_KEY = "role";
156+
public class AiCompletionApplication {
152157

153-
@Autowired
154-
private AiClient aiClient;
158+
private static final Logger log = LoggerFactory.getLogger(AiCompletionApplication.class);
155159

156160
public static void main(String[] args) {
157161
SpringApplication.run(AiCompletionApplication.class, args);
158162
}
159163

160-
@Override
161-
public void run(String... args) throws Exception
162-
{
163-
System.out.println(String.format("Sending completion prompt to AI service. One moment please...\r\n"));
164-
165-
final List<Message> msgs =
166-
Collections.singletonList(new UserMessage("When was Microsoft founded?"));
167-
168-
final var resps = aiClient.generate(new Prompt(msgs));
169-
170-
System.out.println(String.format("Prompt created %d generated response(s).", resps.getGenerations().size()));
171-
172-
resps.getGenerations().stream()
173-
.forEach(gen -> {
174-
final var role = gen.getInfo().getOrDefault(ROLE_INFO_KEY, MessageType.ASSISTANT.getValue());
175-
176-
System.out.println(String.format("Generated respose from \"%s\": %s", role, gen.getText()));
177-
});
164+
@Bean
165+
CommandLineRunner commandLineRunner(ChatClient.Builder builder) {
166+
return args -> {
167+
var chatClient = builder.build();
168+
log.info("Sending completion prompt to AI service. One moment please...");
169+
var response = chatClient.prompt()
170+
.user("When was Microsoft founded?")
171+
.call()
172+
.content();
173+
log.info("Response: {}", response);
174+
};
178175
}
179-
180176
}
181177
```
182178

@@ -192,23 +188,22 @@ ai-completion-demo/
192188
## Output
193189

194190
```output
195-
. ____ _ __ _ _
196-
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
191+
. ____ _ __ _ _
192+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
197193
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
198-
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
199-
' |____| .__|_| |_|_| |_\__, | / / / /
200-
=========|_|==============|___/=/_/_/_/
201-
:: Spring Boot :: (v3.1.5)
194+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
195+
' |____| .__|_| |_|_| |_\__, | / / / /
196+
=========|_|==============|___/=/_/_/_/
202197
203-
2023-11-07T12:47:46.126-06:00 INFO 98687 --- [ main] c.e.a.AiCompletionApplication : No active profile set, falling back to 1 default profile: "default"
204-
2023-11-07T12:47:46.823-06:00 INFO 98687 --- [ main] c.e.a.AiCompletionApplication : Started AiCompletionApplication in 0.925 seconds (process running for 1.238)
205-
Sending completion prompt to AI service. One moment please...
198+
:: Spring Boot :: (v3.3.4)
206199
207-
Prompt created 1 generated response(s).
208-
Generated respose from "assistant": Microsoft was founded on April 4, 1975.
200+
2025-01-09T13:51:48.426-05:00 INFO 8264 --- [AICompletion] [ main] c.e.a.AiCompletionApplication : Starting AiCompletionApplication using Java 17.0.12 with PID 8264 (/Users/vega/dev/msft/spring-ai-samples/ai-completion-demo/target/classes started by vega in /Users/vega/dev/msft/spring-ai-samples/ai-completion-demo)
201+
2025-01-09T13:51:48.427-05:00 INFO 8264 --- [AICompletion] [ main] c.e.a.AiCompletionApplication : No active profile set, falling back to 1 default profile: "default"
202+
2025-01-09T13:51:48.781-05:00 INFO 8264 --- [AICompletion] [ main] c.e.a.AiCompletionApplication : Started AiCompletionApplication in 0.465 seconds (process running for 0.624)
203+
2025-01-09T13:51:48.782-05:00 INFO 8264 --- [AICompletion] [ main] c.e.a.AiCompletionApplication : Sending completion prompt to AI service. One moment please...
204+
2025-01-09T13:51:50.447-05:00 INFO 8264 --- [AICompletion] [ main] c.e.a.AiCompletionApplication : Response: Microsoft was founded on April 4, 1975, by Bill Gates and Paul Allen.
209205
```
210206

211-
212207
## Clean up resources
213208

214209
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.

0 commit comments

Comments
 (0)