1 line
52 KiB
JSON
1 line
52 KiB
JSON
{"repo": "alperenavici/akademikai", "files": {"AkademikAi.Service/Services/AiQuestionService.cs": {"40ba4b73c873d2996a415341ac056f37b1f58ce9": {"t": 1785745811.8200371, "n": 10234, "h": "de30158c8d5373ac", "c": "using AkademikAi.Core.DTOs;\nusing AkademikAi.Entity.Entites;\nusing AkademikAi.Entity.Enums;\nusing AkademikAi.Service.IServices;\nusing Microsoft.Extensions.Logging;\nusing Microsoft.Extensions.Configuration;\nusing System.Text;\nusing System.Text.Json;\n\nnamespace AkademikAi.Service.Services\n{\n public class AiQuestionService : IAiQuestionService\n {\n private readonly HttpClient _httpClient;\n private readonly ILogger<AiQuestionService> _logger;\n private readonly ISubjectService _subjectService;\n private readonly ITopicService _topicService;\n private readonly IConfiguration _configuration;\n private readonly string _aiBaseUrl;\n private readonly string _googleApiKey;\n private readonly string _deepSeekApiKey;\n\n public AiQuestionService(HttpClient httpClient, ILogger<AiQuestionService> logger, \n ISubjectService subjectService, ITopicService topicService,\n IConfiguration configuration)\n {\n _httpClient = httpClient;\n _logger = logger;\n _subjectService = subjectService;\n _topicService = topicService;\n _configuration = configuration;\n _aiBaseUrl = _configuration[\"GoogleAI:BaseUrl\"] ?? \"http://127.0.0.1:8081\";\n _googleApiKey = _configuration[\"GoogleAI:ApiKey\"] ?? throw new ArgumentException(\"Google API Key bulunamadı!\");\n _deepSeekApiKey = _configuration[\"DeepSeekAI:ApiKey\"] ?? throw new ArgumentException(\"DeepSeek API Key bulunamadı!\");\n }\n\n public async Task<List<Questions>> GenerateQuestionsFromAiAsync(CustomExamCreateDto dto)\n {\n try\n {\n var subjectName = await GetSubjectName(dto.SubjectId);\n var topicName = await GetTopicName(dto.TopicId);\n \n _logger.LogInformation($\"AI soru üretimi başlıyor - Ders: {subjectName}, Konu: {topicName}, Zorluk: {dto.Difficulty}, Soru Sayısı: {dto.QuestionCount}\");\n \n var requestData = new\n {\n subject = subjectName,\n topic = topicName,\n difficulty = GetDifficultyString(dto.Difficulty),\n question_count = dto.QuestionCount,\n question_type = \"multiple_choice\",\n google_api_key = _googleApiKey,\n deepseek_api_key = _deepSeekApiKey\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n _logger.LogInformation($\"AI servisi URL: {_aiBaseUrl}/generate_questions\");\n _logger.LogInformation($\"İstek verisi: {json}\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n _logger.LogInformation($\"AI servisi yanıtı: StatusCode={response.StatusCode}, ReasonPhrase={response.ReasonPhrase}\");\n \n if (!response.IsSuccessStatusCode)\n {\n var errorContent = await response.Content.ReadAsStringAsync();\n _logger.LogError($\"AI servisi hata detayı: {errorContent}\");\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var questions = new List<Questions>();\n foreach (var aiQuestion in aiQuestions)\n {\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)dto.Difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n // Konu ilişkisini ekle\n question.QuestionsTopics = new List<QuestionsTopic>\n {\n new QuestionsTopic\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n TopicId = dto.TopicId\n }\n };\n\n questions.Add(question);\n }\n\n return questions;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n public async Task<Questions> GenerateSingleQuestionAsync(string subject, string topic, int difficulty, string questionType = \"multiple_choice\")\n {\n try\n {\n var requestData = new\n {\n subject = subject,\n topic = topic,\n difficulty = GetDifficultyString(difficulty),\n question_count = 1,\n question_type = questionType\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n if (!response.IsSuccessStatusCode)\n {\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var aiQuestion = aiQuestions.First();\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n return question;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"Tek AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n private string GetDifficultyString(int difficulty)\n {\n return difficulty switch\n {\n 1 => \"easy\",\n 2 => \"medium\", \n 3 => \"hard\",\n 0 => \"mixed\",\n _ => \"medium\"\n };\n }\n\n private async Task<string> GetSubjectName(Guid subjectId)\n {\n try\n {\n var subject = await _subjectService.GetByIdAsync(subjectId);\n return subject?.SubjectName ?? \"Bilinmeyen Ders\";\n }\n catch\n {\n return \"Bilinmeyen Ders\";\n }\n }\n\n private async Task<string> GetTopicName(Guid topicId)\n {\n try\n {\n var topic = await _topicService.GetByIdAsync(topicId);\n return topic?.TopicName ?? \"Bilinmeyen Konu\";\n }\n catch\n {\n return \"Bilinmeyen Konu\";\n }\n }\n }\n\n // AI servisinden gelen yanıt için model\n public class AiQuestionResponse\n {\n public string question { get; set; } = string.Empty;\n public List<string> options { get; set; } = new List<string>();\n public string correct_answer { get; set; } = string.Empty;\n public string explanation { get; set; } = string.Empty;\n }\n}\n"}, "ac6a8fa59554efbf525c764e56daa50692b6fe60": {"t": 1785756778.1399336, "n": 10234, "h": "de30158c8d5373ac", "c": "using AkademikAi.Core.DTOs;\nusing AkademikAi.Entity.Entites;\nusing AkademikAi.Entity.Enums;\nusing AkademikAi.Service.IServices;\nusing Microsoft.Extensions.Logging;\nusing Microsoft.Extensions.Configuration;\nusing System.Text;\nusing System.Text.Json;\n\nnamespace AkademikAi.Service.Services\n{\n public class AiQuestionService : IAiQuestionService\n {\n private readonly HttpClient _httpClient;\n private readonly ILogger<AiQuestionService> _logger;\n private readonly ISubjectService _subjectService;\n private readonly ITopicService _topicService;\n private readonly IConfiguration _configuration;\n private readonly string _aiBaseUrl;\n private readonly string _googleApiKey;\n private readonly string _deepSeekApiKey;\n\n public AiQuestionService(HttpClient httpClient, ILogger<AiQuestionService> logger, \n ISubjectService subjectService, ITopicService topicService,\n IConfiguration configuration)\n {\n _httpClient = httpClient;\n _logger = logger;\n _subjectService = subjectService;\n _topicService = topicService;\n _configuration = configuration;\n _aiBaseUrl = _configuration[\"GoogleAI:BaseUrl\"] ?? \"http://127.0.0.1:8081\";\n _googleApiKey = _configuration[\"GoogleAI:ApiKey\"] ?? throw new ArgumentException(\"Google API Key bulunamadı!\");\n _deepSeekApiKey = _configuration[\"DeepSeekAI:ApiKey\"] ?? throw new ArgumentException(\"DeepSeek API Key bulunamadı!\");\n }\n\n public async Task<List<Questions>> GenerateQuestionsFromAiAsync(CustomExamCreateDto dto)\n {\n try\n {\n var subjectName = await GetSubjectName(dto.SubjectId);\n var topicName = await GetTopicName(dto.TopicId);\n \n _logger.LogInformation($\"AI soru üretimi başlıyor - Ders: {subjectName}, Konu: {topicName}, Zorluk: {dto.Difficulty}, Soru Sayısı: {dto.QuestionCount}\");\n \n var requestData = new\n {\n subject = subjectName,\n topic = topicName,\n difficulty = GetDifficultyString(dto.Difficulty),\n question_count = dto.QuestionCount,\n question_type = \"multiple_choice\",\n google_api_key = _googleApiKey,\n deepseek_api_key = _deepSeekApiKey\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n _logger.LogInformation($\"AI servisi URL: {_aiBaseUrl}/generate_questions\");\n _logger.LogInformation($\"İstek verisi: {json}\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n _logger.LogInformation($\"AI servisi yanıtı: StatusCode={response.StatusCode}, ReasonPhrase={response.ReasonPhrase}\");\n \n if (!response.IsSuccessStatusCode)\n {\n var errorContent = await response.Content.ReadAsStringAsync();\n _logger.LogError($\"AI servisi hata detayı: {errorContent}\");\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var questions = new List<Questions>();\n foreach (var aiQuestion in aiQuestions)\n {\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)dto.Difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n // Konu ilişkisini ekle\n question.QuestionsTopics = new List<QuestionsTopic>\n {\n new QuestionsTopic\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n TopicId = dto.TopicId\n }\n };\n\n questions.Add(question);\n }\n\n return questions;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n public async Task<Questions> GenerateSingleQuestionAsync(string subject, string topic, int difficulty, string questionType = \"multiple_choice\")\n {\n try\n {\n var requestData = new\n {\n subject = subject,\n topic = topic,\n difficulty = GetDifficultyString(difficulty),\n question_count = 1,\n question_type = questionType\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n if (!response.IsSuccessStatusCode)\n {\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var aiQuestion = aiQuestions.First();\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n return question;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"Tek AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n private string GetDifficultyString(int difficulty)\n {\n return difficulty switch\n {\n 1 => \"easy\",\n 2 => \"medium\", \n 3 => \"hard\",\n 0 => \"mixed\",\n _ => \"medium\"\n };\n }\n\n private async Task<string> GetSubjectName(Guid subjectId)\n {\n try\n {\n var subject = await _subjectService.GetByIdAsync(subjectId);\n return subject?.SubjectName ?? \"Bilinmeyen Ders\";\n }\n catch\n {\n return \"Bilinmeyen Ders\";\n }\n }\n\n private async Task<string> GetTopicName(Guid topicId)\n {\n try\n {\n var topic = await _topicService.GetByIdAsync(topicId);\n return topic?.TopicName ?? \"Bilinmeyen Konu\";\n }\n catch\n {\n return \"Bilinmeyen Konu\";\n }\n }\n }\n\n // AI servisinden gelen yanıt için model\n public class AiQuestionResponse\n {\n public string question { get; set; } = string.Empty;\n public List<string> options { get; set; } = new List<string>();\n public string correct_answer { get; set; } = string.Empty;\n public string explanation { get; set; } = string.Empty;\n }\n}\n"}, "b5cb03c6c0ae7a57fcc841023705ce13b3f4b0db": {"t": 1785756779.5398314, "n": 9999, "h": "64728c7e68e17b5d", "c": "using AkademikAi.Core.DTOs;\nusing AkademikAi.Entity.Entites;\nusing AkademikAi.Entity.Enums;\nusing AkademikAi.Service.IServices;\nusing Microsoft.Extensions.Logging;\nusing Microsoft.Extensions.Configuration;\nusing System.Text;\nusing System.Text.Json;\n\nnamespace AkademikAi.Service.Services\n{\n public class AiQuestionService : IAiQuestionService\n {\n private readonly HttpClient _httpClient;\n private readonly ILogger<AiQuestionService> _logger;\n private readonly ISubjectService _subjectService;\n private readonly ITopicService _topicService;\n private readonly IConfiguration _configuration;\n private readonly string _aiBaseUrl;\n private readonly string _googleApiKey;\n\n public AiQuestionService(HttpClient httpClient, ILogger<AiQuestionService> logger, \n ISubjectService subjectService, ITopicService topicService,\n IConfiguration configuration)\n {\n _httpClient = httpClient;\n _logger = logger;\n _subjectService = subjectService;\n _topicService = topicService;\n _configuration = configuration;\n _aiBaseUrl = _configuration[\"GoogleAI:BaseUrl\"] ?? \"http://127.0.0.1:8081\";\n _googleApiKey = _configuration[\"GoogleAI:ApiKey\"] ?? throw new ArgumentException(\"Google API Key bulunamadı!\");\n }\n\n public async Task<List<Questions>> GenerateQuestionsFromAiAsync(CustomExamCreateDto dto)\n {\n try\n {\n var subjectName = await GetSubjectName(dto.SubjectId);\n var topicName = await GetTopicName(dto.TopicId);\n \n _logger.LogInformation($\"AI soru üretimi başlıyor - Ders: {subjectName}, Konu: {topicName}, Zorluk: {dto.Difficulty}, Soru Sayısı: {dto.QuestionCount}\");\n \n var requestData = new\n {\n subject = subjectName,\n topic = topicName,\n difficulty = GetDifficultyString(dto.Difficulty),\n question_count = dto.QuestionCount,\n question_type = \"multiple_choice\",\n google_api_key = _googleApiKey\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n _logger.LogInformation($\"AI servisi URL: {_aiBaseUrl}/generate_questions\");\n _logger.LogInformation($\"İstek verisi: {json}\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n _logger.LogInformation($\"AI servisi yanıtı: StatusCode={response.StatusCode}, ReasonPhrase={response.ReasonPhrase}\");\n \n if (!response.IsSuccessStatusCode)\n {\n var errorContent = await response.Content.ReadAsStringAsync();\n _logger.LogError($\"AI servisi hata detayı: {errorContent}\");\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var questions = new List<Questions>();\n foreach (var aiQuestion in aiQuestions)\n {\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)dto.Difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n // Konu ilişkisini ekle\n question.QuestionsTopics = new List<QuestionsTopic>\n {\n new QuestionsTopic\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n TopicId = dto.TopicId\n }\n };\n\n questions.Add(question);\n }\n\n return questions;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n public async Task<Questions> GenerateSingleQuestionAsync(string subject, string topic, int difficulty, string questionType = \"multiple_choice\")\n {\n try\n {\n var requestData = new\n {\n subject = subject,\n topic = topic,\n difficulty = GetDifficultyString(difficulty),\n question_count = 1,\n question_type = questionType\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n if (!response.IsSuccessStatusCode)\n {\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var aiQuestion = aiQuestions.First();\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n return question;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"Tek AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n private string GetDifficultyString(int difficulty)\n {\n return difficulty switch\n {\n 1 => \"easy\",\n 2 => \"medium\", \n 3 => \"hard\",\n 0 => \"mixed\",\n _ => \"medium\"\n };\n }\n\n private async Task<string> GetSubjectName(Guid subjectId)\n {\n try\n {\n var subject = await _subjectService.GetByIdAsync(subjectId);\n return subject?.SubjectName ?? \"Bilinmeyen Ders\";\n }\n catch\n {\n return \"Bilinmeyen Ders\";\n }\n }\n\n private async Task<string> GetTopicName(Guid topicId)\n {\n try\n {\n var topic = await _topicService.GetByIdAsync(topicId);\n return topic?.TopicName ?? \"Bilinmeyen Konu\";\n }\n catch\n {\n return \"Bilinmeyen Konu\";\n }\n }\n }\n\n // AI servisinden gelen yanıt için model\n public class AiQuestionResponse\n {\n public string question { get; set; } = string.Empty;\n public List<string> options { get; set; } = new List<string>();\n public string correct_answer { get; set; } = string.Empty;\n public string explanation { get; set; } = string.Empty;\n }\n}\n"}, "95029572e1ac43ef78d29d6681132fbe7c8f26d6": {"t": 1785756781.155303, "n": 10309, "h": "886f09ab986d52d0", "c": "using AkademikAi.Core.DTOs;\nusing AkademikAi.Entity.Entites;\nusing AkademikAi.Entity.Enums;\nusing AkademikAi.Service.IServices;\nusing Microsoft.Extensions.Configuration;\nusing Microsoft.Extensions.Logging;\nusing System.Text;\nusing System.Text.Json;\n\nnamespace AkademikAi.Service.Services\n{\n public class AiQuestionService : IAiQuestionService\n {\n private readonly HttpClient _httpClient;\n private readonly ILogger<AiQuestionService> _logger;\n private readonly ISubjectService _subjectService;\n private readonly ITopicService _topicService;\n private readonly IConfiguration _configuration;\n private readonly string _aiBaseUrl;\n private readonly string _googleApiKey;\n\n public AiQuestionService(HttpClient httpClient, ILogger<AiQuestionService> logger, \n ISubjectService subjectService, ITopicService topicService,\n IConfiguration configuration)\n {\n _httpClient = httpClient;\n _logger = logger;\n _subjectService = subjectService;\n _topicService = topicService;\n _configuration = configuration;\n \n // Configuration'dan ayarları al\n _aiBaseUrl = _configuration[\"AiServices:AiServiceUrl\"] ?? \"http://127.0.0.1:8081\";\n _googleApiKey = _configuration[\"AiServices:GoogleApiKey\"] ?? throw new InvalidOperationException(\"Google API Key bulunamadı\");\n \n _logger.LogInformation($\"AI Servisi URL: {_aiBaseUrl}\");\n _logger.LogInformation($\"Google API Key yapılandırıldı: {_googleApiKey[..10]}...\");\n }\n\n public async Task<List<Questions>> GenerateQuestionsFromAiAsync(CustomExamCreateDto dto)\n {\n try\n {\n var subjectName = await GetSubjectName(dto.SubjectId);\n var topicName = await GetTopicName(dto.TopicId);\n \n _logger.LogInformation($\"AI soru üretimi başlıyor - Ders: {subjectName}, Konu: {topicName}, Zorluk: {dto.Difficulty}, Soru Sayısı: {dto.QuestionCount}\");\n \n var requestData = new\n {\n subject = subjectName,\n topic = topicName,\n difficulty = GetDifficultyString(dto.Difficulty),\n question_count = dto.QuestionCount,\n question_type = \"multiple_choice\",\n google_api_key = _googleApiKey\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n _logger.LogInformation($\"AI servisi URL: {_aiBaseUrl}/generate_questions\");\n _logger.LogInformation($\"İstek verisi: {json}\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n _logger.LogInformation($\"AI servisi yanıtı: StatusCode={response.StatusCode}, ReasonPhrase={response.ReasonPhrase}\");\n \n if (!response.IsSuccessStatusCode)\n {\n var errorContent = await response.Content.ReadAsStringAsync();\n _logger.LogError($\"AI servisi hata detayı: {errorContent}\");\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var questions = new List<Questions>();\n foreach (var aiQuestion in aiQuestions)\n {\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)dto.Difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n // Konu ilişkisini ekle\n question.QuestionsTopics = new List<QuestionsTopic>\n {\n new QuestionsTopic\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n TopicId = dto.TopicId\n }\n };\n\n questions.Add(question);\n }\n\n return questions;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n public async Task<Questions> GenerateSingleQuestionAsync(string subject, string topic, int difficulty, string questionType = \"multiple_choice\")\n {\n try\n {\n var requestData = new\n {\n subject = subject,\n topic = topic,\n difficulty = GetDifficultyString(difficulty),\n question_count = 1,\n question_type = questionType,\n google_api_key = _googleApiKey\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n if (!response.IsSuccessStatusCode)\n {\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var aiQuestion = aiQuestions.First();\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n return question;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"Tek AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n private string GetDifficultyString(int difficulty)\n {\n return difficulty switch\n {\n 1 => \"easy\",\n 2 => \"medium\", \n 3 => \"hard\",\n 0 => \"mixed\",\n _ => \"medium\"\n };\n }\n\n private async Task<string> GetSubjectName(Guid subjectId)\n {\n try\n {\n var subject = await _subjectService.GetByIdAsync(subjectId);\n return subject?.SubjectName ?? \"Bilinmeyen Ders\";\n }\n catch\n {\n return \"Bilinmeyen Ders\";\n }\n }\n\n private async Task<string> GetTopicName(Guid topicId)\n {\n try\n {\n var topic = await _topicService.GetByIdAsync(topicId);\n return topic?.TopicName ?? \"Bilinmeyen Konu\";\n }\n catch\n {\n return \"Bilinmeyen Konu\";\n }\n }\n }\n\n // AI servisinden gelen yanıt için model\n public class AiQuestionResponse\n {\n public string question { get; set; } = string.Empty;\n public List<string> options { get; set; } = new List<string>();\n public string correct_answer { get; set; } = string.Empty;\n public string explanation { get; set; } = string.Empty;\n }\n}\n"}, "30fbec41ebe91b88aa5bb60a8e34cd65f864d3b8": {"t": 1785756782.345279, "n": 9511, "h": "4378772056d2fdbf", "c": "using AkademikAi.Core.DTOs;\nusing AkademikAi.Entity.Entites;\nusing AkademikAi.Entity.Enums;\nusing AkademikAi.Service.IServices;\nusing Microsoft.Extensions.Logging;\nusing System.Text;\nusing System.Text.Json;\n\nnamespace AkademikAi.Service.Services\n{\n public class AiQuestionService : IAiQuestionService\n {\n private readonly HttpClient _httpClient;\n private readonly ILogger<AiQuestionService> _logger;\n private readonly ISubjectService _subjectService;\n private readonly ITopicService _topicService;\n private readonly string _aiBaseUrl = \"http://127.0.0.1:8081\";\n\n public AiQuestionService(HttpClient httpClient, ILogger<AiQuestionService> logger, \n ISubjectService subjectService, ITopicService topicService)\n {\n _httpClient = httpClient;\n _logger = logger;\n _subjectService = subjectService;\n _topicService = topicService;\n }\n\n public async Task<List<Questions>> GenerateQuestionsFromAiAsync(CustomExamCreateDto dto)\n {\n try\n {\n var subjectName = await GetSubjectName(dto.SubjectId);\n var topicName = await GetTopicName(dto.TopicId);\n \n _logger.LogInformation($\"AI soru üretimi başlıyor - Ders: {subjectName}, Konu: {topicName}, Zorluk: {dto.Difficulty}, Soru Sayısı: {dto.QuestionCount}\");\n \n var requestData = new\n {\n subject = subjectName,\n topic = topicName,\n difficulty = GetDifficultyString(dto.Difficulty),\n question_count = dto.QuestionCount,\n question_type = \"multiple_choice\"\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n _logger.LogInformation($\"AI servisi URL: {_aiBaseUrl}/generate_questions\");\n _logger.LogInformation($\"İstek verisi: {json}\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n _logger.LogInformation($\"AI servisi yanıtı: StatusCode={response.StatusCode}, ReasonPhrase={response.ReasonPhrase}\");\n \n if (!response.IsSuccessStatusCode)\n {\n var errorContent = await response.Content.ReadAsStringAsync();\n _logger.LogError($\"AI servisi hata detayı: {errorContent}\");\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var questions = new List<Questions>();\n foreach (var aiQuestion in aiQuestions)\n {\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)dto.Difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n // Konu ilişkisini ekle\n question.QuestionsTopics = new List<QuestionsTopic>\n {\n new QuestionsTopic\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n TopicId = dto.TopicId\n }\n };\n\n questions.Add(question);\n }\n\n return questions;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n public async Task<Questions> GenerateSingleQuestionAsync(string subject, string topic, int difficulty, string questionType = \"multiple_choice\")\n {\n try\n {\n var requestData = new\n {\n subject = subject,\n topic = topic,\n difficulty = GetDifficultyString(difficulty),\n question_count = 1,\n question_type = questionType\n };\n\n var json = JsonSerializer.Serialize(requestData);\n var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n\n var response = await _httpClient.PostAsync($\"{_aiBaseUrl}/generate_questions\", content);\n \n if (!response.IsSuccessStatusCode)\n {\n throw new Exception($\"AI servisi hatası: {response.StatusCode} - {response.ReasonPhrase}\");\n }\n\n var responseContent = await response.Content.ReadAsStringAsync();\n var aiQuestions = JsonSerializer.Deserialize<List<AiQuestionResponse>>(responseContent, new JsonSerializerOptions\n {\n PropertyNameCaseInsensitive = true\n });\n\n if (aiQuestions == null || !aiQuestions.Any())\n {\n throw new Exception(\"AI servisinden soru alınamadı.\");\n }\n\n var aiQuestion = aiQuestions.First();\n var question = new Questions\n {\n Id = Guid.NewGuid(),\n QuestionText = aiQuestion.question,\n DifficultyLevel = (QuestionsDiff)difficulty,\n Source = \"ai\",\n IsActive = true,\n SolutionText = \"\",\n GeneratedForUserId = null\n };\n\n // Seçenekleri ekle\n question.QuestionsOptions = new List<QuestionsOptions>();\n var optionLabels = new[] { 'A', 'B', 'C', 'D' };\n for (int i = 0; i < aiQuestion.options.Count; i++)\n {\n question.QuestionsOptions.Add(new QuestionsOptions\n {\n Id = Guid.NewGuid(),\n QuestionId = question.Id,\n OptionText = aiQuestion.options[i],\n Label = optionLabels[i],\n IsCorrect = aiQuestion.options[i] == aiQuestion.correct_answer,\n OptionOrder = i + 1\n });\n }\n\n return question;\n }\n catch (Exception ex)\n {\n _logger.LogError(ex, \"Tek AI soru üretimi sırasında hata oluştu\");\n throw new Exception($\"AI soru üretimi başarısız: {ex.Message}\");\n }\n }\n\n private string GetDifficultyString(int difficulty)\n {\n return difficulty switch\n {\n 1 => \"easy\",\n 2 => \"medium\", \n 3 => \"hard\",\n 0 => \"mixed\",\n _ => \"medium\"\n };\n }\n\n private async Task<string> GetSubjectName(Guid subjectId)\n {\n try\n {\n var subject = await _subjectService.GetByIdAsync(subjectId);\n return subject?.SubjectName ?? \"Bilinmeyen Ders\";\n }\n catch\n {\n return \"Bilinmeyen Ders\";\n }\n }\n\n private async Task<string> GetTopicName(Guid topicId)\n {\n try\n {\n var topic = await _topicService.GetByIdAsync(topicId);\n return topic?.TopicName ?? \"Bilinmeyen Konu\";\n }\n catch\n {\n return \"Bilinmeyen Konu\";\n }\n }\n }\n\n // AI servisinden gelen yanıt için model\n public class AiQuestionResponse\n {\n public string question { get; set; } = string.Empty;\n public List<string> options { get; set; } = new List<string>();\n public string correct_answer { get; set; } = string.Empty;\n public string explanation { get; set; } = string.Empty;\n }\n}\n"}}}} |