Skip to content

Commit

Permalink
enable Server Sent Events (SSE) for gemini-1.0-pro
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Apr 1, 2024
1 parent 34e81c3 commit 939040c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Mscc.GenerativeAI/GenerativeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ public string? AccessToken
}
}

/// <summary>
/// You can enable Server Sent Events (SSE) for gemini-1.0-pro
/// </summary>
/// <remarks>
/// See <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events">Server-sent Events</a>
/// </remarks>
public bool UseServerSentEvents { get; set; } = false;

/// <summary>
/// Initializes a new instance of the <see cref="GenerativeModel"/> class.
/// The default constructor attempts to read <c>.env</c> file and environment variables.
Expand Down Expand Up @@ -614,6 +622,10 @@ public async Task<GenerateContentResponse> GenerateContent(GenerateContentReques
request.Tools ??= _tools;

var url = ParseUrl(Url, Method);
if (UseServerSentEvents && _model == GenerativeAI.Model.Gemini10Pro)
{
url = url.AddQueryString(new Dictionary<string, string?>() { ["key"] = "sse" });
}
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
Expand Down Expand Up @@ -703,6 +715,10 @@ public async IAsyncEnumerable<GenerateContentResponse> GenerateContentStream(Gen

var method = "streamGenerateContent";
var url = ParseUrl(Url, method);
if (UseServerSentEvents && _model == GenerativeAI.Model.Gemini10Pro)
{
url = url.AddQueryString(new Dictionary<string, string?>() { ["key"] = "sse" });
}

// Ref: https://code-maze.com/using-streams-with-httpclient-to-improve-performance-and-memory-usage/
// Ref: https://www.stevejgordon.co.uk/using-httpcompletionoption-responseheadersread-to-improve-httpclient-performance-dotnet
Expand Down

0 comments on commit 939040c

Please sign in to comment.