Spring AI

Spring AI

Spring AI supports ChatGPT, the AI language model by OpenAI. ChatGPT has been instrumental in sparking interest in AI-driven text generation, thanks to its creation of industry-leading text generation models and embeddings.

Prerequisites

You will need to create an API with OpenAI to access ChatGPT models. Create an account at OpenAI signup page and generate the token on the API Keys page. The Spring AI project defines a configuration property named spring.ai.openai.api-key that you should set to the value of the API Key obtained from openai.com. Exporting an environment variable is one way to set that configuration property:

spring.ai.openai.api-key= your key
spring.ai.openai.chat.options.model=gpt-4o
        


Add Repositories and BOM

Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the OpenAI Chat Client. To enable it add the following dependency to your project’s Maven pom.xml file:

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


or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}        



package com.zizo.ai.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;





@RestController
@RequestMapping("ai/v1")
public class AiController {


	
	private final ChatClient chatclient ;
	

	
	public AiController(ChatClient.Builder chatClientBuilder) {
		super();
		this.chatclient = chatClientBuilder.build();
	}

	

	@GetMapping("/film2/{actor}")
	public ActorFilms openAIfilm2(@PathVariable String actor) {
		
		
	
		
		return chatclient.prompt().user(u->u.text("best five film for ").param("actor",actor))
				
				.call()
				.entity(ActorFilms.class);
	}
	
	
	record ActorFilms (String actor,List<String> movies) {}
	
	
	
	
	
}
        

Response


要查看或添加评论,请登录

Ahmed Abdelaziz的更多文章

  • Java 23

    Java 23

    New and updated Java language features, core API, and the JVM – Java 23 packs it all – for new Java developers to…

  • Kafka Producer And Consumer In Spring Boot

    Kafka Producer And Consumer In Spring Boot

    Appache Kafka is a distributed event streaming platform that is widely used for handling real-time data streams in…

  • Docker with Spring Boot in a simple way

    Docker with Spring Boot in a simple way

    This guide walks you through the process of building a Docker image for running a Spring Boot application. We start…

  • Quarkus Framework and Comparison with Spring Boot

    Quarkus Framework and Comparison with Spring Boot

    In this article, we’ll give an overview of the Quarkus framework and compare it with Spring Boot – the most popular…

  • Aspect Oriented Programming and AOP in Spring Framework

    Aspect Oriented Programming and AOP in Spring Framework

    Aspect-Oriented Programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation…

    2 条评论
  • Spring Boot Caching with Redis

    Spring Boot Caching with Redis

    Spring Boot Cache Providers Cache providers allow us to transparently and clearly configure the cache in the…

  • The DispatcherServlet

    The DispatcherServlet

    The Engine of Request Handling in Spring Boot. 1.

  • Spring Data with MongoDB

    Spring Data with MongoDB

    1. Overview This article will be a quick and practical introduction to Spring Data MongoDB.

  • Transactionality

    Transactionality

    By default, methods inherited from inherit the transactional configuration from . For read operations, the transaction…

  • What is the use of @Qualifier annotation in Spring?

    What is the use of @Qualifier annotation in Spring?

    he use of @Qualifier is to support the @Autowired annotation when it needs help. Typically, @Autowired can implicitly…

社区洞察

其他会员也浏览了