JSCODE Logo
블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] OpenAI API를 활용한 채팅 API 환경 구축

JSCODE 시니
JSCODE 시니
2026. 06. 13.
author
JSCODE 시니
category
Spring AI
createdAt
Jun 13, 2026 09:50 AM
isPublic
isPublic
series
실무에 바로 적용하는 Spring AI: Spring 서비스에 챗봇·RAG·MCP 도입하기
slug
practice-building-chat-api-with-openai
type
post
updatedAt

✅ 1. build.gradle 수정

dependencies { implementation 'org.springframework.boot:spring-boot-starter-webmvc' // implementation 'org.springframework.ai:spring-ai-starter-model-ollama' implementation 'org.springframework.ai:spring-ai-starter-model-openai' testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' }
 
 
 

✅ 2. application.yml 수정

spring: application: name: chat ai: openai: # 환경변수 값 api-key: ${OPENAI_API_KEY} chat: # 실제 사용할 대화형 AI의 모델명 model: openai/gpt-4.1-nano # GitHub Models 서버로 요청을 보냄 base-url: https://models.github.ai/inference # 세부 API 경로 completions-path: /chat/completions
 
 
 

✅ 3. 환경 변수 설정

notion image
  • Edit Configurations 클릭
 
notion image
  • Modify options 클릭
 
notion image
  • 환경 변수 추가를 위해 Environment variables 클릭
 
notion image
  • 서류 문서 모양 클릭
 
notion image
  • + 버튼 클릭
 
notion image
  • Name : OPENAI_API_KEY
  • Value : Github에서 발급받은 토큰 값 입력
  • 오탈자가 나지 않도록 주의할 것!!!
 
 

✅ 4. Controller 구현

Chat Client API :: Spring AI Reference
The ChatClient offers a fluent API for communicating with an AI Model. It supports both a synchronous and streaming programming model.
Chat Client API :: Spring AI Reference
https://docs.spring.io/spring-ai/reference/api/chatclient.html
  • 우리가 AI와 쉽게 소통할 수 있도록 Spring AI에서 제공하는 객체가 Chat client
 
ChatController.java
package com.jscode.chat.controller; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ChatController { private final ChatClient chatClient; public ChatController(ChatClient.Builder chatClientBuilder){ this.chatClient = chatClientBuilder.build(); } @GetMapping("/ai") public String generation(String userPrompt){ return this.chatClient.prompt() .user(userPrompt) .call() .content(); // 받아온 응답 중 메타데이터는 버리고, 순수 content만 추출! } }