Chat Session - gunpal5/Google_GenerativeAI GitHub Wiki

Introduction

This page explains how to create and use chat sessions with the Google Generative AI C# SDK. Chat sessions provide a way to maintain context and history across multiple interactions with the model, allowing for more natural and engaging conversations. You interact with a chat session using the familiar GenerateContentAsync and StreamContentAsync methods, leveraging the session's built-in history management.

Creating a Chat Session

You can create a chat session using the StartChat method of GenerativeModel. This method initializes a ChatSession object, which manages the conversation history and settings.

ChatSession session = model.StartChat(); // Starts a new chat session

The StartChat method accepts several optional parameters:

  • history: A list of Content objects representing the initial chat history. This allows you to start a conversation with a pre-existing context. If not provided, the session starts with an empty history.
  • config: A GenerationConfig object to customize the model's generation behavior for the session (e.g., temperature, max output tokens). If not provided, the client's default configuration is used.
  • safetySettings: A collection of SafetySetting objects to configure safety filters for the session. If not provided, the client's default safety settings are used.
  • systemInstruction: A string containing instructions for the model that sets the overall behavior of the assistant.

Example with History

var history = new List<Content>
{
    new Content("Hello!", Roles.User),
    new Content("Hi there! How can I help you?", Roles.Model)
};

ChatSession sessionWithHistory = client.StartChat(history: history);

Example with Configuration

var config = new GenerationConfig
{
    Temperature = 0.8f,
    MaxOutputTokens = 100
};

ChatSession sessionWithConfig = client.StartChat(config: config);

Interacting with the Chat Session

Once you have a ChatSession object, you can use it to send messages and receive responses using the GenerateContentAsync and StreamContentAsync methods. The ChatSession object automatically manages the conversation history.

Using GenerateContentAsync

ChatSession session = client.StartChat();

string userMessage = "What is the capital of France?";

// Correct way to use GenerateContentAsync within a ChatSession:

GenerateContentResponse response = await session.GenerateContentAsync(userMessage); // Pass the request

Console.WriteLine($"Assistant: {response.Text()}");

userMessage = "And what is its population?";

response = await session.GenerateContentAsync(userMessage);
Console.WriteLine($"Assistant: {response.Text()}");

Using StreamContentAsync

ChatSession session = client.StartChat();

string userMessage = "Tell me a story.";

var request = new GenerateContentRequest();
request.AddText(userMessage);

await foreach (var response in session.StreamContentAsync(request)) 
{
    Console.Write(response.Text());
}

// Or simplified with overloaded method:

await foreach (var response in session.StreamContentAsync(userMessage)) 
{
    Console.Write(response.Text());
}

API Reference

⚠️ **GitHub.com Fallback** ⚠️