Showing posts with label OpenAI. Show all posts
Showing posts with label OpenAI. Show all posts

Thursday, June 20, 2024

Getting started Spring AI with OpenAI Chat Model

 Problem statement :  develop spring boot API which connect's to Open AI and will give top 5

                                       personalities of any sports. Other than sports,

                                       if you give another details should display warning message.


We have different AI Chat models as mentioned below

This is post will explain you about , how we can use Open AI model to build simple spring boot application

Step 1 :  Signup : https://platform.openai.com/signup

              generate key -  https://platform.openai.com/account/api-keys





Step 2 :           spring boot intializr : https://start.spring.io/

dependencies :  OpenAI and other related dependencies

                        Provide group  artifact and other required details

                       import project to IntelliJ

Step3 :          Write Controller 

package com.personal.openai;

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OpenAIController {


@Autowired
private OpenAiChatModel openAiChatModel;

@GetMapping("/sports")
public String getSportPersonaDetails(@RequestParam String name){
var systemMessage = new SystemMessage("Your primary function is to provide details about sports," +
" if anything else asks simply say that i can provide only sports details");
String promptMessage = String.format(" Could you please provide detailed information" +
" on the top 5 sports persons currently in %s ?",name);
Message message = new UserMessage(promptMessage);
return openAiChatModel.call(message,systemMessage);

}
}


Step 4: update the properties/yaml

spring.application.name=openai
spring.ai.openai.api-key=sk-proj-NNcDe2HRc1UUjcbIRkRsT3Blbk
spring.ai.openai.chat.enabled=true
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7


Step 5 : Run the code and start the server


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.personal</groupId>
<artifactId>openai</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>openai</name>
<description>Demo project for Spring Boot ai</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M1</spring-ai.version>
</properties>
<dependencies>
<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>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>


 

Step 6:  Output :   http://localhost:8080/sports?name=cricket

              http://localhost:8080/sports?name=tollywood

              
This how we can work on any Models. 



















        

AddToAny

Contact Form

Name

Email *

Message *