Spaces:
Sleeping
Sleeping
Download src/ClipForge.Infrastructure/Services/OpenAiClient.cs from clipeforge/app: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Services/OpenAiClient.cs
- Command line
-
hf download hf://spaces/clipeforge/app/src/ClipForge.Infrastructure/Services/OpenAiClient.cs
-
curl -L -o OpenAiClient.cs https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Services/OpenAiClient.cs
2.84 kB
| using System.Net.Http.Headers; | |
| using System.Text; | |
| using System.Text.Json; | |
| using ClipForge.Core.Interfaces.Services; | |
| using ClipForge.Infrastructure.Configuration; | |
| using Microsoft.Extensions.Logging; | |
| using Microsoft.Extensions.Options; | |
| namespace ClipForge.Infrastructure.Services; | |
| public class OpenAiClient : IOpenAiClient | |
| { | |
| private readonly HttpClient _httpClient; | |
| private readonly OpenAiSettings _settings; | |
| private readonly ILogger<OpenAiClient> _logger; | |
| public OpenAiClient(HttpClient httpClient, IOptions<OpenAiSettings> settings, ILogger<OpenAiClient> logger) | |
| { | |
| _httpClient = httpClient; | |
| _settings = settings.Value; | |
| _logger = logger; | |
| _httpClient.BaseAddress = new Uri(_settings.BaseUrl.TrimEnd('/') + "/"); | |
| _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _settings.ApiKey); | |
| } | |
| public async Task<string> GenerateChatCompletionAsync(string systemPrompt, string userPrompt, CancellationToken ct = default) | |
| { | |
| var requestBody = new | |
| { | |
| model = _settings.Model, | |
| messages = new[] | |
| { | |
| new { role = "system", content = systemPrompt }, | |
| new { role = "user", content = userPrompt } | |
| }, | |
| temperature = 0.8, | |
| max_tokens = 4000 | |
| }; | |
| var json = JsonSerializer.Serialize(requestBody); | |
| var content = new StringContent(json, Encoding.UTF8, "application/json"); | |
| _logger.LogInformation("Sending chat completion request to model {Model}", _settings.Model); | |
| var response = await _httpClient.PostAsync("chat/completions", content, ct); | |
| response.EnsureSuccessStatusCode(); | |
| var responseJson = await response.Content.ReadAsStringAsync(ct); | |
| using var doc = JsonDocument.Parse(responseJson); | |
| var message = doc.RootElement | |
| .GetProperty("choices")[0] | |
| .GetProperty("message") | |
| .GetProperty("content") | |
| .GetString(); | |
| return message ?? string.Empty; | |
| } | |
| public async Task<byte[]> GenerateSpeechAsync(string text, string voice = "alloy", CancellationToken ct = default) | |
| { | |
| var requestBody = new | |
| { | |
| model = _settings.TtsModel, | |
| input = text, | |
| voice, | |
| response_format = "mp3" | |
| }; | |
| var json = JsonSerializer.Serialize(requestBody); | |
| var content = new StringContent(json, Encoding.UTF8, "application/json"); | |
| _logger.LogInformation("Generating TTS with voice {Voice}, text length: {Length}", voice, text.Length); | |
| var response = await _httpClient.PostAsync("audio/speech", content, ct); | |
| response.EnsureSuccessStatusCode(); | |
| return await response.Content.ReadAsByteArrayAsync(ct); | |
| } | |
| } | |