Skip to content

Commit deff1ee

Browse files
author
Xiting Zhang
committed
update
1 parent 59d160b commit deff1ee

File tree

2 files changed

+116
-26
lines changed

2 files changed

+116
-26
lines changed

sdk/ai/azure-ai-voicelive/README.md

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -352,32 +352,42 @@ VoiceLiveSessionOptions options = new VoiceLiveSessionOptions()
352352
.setInstructions("You have access to weather information. Use get_current_weather when asked about weather.");
353353
354354
// 3. Handle function call events
355-
session.receiveEvents()
356-
.subscribe(event -> {
357-
if (event instanceof SessionUpdateConversationItemCreated) {
358-
SessionUpdateConversationItemCreated itemCreated = (SessionUpdateConversationItemCreated) event;
359-
if (itemCreated.getItem().getType() == ItemType.FUNCTION_CALL) {
360-
ResponseFunctionCallItem functionCall = (ResponseFunctionCallItem) itemCreated.getItem();
361-
362-
// Wait for arguments
363-
String callId = functionCall.getCallId();
364-
String arguments = waitForArguments(session, callId); // Helper method
365-
366-
// Execute function
367-
Map<String, Object> result = getCurrentWeather(arguments);
368-
String resultJson = new ObjectMapper().writeValueAsString(result);
369-
370-
// Return result
371-
FunctionCallOutputItem output = new FunctionCallOutputItem(callId, resultJson);
372-
ClientEventConversationItemCreate createItem = new ClientEventConversationItemCreate()
373-
.setItem(output)
374-
.setPreviousItemId(functionCall.getId());
375-
376-
session.sendEvent(createItem).subscribe();
377-
session.sendEvent(new ClientEventResponseCreate()).subscribe();
378-
}
379-
}
380-
});
355+
client.startSession("gpt-4o-realtime-preview")
356+
.flatMap(session -> {
357+
session.receiveEvents()
358+
.subscribe(event -> {
359+
if (event instanceof SessionUpdateConversationItemCreated) {
360+
SessionUpdateConversationItemCreated itemCreated = (SessionUpdateConversationItemCreated) event;
361+
if (itemCreated.getItem().getType() == ItemType.FUNCTION_CALL) {
362+
ResponseFunctionCallItem functionCall = (ResponseFunctionCallItem) itemCreated.getItem();
363+
364+
// Wait for arguments
365+
String callId = functionCall.getCallId();
366+
String arguments = waitForArguments(session, callId); // Helper method
367+
368+
// Execute function
369+
try {
370+
Map<String, Object> result = getCurrentWeather(arguments);
371+
String resultJson = new ObjectMapper().writeValueAsString(result);
372+
373+
// Return result
374+
FunctionCallOutputItem output = new FunctionCallOutputItem(callId, resultJson);
375+
ClientEventConversationItemCreate createItem = new ClientEventConversationItemCreate()
376+
.setItem(output)
377+
.setPreviousItemId(functionCall.getId());
378+
379+
session.sendEvent(createItem).subscribe();
380+
session.sendEvent(new ClientEventResponseCreate()).subscribe();
381+
} catch (Exception e) {
382+
System.err.println("Error executing function: " + e.getMessage());
383+
}
384+
}
385+
}
386+
});
387+
388+
return Mono.just(session);
389+
})
390+
.block();
381391
```
382392
383393
**Key points:**

sdk/ai/azure-ai-voicelive/src/samples/java/com/azure/ai/voicelive/ReadmeSamples.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import com.azure.ai.voicelive.models.AudioNoiseReduction;
1010
import com.azure.ai.voicelive.models.AudioNoiseReductionType;
1111
import com.azure.ai.voicelive.models.AzureCustomVoice;
12+
import com.azure.ai.voicelive.models.ClientEventConversationItemCreate;
13+
import com.azure.ai.voicelive.models.ClientEventResponseCreate;
14+
import com.azure.ai.voicelive.models.FunctionCallOutputItem;
15+
import com.azure.ai.voicelive.models.ItemType;
16+
import com.azure.ai.voicelive.models.ResponseFunctionCallItem;
17+
import com.azure.ai.voicelive.models.SessionUpdateConversationItemCreated;
18+
import com.azure.ai.voicelive.models.VoiceLiveFunctionDefinition;
1219
import com.azure.ai.voicelive.models.AzurePersonalVoice;
1320
import com.azure.ai.voicelive.models.AzureStandardVoice;
1421
import com.azure.ai.voicelive.models.ClientEventSessionUpdate;
@@ -30,9 +37,12 @@
3037
import com.azure.core.util.BinaryData;
3138
import com.azure.identity.AzureCliCredentialBuilder;
3239
import com.azure.identity.DefaultAzureCredentialBuilder;
40+
import com.fasterxml.jackson.databind.ObjectMapper;
3341
import reactor.core.publisher.Mono;
3442

3543
import java.io.IOException;
44+
import java.util.HashMap;
45+
import java.util.Map;
3646
import java.nio.file.Files;
3747
import java.nio.file.Path;
3848
import java.nio.file.Paths;
@@ -332,7 +342,77 @@ public void voiceConfigurationAzure() {
332342
// END: com.azure.ai.voicelive.voice.azure
333343
}
334344

345+
/**
346+
* Sample for function calling
347+
*/
348+
public void functionCalling() {
349+
VoiceLiveAsyncClient client = new VoiceLiveClientBuilder()
350+
.endpoint(endpoint)
351+
.credential(new AzureKeyCredential(apiKey))
352+
.buildAsyncClient();
353+
354+
// BEGIN: com.azure.ai.voicelive.functioncalling
355+
// 1. Define function tool with parameters
356+
VoiceLiveFunctionDefinition getWeatherFunction = new VoiceLiveFunctionDefinition("get_current_weather")
357+
.setDescription("Get the current weather in a given location")
358+
.setParameters(BinaryData.fromObject(parametersSchema)); // JSON schema
359+
360+
// 2. Configure session with tools
361+
VoiceLiveSessionOptions options = new VoiceLiveSessionOptions()
362+
.setTools(Arrays.asList(getWeatherFunction))
363+
.setInstructions("You have access to weather information. Use get_current_weather when asked about weather.");
364+
365+
// 3. Handle function call events
366+
client.startSession("gpt-4o-realtime-preview")
367+
.flatMap(session -> {
368+
session.receiveEvents()
369+
.subscribe(event -> {
370+
if (event instanceof SessionUpdateConversationItemCreated) {
371+
SessionUpdateConversationItemCreated itemCreated = (SessionUpdateConversationItemCreated) event;
372+
if (itemCreated.getItem().getType() == ItemType.FUNCTION_CALL) {
373+
ResponseFunctionCallItem functionCall = (ResponseFunctionCallItem) itemCreated.getItem();
374+
375+
// Wait for arguments
376+
String callId = functionCall.getCallId();
377+
String arguments = waitForArguments(session, callId); // Helper method
378+
379+
// Execute function
380+
try {
381+
Map<String, Object> result = getCurrentWeather(arguments);
382+
String resultJson = new ObjectMapper().writeValueAsString(result);
383+
384+
// Return result
385+
FunctionCallOutputItem output = new FunctionCallOutputItem(callId, resultJson);
386+
ClientEventConversationItemCreate createItem = new ClientEventConversationItemCreate()
387+
.setItem(output)
388+
.setPreviousItemId(functionCall.getId());
389+
390+
session.sendEvent(createItem).subscribe();
391+
session.sendEvent(new ClientEventResponseCreate()).subscribe();
392+
} catch (Exception e) {
393+
System.err.println("Error executing function: " + e.getMessage());
394+
}
395+
}
396+
}
397+
});
398+
399+
return Mono.just(session);
400+
})
401+
.block();
402+
// END: com.azure.ai.voicelive.functioncalling
403+
}
404+
335405
// Helper methods
406+
private Object parametersSchema = new Object();
407+
408+
private String waitForArguments(VoiceLiveSessionAsyncClient session, String callId) {
409+
return "{}";
410+
}
411+
412+
private Map<String, Object> getCurrentWeather(String arguments) {
413+
return new HashMap<>();
414+
}
415+
336416
private byte[] readAudioChunk() {
337417
return new byte[0];
338418
}

0 commit comments

Comments
 (0)