Skip to content

Commit 7ed31a4

Browse files
committed
init API for bidi transcript
1 parent a7e1be4 commit 7ed31a4

2 files changed

Lines changed: 119 additions & 22 deletions

File tree

‎firebaseai/src/LiveGenerationConfig.cs‎

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,63 @@
2020

2121
namespace Firebase.AI {
2222

23+
/// <summary>
24+
/// A struct used to configure speech generation settings.
25+
/// </summary>
26+
public readonly struct SpeechConfig
27+
{
28+
internal readonly string voice;
29+
30+
private SpeechConfig(string voice)
31+
{
32+
this.voice = voice;
33+
}
34+
35+
/// <summary>
36+
/// See https://cloud.google.com/text-to-speech/docs/chirp3-hd for the list of available voices.
37+
/// </summary>
38+
/// <param name="voice"></param>
39+
/// <returns></returns>
40+
public static SpeechConfig UsePrebuiltVoice(string voice)
41+
{
42+
return new SpeechConfig(voice);
43+
}
44+
45+
/// <summary>
46+
/// Intended for internal use only.
47+
/// This method is used for serializing the object to JSON for the API request.
48+
/// </summary>
49+
internal Dictionary<string, object> ToJson()
50+
{
51+
Dictionary<string, object> dict = new();
52+
53+
if (!string.IsNullOrWhiteSpace(voice))
54+
{
55+
dict["voiceConfig"] = new Dictionary<string, object>() {
56+
{ "prebuiltVoiceConfig" , new Dictionary<string, object>() {
57+
{ "voiceName", voice }
58+
} }
59+
};
60+
}
61+
62+
return dict;
63+
}
64+
}
65+
2366
/// <summary>
24-
/// A struct used to configure speech generation settings.
67+
/// A struct used to configure speech transcription settings.
2568
/// </summary>
26-
public readonly struct SpeechConfig {
27-
internal readonly string voice;
69+
public readonly struct AudioTranscriptionConfig {
2870

29-
private SpeechConfig(string voice) {
30-
this.voice = voice;
71+
private AudioTranscriptionConfig() {
3172
}
3273

3374
/// <summary>
34-
/// See https://cloud.google.com/text-to-speech/docs/chirp3-hd for the list of available voices.
75+
/// Creates a new transcription configuration.
3576
/// </summary>
36-
/// <param name="voice"></param>
37-
/// <returns></returns>
38-
public static SpeechConfig UsePrebuiltVoice(string voice) {
39-
return new SpeechConfig(voice);
77+
/// <returns>A new transcription configuration.</returns>
78+
public static AudioTranscriptionConfig GetInstance() {
79+
return new AudioTranscriptionConfig();
4080
}
4181

4282
/// <summary>
@@ -45,15 +85,6 @@ public static SpeechConfig UsePrebuiltVoice(string voice) {
4585
/// </summary>
4686
internal Dictionary<string, object> ToJson() {
4787
Dictionary<string, object> dict = new();
48-
49-
if (!string.IsNullOrWhiteSpace(voice)) {
50-
dict["voiceConfig"] = new Dictionary<string, object>() {
51-
{ "prebuiltVoiceConfig" , new Dictionary<string, object>() {
52-
{ "voiceName", voice }
53-
} }
54-
};
55-
}
56-
5788
return dict;
5889
}
5990
}
@@ -62,6 +93,8 @@ internal Dictionary<string, object> ToJson() {
6293
/// A struct defining model parameters to be used when generating live session content.
6394
/// </summary>
6495
public readonly struct LiveGenerationConfig {
96+
private readonly AudioTranscriptionConfig? _inputAudioTranscription;
97+
private readonly AudioTranscriptionConfig? _outputAudioTranscription;
6598
private readonly SpeechConfig? _speechConfig;
6699
private readonly List<ResponseModality> _responseModalities;
67100
private readonly float? _temperature;
@@ -81,6 +114,10 @@ public readonly struct LiveGenerationConfig {
81114
/// for more details.
82115
/// </summary>
83116
///
117+
/// <param name="inputAudioTranscription">The transcription configuration to use if transcribing audio input.</param>
118+
///
119+
/// <param name="outputAudioTranscription">The transcription configuration to use if transcribing audio output.</param>
120+
///
84121
/// <param name="speechConfig">The speech configuration to use if generating audio output.</param>
85122
///
86123
/// <param name="responseModalities">A list of response types to receive from the model.
@@ -155,6 +192,8 @@ public readonly struct LiveGenerationConfig {
155192
/// [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
156193
/// for more details.</param>
157194
public LiveGenerationConfig(
195+
AudioTranscriptionConfig? inputAudioTranscription = null,
196+
AudioTranscriptionConfig? outputAudioTranscription = null,
158197
SpeechConfig? speechConfig = null,
159198
IEnumerable<ResponseModality> responseModalities = null,
160199
float? temperature = null,
@@ -163,6 +202,8 @@ public LiveGenerationConfig(
163202
int? maxOutputTokens = null,
164203
float? presencePenalty = null,
165204
float? frequencyPenalty = null) {
205+
_inputAudioTranscription = inputAudioTranscription;
206+
_outputAudioTranscription = outputAudioTranscription;
166207
_speechConfig = speechConfig;
167208
_responseModalities = responseModalities != null ?
168209
new List<ResponseModality>(responseModalities) : new List<ResponseModality>();
@@ -178,8 +219,11 @@ public LiveGenerationConfig(
178219
/// Intended for internal use only.
179220
/// This method is used for serializing the object to JSON for the API request.
180221
/// </summary>
181-
internal Dictionary<string, object> ToJson() {
222+
internal Dictionary<string, object> ToJson()
223+
{
182224
Dictionary<string, object> jsonDict = new();
225+
if (_inputAudioTranscription.HasValue) jsonDict["inputAudioTranscription"] = _inputAudioTranscription?.ToJson();
226+
if (_outputAudioTranscription.HasValue) jsonDict["outputAudioTranscription"] = _outputAudioTranscription?.ToJson();
183227
if (_speechConfig.HasValue) jsonDict["speechConfig"] = _speechConfig?.ToJson();
184228
if (_responseModalities != null && _responseModalities.Any()) {
185229
jsonDict["responseModalities"] =

‎firebaseai/src/LiveSessionResponse.cs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public string Text {
5050
}
5151
}
5252

53+
/// <summary>
54+
/// The response's content that was a transcription, if it exists.
55+
/// </summary>
56+
public string Transcription {
57+
get {
58+
if (Message is Transcription transcription) {
59+
return transcription.Text;
60+
}
61+
return null;
62+
}
63+
}
64+
5365
/// <summary>
5466
/// The response's content that was audio, if it exists.
5567
/// </summary>
@@ -132,6 +144,38 @@ private LiveSessionResponse(ILiveSessionMessage liveSessionMessage) {
132144
/// </summary>
133145
public interface ILiveSessionMessage { }
134146

147+
/// <summary>
148+
/// A transcription of the audio sent in a live session.
149+
/// </summary>
150+
public readonly struct Transcription {
151+
/// <summary>
152+
/// The transcribed text.
153+
/// </summary>
154+
public readonly string Text { get; }
155+
156+
public readonly bool Finished { get; }
157+
158+
private Transcription(string text, bool finished) {
159+
Text = text;
160+
Finished = finished;
161+
}
162+
163+
private Transcription(string text)
164+
{
165+
Text = text;
166+
Finished = true;
167+
}
168+
169+
/// <summary>
170+
/// Intended for internal use only.
171+
/// This method is used for deserializing JSON responses and should not be called directly.
172+
/// </summary>
173+
internal static Transcription FromJson(Dictionary<string, object> jsonDict) {
174+
return new Transcription(
175+
jsonDict.ParseValue<string>("text"), jsonDict.ParseValue<bool>("finished")
176+
);
177+
}
178+
}
135179
/// <summary>
136180
/// Content generated by the model in a live session.
137181
/// </summary>
@@ -153,10 +197,17 @@ public interface ILiveSessionMessage { }
153197
/// </summary>
154198
public readonly bool Interrupted { get; }
155199

156-
private LiveSessionContent(ModelContent? content, bool turnComplete, bool interrupted) {
200+
public readonly Transcription InputTranscription { get; }
201+
public readonly Transcription OutputTranscription { get; }
202+
203+
204+
private LiveSessionContent(ModelContent? content, bool turnComplete, bool interrupted,
205+
Transcription? inputTranscription, Transcription? outputTranscription) {
157206
Content = content;
158207
TurnComplete = turnComplete;
159208
Interrupted = interrupted;
209+
InputTranscription = inputTranscription;
210+
OutputTranscription = outputTranscription;
160211
}
161212

162213
/// <summary>
@@ -167,7 +218,9 @@ internal static LiveSessionContent FromJson(Dictionary<string, object> jsonDict)
167218
return new LiveSessionContent(
168219
jsonDict.ParseNullableObject("modelTurn", ModelContent.FromJson),
169220
jsonDict.ParseValue<bool>("turnComplete"),
170-
jsonDict.ParseValue<bool>("interrupted")
221+
jsonDict.ParseValue<bool>("interrupted"),
222+
jsonDict.ParseValue<Transcription>("inputTranscription", Transcription.FromJson),
223+
jsonDict.ParseValue<Transcription>("outputTranscription", Transcription.FromJson)
171224
);
172225
}
173226
}

0 commit comments

Comments
 (0)