|
| 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) |
0 commit comments