Добавил слой Infrastructure
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Infrastructure.ExternalServices;
|
||||
|
||||
public class LlmClient : ILlmClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly ILogger<LlmClient> _logger;
|
||||
|
||||
public LlmClient(HttpClient http, IConfiguration config, ILogger<LlmClient> logger)
|
||||
{
|
||||
_http = http; _config = config; _logger = logger;
|
||||
}
|
||||
|
||||
public async Task<LlmReviewAnalysis> AnalyzeReviewAsync(string reviewText, string lectureContext)
|
||||
{
|
||||
var prompt = $"""
|
||||
Analyze the following student review of a lecture. Return a JSON object with:
|
||||
- quality_score: float 0-1 indicating review quality
|
||||
- sentiment: "Positive", "Neutral", or "Negative"
|
||||
- tags: array of relevant topic tags
|
||||
- is_informative: boolean indicating if the review is informative
|
||||
|
||||
Lecture context: {lectureContext}
|
||||
Review text: {reviewText}
|
||||
""";
|
||||
|
||||
var request = new
|
||||
{
|
||||
model = _config["Llm:Model"] ?? "gpt-4o-mini",
|
||||
messages = new[] { new { role = "user", content = prompt } },
|
||||
temperature = 0.3,
|
||||
response_format = new { type = "json_object" }
|
||||
};
|
||||
|
||||
var apiKey = _config["Llm:ApiKey"] ?? "";
|
||||
_http.DefaultRequestHeaders.Clear();
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
_http.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
|
||||
|
||||
var response = await _http.PostAsJsonAsync("chat/completions", request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadFromJsonAsync<JsonElement>();
|
||||
var content = json.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString()!;
|
||||
var analysis = JsonSerializer.Deserialize<LlmRawResponse>(content,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })!;
|
||||
|
||||
return new LlmReviewAnalysis(analysis.QualityScore, analysis.Sentiment, analysis.Tags, analysis.IsInformative);
|
||||
}
|
||||
|
||||
private record LlmRawResponse(double QualityScore, string Sentiment, string[] Tags, bool IsInformative);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniVerse.Application.DTOs.Sync;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Infrastructure.ExternalServices;
|
||||
|
||||
public class ModeusApiClient : IModeusApiClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly ILogger<ModeusApiClient> _logger;
|
||||
|
||||
public ModeusApiClient(HttpClient http, IConfiguration config, ILogger<ModeusApiClient> logger)
|
||||
{
|
||||
_http = http; _logger = logger;
|
||||
var apiKey = config["ModeusApi:ApiKey"];
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
_http.DefaultRequestHeaders.Add("X-API-Key", apiKey);
|
||||
}
|
||||
|
||||
public async Task<ModeusEventsResponse> SearchEventsAsync(SyncScheduleRequest request)
|
||||
{
|
||||
var body = new { specialtyCode = request.SpecialtyCode, timeMin = request.TimeMin, timeMax = request.TimeMax, typeId = request.TypeId };
|
||||
var response = await _http.PostAsJsonAsync("/api/proxy/events/search", body);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<ModeusEventsResponse>() ?? new(new());
|
||||
}
|
||||
|
||||
public async Task<ModeusRoomsResponse> SearchRoomsAsync()
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/proxy/rooms/search", new { });
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<ModeusRoomsResponse>() ?? new(new());
|
||||
}
|
||||
|
||||
public async Task<List<ModeusEmployee>> SearchEmployeeAsync(string fullname)
|
||||
{
|
||||
var response = await _http.GetFromJsonAsync<List<ModeusEmployee>>(
|
||||
$"/api/schedule/searchemployee?fullname={Uri.EscapeDataString(fullname)}");
|
||||
return response ?? new();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user