Showing posts with label Spring AI. Show all posts
Showing posts with label Spring AI. 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 :

              generate key -

Step 2 :           spring boot intializr :

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

public class OpenAIController {

private OpenAiChatModel openAiChatModel;

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


Step 4: update the properties/yaml

Step 5 : Run the code and start the server


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot ai</description>


<name>Spring Milestones</name>



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


This how we can work on any Models. 



Contact Form


Email *

Message *