-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a17030
commit f530180
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ChatAAC.Models; | ||
|
||
namespace ChatAAC.Services; | ||
|
||
public class AiService | ||
{ | ||
private readonly OllamaClient _ollamaClient = new(); | ||
private readonly ITtsService _ttsService = InitializeTtsService(); | ||
|
||
|
||
private static ITtsService InitializeTtsService() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
return new MacTtsService(); | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return new WindowsTtsService(); | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
return new LinuxTtsService(); | ||
|
||
throw new PlatformNotSupportedException("The platform is not supported for TTS."); | ||
} | ||
|
||
public async Task<string> GetAiResponseAsync(string prompt, string form, string tense) | ||
{ | ||
var chatRequest = new ChatRequest | ||
{ | ||
Prompt = prompt, | ||
Form = form, | ||
Tense = tense | ||
}; | ||
|
||
var response = await _ollamaClient.ChatAsync(chatRequest); | ||
return await CombineResponseAsync(response); | ||
} | ||
|
||
public async Task SpeakResponseAsync(string response) | ||
{ | ||
await _ttsService.SpeakAsync(response); | ||
} | ||
|
||
private static async Task<string> CombineResponseAsync(IAsyncEnumerable<string> responseStream) | ||
{ | ||
var builder = new StringBuilder(); | ||
await foreach (var chunk in responseStream) | ||
builder.Append(chunk); | ||
|
||
return builder.ToString(); | ||
} | ||
} |