Skip to content

Commit e46e549

Browse files
committed
Updating AI Completion Quick Start based on the updated demo code
1 parent d1f63fc commit e46e549

File tree

1 file changed

+107
-109
lines changed
  • articles/ai-services/openai/includes

1 file changed

+107
-109
lines changed

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

Lines changed: 107 additions & 109 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

@@ -73,112 +73,110 @@ ai-completion-demo/
7373

7474
```xml
7575
<?xml version="1.0" encoding="UTF-8"?>
76-
<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">
78-
<modelVersion>4.0.0</modelVersion>
79-
<parent>
80-
<groupId>org.springframework.boot</groupId>
81-
<artifactId>spring-boot-starter-parent</artifactId>
82-
<version>3.2.0</version>
83-
<relativePath/> <!-- lookup parent from repository -->
84-
</parent>
85-
<groupId>com.example</groupId>
86-
<artifactId>ai-completion-demo</artifactId>
87-
<version>0.0.1-SNAPSHOT</version>
88-
<name>AICompletion</name>
89-
<description>Demo project for Spring Boot</description>
90-
<properties>
91-
<java.version>17</java.version>
92-
</properties>
93-
<dependencies>
94-
<dependency>
95-
<groupId>org.springframework.boot</groupId>
96-
<artifactId>spring-boot-starter</artifactId>
97-
</dependency>
98-
<dependency>
99-
<groupId>org.springframework.experimental.ai</groupId>
100-
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
101-
<version>0.7.0-SNAPSHOT</version>
102-
</dependency>
103-
<dependency>
104-
<groupId>org.springframework.boot</groupId>
105-
<artifactId>spring-boot-starter-test</artifactId>
106-
<scope>test</scope>
107-
</dependency>
108-
</dependencies>
109-
<build>
110-
<plugins>
111-
<plugin>
112-
<groupId>org.springframework.boot</groupId>
113-
<artifactId>spring-boot-maven-plugin</artifactId>
114-
</plugin>
115-
</plugins>
116-
</build>
117-
<repositories>
118-
<repository>
119-
<id>spring-snapshots</id>
120-
<name>Spring Snapshots</name>
121-
<url>https://repo.spring.io/snapshot</url>
122-
<releases>
123-
<enabled>false</enabled>
124-
</releases>
125-
</repository>
126-
</repositories>
127-
</project>
76+
<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">
78+
<modelVersion>4.0.0</modelVersion>
79+
<parent>
80+
<groupId>org.springframework.boot</groupId>
81+
<artifactId>spring-boot-starter-parent</artifactId>
82+
<version>3.3.4</version>
83+
<relativePath/> <!-- lookup parent from repository -->
84+
</parent>
85+
<groupId>com.example</groupId>
86+
<artifactId>ai-completion-demo</artifactId>
87+
<version>0.0.1-SNAPSHOT</version>
88+
<name>AICompletion</name>
89+
<description>Demo project for Spring Boot</description>
90+
<properties>
91+
<java.version>17</java.version>
92+
<spring-ai.version>1.0.0-M5</spring-ai.version>
93+
</properties>
94+
<dependencies>
95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter</artifactId>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.springframework.ai</groupId>
101+
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-starter-test</artifactId>
106+
<scope>test</scope>
107+
</dependency>
108+
</dependencies>
109+
110+
<dependencyManagement>
111+
<dependencies>
112+
<dependency>
113+
<groupId>org.springframework.ai</groupId>
114+
<artifactId>spring-ai-bom</artifactId>
115+
<version>${spring-ai.version}</version>
116+
<type>pom</type>
117+
<scope>import</scope>
118+
</dependency>
119+
</dependencies>
120+
</dependencyManagement>
121+
122+
<build>
123+
<plugins>
124+
<plugin>
125+
<groupId>org.springframework.boot</groupId>
126+
<artifactId>spring-boot-maven-plugin</artifactId>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
<repositories>
131+
<repository>
132+
<id>spring-milestones</id>
133+
<name>Spring Milestones</name>
134+
<url>https://repo.spring.io/milestone</url>
135+
<snapshots>
136+
<enabled>false</enabled>
137+
</snapshots>
138+
</repository>
139+
</repositories>
140+
</project>
128141
```
129142

130143
1. From the *src/main/java/com/example/aicompletiondemo* folder, open *AiCompletionApplication.java* in your preferred editor or IDE and paste in the following code:
131144

132-
```java
133-
package com.example.aicompletiondemo;
134-
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;
144-
import org.springframework.boot.CommandLineRunner;
145-
import org.springframework.boot.SpringApplication;
146-
import org.springframework.boot.autoconfigure.SpringBootApplication;
147-
148-
@SpringBootApplication
149-
public class AiCompletionApplication implements CommandLineRunner
150-
{
151-
private static final String ROLE_INFO_KEY = "role";
152-
153-
@Autowired
154-
private AiClient aiClient;
155-
156-
public static void main(String[] args) {
157-
SpringApplication.run(AiCompletionApplication.class, args);
158-
}
159-
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-
});
178-
}
179-
180-
}
181-
```
145+
```java
146+
package com.example.aicompletiondemo;
147+
148+
import org.slf4j.Logger;
149+
import org.slf4j.LoggerFactory;
150+
import org.springframework.ai.chat.client.ChatClient;
151+
import org.springframework.boot.CommandLineRunner;
152+
import org.springframework.boot.SpringApplication;
153+
import org.springframework.boot.autoconfigure.SpringBootApplication;
154+
import org.springframework.context.annotation.Bean;
155+
156+
@SpringBootApplication
157+
public class AiCompletionApplication {
158+
159+
private static final Logger log = LoggerFactory.getLogger(AiCompletionApplication.class);
160+
161+
public static void main(String[] args) {
162+
SpringApplication.run(AiCompletionApplication.class, args);
163+
}
164+
165+
@Bean
166+
CommandLineRunner commandLineRunner(ChatClient.Builder builder) {
167+
return args -> {
168+
var chatClient = builder.build();
169+
log.info("Sending completion prompt to AI service. One moment please...");
170+
var response = chatClient.prompt()
171+
.user("When was Microsoft founded?")
172+
.call()
173+
.content();
174+
175+
log.info("Response: {}", response);
176+
};
177+
}
178+
}
179+
```
182180

183181
> [!IMPORTANT]
184182
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
@@ -198,14 +196,14 @@ ai-completion-demo/
198196
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
199197
' |____| .__|_| |_|_| |_\__, | / / / /
200198
=========|_|==============|___/=/_/_/_/
201-
:: Spring Boot :: (v3.1.5)
202199
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...
200+
:: Spring Boot :: (v3.3.4)
206201
207-
Prompt created 1 generated response(s).
208-
Generated respose from "assistant": Microsoft was founded on April 4, 1975.
202+
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)
203+
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"
204+
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)
205+
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...
206+
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.
209207
```
210208

211209

0 commit comments

Comments
 (0)