Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: Add logs with LogLevel using the Standard logging in .NET #43

Merged
merged 81 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
77fcb78
add Microsoft.Extensions.Logging
jochenkirstaetter Sep 19, 2024
fbfd2a8
exclude DotSettings
jochenkirstaetter Sep 19, 2024
42e704d
add abstract base class for Logging
jochenkirstaetter Sep 19, 2024
f965028
derive classes from abstract class for logging
jochenkirstaetter Sep 19, 2024
4916c7b
add static LoggerMessageAttribute methods
jochenkirstaetter Sep 19, 2024
c097f66
resolve conflict
jochenkirstaetter Oct 9, 2024
ada108b
exclude DotSettings
jochenkirstaetter Sep 19, 2024
0a33af4
add abstract base class for Logging
jochenkirstaetter Sep 19, 2024
384bd91
derive classes from abstract class for logging
jochenkirstaetter Sep 19, 2024
d03b7de
add static LoggerMessageAttribute methods
jochenkirstaetter Sep 19, 2024
b0ffec1
Merge remote-tracking branch 'origin/logging' into logging
jochenkirstaetter Oct 9, 2024
42c4e85
update XMLdoc for ILogger
jochenkirstaetter Oct 9, 2024
228628a
#35 GenerativeModel.UploadFile locks the uploaded file - Using block …
rsmithsa Oct 11, 2024
78452c9
drop .NET 7 and .NET Standard 2.0
jochenkirstaetter Oct 3, 2024
3d55bec
update image URL due to removal by Google AI Studio #32
jochenkirstaetter Oct 3, 2024
69820d4
improve language
jochenkirstaetter Oct 3, 2024
b70f6f5
add properties `State`, `Error`, and `VideoMetadata` to type `FileRes…
jochenkirstaetter Oct 13, 2024
94c34dc
enhance returned error information #33
jochenkirstaetter Oct 13, 2024
c4968aa
enhance returned error information #33
jochenkirstaetter Oct 13, 2024
8328033
update enums according to $discovery
jochenkirstaetter Oct 13, 2024
99a5cb3
sync target frameworks among projects
jochenkirstaetter Oct 13, 2024
62165cd
improve test cases
jochenkirstaetter Oct 13, 2024
a56a6f5
overload method of `UploadMedia` to support stream types #38
jochenkirstaetter Oct 13, 2024
57cd625
bump version
jochenkirstaetter Oct 13, 2024
e8b23ed
add XMLdoc for stream upload
jochenkirstaetter Oct 14, 2024
b9a47e6
Contents is required not optional
jochenkirstaetter Oct 14, 2024
a450f67
add extension methods for `GeneratedFiles` and `CachedContents`
jochenkirstaetter Oct 14, 2024
1b677f3
add constant for `CachedContents`
jochenkirstaetter Oct 14, 2024
bd25a62
add XMLdoc
jochenkirstaetter Oct 14, 2024
ec46edf
add methods using File API to class `GoogleAI`
jochenkirstaetter Oct 14, 2024
08aeac7
add XMLdoc
jochenkirstaetter Oct 14, 2024
8431d7c
add XMLdoc
jochenkirstaetter Oct 14, 2024
d8b1718
change access modifier of some properties
jochenkirstaetter Oct 14, 2024
aec5cf1
expose property `Region`
jochenkirstaetter Oct 14, 2024
4fb24c8
mark methods using File API as obsolete/deprecated.
jochenkirstaetter Oct 14, 2024
849aee5
add functionality for `CachedContents`
jochenkirstaetter Oct 14, 2024
4fd4bf7
add types for `GeneratedFile`
jochenkirstaetter Oct 14, 2024
54b1e05
bump version
jochenkirstaetter Oct 14, 2024
96c5760
[skip ci] add example how to use system instruction #27
jochenkirstaetter Oct 14, 2024
fe5d24f
enable windows longpaths
jochenkirstaetter Oct 23, 2024
61d7b01
add support channel
jochenkirstaetter Oct 23, 2024
1a11fa8
add model `gemini-1.5-flash-8b-001`
jochenkirstaetter Oct 29, 2024
f5b38aa
add Logprobs handling
jochenkirstaetter Oct 29, 2024
23c3512
add FinishReason.Language
jochenkirstaetter Oct 29, 2024
dddaeeb
new properties documented
jochenkirstaetter Oct 29, 2024
6c69683
sanitize name of cached content
jochenkirstaetter Oct 29, 2024
2bd88cc
add required model name and optional cached content to request
jochenkirstaetter Oct 29, 2024
f8b9cb9
extend `VideoMetadata`
jochenkirstaetter Oct 29, 2024
5cee46e
enable cached content in chat session
jochenkirstaetter Oct 29, 2024
033ceb9
disable Http3Support (issue in .NET 8 or lower)
jochenkirstaetter Oct 29, 2024
5e5804b
add context caching
jochenkirstaetter Oct 29, 2024
8093c84
add constructor using `FileData`
jochenkirstaetter Oct 29, 2024
f2abb7a
add constructor using `FileData`
jochenkirstaetter Oct 29, 2024
a2f9903
add accessors to `FileResource`
jochenkirstaetter Oct 29, 2024
5934653
add context caching
jochenkirstaetter Oct 29, 2024
f74dc9d
add integration tests for cached content
jochenkirstaetter Oct 29, 2024
709db90
add sample files from Python SDK for Gemini
jochenkirstaetter Oct 29, 2024
f50da84
check pipe organ using inline data
jochenkirstaetter Oct 29, 2024
9bfb905
provide more integration tests using File API
jochenkirstaetter Oct 29, 2024
d828c35
add integration test using JSON schema
jochenkirstaetter Oct 29, 2024
73b951c
add code execution
jochenkirstaetter Oct 29, 2024
a6b31bf
housekeeping
jochenkirstaetter Oct 29, 2024
ae918d5
housekeeping
jochenkirstaetter Oct 29, 2024
59376fd
extend list of supported MIME types
jochenkirstaetter Oct 29, 2024
7fc72dd
bump version
jochenkirstaetter Oct 29, 2024
3e0316d
update NuGet packages
jochenkirstaetter Oct 29, 2024
0f5b254
convert xUnit test using return type `Task`
jochenkirstaetter Oct 29, 2024
d8de581
mention history for chat session
jochenkirstaetter Oct 30, 2024
3edcb35
fix Application Default Credentials (ADC) has been loaded automatica…
jochenkirstaetter Oct 30, 2024
ce8429e
No warnings regarding AOT
jochenkirstaetter Oct 30, 2024
2bccb3d
housekeeping regarding XMLdoc, typos, and non-nullable properties
jochenkirstaetter Oct 30, 2024
2f10ff6
update CHANGELOG
jochenkirstaetter Oct 30, 2024
a6e4a59
fix Exception thrown in Google App Engine #26
jochenkirstaetter Oct 30, 2024
3f4fcef
housekeeping
jochenkirstaetter Oct 30, 2024
e56144f
derive classes from abstract class for logging
jochenkirstaetter Sep 19, 2024
bfd58ef
amend access modifiers
jochenkirstaetter Oct 30, 2024
716e1a4
add logs with LogLevel using the Standard logging in .NET #6
jochenkirstaetter Oct 30, 2024
7795d19
log exception of running an external application
jochenkirstaetter Oct 30, 2024
336d540
bump version
jochenkirstaetter Oct 30, 2024
1d4848f
refactoring after rebase and botched merges
jochenkirstaetter Oct 30, 2024
3763c88
resolve merge conflicts
jochenkirstaetter Oct 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 +17,21 @@

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
protected static readonly HttpClient Client = new HttpClient(new HttpClientHandler
{
SslProtocols = SslProtocols.Tls12
Expand Down Expand Up @@ -85,6 +85,19 @@
}
}

/// <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 @@
}
}
}

/// <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 @@
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 @@
}
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
Loading