Skip to content

Commit

Permalink
Merge pull request #43 from mscraftsman/logging
Browse files Browse the repository at this point in the history
Feature suggestion: Add logs with LogLevel using the Standard logging in .NET
  • Loading branch information
jochenkirstaetter authored Oct 30, 2024
2 parents f3bbd4e + 3763c88 commit aa52b71
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 469 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,4 @@ FodyWeavers.xsd
**/appsettings.Development.json
**/client_secret*.json
*.p12
*.DotSettings
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Alternatively, add the following line to your `.csproj` file.

```text
<ItemGroup>
<PackageReference Include="Mscc.GenerativeAI" Version="1.8.0" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.8.1" />
</ItemGroup>
```

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.0
1.8.1
6 changes: 6 additions & 0 deletions src/Mscc.GenerativeAI.Google/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
### Fixed

## 1.8.1

### Changed

- bump version

## 1.8.0

### Changed
Expand Down
6 changes: 6 additions & 0 deletions src/Mscc.GenerativeAI.Web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
### Fixed

## 1.8.1

### Changed

- bump version

## 1.8.0

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;
#endif
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
Expand All @@ -16,19 +17,18 @@

namespace Mscc.GenerativeAI
{
public abstract class BaseGeneration
public abstract class BaseModel : BaseLogger
{
private const string EndpointGoogleAi = "https://generativelanguage.googleapis.com";
protected const string EndpointGoogleAi = "https://generativelanguage.googleapis.com";

protected readonly string _region = "us-central1";
protected readonly string _publisher = "google";

Check warning on line 24 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_publisher' is not CLS-compliant
protected readonly JsonSerializerOptions _options;

Check warning on line 25 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_options' is not CLS-compliant
internal readonly Credentials? _credentials;

protected string _model;

Check warning on line 27 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_model' is not CLS-compliant
protected string? _apiKey;

Check warning on line 28 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_apiKey' is not CLS-compliant
protected string? _accessToken;

Check warning on line 29 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_accessToken' is not CLS-compliant
protected string? _projectId;

Check warning on line 30 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_projectId' is not CLS-compliant
protected string _region = "us-central1";

Check warning on line 31 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_region' is not CLS-compliant

#if NET472_OR_GREATER || NETSTANDARD2_0
protected static readonly Version _httpVersion = HttpVersion.Version11;

Check warning on line 34 in src/Mscc.GenerativeAI/BaseModel.cs

View workflow job for this annotation

GitHub Actions / Build, pack and push to GitHub Package Registry (GPR) (ubuntu-latest, 8.x)

Identifier '_httpVersion' is not CLS-compliant
Expand Down Expand Up @@ -85,6 +85,19 @@ public string? ApiKey
}
}

/// <summary>
/// Sets the access token to use for the request.
/// </summary>
public string? AccessToken
{
set
{
_accessToken = value;
if (value != null)
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
}
}

/// <summary>
/// Sets the project ID to use for the request.
/// </summary>
Expand All @@ -106,18 +119,14 @@ public string? ProjectId
}
}
}

/// <summary>
/// Sets the access token to use for the request.
/// Returns the region to use for the request.
/// </summary>
public string? AccessToken
public string Region
{
set
{
_accessToken = value;
if (value != null)
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
}
get => _region;
set => _region = value;
}

/// <summary>
Expand All @@ -129,32 +138,42 @@ public TimeSpan Timeout
set => Client.Timeout = value;
}

public BaseGeneration()
/// <summary>
///
/// </summary>
/// <param name="logger">Optional. Logger instance used for logging</param>
public BaseModel(ILogger? logger = null) : base(logger)
{
_options = DefaultJsonSerializerOptions();
GenerativeAIExtensions.ReadDotEnv();
var credentialsFile =
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ??
Environment.GetEnvironmentVariable("GOOGLE_WEB_CREDENTIALS") ??
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "gcloud",
"application_default_credentials.json");
_credentials = GetCredentialsFromFile(credentialsFile);

ApiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN"); // ?? GetAccessTokenFromAdc();
Model = Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ??
GenerativeAI.Model.Gemini15Pro;
_region = Environment.GetEnvironmentVariable("GOOGLE_REGION") ?? _region;
}

