Spring AI Integration with OpenAI

Cem Dırman
4 min readJul 19, 2024

--

Generated by DALL-E
Generated by DALL-E

Spring AI applies the portability and modular design principles of the Spring ecosystem to the AI domain while developing AI applications. It helps in developing AI applications using Java objects.

Key Features and Capabilities

  1. Portable API Support:
  • Model Types: Chat, text-to-image, embedding.
  • API Options: Synchronous and streaming.
  • Model Providers: OpenAI, Microsoft, Amazon, Google, Hugging Face, Anthropic, AI21 Labs, Meta, Azure Open AI, PaLM2, Gemini, Groq, MistralAI, MiniMax, Moonshot AI, Ollama, QianFan, ZhiPu AI, Watsonx.AI.

2. Vector Store API:

  • Databases: Azure AI Service, Apache Cassandra, Chroma, Elasticsearch, GemFire, Milvus, MongoDB Atlas, Neo4j, OpenSearch, Oracle, PGvector, Pinecone, Qdrant, Redis, SAP Hana, Typesense, Weaviate.
  • SQL-like Metadata Filtering API.

3. Spring Boot Integration:

  • Automatic Configuration and Starters for AI models and vector stores.

4. ETL Framework:

  • Document Transfer and Data Engineering.
  • Reading documents in various formats (PDF, JSON, etc.).
  • Data manipulation and storage in the vector database.

Supported Models and Providers

  • Chat Models:
  • Amazon Bedrock, Anthropic, Cohere’s Command, AI21 Labs’ Jurassic-2, Meta’s LLama, Amazon’s Titan, Anthropic Claud, Azure Open AI, Google Vertex AI, PaLM2, Gemini, Groq, HuggingFace, MistralAI, MiniMax, Moonshot AI, Ollama, OpenAI, QianFan, ZhiPu AI, Watsonx.AI.
  • Text-to-Image Models:
  • OpenAI (DALL-E), StabilityAI.
  • Embedding Models:
  • Azure OpenAI, Amazon Bedrock, Cohere, Titan, Mistral AI, MiniMax, Ollama, (ONNX) Transformers, OpenAI, PostgresML, QianFan, VertexAI, ZhiPu AI.

What to Consider When Writing Prompts

  1. Clarity and Precision
  • Avoid Ambiguity: Clearly state what you want. Avoid vague statements.
  • Clear Questions: Ask questions in a clear and direct manner.

2. Context and Details

  • Provide Context: Give the context of your question or request. This helps in receiving better and contextually accurate responses.
  • Specifications and Criteria: If the response needs to be formatted in a specific way or meet certain criteria, state this clearly.

3. Examples and Expectations

  • Provide Examples: If possible, give examples of what kind of response you are expecting.
  • State Expectations: Specify your expectations regarding the length, detail, or format of the response.

4. State the Subject and Purpose

  • Subject Title: Provide information about the subject so the model can better utilize its knowledge on the topic.
  • Purpose: Specify the purpose of the response. For example, if you are writing an article or need a code example, mention this.

Examples

  • Code Example Request:
  • Bad Prompt: “Give me Java code.”
  • Good Prompt: “I want to write a Java program. Can you write a function that takes a number from the user and calculates its factorial? Please provide the code with explanations.”
  • Article Writing:
  • Bad Prompt: “Write me an article.”
  • Good Prompt: “Can you write an article about AI applications in the healthcare sector? Focus particularly on the role of AI in disease diagnosis and treatment. The article should be around 1000 words and include recent research findings.”
  • Information Request:
  • Bad Prompt: “What is a vector database?”
  • Good Prompt: “Can you explain what vector databases are and how they work? Please focus on their usage in machine learning and AI applications.”

Other Important Points to Consider

  • Be Polite and Respectful: Always use polite and respectful language.
  • Request More Information If Needed: If your question requires more information, ask for it or share your existing knowledge.
  • Avoid Repetitions: Avoid repeating the same information and cluttering the question with unnecessary details.

Let’s Code

We will write a web application using Spring AI. I want to create an order and have it recommend products based on the order history. Additionally, I want it to create an image showing what kind of developer I resemble based on the products I purchase.


<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Recommendation Service

import com.bilisimio.aidemo.order.Order;
import com.bilisimio.aidemo.order.OrderRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class RecommendationService {

private final OrderRepository orderRepository;

private final OpenAiChatModel openAiChatModel;

public String recommend() {

List<Order> orders = orderRepository.findAll();

String orderHistory = orders.stream()
.map(Order::getProductName)
.collect(Collectors.joining(", "));

String prompt = "I'm a software engineer. Based on my following order history: " + orderHistory
+ ", can you suggest 10 new products that I might like and explain why you recommend each of them? "
+ "Please consider my preferences and previous purchases. Reply in Turkish";

String response = openAiChatModel.call(prompt);
log.info(response);
return response;
}

}
spring.ai.openai.api-key=#your-openai-api-key

The key here is to input the correct prompt. To achieve this, you should consider the above points.

After preparing the prompt, all you need to do is call the method. In return, you will receive a response similar to the one below.

Answer

Elbette, satın alma geçmişinize bakarak, aşağıdaki ürünlerin ilginizi çekebileceğini düşünüyorum. İşte önerilerim:

1. **Apple Magic Keyboard**: MacBook Pro 13 M2 Pro ile uyumlu ve rahat yazı yazma deneyimi sunar. Ayrıca, taşınabilirliği ile de ofis dışında çalışırken işinizi kolaylaştırır.

2. **Apple Magic Trackpad 2**: MacBook Pro'nuz ile mükemmel uyum sağlar ve trackpad kullanmayı sevenler için geniş bir yüzey sunar. Logitech MX Master 3S'in yanında kullanmak hoşunuza gidebilir.

3. **Dell UltraSharp U2720Q 27" 4K USB-C Monitör**: MacBook Pro’nuzun ekranını genişletmek için ideal. 4K çözünürlük ve USB-C bağlantısı ile yüksek kaliteli görüntü ve kolay bağlantı sağlar.

Image Service

@Service
@RequiredArgsConstructor
@Slf4j
public class ImageGenerationService {

private final OpenAiImageModel openAiImageModel;
private final OrderRepository orderRepository;

public String generate() {

List<Order> orders = orderRepository.findAll();

String orderHistory = orders.stream().map(Order::getProductName).collect(Collectors.joining(", "));

String promptWhoAmI = "I'm a software engineer.";
String promptWhatIWear = "and i wear always basic t-shirt and sunglasses or eyeglasses.";
String promptWhatIUse = " Based on my following order history: " + orderHistory;
String promptWhatIWant = " Can you draw me";

ImagePrompt imagePrompt = new ImagePrompt(List.of(new ImageMessage(promptWhoAmI),
new ImageMessage(promptWhatIWear),
new ImageMessage(promptWhatIUse),
new ImageMessage(promptWhatIWant)));

ImageResponse response = openAiImageModel.call(imagePrompt);

log.info(response.getMetadata().toString());

return response.getResult().getOutput().getUrl();
}
}
Generated by DALL-E

https://github.com/cemdrman/spring-ai-demo

I hope you find this helpful.

--

--