hades

[Spring] ์Šคํ”„๋ง AI ๋ณธ๋ฌธ

๐Ÿƒ๐Ÿป‍โ™‚๏ธ ๊ธฐ๋ณธํ›ˆ๋ จ/Spring

[Spring] ์Šคํ”„๋ง AI

hades1 2024. 8. 14. 20:06

๋ณธ ๊ธ€์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ, AI์— ์งˆ๋ฌธ์„ ํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ›์•„์•ผ ํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด AI api๋ฅผ ํ•™์Šตํ•˜๋ฉด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ธ€์ด๋‹ค. Spring์—์„œ๋Š” AI๋ฅผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์ง€์›ํ•ด์ฃผ๋Š” dependency๊ฐ€ ์žˆ๋‹ค. ์œ ํŠœ๋ธŒ์˜ ๋งํฌ๋„ ํ•„์š”ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ๊ตฌ๊ธ€ ์„œ๋น„์Šค์ธ Gemini AI๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

์ฒ˜์Œ, build.gradle์—์„œ Edit Starters๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด Gemini AI๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ, dependency๊ฐ€ ์ธ์‹๋˜์ง€ ์•Š๊ณ , ๋นจ๊ฐ„์ค„ ์ฒœ์ง€๊ฐ€ ๋˜์—ˆ๋‹ค. ์บ์‹œ ์‚ญ์ œ, ์žฌ๋นŒ๋“œ ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•ด๋ณด์•˜์ง€๋งŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. (๊นŠ์€ ๋นก์นจ)

 

ํ˜น์‹œ๋‚˜ ํ•ด์„œ Spring Initilizr๋ฅผ ์ด์šฉํ•˜์—ฌ Gemini๋ฅผ ์ถ”๊ฐ€ํ•œ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , build.gradle์„ ํ™•์ธํ•ด๋ณด์•˜๋‹ค.

 

์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ์—๋Š” ์ด์ „์— ์—†์—ˆ๋˜ ๋ถ€๋ถ„๊ณผ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค. ๊ทธ ๋ถ€๋ถ„๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

ext {
	set('springAiVersion', "1.0.0-M1")
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}"	// edit starters๋กœ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ๋Š” mavenBom "org.springframework.ai:spring-ai-bom:$springAiVersion" ์˜€์Œ
	}
}

${springAiVersion}์„ ์ „๋‹ฌํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ „๋‹ฌํ•ด์ฃผ์ง€ ๋ชปํ•ด์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ด์—ˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ์ œ๋Œ€๋กœ ๋„ฃ์–ด์ฃผ๋ฉด, ๋นจ๊ฐ„์ค„์ด ๋ง๋”ํ•˜๊ฒŒ ์‚ฌ๋ผ์ง„๋‹ค. (ํŽธ์•ˆ)

 

build๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋˜์—ˆ์œผ๋ฉด, application.yml์— ์•„๋ž˜ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

Spring:
  ai:
    vertex:
      ai:
        gemini:
          project-id: YOUR_PROJECT_ID
          location: asia-northeast3

 

์ถ”๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์งˆ๋ฌธ์„ ์ฃผ๊ณ  ์‘๋‹ต์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

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

    private final VertexAiGeminiChatModel vertexAiGeminiChatModel;

    @GetMapping("/chat")
    public String chat(@RequestBody String message) {
        String vertexAiGeminiResponse = vertexAiGeminiChatModel.call(message);
        System.out.println("vertexAiGeminiResponse = " + vertexAiGeminiResponse);
        return vertexAiGeminiResponse;
    }

}

 

์ด์ œ http๋ฅผ ์ด์šฉํ•˜์—ฌ ์‘๋‹ต์„ ๋ณด๋‚ด๋ฉด ๋์ด ๋‚  ๊ฒƒ ๊ฐ™์•˜๋‹ค.

###
GET http://localhost:8080/api/chat

{
    "message" : "๊ธฐ๋ถ„์ด ์šฐ์šธํ•  ๋•Œ ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์ด ๋จน์€ ๋ฐฐ๋‹ฌ ์Œ์‹์„ ๊ตฌ์ฒด์ ์œผ๋กœ 5๊ฐœ๋งŒ ๋งํ•ด์ค˜. ์ด๋ฆ„ ์™ธ์—๋Š” ์•„๋ฌด ๊ฒƒ๋„ ๋งํ•˜์ง€๋งˆ."
}

###

 

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๊ณ , ์•„๋ž˜ ๋งํฌ์— ์ •๋ฆฌํ•ด๋†“์•˜๋‹ค.

https://hades1.tistory.com/138

 

[Google Cloud] Your default credentials were not found

๐Ÿ†˜ ์—๋ŸฌCaused by: java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc ๐Ÿ” ๋ถ„์„Google

hades1.tistory.com

 

์ž ์ด์ œ, ๊ณ„์ • ๋“ฑ๋ก๋„ ์ œ๋Œ€๋กœ ํ–ˆ์œผ๋‹ˆ ์ด์ œ ๋‚˜์™€๋ž~ ๊ธฐ๋Œ€ํ–ˆ์ง€๋งŒ, ์–ด๋ฆผ๋„ ์—†๋‹ค. 

https://hades1.tistory.com/139

 

[Google Cloud] has not been used in project before or it is disabled.

๐Ÿ†˜ ์—๋ŸฌCaused by: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Vertex AI API has not been used in project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/aiplatform.googleapis.com/overview?project 

hades1.tistory.com

 

๊ณ„์ • ๋“ฑ๋ก, ๊ฒฐ์ œ ์ˆ˜๋‹จ ๋“ฑ๋ก์œผ๋กœ API ํ™œ์„ฑํ™”๋ฅผ ๋ชจ๋‘ ์‹œํ‚ค๊ณ , http ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋งค์šฐ ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.