minor refine
This commit is contained in:
parent
b0b6c4b651
commit
73c2e9c72e
|
|
@ -14,7 +14,7 @@ public interface IRealTimeCompletion
|
|||
Func<string, string, Task> onModelAudioDeltaReceived,
|
||||
Func<Task> onModelAudioResponseDone,
|
||||
Func<string, Task> onModelAudioTranscriptDone,
|
||||
Func<List<RoleDialogModel>, Task<bool>> onModelResponseDone,
|
||||
Func<List<RoleDialogModel>, Task> onModelResponseDone,
|
||||
Func<string, Task> onConversationItemCreated,
|
||||
Func<RoleDialogModel, Task> onInputAudioTranscriptionDone,
|
||||
Func<Task> onInterruptionDetected);
|
||||
|
|
|
|||
|
|
@ -124,7 +124,10 @@ public class RealtimeHub : IRealtimeHub
|
|||
if (isReconnect) break;
|
||||
}
|
||||
|
||||
return isReconnect;
|
||||
if (isReconnect)
|
||||
{
|
||||
await _completer.Reconnect(_conn);
|
||||
}
|
||||
},
|
||||
onConversationItemCreated: async response =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
private Func<string, string, Task> _onModelAudioDeltaReceived;
|
||||
private Func<Task> _onModelAudioResponseDone;
|
||||
private Func<string, Task> _onModelAudioTranscriptDone;
|
||||
private Func<List<RoleDialogModel>, Task<bool>> _onModelResponseDone;
|
||||
private Func<List<RoleDialogModel>, Task> _onModelResponseDone;
|
||||
private Func<string, Task> _onConversationItemCreated;
|
||||
private Func<RoleDialogModel, Task> _onInputAudioTranscriptionDone;
|
||||
private Func<Task> _onInterruptionDetected;
|
||||
|
|
@ -68,11 +68,13 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
Func<string, string, Task> onModelAudioDeltaReceived,
|
||||
Func<Task> onModelAudioResponseDone,
|
||||
Func<string, Task> onModelAudioTranscriptDone,
|
||||
Func<List<RoleDialogModel>, Task<bool>> onModelResponseDone,
|
||||
Func<List<RoleDialogModel>, Task> onModelResponseDone,
|
||||
Func<string, Task> onConversationItemCreated,
|
||||
Func<RoleDialogModel, Task> onInputAudioTranscriptionDone,
|
||||
Func<Task> onInterruptionDetected)
|
||||
{
|
||||
_logger.LogInformation($"Connecting {Provider} realtime server...");
|
||||
|
||||
_conn = conn;
|
||||
_onModelReady = onModelReady;
|
||||
_onModelAudioDeltaReceived = onModelAudioDeltaReceived;
|
||||
|
|
@ -106,8 +108,6 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
|
||||
private async Task ReceiveMessage()
|
||||
{
|
||||
var isReconnect = false;
|
||||
|
||||
await foreach (ChatSessionUpdate update in _session.ReceiveUpdatesAsync(CancellationToken.None))
|
||||
{
|
||||
var receivedText = update?.RawResponse;
|
||||
|
|
@ -142,7 +142,7 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
if (functionCall != null)
|
||||
{
|
||||
var messages = OnFunctionCall(_conn, functionCall);
|
||||
isReconnect = await _onModelResponseDone(messages);
|
||||
await _onModelResponseDone(messages);
|
||||
}
|
||||
}
|
||||
else if (response.ServerContent != null)
|
||||
|
|
@ -161,9 +161,9 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
{
|
||||
// Handle input transcription
|
||||
var inputTranscription = _inputStream.GetText();
|
||||
if (!string.IsNullOrEmpty(inputTranscription))
|
||||
if (!string.IsNullOrWhiteSpace(inputTranscription))
|
||||
{
|
||||
var message = OnUserAudioTranscriptionCompleted(_conn, inputTranscription);
|
||||
var message = OnUserAudioTranscriptionCompleted(_conn, inputTranscription ?? string.Empty);
|
||||
await _onInputAudioTranscriptionDone(message);
|
||||
}
|
||||
_inputStream.Clear();
|
||||
|
|
@ -190,17 +190,15 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
|
||||
// Handle output transcription
|
||||
var outputTranscription = _outputStream.GetText();
|
||||
var messages = await OnResponseDone(_conn, outputTranscription ?? string.Empty, response.UsageMetaData);
|
||||
isReconnect = await _onModelResponseDone(messages);
|
||||
if (!string.IsNullOrWhiteSpace(outputTranscription))
|
||||
{
|
||||
var messages = await OnResponseDone(_conn, outputTranscription ?? string.Empty, response.UsageMetaData);
|
||||
await _onModelResponseDone(messages);
|
||||
}
|
||||
_inputStream.Clear();
|
||||
_outputStream.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (isReconnect)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -209,16 +207,9 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
}
|
||||
}
|
||||
|
||||
if (isReconnect)
|
||||
{
|
||||
await Reconnect(_conn);
|
||||
}
|
||||
else
|
||||
{
|
||||
_inputStream.Dispose();
|
||||
_outputStream.Dispose();
|
||||
_session.Dispose();
|
||||
}
|
||||
_inputStream.Dispose();
|
||||
_outputStream.Dispose();
|
||||
_session.Dispose();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -369,8 +360,8 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
|||
Model = Model.ToModelId(),
|
||||
SystemInstruction = request.SystemInstruction,
|
||||
Tools = request.Tools?.ToArray(),
|
||||
InputAudioTranscription = realtimeSetting.InputAudioTranscribe ? new() : null,
|
||||
OutputAudioTranscription = realtimeSetting.InputAudioTranscribe ? new() : null
|
||||
InputAudioTranscription = new(),
|
||||
OutputAudioTranscription = new()
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public sealed class MicrosoftExtensionsAITextEmbeddingProvider : ITextEmbedding
|
|||
|
||||
/// <inheritdoc/>
|
||||
public async Task<float[]> GetVectorAsync(string text) =>
|
||||
(await _generator.GenerateEmbeddingVectorAsync(text, CreateOptions())).ToArray();
|
||||
(await _generator.GenerateVectorAsync(text, CreateOptions())).ToArray();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<float[]>> GetVectorsAsync(List<string> texts)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@
|
|||
<OutputPath>$(SolutionDir)packages</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Providers\Realtime\2lpg51hd.uih~" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="OpenAI" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
|||
private Func<string, string, Task> _onModelAudioDeltaReceived;
|
||||
private Func<Task> _onModelAudioResponseDone;
|
||||
private Func<string, Task> _onModelAudioTranscriptDone;
|
||||
private Func<List<RoleDialogModel>, Task<bool>> _onModelResponseDone;
|
||||
private Func<List<RoleDialogModel>, Task> _onModelResponseDone;
|
||||
private Func<string, Task> _onConversationItemCreated;
|
||||
private Func<RoleDialogModel, Task> _onInputAudioTranscriptionDone;
|
||||
private Func<Task> _onInterruptionDetected;
|
||||
|
|
@ -46,11 +46,13 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
|||
Func<string, string, Task> onModelAudioDeltaReceived,
|
||||
Func<Task> onModelAudioResponseDone,
|
||||
Func<string, Task> onModelAudioTranscriptDone,
|
||||
Func<List<RoleDialogModel>, Task<bool>> onModelResponseDone,
|
||||
Func<List<RoleDialogModel>, Task> onModelResponseDone,
|
||||
Func<string, Task> onConversationItemCreated,
|
||||
Func<RoleDialogModel, Task> onInputAudioTranscriptionDone,
|
||||
Func<Task> onInterruptionDetected)
|
||||
{
|
||||
_logger.LogInformation($"Connecting {Provider} realtime server...");
|
||||
|
||||
_conn = conn;
|
||||
_onModelReady = onModelReady;
|
||||
_onModelAudioDeltaReceived = onModelAudioDeltaReceived;
|
||||
|
|
@ -87,7 +89,6 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
|||
|
||||
private async Task ReceiveMessage(RealtimeModelSettings realtimeSettings)
|
||||
{
|
||||
var isReconnect = false;
|
||||
DateTime? startTime = null;
|
||||
|
||||
await foreach (ChatSessionUpdate update in _session.ReceiveUpdatesAsync(CancellationToken.None))
|
||||
|
|
@ -169,7 +170,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
|||
else
|
||||
{
|
||||
var messages = await OnResponsedDone(_conn, receivedText);
|
||||
isReconnect = await _onModelResponseDone(messages);
|
||||
await _onModelResponseDone(messages);
|
||||
}
|
||||
}
|
||||
else if (response.Type == "conversation.item.created")
|
||||
|
|
@ -208,21 +209,9 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
|||
{
|
||||
_logger.LogInformation($"{response.Type}: {receivedText}");
|
||||
}
|
||||
|
||||
if (isReconnect)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isReconnect)
|
||||
{
|
||||
await Reconnect(_conn);
|
||||
}
|
||||
else
|
||||
{
|
||||
_session.Dispose();
|
||||
}
|
||||
_session.Dispose();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace BotSharp.Plugin.Google.Core
|
|||
async (s, s1) => { Console.WriteLine(s); },
|
||||
async () => { },
|
||||
async (s) => { Console.WriteLine(s); },
|
||||
async list => { Console.WriteLine(list); return false; },
|
||||
async list => { Console.WriteLine(list); },
|
||||
async s => { Console.WriteLine(s); },
|
||||
async model => { Console.WriteLine(model); },
|
||||
async () => { Console.WriteLine("UserInterrupted"); });
|
||||
|
|
|
|||
Loading…
Reference in a new issue