Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating AI Completion Quick Start based on the updated demo code #165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 131 additions & 133 deletions articles/ai-services/openai/includes/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ms.author: mbullwin
ms.date: 11/27/2023
---

[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)
[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)

## Prerequisites

Expand Down Expand Up @@ -43,26 +43,26 @@ spring init -a ai-completion-demo -n AICompletion --force --build maven -x
The generated files and folders resemble the following structure:

```
ai-completion-demo/
|-- pom.xml
|-- mvn
|-- mvn.cmd
|-- HELP.md
|-- src/
|-- main/
| |-- resources/
| | |-- application.properties
| |-- java/
| |-- com/
| |-- example/
| |-- aicompletiondemo/
| |-- AiCompletionApplication.java
|-- test/
|-- java/
|-- com/
|-- example/
|-- aicompletiondemo/
|-- AiCompletionApplicationTests.java
ai-completion-demo/
|-- pom.xml
|-- mvn
|-- mvn.cmd
|-- HELP.md
|-- src/
|-- main/
| |-- resources/
| | |-- application.properties
| |-- java/
| |-- com/
| |-- example/
| |-- aicompletiondemo/
| |-- AiCompletionApplication.java
|-- test/
|-- java/
|-- com/
|-- example/
|-- aicompletiondemo/
|-- AiCompletionApplicationTests.java
```

## Edit the Spring application
Expand All @@ -72,112 +72,110 @@ ai-completion-demo/
From the root of the project directory, open the *pom.xml* file in your preferred editor or IDE and overwrite the file with following content:

```xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ai-completion-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AICompletion</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ai-completion-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AICompletion</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M5</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
```

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:

```java
package com.example.aicompletiondemo;

import java.util.Collections;
import java.util.List;

import org.springframework.ai.client.AiClient;
import org.springframework.ai.prompt.Prompt;
import org.springframework.ai.prompt.messages.Message;
import org.springframework.ai.prompt.messages.MessageType;
import org.springframework.ai.prompt.messages.UserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AiCompletionApplication implements CommandLineRunner
{
private static final String ROLE_INFO_KEY = "role";

@Autowired
private AiClient aiClient;

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

@Override
public void run(String... args) throws Exception
{
System.out.println(String.format("Sending completion prompt to AI service. One moment please...\r\n"));

final List<Message> msgs =
Collections.singletonList(new UserMessage("When was Microsoft founded?"));

final var resps = aiClient.generate(new Prompt(msgs));

System.out.println(String.format("Prompt created %d generated response(s).", resps.getGenerations().size()));

resps.getGenerations().stream()
.forEach(gen -> {
final var role = gen.getInfo().getOrDefault(ROLE_INFO_KEY, MessageType.ASSISTANT.getValue());

System.out.println(String.format("Generated respose from \"%s\": %s", role, gen.getText()));
});
}

}
package com.example.aicompletiondemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class AiCompletionApplication {

private static final Logger log = LoggerFactory.getLogger(AiCompletionApplication.class);

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

@Bean
CommandLineRunner commandLineRunner(ChatClient.Builder builder) {
return args -> {
var chatClient = builder.build();
log.info("Sending completion prompt to AI service. One moment please...");
var response = chatClient.prompt()
.user("When was Microsoft founded?")
.call()
.content();

log.info("Response: {}", response);
};
}
}
```

> [!IMPORTANT]
Expand All @@ -192,20 +190,20 @@ ai-completion-demo/
## Output

```output
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.5)
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/

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"
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)
Sending completion prompt to AI service. One moment please...
:: Spring Boot :: (v3.3.4)

Prompt created 1 generated response(s).
Generated respose from "assistant": Microsoft was founded on April 4, 1975.
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)
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"
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)
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...
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.
```


Expand Down