Generative AI - Open AI

Artificial Intelligence (AI) is transforming the way we build and interact with software, enabling the creation of intelligent applications that can understand, learn, and respond to human inputs in a natural way. One of the most exciting advancements in this field is Generative AI, which can produce new content based on input data, such as generating text, images, or even audio. Spring AI, a powerful framework within the Spring ecosystem, is designed to make it easier for developers to integrate AI capabilities into their applications.

Introduction to Spring AI

Spring AI aims to simplify the development of AI-driven applications by providing a set of abstractions that make it easy to work with various AI models and data stores. Inspired by Python projects like LangChain and LlamaIndex, Spring AI is not limited to any specific programming language, making AI functionalities accessible to a broader range of developers.

Key Features of Spring AI

Support for Major AI Models: Spring AI supports models from leading providers such as OpenAI, Microsoft, Amazon, Google, and Hugging Face. This includes models for chat, text-to-image generation, audio transcription, and text-to-speech.

Portable API: The framework offers a consistent API across different AI providers, allowing developers to switch models with minimal code changes. Both synchronous and asynchronous APIs are supported, providing flexibility in how AI models are integrated.

Vector Databases: Spring AI includes support for major vector databases like Apache Cassandra, Milvus, MongoDB Atlas, Neo4j, PostgreSQL, PineCone, Redis, and Weaviate. These databases are crucial for applications that require fast and efficient similarity searches, such as recommendation systems or semantic search engines.

Spring Boot Integration: With Spring Boot auto-configuration and starters for AI models and vector stores, setting up and configuring your AI-driven application becomes straightforward and hassle-free.

Core Concepts

Prompts and Prompt Engineering

Prompts are the inputs that guide AI models to produce specific outputs. Crafting effective prompts, known as “Prompt Engineering,” is essential for getting the desired results from AI models. Prompts can have different roles, such as system prompts that set the context and user prompts that provide the specific query or command.

Tokens

Tokens are the basic units of text that AI models use to process input and generate output. Understanding how tokens work is crucial because the cost of using hosted AI models is often based on the number of tokens processed. Additionally, models have token limits that define the maximum amount of text they can handle in a single request.

Getting Started

The TrekkingServiceImpl class is a service that provides a method to generate trekking plan ideas based on a specific country. The getTrekkingPlanIdea method creates a detailed prompt that specifies the desired JSON structure for the trekking data and sends it to the AI model. The generated result is then converted into a list of TrekkingDTO objects using a BeanOutputConverter.

@Service
@AllArgsConstructor
public class TrekkingServiceImpl implements TrekkingService{

    private final ChatModel aiClient;

    @Override
    public List< TrekkingDTO > getTrekkingPlanIdea ( String country ) {

        String prompt = """
        Generate a list of top 3 treks in a specified country. 
        Each trek should be represented as a JSON object that matches the following structure: 
        {\\n  \\"name\\": \\"Name of the trek\\",\\n  \\"country\\": \\"Name of the country\\",\\n  \\"length\\": \\"Length in kilometers\\",\\n  \\"stages\\": \\"Number of stages\\",\\n  \\"level\\": \\"Difficulty level from 1 to 10\\",\\n  \\"rating\\": \\"Rating from 1 to 5\\",\\n  \\"description\\": \\"Brief description of the trek\\"\\n}. Example of JSON response:\\n[\\n  {\\n    \\"name\\": \\"Path of the Gods\\",\\n    \\"country\\": \\"Italy\\",\\n    \\"length\\": 7.8,\\n    \\"stages\\": 1,\\n    \\"level\\": 3,\\n    \\"rating\\": 5,\\n    \\"description\\": \\"A scenic route on the Amalfi Coast.\\"\\n  },\\n  {\\n    \\"name\\": \\"Tour du Mont Blanc\\",\\n    \\"country\\": \\"France\\",\\n    \\"length\\": 170.0,\\n    \\"stages\\": 11,\\n    \\"level\\": 8,\\n    \\"rating\\": 5,\\n    \\"description\\": \\"A challenging trek around the Mont Blanc massif.\\"\\n  }\\n]. 
        Generate the list for the specified country: {country}
        """;

        BeanOutputConverter< List< TrekkingDTO > > converter = new BeanOutputConverter<>( new ParameterizedTypeReference< List< TrekkingDTO > >(){} );

        PromptTemplate template = new PromptTemplate( prompt );
        template.add( "country", country );
        template.add( "format", converter.getFormat() );

        Generation generation = aiClient.call( template.create() ).getResult();

       return converter.convert( generation.getOutput().getContent() );
    }

}

Maven.xml

<spring-ai.version>1.0.0-M1</spring-ai.version>

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

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

<repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

Rest Controller

@RestController
@RequestMapping( "/api/trekking")
@RequiredArgsConstructor
public class TrekkingController {

    private final TrekkingService service;

    @GetMapping( value = "/top" )
    public List< TrekkingDTO > retrieve( @RequestParam String country ){

        return service.getTrekkingPlanIdea( country );
    }

}

Call the service

curl -X GET "http://localhost:8080/api/trekking/top?country=italy"
[
    {
        "name":"Path of the Gods",
        "country":"Italy",
        "length":7.8,
        "stages":1,
        "level":3,
        "rating":5,
        "description":"A scenic route on the Amalfi Coast offering breathtaking views of the coastline."
    },
    {
        "name":"Alta Via 1",
        "country":"Italy",
        "length":120.0,
        "stages":12,
        "level":7,
        "rating":5,
        "description":"A classic trek through the Dolomites, known for its stunning mountain scenery and challenging routes."
    },
    {
        "name":"Cinque Terre Coastal Trail",
        "country":"Italy",
        "length":12.0,
        "stages":1,
        "level":4,
        "rating":4,
        "description":"A beautiful trek connecting the five picturesque villages of Cinque Terre along the Ligurian coast."
    }
]

Conclusion

Spring AI provides a powerful and flexible framework for integrating Generative AI into Java applications. Its support for various AI models, vector databases, and advanced techniques like function calling and RAG makes it an excellent choice for developers looking to build intelligent applications. By simplifying the complexities of AI integration, Spring AI enables developers to focus on creating innovative solutions that leverage the power of artificial intelligence.

Categories:

Comments are closed