|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: API Reference |
| 4 | +description: Complete API documentation with examples and usage patterns for SmartRAG |
| 5 | +lang: en |
| 6 | +--- |
| 7 | + |
| 8 | +# API Reference |
| 9 | + |
| 10 | +Complete API documentation with examples and usage patterns for SmartRAG. |
| 11 | + |
| 12 | +## Core Interfaces |
| 13 | + |
| 14 | +### IDocumentService |
| 15 | + |
| 16 | +The main service for document operations. |
| 17 | + |
| 18 | +```csharp |
| 19 | +public interface IDocumentService |
| 20 | +{ |
| 21 | + Task<Document> UploadDocumentAsync(IFormFile file); |
| 22 | + Task<IEnumerable<Document>> GetAllDocumentsAsync(); |
| 23 | + Task<Document> GetDocumentByIdAsync(string id); |
| 24 | + Task<bool> DeleteDocumentAsync(string id); |
| 25 | + Task<IEnumerable<DocumentChunk>> SearchDocumentsAsync(string query, int maxResults = 10); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### IDocumentParserService |
| 30 | + |
| 31 | +Service for parsing and processing documents. |
| 32 | + |
| 33 | +```csharp |
| 34 | +public interface IDocumentParserService |
| 35 | +{ |
| 36 | + Task<string> ExtractTextAsync(IFormFile file); |
| 37 | + Task<IEnumerable<DocumentChunk>> ParseDocumentAsync(string text, string documentId); |
| 38 | + Task<IEnumerable<DocumentChunk>> ParseDocumentAsync(Stream stream, string fileName, string documentId); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### IDocumentRepository |
| 43 | + |
| 44 | +Repository for document storage operations. |
| 45 | + |
| 46 | +```csharp |
| 47 | +public interface IDocumentRepository |
| 48 | +{ |
| 49 | + Task<Document> AddAsync(Document document); |
| 50 | + Task<Document> GetByIdAsync(string id); |
| 51 | + Task<IEnumerable<Document>> GetAllAsync(); |
| 52 | + Task<bool> DeleteAsync(string id); |
| 53 | + Task<IEnumerable<DocumentChunk>> SearchAsync(string query, int maxResults = 10); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Models |
| 58 | + |
| 59 | +### Document |
| 60 | + |
| 61 | +Represents a document in the system. |
| 62 | + |
| 63 | +```csharp |
| 64 | +public class Document |
| 65 | +{ |
| 66 | + public string Id { get; set; } |
| 67 | + public string FileName { get; set; } |
| 68 | + public string FileType { get; set; } |
| 69 | + public long FileSize { get; set; } |
| 70 | + public DateTime UploadDate { get; set; } |
| 71 | + public string Content { get; set; } |
| 72 | + public IEnumerable<DocumentChunk> Chunks { get; set; } |
| 73 | + public Dictionary<string, object> Metadata { get; set; } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### DocumentChunk |
| 78 | + |
| 79 | +Represents a chunk of a document. |
| 80 | + |
| 81 | +```csharp |
| 82 | +public class DocumentChunk |
| 83 | +{ |
| 84 | + public string Id { get; set; } |
| 85 | + public string DocumentId { get; set; } |
| 86 | + public string Content { get; set; } |
| 87 | + public int ChunkIndex { get; set; } |
| 88 | + public float[] Embedding { get; set; } |
| 89 | + public Dictionary<string, object> Metadata { get; set; } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### SmartRagOptions |
| 94 | + |
| 95 | +Configuration options for SmartRAG. |
| 96 | + |
| 97 | +```csharp |
| 98 | +public class SmartRagOptions |
| 99 | +{ |
| 100 | + public AIProvider AIProvider { get; set; } |
| 101 | + public StorageProvider StorageProvider { get; set; } |
| 102 | + public string ApiKey { get; set; } |
| 103 | + public string ModelName { get; set; } |
| 104 | + public int ChunkSize { get; set; } = 1000; |
| 105 | + public int ChunkOverlap { get; set; } = 200; |
| 106 | + public string QdrantUrl { get; set; } |
| 107 | + public string CollectionName { get; set; } |
| 108 | + public string RedisConnectionString { get; set; } |
| 109 | + public int DatabaseId { get; set; } |
| 110 | + public string ConnectionString { get; set; } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Enums |
| 115 | + |
| 116 | +### AIProvider |
| 117 | + |
| 118 | +```csharp |
| 119 | +public enum AIProvider |
| 120 | +{ |
| 121 | + Anthropic, |
| 122 | + OpenAI, |
| 123 | + AzureOpenAI, |
| 124 | + Gemini, |
| 125 | + Custom |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### StorageProvider |
| 130 | + |
| 131 | +```csharp |
| 132 | +public enum StorageProvider |
| 133 | +{ |
| 134 | + Qdrant, |
| 135 | + Redis, |
| 136 | + Sqlite, |
| 137 | + InMemory, |
| 138 | + FileSystem, |
| 139 | + Custom |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## Service Registration |
| 144 | + |
| 145 | +### AddSmartRAG Extension |
| 146 | + |
| 147 | +```csharp |
| 148 | +public static class ServiceCollectionExtensions |
| 149 | +{ |
| 150 | + public static IServiceCollection AddSmartRAG( |
| 151 | + this IServiceCollection services, |
| 152 | + Action<SmartRagOptions> configureOptions) |
| 153 | + { |
| 154 | + var options = new SmartRagOptions(); |
| 155 | + configureOptions(options); |
| 156 | + |
| 157 | + services.Configure<SmartRagOptions>(opt => |
| 158 | + { |
| 159 | + opt.AIProvider = options.AIProvider; |
| 160 | + opt.StorageProvider = options.StorageProvider; |
| 161 | + opt.ApiKey = options.ApiKey; |
| 162 | + // ... other options |
| 163 | + }); |
| 164 | + |
| 165 | + // Register services based on configuration |
| 166 | + services.AddScoped<IDocumentService, DocumentService>(); |
| 167 | + services.AddScoped<IDocumentParserService, DocumentParserService>(); |
| 168 | + |
| 169 | + // Register appropriate repository |
| 170 | + switch (options.StorageProvider) |
| 171 | + { |
| 172 | + case StorageProvider.Qdrant: |
| 173 | + services.AddScoped<IDocumentRepository, QdrantDocumentRepository>(); |
| 174 | + break; |
| 175 | + case StorageProvider.Redis: |
| 176 | + services.AddScoped<IDocumentRepository, RedisDocumentRepository>(); |
| 177 | + break; |
| 178 | + // ... other cases |
| 179 | + } |
| 180 | + |
| 181 | + return services; |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +## Usage Examples |
| 187 | + |
| 188 | +### Basic Document Upload |
| 189 | + |
| 190 | +```csharp |
| 191 | +[HttpPost("upload")] |
| 192 | +public async Task<ActionResult<Document>> UploadDocument(IFormFile file) |
| 193 | +{ |
| 194 | + try |
| 195 | + { |
| 196 | + var document = await _documentService.UploadDocumentAsync(file); |
| 197 | + return Ok(document); |
| 198 | + } |
| 199 | + catch (Exception ex) |
| 200 | + { |
| 201 | + return BadRequest(ex.Message); |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### Document Search |
| 207 | + |
| 208 | +```csharp |
| 209 | +[HttpGet("search")] |
| 210 | +public async Task<ActionResult<IEnumerable<DocumentChunk>>> SearchDocuments( |
| 211 | + [FromQuery] string query, |
| 212 | + [FromQuery] int maxResults = 10) |
| 213 | +{ |
| 214 | + try |
| 215 | + { |
| 216 | + var results = await _documentService.SearchDocumentsAsync(query, maxResults); |
| 217 | + return Ok(results); |
| 218 | + } |
| 219 | + catch (Exception ex) |
| 220 | + { |
| 221 | + return BadRequest(ex.Message); |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +### Custom Configuration |
| 227 | + |
| 228 | +```csharp |
| 229 | +services.AddSmartRAG(options => |
| 230 | +{ |
| 231 | + options.AIProvider = AIProvider.Anthropic; |
| 232 | + options.StorageProvider = StorageProvider.Qdrant; |
| 233 | + options.ApiKey = Configuration["SmartRAG:ApiKey"]; |
| 234 | + options.ChunkSize = 800; |
| 235 | + options.ChunkOverlap = 150; |
| 236 | + options.QdrantUrl = "http://localhost:6333"; |
| 237 | + options.CollectionName = "my_documents"; |
| 238 | +}); |
| 239 | +``` |
| 240 | + |
| 241 | +## Error Handling |
| 242 | + |
| 243 | +### Common Exceptions |
| 244 | + |
| 245 | +```csharp |
| 246 | +public class SmartRagException : Exception |
| 247 | +{ |
| 248 | + public SmartRagException(string message) : base(message) { } |
| 249 | + public SmartRagException(string message, Exception innerException) |
| 250 | + : base(message, innerException) { } |
| 251 | +} |
| 252 | + |
| 253 | +public class DocumentProcessingException : SmartRagException |
| 254 | +{ |
| 255 | + public DocumentProcessingException(string message) : base(message) { } |
| 256 | +} |
| 257 | + |
| 258 | +public class StorageException : SmartRagException |
| 259 | +{ |
| 260 | + public StorageException(string message) : base(message) { } |
| 261 | +} |
| 262 | +``` |
| 263 | + |
| 264 | +### Error Response Model |
| 265 | + |
| 266 | +```csharp |
| 267 | +public class ErrorResponse |
| 268 | +{ |
| 269 | + public string Message { get; set; } |
| 270 | + public string ErrorCode { get; set; } |
| 271 | + public DateTime Timestamp { get; set; } |
| 272 | + public string RequestId { get; set; } |
| 273 | +} |
| 274 | +``` |
| 275 | + |
| 276 | +## Logging |
| 277 | + |
| 278 | +### Logger Messages |
| 279 | + |
| 280 | +```csharp |
| 281 | +public static class ServiceLogMessages |
| 282 | +{ |
| 283 | + public static readonly Action<ILogger, string, Exception> DocumentUploadStarted = |
| 284 | + LoggerMessage.Define<string>(LogLevel.Information, |
| 285 | + new EventId(1001, nameof(DocumentUploadStarted)), |
| 286 | + "Document upload started for file: {FileName}"); |
| 287 | + |
| 288 | + public static readonly Action<ILogger, string, Exception> DocumentUploadCompleted = |
| 289 | + LoggerMessage.Define<string>(LogLevel.Information, |
| 290 | + new EventId(1002, nameof(DocumentUploadCompleted)), |
| 291 | + "Document upload completed for file: {FileName}"); |
| 292 | +} |
| 293 | +``` |
| 294 | + |
| 295 | +## Performance Considerations |
| 296 | + |
| 297 | +### Chunking Strategy |
| 298 | + |
| 299 | +- **Small chunks**: Better for precise search, more API calls |
| 300 | +- **Large chunks**: Better context, fewer API calls |
| 301 | +- **Overlap**: Ensures important information isn't split |
| 302 | + |
| 303 | +### Batch Operations |
| 304 | + |
| 305 | +```csharp |
| 306 | +public async Task<IEnumerable<Document>> UploadDocumentsAsync(IEnumerable<IFormFile> files) |
| 307 | +{ |
| 308 | + var tasks = files.Select(file => UploadDocumentAsync(file)); |
| 309 | + return await Task.WhenAll(tasks); |
| 310 | +} |
| 311 | +``` |
| 312 | + |
| 313 | +## Need Help? |
| 314 | + |
| 315 | +If you need assistance with the API: |
| 316 | + |
| 317 | +- [Back to Documentation]({{ site.baseurl }}/en/) - Main documentation |
| 318 | +- [Open an issue](https://github.com/byerlikaya/SmartRAG/issues) - GitHub Issues |
| 319 | +- [Contact support](mailto:b.yerlikaya@outlook.com) - Email support |
0 commit comments