Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'com.google.genai:google-genai:1.6.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
runtimeOnly 'com.mysql:mysql-connector-j'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.kernellabs.kernellabs.application;

import autovalue.shaded.com.google.common.collect.ImmutableList;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.GoogleSearch;
import com.google.genai.types.Tool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class GeminiService {

private final Client client;
private final Tool googleSearchTool;
private final String modelName;

public GeminiService(
@Value("${gemini.api.key}") String apiKey,
@Value("${gemini.model:gemini-2.5-flash}") String modelName
) {
this.client = Client.builder()
.apiKey(apiKey)
.build();

this.googleSearchTool = Tool.builder()
.googleSearch(GoogleSearch.builder().build())
.build();

this.modelName = modelName;
}

public String generateAnswer(String prompt) {
GenerateContentConfig config = GenerateContentConfig.builder()
.tools(ImmutableList.of(googleSearchTool))
.build();

// ← 여기를 client.models()가 아니라 client.models 로 접근
GenerateContentResponse res = client.models
.generateContent(modelName, prompt, config);

return res.text();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kernellabs.kernellabs.presentation.controller;

import com.kernellabs.kernellabs.application.GeminiService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/genie")
@AllArgsConstructor
public class GeminiController {
private final GeminiService geminiService;

@PostMapping("/chat")
public ResponseEntity<ChatResponse> chat(@Valid @RequestBody ChatRequest req) {
String answer = geminiService.generateAnswer(req.getPrompt());
return ResponseEntity.ok(new ChatResponse(answer));
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class ChatRequest {
private String prompt;
}

@Data @NoArgsConstructor @AllArgsConstructor
public static class ChatResponse {
private String answer;
}
}