Добавьте файлы проекта.
This commit is contained in:
92
CodeBase/Services/VectorizationService.cs
Normal file
92
CodeBase/Services/VectorizationService.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using CodeBase.Models;
|
||||
|
||||
namespace CodeBase.Services
|
||||
{
|
||||
public class VectorizeRequest
|
||||
{
|
||||
public string text { get; set; }
|
||||
}
|
||||
|
||||
public class VectorizeResponse
|
||||
{
|
||||
public float[] vector { get; set; }
|
||||
}
|
||||
|
||||
public class VectorizationService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public VectorizationService()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
// Адрес нашего локального Python-сервиса
|
||||
_httpClient.BaseAddress = new Uri("http://localhost:8000/");
|
||||
}
|
||||
|
||||
|
||||
public async Task<float[]> GetVectorAsync(string text)
|
||||
{
|
||||
var requestBody = new VectorizeRequest { text = text };
|
||||
|
||||
try
|
||||
{
|
||||
// Стучимся на наш Python-сервер (порт 8000)
|
||||
var response = await _httpClient.PostAsJsonAsync("vectorize", requestBody);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var result = await response.Content.ReadFromJsonAsync<VectorizeResponse>();
|
||||
return result?.vector;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[-] Ошибка API при векторизации вопроса: {response.StatusCode}");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[-] Ошибка подключения к Python-сервису: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<CodeChunk>> EnrichChunksWithVectorsAsync(List<CodeChunk> chunks)
|
||||
{
|
||||
foreach (var chunk in chunks)
|
||||
{
|
||||
// 1. Склеиваем контекст
|
||||
// Мы даем нейросети подсказку о том, где именно лежит этот код
|
||||
string contextText = $"File: {chunk.FilePath}\nClass: {chunk.ClassName}\nMethod: {chunk.MethodName}\nCode:\n{chunk.Content}";
|
||||
|
||||
var requestBody = new VectorizeRequest { text = contextText };
|
||||
|
||||
try
|
||||
{
|
||||
// 2. Стучимся в Python
|
||||
var response = await _httpClient.PostAsJsonAsync("vectorize", requestBody);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 3. Достаем массив чисел (обычно 768 элементов для GraphCodeBERT)
|
||||
var result = await response.Content.ReadFromJsonAsync<VectorizeResponse>();
|
||||
if (result != null && result.vector != null)
|
||||
{
|
||||
// 4. Сохраняем вектор прямо в наш объект в памяти
|
||||
chunk.Vector = result.vector;
|
||||
Console.WriteLine($"[+] Векторизован метод: {chunk.MethodName}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[-] Ошибка API для {chunk.MethodName}: {response.StatusCode}");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[-] Ошибка подключения к Python-сервису: {ex.Message}");
|
||||
}
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user