Skip to content

Commit

Permalink
use .env and read environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Mar 21, 2024
1 parent 16403ce commit 297a776
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/Mscc.GenerativeAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- compatibility methods for PaLM models
- Feature suggestion: Retry mechanism ([#2](https://github.com/mscraftsman/generative-ai/issues/2))

### Changed
### Fixed
Expand All @@ -19,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- missing comments and better explanations
- add GoogleAI type ([#3](https://github.com/mscraftsman/generative-ai/issues/3))
- read environment variables in GoogleAI and VertexAI

## 0.8.3

Expand Down
2 changes: 2 additions & 0 deletions src/Mscc.GenerativeAI/GoogleAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public sealed class GoogleAI
/// </summary>
private GoogleAI()
{
GenerativeModelExtensions.ReadDotEnv();

_apiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
}

Expand Down
42 changes: 32 additions & 10 deletions src/Mscc.GenerativeAI/VertexAI.cs
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);
}
}
}

0 comments on commit 297a776

Please sign in to comment.