Added vector search

This commit is contained in:
2026-03-18 20:17:45 +00:00
parent ac1a910bff
commit 8f6e7e2214
8 changed files with 65 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ using System.Security.Claims;
using Seasoned.Backend.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Pgvector;
using Pgvector.EntityFrameworkCore;
namespace Seasoned.Backend.Controllers;
@@ -55,6 +57,9 @@ public class RecipeController : ControllerBase
UserId = userId
};
var fullText = $"{recipeDto.Title} {string.Join(" ", recipeDto.Ingredients)} {string.Join(" ", recipeDto.Instructions)}";
recipe.Embedding = await _recipeService.GetEmbeddingAsync(fullText);
_context.Recipes.Add(recipe);
await _context.SaveChangesAsync();
@@ -110,4 +115,23 @@ public class RecipeController : ControllerBase
var result = await _recipeService.ConsultChefAsync(request.Prompt);
return Ok(result);
}
[HttpGet("search")]
public async Task<ActionResult<IEnumerable<Recipe>>> SearchRecipes([FromQuery] string query)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(query))
return await GetMyRecipes();
var queryVector = await _recipeService.GetEmbeddingAsync(query);
var results = await _context.Recipes
.Where(r => r.UserId == userId)
.OrderBy(r => r.Embedding!.CosineDistance(queryVector))
.Take(15)
.ToListAsync();
return Ok(results);
}
}