2025-02-22 20:51:06 +01:00
using System.Text ;
using AIStudio.Chat ;
namespace AIStudio.Tools.RAG ;
public static class IRetrievalContextExtensions
{
2026-06-10 21:01:27 +02:00
private static readonly ILogger < IRetrievalContext > LOGGER = Program . LOGGER_FACTORY . CreateLogger < IRetrievalContext >();
2025-02-22 20:51:06 +01:00
public static async Task < string > AsMarkdown ( this IReadOnlyList < IRetrievalContext > retrievalContexts , StringBuilder ? sb = null , CancellationToken token = default )
{
sb ??= new StringBuilder ();
var index = 0 ;
foreach ( var retrievalContext in retrievalContexts )
{
index ++;
await retrievalContext . AsMarkdown ( sb , index , retrievalContexts . Count , token );
}
return sb . ToString ();
}
public static async Task < string > AsMarkdown ( this IRetrievalContext retrievalContext , StringBuilder ? sb = null , int index = - 1 , int numTotalRetrievalContexts = - 1 , CancellationToken token = default )
{
sb ??= new StringBuilder ();
switch ( index )
{
case > 0 when numTotalRetrievalContexts is - 1 :
sb . AppendLine ( $"# Retrieval context {index}" );
break ;
case > 0 when numTotalRetrievalContexts > 0 :
sb . AppendLine ( $"# Retrieval context {index} of {numTotalRetrievalContexts}" );
break ;
default :
sb . AppendLine ( "# Retrieval context" );
break ;
}
sb . AppendLine ( $"Data source name: {retrievalContext.DataSourceName}" );
sb . AppendLine ( $"Content category: {retrievalContext.Category}" );
sb . AppendLine ( $"Content type: {retrievalContext.Type}" );
sb . AppendLine ( $"Content path: {retrievalContext.Path}" );
if ( retrievalContext . Links . Count > 0 )
{
sb . AppendLine ( "Additional links:" );
foreach ( var link in retrievalContext . Links )
sb . AppendLine ( $"- {link}" );
}
switch ( retrievalContext )
{
case RetrievalTextContext textContext :
sb . AppendLine ();
sb . AppendLine ( "Matched text content:" );
sb . AppendLine ( "````" );
sb . AppendLine ( textContext . MatchedText );
sb . AppendLine ( "````" );
if ( textContext . SurroundingContent . Count > 0 )
{
sb . AppendLine ();
sb . AppendLine ( "Surrounding text content:" );
foreach ( var surrounding in textContext . SurroundingContent )
{
sb . AppendLine ();
sb . AppendLine ( "````" );
sb . AppendLine ( surrounding );
sb . AppendLine ( "````" );
}
}
break ;
case RetrievalImageContext imageContext :
sb . AppendLine ();
sb . AppendLine ( "Matched image content as base64-encoded data:" );
sb . AppendLine ( "````" );
2025-12-30 18:30:32 +01:00
sb . AppendLine ( await imageContext . TryAsBase64 ( token ) is ( success : true , { } base64Image )
? base64Image
: string . Empty );
2025-02-22 20:51:06 +01:00
sb . AppendLine ( "````" );
break ;
default :
LOGGER . LogWarning ( $"The retrieval content type '{retrievalContext.Type}' of data source '{retrievalContext.DataSourceName}' at location '{retrievalContext.Path}' is not supported yet." );
break ;
}
sb . AppendLine ();
return sb . ToString ();
}
}