Class MicrosoftExtensions
- Namespace
- GenerativeAI.Microsoft.Extensions
- Assembly
- GenerativeAI.Microsoft.dll
Provides extension methods for interoperability between Google_GenerativeAI and Microsoft.Extensions.AI models.
public static class MicrosoftExtensions
- Inheritance
-
MicrosoftExtensions
- Inherited Members
Methods
AsEmbeddingGenerator(IPlatformAdapter, string)
Creates a GenerativeAIEmbeddingGenerator from a platform adapter and optional model name.
public static GenerativeAIEmbeddingGenerator AsEmbeddingGenerator(this IPlatformAdapter adapter, string modelName = "models/gemini-embedding-001")
Parameters
adapterIPlatformAdapterThe platform adapter to use for API calls.
modelNamestringThe name of the embedding model to use. Defaults to "models/gemini-embedding-001".
Returns
- GenerativeAIEmbeddingGenerator
A new instance of GenerativeAIEmbeddingGenerator.
AsEmbeddingGenerator(string, string)
Creates a GenerativeAIEmbeddingGenerator from an API key and optional model name.
public static GenerativeAIEmbeddingGenerator AsEmbeddingGenerator(this string apiKey, string modelName = "models/gemini-embedding-001")
Parameters
apiKeystringThe API key for authenticating with Google AI.
modelNamestringThe name of the embedding model to use. Defaults to "models/gemini-embedding-001".
Returns
- GenerativeAIEmbeddingGenerator
A new instance of GenerativeAIEmbeddingGenerator.
GetFunctions(ChatResponse)
Retrieves the first occurrence of a function call content from the provided Microsoft.Extensions.AI.ChatResponse message contents.
public static List<FunctionCallContent>? GetFunctions(this ChatResponse response)
Parameters
responseChatResponseThe Microsoft.Extensions.AI.ChatResponse object containing the messages and their associated contents.
Returns
- List<FunctionCallContent>
A Microsoft.Extensions.AI.FunctionCallContent object if a function call is present; otherwise, null.
PromoteOversizedAttachmentsAsync(IList<ChatMessage>, GenerativeModel, CancellationToken)
Auto-promotes a Microsoft.Extensions.AI.DataContent to the Files API when its
payload exceeds Gemini's 20 MB inline_data limit. Walks the message
list, uploads any oversized binary attachments via model's
Files client, and rewrites those entries to point at the resulting
remote file URI. Cheap when nothing is oversized — the loop just
short-circuits. Call this BEFORE ToGenerateContentRequest(IEnumerable<ChatMessage>, ChatOptions?)
so the converted parts use FileData instead of InlineData.
public static Task PromoteOversizedAttachmentsAsync(this IList<ChatMessage> chatMessages, GenerativeModel model, CancellationToken cancellationToken = default)
Parameters
chatMessagesIList<ChatMessage>modelGenerativeModelcancellationTokenCancellationToken
Returns
ToAiContents(List<Part>?, ChatOptions?)
Transforms a list of Part objects into a collection of Microsoft.Extensions.AI.AIContent instances.
public static IList<AIContent> ToAiContents(this List<Part>? parts, ChatOptions? options = null)
Parameters
partsList<Part>The list of Part objects to be converted.
optionsChatOptionsOptional chat options for context-aware transformations.
Returns
- IList<AIContent>
A collection of Microsoft.Extensions.AI.AIContent instances derived from the provided Part objects, or null if the input list is null.
ToChatMessages(List<Content>?, ChatOptions?)
Converts a GenerateContentResponse instance into a Microsoft.Extensions.AI.ChatMessage.
public static IEnumerable<ChatMessage> ToChatMessages(this List<Content>? contents, ChatOptions? options = null)
Parameters
contentsList<Content>The Content to be transformed into a Microsoft.Extensions.AI.ChatMessage.
optionsChatOptionsOptional chat options used while converting the response (for example, tool schemas).
Returns
- IEnumerable<ChatMessage>
A Microsoft.Extensions.AI.ChatMessage representing the data contained in the GenerateContentResponse.
ToChatResponse(GenerateContentResponse?, ChatOptions?)
Transforms a GenerateContentResponse into a Microsoft.Extensions.AI.ChatResponse.
public static ChatResponse? ToChatResponse(this GenerateContentResponse? response, ChatOptions? options = null)
Parameters
responseGenerateContentResponseThe GenerateContentResponse instance to convert.
optionsChatOptionsOptional chat options used while converting the response (for example, tool schemas).
Returns
- ChatResponse
A Microsoft.Extensions.AI.ChatResponse object if the transformation is successful; otherwise, null.
ToChatResponseUpdate(GenerateContentResponse?, ChatOptions?)
Converts a GenerateContentResponse instance into a Microsoft.Extensions.AI.ChatResponseUpdate object.
public static ChatResponseUpdate ToChatResponseUpdate(this GenerateContentResponse? response, ChatOptions? options = null)
Parameters
responseGenerateContentResponseThe input GenerateContentResponse to transform into a chat response update.
optionsChatOptionsOptional chat options used while converting the response (for example, tool schemas).
Returns
- ChatResponseUpdate
A new Microsoft.Extensions.AI.ChatResponseUpdate object reflecting the data in the provided GenerateContentResponse.
ToGeminiEmbedContentRequest(IEnumerable<string>, EmbeddingGenerationOptions?)
Converts a collection of strings and optional embedding generation settings into an EmbedContentRequest.
public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string> values, EmbeddingGenerationOptions? options)
Parameters
valuesIEnumerable<string>The collection of strings to be embedded.
optionsEmbeddingGenerationOptionsOptional embedding generation settings, such as model identification.
Returns
- EmbedContentRequest
An EmbedContentRequest instance created from the provided values and options.
ToGenerateContentRequest(IEnumerable<ChatMessage>, ChatOptions?)
Converts a collection of Microsoft.Extensions.AI.ChatMessage objects into a GenerateContentRequest instance.
public static GenerateContentRequest ToGenerateContentRequest(this IEnumerable<ChatMessage> chatMessages, ChatOptions? options = null)
Parameters
chatMessagesIEnumerable<ChatMessage>The collection of chat messages to transform.
optionsChatOptionsOptional settings to customize the generation process.
Returns
- GenerateContentRequest
A GenerateContentRequest instance derived from the provided chat messages.
ToGeneratedEmbeddings(EmbedContentRequest, EmbedContentResponse)
Converts an EmbedContentRequest and an EmbedContentResponse into a Microsoft.Extensions.AI.GeneratedEmbeddings<TEmbedding> instance containing embeddings of type Embedding<float>.
public static GeneratedEmbeddings<Embedding<float>> ToGeneratedEmbeddings(EmbedContentRequest request, EmbedContentResponse response)
Parameters
requestEmbedContentRequestThe request containing the embedding parameters and metadata.
responseEmbedContentResponseThe response containing the embedding result.
Returns
- GeneratedEmbeddings<Embedding<float>>
A Microsoft.Extensions.AI.GeneratedEmbeddings<TEmbedding> instance containing the generated embeddings and associated metadata.
ToGenerationConfig(ChatOptions?)
Maps Microsoft.Extensions.AI.ChatOptions into a GenerationConfig object used by GenerativeAI.
public static GenerationConfig? ToGenerationConfig(this ChatOptions? options)
Parameters
optionsChatOptionsThe chat options defining parameters for content generation.
Returns
- GenerationConfig
A GenerationConfig instance or null, depending on the input.
ToJsonNodeResponse(object?)
Converts a given response object into a JsonNode representation.
public static JsonNode ToJsonNodeResponse(this object? response)
Parameters
responseobjectThe response object to convert. It can be of type Microsoft.Extensions.AI.FunctionResultContent, JsonNode, or other compatible types.
Returns
- JsonNode
A JsonNode instance representing the response. Throws an exception if the response is not a valid JSON node.
ToPart(AIContent, ChatOptions?)
Converts an Microsoft.Extensions.AI.AIContent instance into a Part object.
public static Part? ToPart(this AIContent content, ChatOptions? options = null)
Parameters
contentAIContentThe AI content to transform into a part object.
optionsChatOptionsOptional chat options for context-aware transformations.
Returns
WithTaskType(EmbeddingGenerationOptions, TaskType)
Sets the task type for embedding generation.
public static EmbeddingGenerationOptions WithTaskType(this EmbeddingGenerationOptions options, TaskType taskType)
Parameters
optionsEmbeddingGenerationOptionsThe embedding generation options to modify.
taskTypeTaskTypeThe task type for which the embedding will be optimized.
Returns
- EmbeddingGenerationOptions
The modified embedding generation options.