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
chatSessionBackUpDataChatSessionBackUpDataplatformIPlatformAdaptertoolListList<IFunctionTool>httpClientHttpClientloggerILogger
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
chatSessionBackUpDataChatSessionBackUpDataThe backup data containing chat session state.
apiKeystringThe API key for authentication.
toolListList<IFunctionTool>Optional list of function tools for the session.
httpClientHttpClientOptional HTTP client for making requests.
loggerILoggerOptional 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
historyList<Content>platformIPlatformAdaptermodelstringconfigGenerationConfigsafetySettingsICollection<SafetySetting>systemInstructionstringhttpClientHttpClientloggerILogger
ChatSession(List<Content>?, string, ModelParams, HttpClient?, ILogger?)
public ChatSession(List<Content>? history, string apiKey, ModelParams modelParams, HttpClient? client = null, ILogger? logger = null)
Parameters
historyList<Content>apiKeystringmodelParamsModelParamsclientHttpClientloggerILogger
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
historyList<Content>apiKeystringmodelstringconfigGenerationConfigsafetySettingsICollection<SafetySetting>systemInstructionstringhttpClientHttpClientloggerILogger
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
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
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
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
originalRequestGenerateContentRequestThe request containing the original content to be processed.
responseGenerateContentResponseThe 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
GenerateContentAsync(GenerateContentRequest, CancellationToken)
Generates content asynchronously based on the given input parameters.
public override Task<GenerateContentResponse> GenerateContentAsync(GenerateContentRequest request, CancellationToken cancellationToken = default)
Parameters
requestGenerateContentRequestAn instance of GenerateContentRequest containing the input configuration and settings for content generation.
cancellationTokenCancellationTokenToken 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
requestGenerateContentRequestThe 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
requestGenerateContentRequestThe content generation request containing input configurations and options.
cancellationTokenCancellationTokenAn optional token to monitor for cancellation requests.
Returns
- IAsyncEnumerable<GenerateContentResponse>
An asynchronous enumerable of GenerateContentResponse objects containing the streamed generation results.
- See Also