Skip to content

Commit

Permalink
system instruction is an instance of content
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Apr 12, 2024
1 parent a923ca0 commit a6bd5bb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 38 deletions.
7 changes: 7 additions & 0 deletions src/Mscc.GenerativeAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- implement Automatic Function Call (AFC)
- implement Server-Sent Events (SSE)

### Changed
### Fixed

## 1.1.3

### Changed

- improve Grounding for Google Search and Vertex AI Search

### Fixed

- system instruction is an instance of content, not a list of same.

## 1.1.2

### Added
Expand Down
6 changes: 3 additions & 3 deletions src/Mscc.GenerativeAI/GenerativeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GenerativeModel
private List<SafetySetting>? _safetySettings;
private GenerationConfig? _generationConfig;
private List<Tool>? _tools;
private List<Content>? _systemInstruction;
private Content? _systemInstruction;

#if NET472_OR_GREATER || NETSTANDARD2_0
private static readonly Version _httpVersion = HttpVersion.Version11;
Expand Down Expand Up @@ -236,7 +236,7 @@ internal GenerativeModel(string? apiKey = null,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
ApiKey = apiKey ?? _apiKey;
Model = model ?? _model;
Expand All @@ -261,7 +261,7 @@ internal GenerativeModel(string? projectId = null, string? region = null,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
_useVertexAi = true;
AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN") ??
Expand Down
2 changes: 1 addition & 1 deletion src/Mscc.GenerativeAI/GoogleAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public GenerativeModel GenerativeModel(string model = Model.Gemini10Pro,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null)
Content? systemInstruction = null)
{
if (_apiKey is null && _accessToken is null)
throw new ArgumentNullException("apiKey or accessToken",
Expand Down
2 changes: 1 addition & 1 deletion src/Mscc.GenerativeAI/IGenerativeAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public GenerativeModel GenerativeModel(string model = Model.Gemini10Pro,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null);
Content? systemInstruction = null);

/// <summary>
/// Gets information about a specific Model.
Expand Down
10 changes: 5 additions & 5 deletions src/Mscc.GenerativeAI/Types/GenerateContentRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class GenerateContentRequest
/// The text strings count toward the token limit.
/// The role field of systemInstruction is ignored and doesn't affect the performance of the model.
/// </summary>
public List<Content>? SystemInstruction { get; set; }
public Content? SystemInstruction { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="GenerateContentRequest"/> class.
Expand All @@ -58,7 +58,7 @@ public GenerateContentRequest(string prompt,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
if (prompt == null) throw new ArgumentNullException(nameof(prompt));

Expand Down Expand Up @@ -89,7 +89,7 @@ public GenerateContentRequest(List<IPart> parts,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
if (parts == null) throw new ArgumentNullException(nameof(parts));

Expand Down Expand Up @@ -117,7 +117,7 @@ public GenerateContentRequest(FileResource file,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
if (file == null) throw new ArgumentNullException(nameof(file));

Expand Down Expand Up @@ -145,7 +145,7 @@ public GenerateContentRequest(List<Part> parts,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null) : this()
Content? systemInstruction = null) : this()
{
if (parts == null) throw new ArgumentNullException(nameof(parts));

Expand Down
2 changes: 1 addition & 1 deletion src/Mscc.GenerativeAI/VertexAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public GenerativeModel GenerativeModel(string model = Model.Gemini10Pro,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null,
List<Content>? systemInstruction = null)
Content? systemInstruction = null)
{
if (_projectId is null) throw new ArgumentNullException("projectId");
if (_region is null) throw new ArgumentNullException("region");
Expand Down
23 changes: 5 additions & 18 deletions tests/Mscc.GenerativeAI/GoogleAi_Gemini15Pro_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,10 @@ public async void Generate_Content_SystemInstruction()
{
// Arrange
var prompt = "Good morning! How are you?";
var parts = new List<IPart>
var systemInstruction = new Content
{
new TextData { Text = "You are a cat. Your name is Neko." }
Parts = new() { new TextData { Text = "You are a friendly pirate. Speak like one." }}
};
var systemInstruction = new List<Content> { new Content
{
Role = Role.System,
Parts = parts
}};
IGenerativeAI genAi = new GoogleAI(_fixture.ApiKey);
var model = genAi.GenerativeModel(_model, systemInstruction: systemInstruction);
var request = new GenerateContentRequest(prompt);
Expand All @@ -597,15 +592,10 @@ public async void Generate_Content_SystemInstruction_WithSafetySettings()
// Arrange
var prompt = @"User input: I like bagels.
Answer:";
var parts = new List<IPart>
var systemInstruction = new Content
{
new TextData { Text = "You are a helpful language translator. Your mission is to translate text in English to French." }
Parts = new() { new TextData { Text = "You are a helpful language translator. Your mission is to translate text in English to French." }}
};
var systemInstruction = new List<Content> { new Content
{
Role = Role.System,
Parts = parts
}};
var generationConfig = new GenerationConfig()
{
Temperature = 0.9f,
Expand All @@ -632,10 +622,7 @@ public async void Generate_Content_SystemInstruction_WithSafetySettings()
response.Should().NotBeNull();
response.Candidates.Should().NotBeNull().And.HaveCount(1);
response.Text.Should().NotBeNull();
_output.WriteLine($"Answer:\n{response?.Text}");
_output.WriteLine($"Usage metadata: {response.UsageMetadata.TotalTokenCount}");
_output.WriteLine($"Finish reason: {response.Candidates[0].FinishReason}");
_output.WriteLine($"Safety settings: {response.Candidates[0].SafetyRatings}");
_output.WriteLine($"{prompt} {response?.Text}");
}

[Fact(Skip = "URL scheme not supported")]
Expand Down
12 changes: 3 additions & 9 deletions tests/Mscc.GenerativeAI/VertexAi_Gemini15Pro_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,10 @@ public async void Generate_Content_SystemInstruction()
// Arrange
var prompt = @"User input: I like bagels.
Answer:";
var parts = new List<IPart>
var systemInstruction = new Content
{
new TextData { Text = "You are a helpful language translator." },
new TextData { Text = "Your mission is to translate text in English to French." }
Parts = new() { new TextData { Text = "You are a friendly pirate. Speak like one. Your mission is to translate text in English to French." }}
};
var systemInstruction = new List<Content> { new Content
{
Role = Role.System,
Parts = parts
}};
var generationConfig = new GenerationConfig()
{
Temperature = 0.9f,
Expand Down Expand Up @@ -282,7 +276,7 @@ public async void Generate_Content_SystemInstruction()
response.Should().NotBeNull();
response.Candidates.Should().NotBeNull().And.HaveCount(1);
response.Text.Should().NotBeNull();
_output.WriteLine($"Answer:\n{response?.Text}");
_output.WriteLine($"{prompt} {response?.Text}");
_output.WriteLine($"Usage metadata: {response.UsageMetadata.TotalTokenCount}");
_output.WriteLine($"Finish reason: {response.Candidates[0].FinishReason}");
_output.WriteLine($"Safety settings: {response.Candidates[0].SafetyRatings}");
Expand Down

0 comments on commit a6bd5bb

Please sign in to comment.