Generate Content - gunpal5/Google_GenerativeAI GitHub Wiki
This page explains how to generate content using the Google Generative AI C# SDK. There are two primary methods for generating content: using predefined GenerateContentAsync
methods and creating a GenerateContentRequest
object and passing it to GenerateContentAsync
. This page will detail both approaches and provide examples for each.
The SDK provides several overloaded GenerateContentAsync
methods to simplify common content generation scenarios. These methods offer convenient ways to generate content with text prompts, files, or a combination of both.
This method generates content based on a simple text prompt.
string prompt = "Write a short poem about nature.";
GenerateContentResponse response = await client.GenerateContentAsync(prompt);
// Access the generated text
string generatedText = response.Candidates[0].Content;
Console.WriteLine(generatedText);
This method allows you to include a file along with the text prompt. This is useful for multimodal content generation. For more details on multimodal capability visit Multimodal Features
string prompt = "Describe the image.";
string fileUri = "https://path/to/image.jpg";
string mimeType = "image/jpeg";
GenerateContentResponse response = await client.GenerateContentAsync(prompt, fileUri, mimeType);
string generatedText = response.Text();
Console.WriteLine(generatedText);
This method offers the most flexibility, allowing you to construct complex content requests using a sequence of Part
objects. This is useful for building more structured prompts, including different roles for each part.
var parts = new List<Part>
{
new Part { Text = "What are the benefits of using cloud computing?" },
new Part { Text = "Cloud computing offers scalability, cost-effectiveness, and increased accessibility." }
};
GenerateContentResponse response = await client.GenerateContentAsync(parts);
string generatedText = response.Text();
Console.WriteLine(generatedText);
For more advanced scenarios, you can create a GenerateContentRequest
object and customize it with various options. This allows for finer-grained control over the content generation process.
var request = new GenerateContentRequest();
// Add text input
request.AddText("Summarize the following article: ...");
// Or add content with role (using Content object and Parts)
var content = new Content { Role = Roles.User }; // Explicit role
content.AddText("Summarize the following article: ...");
// Or add content with Parts directly to the request
var partsForRequest = new List<Part> { new Part { Text = "Summarize the following article: ..." } };
request.AddParts(partsForRequest)
// Add a tool if needed
var tool = new Tool();
request.AddTool(tool);
// Use JSON mode if needed
request.UseJsonMode<MyResponseClass>();
// Set generation config
request.GenerationConfig = new GenerationConfig
{
Temperature = 0.7f,
MaxOutputTokens = 200
};
GenerateContentResponse response = await client.GenerateContentAsync(request);
string generatedText = response.Text();
Console.WriteLine(generatedText);
Extension methods are provided to simplify the creation of GenerateContentRequest
objects. These methods allow you to easily add text, files, tools, and configure JSON mode. For more details view JSON Mode Wiki Page
var request = new GenerateContentRequest();
// Add text input
request.AddText("Describe this image in detail.");
// Add a remote file
request.AddRemoteFile("http://mydomain.com/path/to/image.jpg", "image/jpeg");
GenerateContentResponse response = await client.GenerateContentAsync(request);
string generatedText = response.Text();
Console.WriteLine(generatedText);