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

Enhance ai speech assistant session #217

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1 @@
alter table `ai_speech_assistant_function_call` add column `type` int not null default 0;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SmartTalk.Messages.Enums.AiSpeechAssistant;

namespace SmartTalk.Core.Domain.AISpeechAssistant;

Expand All @@ -20,6 +21,9 @@ public class AiSpeechAssistantFunctionCall : IEntity, IHasCreatedFields
[Column("content")]
public string Content { get; set; }

[Column("type")]
public AiSpeechAssistantSessionConfigType Type { get; set; }

[Column("created_date")]
public DateTimeOffset CreatedDate { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -729,32 +729,39 @@ private async Task SendToWebSocketAsync(WebSocket socket, object message)

private async Task SendSessionUpdateAsync(WebSocket openAiWebSocket, Domain.AISpeechAssistant.AiSpeechAssistant assistant, string prompt)
{
var tools = await InitialSessionToolsAsync(assistant).ConfigureAwait(false);
var configs = await InitialSessionConfigAsync(assistant).ConfigureAwait(false);

var sessionUpdate = new
{
type = "session.update",
session = new
{
turn_detection = new { type = "server_vad" },
turn_detection = InitialSessionTurnDirection(configs),
input_audio_format = "g711_ulaw",
output_audio_format = "g711_ulaw",
voice = string.IsNullOrEmpty(assistant.Voice) ? "alloy" : assistant.Voice,
instructions = prompt,
modalities = new[] { "text", "audio" },
temperature = 0.8,
input_audio_transcription = new { model = "whisper-1" },
tools = tools
tools = configs.Where(x => x.Type == AiSpeechAssistantSessionConfigType.Tool)
}
};

await SendToWebSocketAsync(openAiWebSocket, sessionUpdate);
}

private async Task<IEnumerable<OpenAiRealtimeToolDto>> InitialSessionToolsAsync(Domain.AISpeechAssistant.AiSpeechAssistant assistant, CancellationToken cancellationToken = default)
private async Task<List<(AiSpeechAssistantSessionConfigType Type, object Config)>> InitialSessionConfigAsync(Domain.AISpeechAssistant.AiSpeechAssistant assistant, CancellationToken cancellationToken = default)
{
var functions = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantFunctionCallByAssistantIdAsync(assistant.Id, cancellationToken).ConfigureAwait(false);

return functions.Count == 0 ? [] : functions.Where(x => !string.IsNullOrWhiteSpace(x.Content)).Select(x => JsonConvert.DeserializeObject<OpenAiRealtimeToolDto>(x.Content));
return functions.Count == 0 ? [] : functions.Where(x => !string.IsNullOrWhiteSpace(x.Content)).Select(x => (x.Type, JsonConvert.DeserializeObject<object>(x.Content))).ToList();
}

private object InitialSessionTurnDirection(List<(AiSpeechAssistantSessionConfigType Type, object Config)> configs)
{
var turnDetection = configs.FirstOrDefault(x => x.Type == AiSpeechAssistantSessionConfigType.TurnDirection);

return turnDetection.Config ?? new { type = "server_vad" };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SmartTalk.Messages.Enums.AiSpeechAssistant;

public enum AiSpeechAssistantSessionConfigType
{
Tool,
TurnDirection
}