public BaseGeneration(string? projectId = null, string? region = null,
string? model = null) : this()
/// <summary>
///
/// </summary>
/// <param name="projectId"></param>
/// <param name="region"></param>
/// <param name="model"></param>
/// <param name="logger">Optional. Logger instance used for logging</param>
public BaseModel(string? projectId = null, string? region = null,
string? model = null, ILogger? logger = null) : this(logger)
{
AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN") ??
var credentialsFile =
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ??
Environment.GetEnvironmentVariable("GOOGLE_WEB_CREDENTIALS") ??
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "gcloud",
"application_default_credentials.json");
var credentials = GetCredentialsFromFile(credentialsFile);
AccessToken = _accessToken ??
GetAccessTokenFromAdc();
ProjectId = projectId ??
Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID") ??
_credentials?.ProjectId ??
credentials?.ProjectId ??
_projectId;
_region = region ?? _region;
Model = model ?? _model;
Expand Down Expand Up @@ -318,7 +337,7 @@ private string RunExternalExe(string filename, string arguments)
}
catch (Exception e)
{
// throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
Logger.LogRunExternalExe("OS error while executing " + Format(filename, arguments)+ ": " + e.Message);
return string.Empty;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Mscc.GenerativeAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Feature suggestion: Retry mechanism ([#2](https://github.com/mscraftsman/generative-ai/issues/2))
- Feature suggestion: Add logs with LogLevel using the Standard logging in .NET ([#6](https://github.com/mscraftsman/generative-ai/issues/6))
- implement Automatic Function Call (AFC)

### Changed
### Fixed

## 1.8.1

### Added

- add logs with LogLevel using the Standard logging in .NET ([#6](https://github.com/mscraftsman/generative-ai/issues/6)) - thanks @doggy8088

### Changed

- improve regarding XMLdoc, typos, and non-nullable properties
Expand Down
16 changes: 10 additions & 6 deletions src/Mscc.GenerativeAI/CachedContentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
#endif
using Microsoft.Extensions.Logging;
using System.Text;

namespace Mscc.GenerativeAI
Expand All @@ -14,17 +15,20 @@ namespace Mscc.GenerativeAI
/// Content that has been preprocessed and can be used in subsequent request to GenerativeService.
/// Cached content can be only used with model it was created for.
/// </summary>
public class CachedContentModel : BaseGeneration
public sealed class CachedContentModel : BaseModel
{
protected override string Version => ApiVersion.V1Beta;
private const string EndpointGoogleAi = "https://generativelanguage.googleapis.com";

/// <summary>
///
/// Initializes a new instance of the <see cref="CachedContentModel"/> class.
/// </summary>
public CachedContentModel()
{
}
public CachedContentModel() : this(logger: null) { }

/// <summary>
/// Initializes a new instance of the <see cref="CachedContentModel"/> class.
/// </summary>
/// <param name="logger">Optional. Logger instance used for logging</param>
public CachedContentModel(ILogger? logger = null) : base(logger) { }

/// <summary>
/// Creates CachedContent resource.
Expand Down
21 changes: 13 additions & 8 deletions src/Mscc.GenerativeAI/FilesModel.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Authentication;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
#endif
using Microsoft.Extensions.Logging;

namespace Mscc.GenerativeAI
{
public class FilesModel : BaseGeneration
public sealed class FilesModel : BaseModel
{
protected override string Version => ApiVersion.V1Beta;
private const string EndpointGoogleAi = "https://generativelanguage.googleapis.com";

/// <summary>
/// Initializes a new instance of the <see cref="FilesModel"/> class.
/// </summary>
public FilesModel() : this(logger: null) { }

/// <summary>
/// Initializes a new instance of the <see cref="FilesModel"/> class.
/// </summary>
/// <param name="logger">Optional. Logger instance used for logging</param>
public FilesModel(ILogger? logger) : base(logger) { }

/// <summary>
/// Lists the metadata for Files owned by the requesting project.
Expand Down
Loading

0 comments on commit aa52b71

Please sign in to comment.