-
Couldn't load subscription status.
- Fork 712
Add GitHub Models integration #10170
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
84a200d
Add GitHub Models integration
sebastienros 5ff8775
Fix parameters and doc
sebastienros df30820
Add package logo
sebastienros b483338
Support GITHUB_TOKEN env
sebastienros c3f086f
Fix test
sebastienros 48361bd
Merge branch 'main' into sebros/ghmodels
mitchdenny 0e25124
Merge branch 'main' into sebros/ghmodels
sebastienros 6f21fa5
Rename to GithubModel
sebastienros 52c2814
Merge branch 'sebros/ghmodels' of https://github.com/dotnet/aspire in…
sebastienros 9f3a02f
Remove iisexpress in launch
sebastienros a602f5b
Update playground/GitHubModelsEndToEnd/GitHubModelsEndToEnd.AppHost/G…
sebastienros a169ba2
Merge remote-tracking branch 'origin/main' into sebros/ghmodels
sebastienros ac0bbb3
Define running state on github models
sebastienros 82f49de
Add organization
sebastienros 2713765
Add tests
sebastienros 0dc7f2a
Add health checks
sebastienros c20d894
Add manifest files
sebastienros dc50bc6
Make health checks opt-in
sebastienros 60a70f3
Feedback
sebastienros 105b05c
Fix test
sebastienros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
118 changes: 118 additions & 0 deletions
118
src/Aspire.Hosting.GitHub.Models/GitHubModelsHealthCheck.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Data.Common; | ||
| using System.Net; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| namespace Aspire.Hosting.GitHub.Models; | ||
|
|
||
| /// <summary> | ||
| /// A health check for GitHub Models resources. | ||
| /// </summary> | ||
| /// <param name="httpClient">The HttpClient to use.</param> | ||
| /// <param name="connectionString">The connection string.</param> | ||
| internal sealed class GitHubModelsHealthCheck(HttpClient httpClient, Func<ValueTask<string?>> connectionString) : IHealthCheck | ||
| { | ||
| /// <summary> | ||
| /// Checks the health of the GitHub Models endpoint by sending a test request. | ||
| /// </summary> | ||
| /// <param name="context">The health check context.</param> | ||
| /// <param name="cancellationToken">The cancellation token.</param> | ||
| /// <returns>A task that represents the asynchronous health check operation.</returns> | ||
| public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| try | ||
| { | ||
| var builder = new DbConnectionStringBuilder() { ConnectionString = await connectionString().ConfigureAwait(false) }; | ||
|
|
||
| using var request = new HttpRequestMessage(HttpMethod.Post, new Uri($"{builder["Endpoint"]}/chat/completions")); | ||
|
|
||
| // Add required headers | ||
| request.Headers.Add("Accept", "application/vnd.github+json"); | ||
| request.Headers.Add("Authorization", $"Bearer {builder["Key"]?.ToString()}"); | ||
sebastienros marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| request.Headers.Add("X-GitHub-Api-Version", "2022-11-28"); | ||
|
|
||
| // Create test payload with empty messages to minimize API usage | ||
| var payload = new | ||
| { | ||
| model = builder["Model"]?.ToString(), | ||
| messages = Array.Empty<object>() | ||
| }; | ||
|
|
||
| var jsonPayload = JsonSerializer.Serialize(payload); | ||
| request.Content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json"); | ||
|
|
||
| using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| return response.StatusCode switch | ||
| { | ||
| HttpStatusCode.Unauthorized => HealthCheckResult.Unhealthy("GitHub Models API key is invalid or has insufficient permissions"), | ||
| HttpStatusCode.NotFound or HttpStatusCode.Forbidden or HttpStatusCode.BadRequest => await HandleErrorCode(response, cancellationToken).ConfigureAwait(false), | ||
| _ => HealthCheckResult.Unhealthy($"GitHub Models endpoint returned unexpected status code: {response.StatusCode}") | ||
| }; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Failed to check GitHub Models endpoint: {ex.Message}", ex); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<HealthCheckResult> HandleErrorCode(HttpResponseMessage response, CancellationToken cancellationToken) | ||
| { | ||
| GitHubErrorResponse? errorResponse = null; | ||
|
|
||
| try | ||
| { | ||
| var content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); | ||
| errorResponse = JsonSerializer.Deserialize<GitHubErrorResponse>(content); | ||
|
|
||
| if (errorResponse?.Error?.Code == "unknown_model") | ||
| { | ||
| var message = !string.IsNullOrEmpty(errorResponse.Error.Message) | ||
| ? errorResponse.Error.Message | ||
| : "Unknown model"; | ||
| return HealthCheckResult.Unhealthy($"GitHub Models: {message}"); | ||
| } | ||
| else if (errorResponse?.Error?.Code == "empty_array") | ||
| { | ||
| return HealthCheckResult.Healthy(); | ||
| } | ||
| else if (errorResponse?.Error?.Code == "no_access") | ||
| { | ||
| return HealthCheckResult.Unhealthy($"GitHub Models: {errorResponse.Error.Message}"); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| } | ||
|
|
||
| return HealthCheckResult.Unhealthy($"GitHub Models returned an unsupported resonse: ({response.StatusCode}) {errorResponse?.Error?.Message}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents the error response from GitHub Models API. | ||
| /// </summary> | ||
| private sealed class GitHubErrorResponse | ||
| { | ||
| [JsonPropertyName("error")] | ||
| public GitHubError? Error { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents an error from GitHub Models API. | ||
| /// </summary> | ||
| private sealed class GitHubError | ||
| { | ||
| [JsonPropertyName("code")] | ||
| public string? Code { get; set; } | ||
|
|
||
| [JsonPropertyName("message")] | ||
| public string? Message { get; set; } | ||
|
|
||
| [JsonPropertyName("details")] | ||
| public string? Details { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new UriBuilder(builder["Endpoint"]) { Path = "/chat/completions" }.Build()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With more
!.ToString()!, it becomes quite ugly to read. Do you care much?