Key Concepts
1. OpenAiClient
OpenAiClient
interacts with OpenAI's API, providing methods to send requests and receive responses from OpenAI models. This client is fundamental for accessing various AI functionalities like text generation, image creation, and more.
2. ChatClient
The ChatClient
abstraction allows for interaction with different types of large language models (LLMs) without directly coupling with the model. It offers a standardized interface for sending and receiving messages, supporting synchronous and streaming responses.
3. ImageClient
ImageClient
enables generating, manipulating, and analyzing images using AI models. It simplifies the integration of image-related AI capabilities into applications.
4. SpeechClient
SpeechClient
provides tools for generating and analyzing speech, facilitating text-to-speech and other speech-related tasks using AI models.
5. OutputParsers
OutputParsers
are used to process and interpret AI model responses, converting raw outputs into structured data formats such as Java beans, maps, and lists. Examples include BeanOutputParser
, MapOutputParser
, and ListOutputParser
.
6. PromptTemplate
The PromptTemplate
class helps in creating structured prompts by using placeholders that can be dynamically filled with user-specific data, ensuring consistent and flexible interactions with AI models.
7. Embeddings
Embeddings transform text into numerical arrays or vectors, allowing AI models to process and understand language data. They are crucial for tasks like text classification, semantic search, and product recommendations.
Setting Up Spring AI
Step 1: Create a New Spring Boot Project
Use Spring Initializr to create a new Spring Boot project with dependencies for Spring Web and Spring AI.
Step 2: Add Spring AI Dependency
Add the spring-ai-openai-spring-boot-starter
dependency to your pom.xml
:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
Step 3: Configure API Key
Add your OpenAI API key to application.properties
or application.yml
:
openai.api.key=your_openai_api_key
Step 4: Create Configuration Class
Set up the OpenAI client in a configuration class:
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.openai.OpenAiClient;
@Configuration
public class OpenAiConfig {
@Bean
public OpenAiClient openAiClient() {
return new OpenAiClient();
}
}
Implementing AI Features
Example 1: Text Generation
Service:
package com.example.demo.service;
import org.springframework.ai.openai.OpenAiClient;
import org.springframework.ai.openai.model.CompletionRequest;
import org.springframework.ai.openai.model.CompletionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TextGenerationService {
@Autowired
private OpenAiClient openAiClient;
public String generateText(String prompt) {
CompletionRequest request = new CompletionRequest();
request.setPrompt(prompt);
request.setMaxTokens(150);
CompletionResponse response = openAiClient.createCompletion(request);
return response.getChoices().get(0).getText();
}
}
Controller:
package com.example.demo.controller;
import com.example.demo.service.TextGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TextGenerationController {
@Autowired
private TextGenerationService textGenerationService;
@GetMapping("/generateText")
public String generateText(@RequestParam String prompt) {
return textGenerationService.generateText(prompt);
}
}
Example 2: Image Generation
Service:
package com.example.demo.service;
import org.springframework.ai.openai.ImageClient;
import org.springframework.ai.openai.model.ImageRequest;
import org.springframework.ai.openai.model.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ImageGenerationService {
@Autowired
private ImageClient imageClient;
public String generateImage(String prompt) {
ImageRequest request = new ImageRequest();
request.setPrompt(prompt);
request.setSize("1024x1024");
ImageResponse response = imageClient.generateImage(request);
return response.getImageUrl();
}
}
Controller:
package com.example.demo.controller;
import com.example.demo.service.ImageGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ImageGenerationController {
@Autowired
private ImageGenerationService imageGenerationService;
@GetMapping("/generateImage")
public String generateImage(@RequestParam String prompt) {
return imageGenerationService.generateImage(prompt);
}
}
Example 3: Speech Generation
Service:
package com.example.demo.service;
import org.springframework.ai.openai.SpeechClient;
import org.springframework.ai.openai.model.SpeechRequest;
import org.springframework.ai.openai.model.SpeechResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SpeechGenerationService {
@Autowired
private SpeechClient speechClient;
public byte[] generateSpeech(String text) {
SpeechRequest request = new SpeechRequest();
request.setText(text);
SpeechResponse response = speechClient.generateSpeech(request);
return response.getAudioData();
}
}
Controller:
package com.example.demo.controller;
import com.example.demo.service.SpeechGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
@RestController
public class SpeechGenerationController {
@Autowired
private SpeechGenerationService speechGenerationService;
@GetMapping("/generateSpeech")
public void generateSpeech(@RequestParam String text, HttpServletResponse response) throws IOException {
byte[] audioData = speechGenerationService.generateSpeech(text);
response.setContentType("audio/mpeg");
response.setContentLength(audioData.length);
OutputStream os = response.getOutputStream();
os.write(audioData);
os.flush();
os.close();
}
}
Conclusion
Spring AI provides a comprehensive framework for integrating AI capabilities into Spring Boot applications. By using abstractions like OpenAiClient
, ChatClient
, ImageClient
, and SpeechClient
, along with tools like OutputParsers
and PromptTemplate
, developers can easily incorporate advanced AI functionalities into their projects. This setup not only simplifies the integration process but also ensures that applications are scalable and maintainable.
For more detailed information, you can refer to the Spring AI documentation.
Comments
Post a Comment