Skip to content

Commit

Permalink
Update service registrations and rename DocumentService methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Feb 10, 2025
1 parent a5f8425 commit 9e844f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions SqlDatabaseVectorSearch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
builder.Services.AddSingleton<TokenizerService>();
builder.Services.AddSingleton<ChatService>();

builder.Services.AddScoped<VectorSearchService>();
builder.Services.AddScoped<DocumentService>();
builder.Services.AddScoped<VectorSearchService>();

builder.Services.AddKeyedSingleton<IContentDecoder, PdfContentDecoder>(MediaTypeNames.Application.Pdf);
builder.Services.AddKeyedSingleton<IContentDecoder, DocxContentDecoder>("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Expand Down Expand Up @@ -130,22 +130,22 @@ async IAsyncEnumerable<QuestionResponse> Stream()

documentsApiGroup.MapGet(string.Empty, async (DocumentService documentService, CancellationToken cancellationToken) =>
{
var documents = await documentService.GetDocumentsAsync(cancellationToken);
var documents = await documentService.GetAsync(cancellationToken);
return TypedResults.Ok(documents);
})
.WithSummary("Gets the list of documents");

documentsApiGroup.MapGet("{documentId:guid}/chunks", async (Guid documentId, DocumentService documentService, CancellationToken cancellationToken) =>
{
var documents = await documentService.GetDocumentChunksAsync(documentId, cancellationToken);
var documents = await documentService.GetChunksAsync(documentId, cancellationToken);
return TypedResults.Ok(documents);
})
.WithSummary("Gets the list of chunks of a given document")
.WithDescription("The list does not contain embedding. Use '/api/documents/{documentId}/chunks/{documentChunkId}' to get the embedding for a given chunk.");

documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", async Task<Results<Ok<DocumentChunk>, NotFound>> (Guid documentId, Guid documentChunkId, DocumentService documentService, CancellationToken cancellationToken) =>
{
var chunk = await documentService.GetDocumentChunkEmbeddingAsync(documentId, documentChunkId, cancellationToken);
var chunk = await documentService.GetChunkEmbeddingAsync(documentId, documentChunkId, cancellationToken);
if (chunk is null)
{
return TypedResults.NotFound();
Expand All @@ -158,7 +158,7 @@ async IAsyncEnumerable<QuestionResponse> Stream()

documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, DocumentService documentService, CancellationToken cancellationToken) =>
{
await documentService.DeleteDocumentAsync(documentId, cancellationToken);
await documentService.DeleteAsync(documentId, cancellationToken);
return TypedResults.NoContent();
})
.WithSummary("Deletes a document")
Expand Down
16 changes: 8 additions & 8 deletions SqlDatabaseVectorSearch/Services/DocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ namespace SqlDatabaseVectorSearch.Services;

public class DocumentService(ApplicationDbContext dbContext)
{
public async Task<IEnumerable<Document>> GetDocumentsAsync(CancellationToken cancellationToken = default)
public async Task<IEnumerable<Document>> GetAsync(CancellationToken cancellationToken = default)
{
var documents = await dbContext.Documents.OrderBy(d => d.Name)
.Select(d => new Document(d.Id, d.Name, d.CreationDate, d.Chunks.Count))
.ToListAsync(cancellationToken: cancellationToken);
.ToListAsync(cancellationToken);

return documents;
}

public async Task<IEnumerable<DocumentChunk>> GetDocumentChunksAsync(Guid documentId, CancellationToken cancellationToken = default)
public async Task<IEnumerable<DocumentChunk>> GetChunksAsync(Guid documentId, CancellationToken cancellationToken = default)
{
var documentChunks = await dbContext.DocumentChunks.Where(c => c.DocumentId == documentId).OrderBy(c => c.Index)
.Select(c => new DocumentChunk(c.Id, c.Index, c.Content, null))
.ToListAsync(cancellationToken: cancellationToken);
.ToListAsync(cancellationToken);

return documentChunks;
}

public async Task<DocumentChunk?> GetDocumentChunkEmbeddingAsync(Guid documentId, Guid documentChunkId, CancellationToken cancellationToken = default)
public async Task<DocumentChunk?> GetChunkEmbeddingAsync(Guid documentId, Guid documentChunkId, CancellationToken cancellationToken = default)
{
var documentChunk = await dbContext.DocumentChunks.Where(c => c.Id == documentChunkId && c.DocumentId == documentId)
.Select(c => new DocumentChunk(c.Id, c.Index, c.Content, c.Embedding))
.FirstOrDefaultAsync(cancellationToken: cancellationToken);
.FirstOrDefaultAsync(cancellationToken);

return documentChunk;
}

public Task DeleteDocumentAsync(Guid documentId, CancellationToken cancellationToken = default)
=> dbContext.Documents.Where(d => d.Id == documentId).ExecuteDeleteAsync(cancellationToken: cancellationToken);
public Task DeleteAsync(Guid documentId, CancellationToken cancellationToken = default)
=> dbContext.Documents.Where(d => d.Id == documentId).ExecuteDeleteAsync(cancellationToken);
}
2 changes: 1 addition & 1 deletion SqlDatabaseVectorSearch/Services/VectorSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<ImportDocumentResponse> ImportAsync(Stream stream, string name
if (documentId.HasValue)
{
// If the user is importing a document that already exists, delete the previous one.
await documentService.DeleteDocumentAsync(documentId.Value, cancellationToken);
await documentService.DeleteAsync(documentId.Value, cancellationToken);
}

var document = new Entities.Document { Id = documentId.GetValueOrDefault(), Name = name, CreationDate = timeProvider.GetUtcNow() };
Expand Down

0 comments on commit 9e844f8

Please sign in to comment.