diff --git a/Controllers/AIController.cs b/Controllers/AIController.cs new file mode 100644 index 0000000..44374dd --- /dev/null +++ b/Controllers/AIController.cs @@ -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 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 + { + new Content + { + Parts = new List + { + 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(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 Contents { get; set; } = new(); + } + + public class Content { + [JsonPropertyName("parts")] + public List Parts { get; set; } = new(); + } + + public class Part { + [JsonPropertyName("text")] + public string Text { get; set; } = ""; + } + + public class GeminiResponse { + [JsonPropertyName("candidates")] + public List? Candidates { get; set; } + } + + public class Candidate { + [JsonPropertyName("content")] + public CandidateContent? Content { get; set; } + } + + public class CandidateContent { + [JsonPropertyName("parts")] + public List? Parts { get; set; } + } +} diff --git a/Controllers/WeatherForecastController.cs b/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..a3207ad --- /dev/null +++ b/Controllers/WeatherForecastController.cs @@ -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 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(); + } + } +} diff --git a/NTestAIApi.csproj b/NTestAIApi.csproj new file mode 100644 index 0000000..110e4fb --- /dev/null +++ b/NTestAIApi.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + enable + enable + + + + + + + + diff --git a/NTestAIApi.http b/NTestAIApi.http new file mode 100644 index 0000000..c349e4b --- /dev/null +++ b/NTestAIApi.http @@ -0,0 +1,6 @@ +@NTestAIApi_HostAddress = http://localhost:5143 + +GET {{NTestAIApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..92ef156 --- /dev/null +++ b/Program.cs @@ -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(); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..fc182c8 --- /dev/null +++ b/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/WeatherForecast.cs b/WeatherForecast.cs new file mode 100644 index 0000000..677a698 --- /dev/null +++ b/WeatherForecast.cs @@ -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; } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..e69df08 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Gemini": { + "ApiKey": "AIzaSyBWgJaS794XO4O38iYcNF6XFd85qb0Zy2Q" + } +}