초기 커밋.

This commit is contained in:
김한진 2026-05-11 13:06:09 +09:00
parent c2c1a1d00f
commit f75c3baa91
9 changed files with 225 additions and 0 deletions

106
Controllers/AIController.cs Normal file
View File

@ -0,0 +1,106 @@
using Google.GenAI.Types;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NTestAIApi.Controllers {
[Route("api/[controller]")]
[ApiController]
public class AIController : ControllerBase {
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;
public AIController(IHttpClientFactory httpClientFactory, IConfiguration configuration) {
_httpClientFactory = httpClientFactory;
_configuration = configuration;
}
[HttpGet(Name = "GetAI")]
public async Task<IActionResult> Get([FromQuery] string question, [FromQuery] string? answer, [FromQuery] int? id) {
var apiKey = _configuration["Gemini:ApiKey"];
if (string.IsNullOrWhiteSpace(apiKey)) {
return StatusCode(500, "Gemini API 키가 설정되지 않았습니다.");
}
var url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";
var prompt = $"문제: {question}\n정답: {answer}\n이 문제와 정답이 맞는지 간단히 설명해줘.";
var requestBody = new GeminiRequest {
Contents = new List<Content>
{
new Content
{
Parts = new List<Part>
{
new Part
{
Text = prompt
}
}
}
}
};
var client = _httpClientFactory.CreateClient();
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Headers.Add("X-goog-api-key", apiKey);
requestMessage.Content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");
var response = await client.SendAsync(requestMessage);
var responseText = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode) {
return StatusCode((int)response.StatusCode, responseText);
}
var geminiResponse = JsonSerializer.Deserialize<GeminiResponse>(responseText);
var resultText = geminiResponse?
.Candidates?
.FirstOrDefault()?
.Content?
.Parts?
.FirstOrDefault()?
.Text;
return Ok(new {
prompt,
result = resultText ?? "",
raw = geminiResponse
});
}
}
public class GeminiRequest {
[JsonPropertyName("contents")]
public List<Content> Contents { get; set; } = new();
}
public class Content {
[JsonPropertyName("parts")]
public List<Part> Parts { get; set; } = new();
}
public class Part {
[JsonPropertyName("text")]
public string Text { get; set; } = "";
}
public class GeminiResponse {
[JsonPropertyName("candidates")]
public List<Candidate>? Candidates { get; set; }
}
public class Candidate {
[JsonPropertyName("content")]
public CandidateContent? Content { get; set; }
}
public class CandidateContent {
[JsonPropertyName("parts")]
public List<Part>? Parts { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Mvc;
namespace NTestAIApi.Controllers {
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase {
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get() {
return Enumerable.Range(1, 5).Select(index => new WeatherForecast {
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

14
NTestAIApi.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.GenAI" Version="1.6.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
</ItemGroup>
</Project>

6
NTestAIApi.http Normal file
View File

@ -0,0 +1,6 @@
@NTestAIApi_HostAddress = http://localhost:5143
GET {{NTestAIApi_HostAddress}}/weatherforecast/
Accept: application/json
###

23
Program.cs Normal file
View File

@ -0,0 +1,23 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddHttpClient();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5143",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7214;http://localhost:5143",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

11
WeatherForecast.cs Normal file
View File

@ -0,0 +1,11 @@
namespace NTestAIApi {
public class WeatherForecast {
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

12
appsettings.json Normal file
View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Gemini": {
"ApiKey": "AIzaSyBWgJaS794XO4O38iYcNF6XFd85qb0Zy2Q"
}
}