Table of Contents

Class ChatSession

Namespace
GenerativeAI
Assembly
GenerativeAI.dll
public class ChatSession : GenerativeModel, IGenerativeModel
Inheritance
ChatSession
Implements
Inherited Members
Extension Methods

Constructors

ChatSession(ChatSessionBackUpData, IPlatformAdapter, List<IFunctionTool>?, HttpClient?, ILogger?)

public ChatSession(ChatSessionBackUpData chatSessionBackUpData, IPlatformAdapter platform, List<IFunctionTool>? toolList = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

chatSessionBackUpData ChatSessionBackUpData
platform IPlatformAdapter
toolList List<IFunctionTool>
httpClient HttpClient
logger ILogger

ChatSession(ChatSessionBackUpData, string, List<IFunctionTool>?, HttpClient?, ILogger?)

Initializes a new instance of the ChatSession class from backup data.

public ChatSession(ChatSessionBackUpData chatSessionBackUpData, string apiKey, List<IFunctionTool>? toolList = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

chatSessionBackUpData ChatSessionBackUpData

The backup data containing chat session state.

apiKey string

The API key for authentication.

toolList List<IFunctionTool>

Optional list of function tools for the session.

httpClient HttpClient

Optional HTTP client for making requests.

logger ILogger

Optional logger for diagnostic output.

ChatSession(List<Content>?, IPlatformAdapter, string, GenerationConfig?, ICollection<SafetySetting>?, string?, HttpClient?, ILogger?)

public ChatSession(List<Content>? history, IPlatformAdapter platform, string model, GenerationConfig? config = null, ICollection<SafetySetting>? safetySettings = null, string? systemInstruction = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

history List<Content>
platform IPlatformAdapter
model string
config GenerationConfig
safetySettings ICollection<SafetySetting>
systemInstruction string
httpClient HttpClient
logger ILogger

ChatSession(List<Content>?, string, ModelParams, HttpClient?, ILogger?)

public ChatSession(List<Content>? history, string apiKey, ModelParams modelParams, HttpClient? client = null, ILogger? logger = null)

Parameters

history List<Content>
apiKey string
modelParams ModelParams
client HttpClient
logger ILogger

ChatSession(List<Content>?, string, string, GenerationConfig?, ICollection<SafetySetting>?, string?, HttpClient?, ILogger?)

public ChatSession(List<Content>? history, string apiKey, string model, GenerationConfig? config = null, ICollection<SafetySetting>? safetySettings = null, string? systemInstruction = null, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

history List<Content>
apiKey string
model string
config GenerationConfig
safetySettings ICollection<SafetySetting>
systemInstruction string
httpClient HttpClient
logger ILogger

Properties

History

Gets or sets the history of the current chat session, representing a collection of user and model interactions recorded throughout the session. Updates to this property will reset both LastRequestContent and LastResponseContent.

public List<Content> History { get; set; }

Property Value

List<Content>

Remarks

Assigning a new value to this property effectively resets the session, clearing any previously tracked request or response content. The history contains instances of Content that represent messages exchanged between the user and the model.

LastRequestContent

Gets the content of the last request made in the current chat session, allowing access to the most recently sent message or query. This property is updated each time a request is successfully made within the session.

public Content? LastRequestContent { get; }

Property Value

Content

Remarks

The value of this property is null if no request has been made or if the session was reset by assigning a new history to the History property.

LastResponseContent

Gets the content of the last response received in the current chat session, providing access to the most recently generated reply or output from the model. This property is updated each time a response is successfully received within the session.

public Content? LastResponseContent { get; }

Property Value

Content

Remarks

The value of this property is null if no response has been received or if the session was reset by assigning a new history to the History property.

Methods

BeforeRegeneration(GenerateContentRequest, GenerateContentResponse)

protected override List<Content> BeforeRegeneration(GenerateContentRequest originalRequest, GenerateContentResponse response)

Parameters

originalRequest GenerateContentRequest

The request containing the original content to be processed.

response GenerateContentResponse

The response from which candidate content will be selected and added to the result.

Returns

List<Content>

A list of content objects that includes filtered and updated content based on the original request and response.

CreateChatSessionBackUpData()

public ChatSessionBackUpData CreateChatSessionBackUpData()

Returns

ChatSessionBackUpData

GenerateContentAsync(GenerateContentRequest, CancellationToken)

Generates content asynchronously based on the given input parameters.

public override Task<GenerateContentResponse> GenerateContentAsync(GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

request GenerateContentRequest

An instance of GenerateContentRequest containing the input configuration and settings for content generation.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<GenerateContentResponse>

A task that represents the asynchronous operation, containing the GenerateContentResponse with the generated content.

See Also

PrepareRequest(GenerateContentRequest)

Prepares the GenerateContentRequest for content generation by populating global properties, configuring JSON mode, adding tools, cached content, and validating the request.

protected override void PrepareRequest(GenerateContentRequest request)

Parameters

request GenerateContentRequest

The content generation request to be prepared with required settings and validations.

Remarks

This method ensures that the request is appropriately configured with default global parameters, tools, and any necessary system-level instructions before proceeding with content generation.

StreamContentAsync(GenerateContentRequest, CancellationToken)

Streams content generation results asynchronously based on a given request and optional cancellation token.

public override IAsyncEnumerable<GenerateContentResponse> StreamContentAsync(GenerateContentRequest request, CancellationToken cancellationToken = default)

Parameters

request GenerateContentRequest

The content generation request containing input configurations and options.

cancellationToken CancellationToken

An optional token to monitor for cancellation requests.

Returns

IAsyncEnumerable<GenerateContentResponse>

An asynchronous enumerable of GenerateContentResponse objects containing the streamed generation results.

See Also