Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx = default)
var kibanaApiKey = await parameterProvider.GetParam("docs-kibana-apikey", true, ctx);

using var request = new HttpRequestMessage(HttpMethod.Post,
$"{kibanaUrl}/api/agent_builder/converse/async")
{
Content = new StringContent(requestBody, Encoding.UTF8, "application/json")
};
$"{kibanaUrl}/api/agent_builder/converse/async");
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("kbn-xsrf", "true");
request.Headers.Authorization = new AuthenticationHeaderValue("ApiKey", kibanaApiKey);

using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ctx);
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ctx);

// Ensure the response is successful before streaming
if (!response.IsSuccessStatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Elastic.Documentation.Api.Infrastructure.Adapters.AskAi;

public class LlmGatewayAskAiGateway(HttpClient httpClient, GcpIdTokenProvider tokenProvider, LlmGatewayOptions options) : IAskAiGateway<Stream>
public class LlmGatewayAskAiGateway(HttpClient httpClient, IGcpIdTokenProvider tokenProvider, LlmGatewayOptions options) : IAskAiGateway<Stream>
{
/// <summary>
/// Model name used by LLM Gateway (from PlatformContext.UseCase)
Expand All @@ -25,10 +25,8 @@ public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx = default)
{
var llmGatewayRequest = LlmGatewayRequest.CreateFromRequest(askAiRequest);
var requestBody = JsonSerializer.Serialize(llmGatewayRequest, LlmGatewayContext.Default.LlmGatewayRequest);
using var request = new HttpRequestMessage(HttpMethod.Post, options.FunctionUrl)
{
Content = new StringContent(requestBody, Encoding.UTF8, "application/json")
};
using var request = new HttpRequestMessage(HttpMethod.Post, options.FunctionUrl);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
var authToken = await tokenProvider.GenerateIdTokenAsync(options.ServiceAccount, options.TargetAudience, ctx);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authToken);
request.Headers.Add("User-Agent", "elastic-docs-proxy/1.0");
Expand All @@ -37,7 +35,7 @@ public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx = default)

// Use HttpCompletionOption.ResponseHeadersRead to get headers immediately
// This allows us to start streaming as soon as headers are received
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ctx);
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ctx);

// Ensure the response is successful before streaming
if (!response.IsSuccessStatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Elastic.Documentation.Api.Infrastructure.Gcp;

// This is a custom implementation to create an ID token for GCP.
// Because Google.Api.Auth.OAuth2 is not compatible with AOT
public class GcpIdTokenProvider(HttpClient httpClient)
public class GcpIdTokenProvider(HttpClient httpClient) : IGcpIdTokenProvider
{
// Cache tokens by target audience to avoid regenerating them on every request
private static readonly ConcurrentDictionary<string, CachedToken> TokenCache = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

namespace Elastic.Documentation.Api.Infrastructure.Gcp;

/// <summary>
/// Interface for generating GCP ID tokens.
/// Abstraction allows for testing and alternative implementations.
/// </summary>
public interface IGcpIdTokenProvider
{
/// <summary>
/// Generates an ID token for the specified service account and target audience.
/// </summary>
/// <param name="serviceAccount">Service account JSON key</param>
/// <param name="targetAudience">Target audience URL</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>ID token</returns>
Task<string> GenerateIdTokenAsync(string serviceAccount, string targetAudience, Cancel cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static void AddAskAiUsecase(IServiceCollection services, AppEnv appEnv)

try
{
_ = services.AddSingleton<GcpIdTokenProvider>();
_ = services.AddSingleton<IGcpIdTokenProvider, GcpIdTokenProvider>();
logger?.LogInformation("GcpIdTokenProvider registered successfully");

_ = services.AddScoped<LlmGatewayOptions>();
Expand Down
Loading
Loading