-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use .env and read environment variables
- Loading branch information
1 parent
16403ce
commit 297a776
Showing
3 changed files
with
37 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,57 @@ | ||
using System.Collections.Generic; | ||
#if NET472_OR_GREATER || NETSTANDARD2_0 | ||
using System; | ||
using System.Collections.Generic; | ||
#endif | ||
|
||
namespace Mscc.GenerativeAI | ||
{ | ||
public class VertexAI | ||
/// <summary> | ||
/// Entry point to access Gemini API running in Vertex AI. | ||
/// </summary> | ||
public sealed class VertexAI | ||
{ | ||
private readonly string projectId; | ||
private readonly string region; | ||
private readonly string? _projectId; | ||
private readonly string _region = "us-central1"; | ||
|
||
/// <summary> | ||
/// Default constructor attempts to read environment variables and | ||
/// sets default values, if available | ||
/// </summary> | ||
private VertexAI() | ||
{ | ||
GenerativeModelExtensions.ReadDotEnv(); | ||
|
||
_projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); | ||
_region = Environment.GetEnvironmentVariable("GOOGLE_REGION") ?? _region; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor to initialize access to Vertex AI Gemini API. | ||
/// </summary> | ||
/// <param name="projectId">Identifier of the Google Cloud project</param> | ||
/// <param name="region">Region to use</param> | ||
public VertexAI(string projectId, string region) | ||
public VertexAI(string projectId, string region) : this() | ||
{ | ||
this.projectId = projectId; | ||
this.region = region; | ||
_projectId ??= projectId; | ||
_region ??= region; | ||
} | ||
|
||
/// <summary> | ||
/// Create a generative model on Vertex AI to use. | ||
/// </summary> | ||
/// <param name="model">Model to use (default: "gemini-1.0-pro")</param> | ||
/// <param name="generationConfig"></param> | ||
/// <param name="safetySettings"></param> | ||
/// <param name="generationConfig">Optional. Configuration options for model generation and outputs.</param> | ||
/// <param name="safetySettings">Optional. A list of unique SafetySetting instances for blocking unsafe content.</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
/// <returns></returns> | ||
public GenerativeModel GenerativeModel(string model = Model.Gemini10Pro, | ||
GenerationConfig? generationConfig = null, | ||
List<SafetySetting>? safetySettings = null) | ||
{ | ||
return new GenerativeModel(projectId, region, model, generationConfig, safetySettings); | ||
if (_projectId is null) throw new ArgumentNullException("projectId"); | ||
if (_region is null) throw new ArgumentNullException("region"); | ||
|
||
return new GenerativeModel(_projectId, _region, model, generationConfig, safetySettings); | ||
} | ||
} | ||
} |