Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 38 additions & 25 deletions sdk/ai/azure-ai-agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,30 @@ conversationsClient.items().create(

To scope conversation operations to a delegated end user, set `FOUNDRY_USER_IDENTITY` to an opaque application-generated value and apply it as the `x-ms-user-identity` header. The caller must have the `agents/endpoints/UserIdentityImpersonation/action` RBAC permission. See the sync [UserIdentityConversation.java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/conversations/UserIdentityConversation.java) and async [UserIdentityConversationAsync.java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/conversations/UserIdentityConversationAsync.java) samples.

#### Configure the agent endpoint

Before invoking the agent, point its endpoint at the version you just created and enable the OpenAI Responses protocol on it:

```java com.azure.ai.agents.configure_agent_endpoint
AgentEndpointConfig endpointConfig = new AgentEndpointConfig()
.setVersionSelector(new VersionSelector().setVersionSelectionRules(Collections.singletonList(
new FixedRatioVersionSelectionRule(100).setAgentVersion(agent.getVersion()))))
.setProtocolConfiguration(new ProtocolConfiguration().setResponses(new ResponsesProtocolConfiguration()));

agentsClient.updateAgentDetails(agent.getName(),
new UpdateAgentDetailsOptions().setAgentEndpoint(endpointConfig));
```

#### Text generation with Responses

And the final step that ties everything together, we pass the `AgentReference` and the `conversation.id()` as parameters for the `Response` creation:
With the agent endpoint configured, invoke the OpenAI Responses API through an agent-scoped OpenAI client:

```java com.azure.ai.agents.create_response
AgentReference agentReference = new AgentReference(agent.getName()).setVersion(agent.getVersion());
Response response = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
ResponseCreateParams.builder().conversation(conversation.id()));
// To extract Azure-specific response details:
AzureCreateResponseDetails azureResults = ResponsesClient.getAzureFields(response);
OpenAIClient agentScopedClient = builder.buildAgentScopedOpenAIClient(agent.getName());

Response response = agentScopedClient.responses().create(ResponseCreateParams.builder()
.conversation(conversation.id())
.build());
```

### Using Agent tools
Expand Down Expand Up @@ -744,16 +757,16 @@ The synchronous streaming methods return `IterableStream<ResponseStreamEvent>`,
ResponseAccumulator responseAccumulator = ResponseAccumulator.create();

// Stream response - text is printed as it arrives
IterableStream<ResponseStreamEvent> events =
responsesClient.createStreamingAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
try (StreamResponse<ResponseStreamEvent> events = openAIClient.responses().createStreaming(
ResponseCreateParams.builder()
.input("Tell me a short story about a brave explorer."));
.input("Tell me a short story about a brave explorer.")
.build())) {

for (ResponseStreamEvent event : events) {
responseAccumulator.accumulate(event);
event.outputTextDelta()
.ifPresent(textEvent -> System.out.print(textEvent.delta()));
events.stream().forEach(event -> {
responseAccumulator.accumulate(event);
event.outputTextDelta()
.ifPresent(textEvent -> System.out.print(textEvent.delta()));
});
}
System.out.println(); // newline after streamed text

Expand All @@ -773,21 +786,21 @@ The asynchronous streaming methods return `Flux<ResponseStreamEvent>`, integrati
ResponseAccumulator responseAccumulator = ResponseAccumulator.create();

// Stream response asynchronously - text is printed as each chunk arrives
return responsesAsyncClient.createStreamingAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
ResponseCreateParams.builder()
.input("Tell me a short story about a brave explorer."))
.doOnNext(event -> {
responseAccumulator.accumulate(event);
event.outputTextDelta()
.ifPresent(textEvent -> System.out.print(textEvent.delta()));
})
.then(Mono.fromCallable(() -> {
return Mono.fromFuture(openAIAsyncClient.responses()
.createStreaming(ResponseCreateParams.builder()
.input("Tell me a short story about a brave explorer.")
.build())
.subscribe(event -> responseAccumulator.accumulate(event)
.outputTextDelta()
.ifPresent(textEvent -> System.out.print(textEvent.delta())))
.onCompleteFuture())
.doOnSuccess(unused -> {
System.out.println(); // newline after streamed text

// Access the complete accumulated response
Response response = responseAccumulator.response();
System.out.println("\nResponse ID: " + response.id());
});
```

See the full samples in [SimpleStreamingAsync.java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/streaming/SimpleStreamingAsync.java), [FunctionCallStreamingAsync.java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/streaming/FunctionCallStreamingAsync.java), and [CodeInterpreterStreamingAsync.java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/streaming/CodeInterpreterStreamingAsync.java).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@

package com.azure.ai.agents;

import com.azure.ai.agents.models.AgentReference;
import com.azure.ai.agents.models.AzureCreateResponseOptions;
import com.azure.ai.agents.models.AgentEndpointConfig;
import com.azure.ai.agents.models.AgentVersionDetails;
import com.azure.ai.agents.models.FixedRatioVersionSelectionRule;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.ai.agents.models.ProtocolConfiguration;
import com.azure.ai.agents.models.ResponsesProtocolConfiguration;
import com.azure.ai.agents.models.UpdateAgentDetailsOptions;
import com.azure.ai.agents.models.VersionSelector;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseOutputItem;
import com.openai.models.responses.ResponseOutputMessage;
import com.openai.services.blocking.ConversationService;

import java.util.Collections;

/**
* This sample demonstrates how to use the createWithAgentConversation helper method
* to create a response with a conversation.
* This sample demonstrates how to to create a response with a conversation
* against an agent.
*/
public class CreateResponseWithConversation {
public static void main(String[] args) {
Expand All @@ -32,7 +39,6 @@ public static void main(String[] args) {

AgentsClient agentsClient = builder.buildAgentsClient();
ConversationService conversationService = builder.buildOpenAIClient().conversations();
ResponsesClient responsesClient = builder.buildResponsesClient();

AgentVersionDetails agent = null;
String conversationId = null;
Expand All @@ -45,20 +51,26 @@ public static void main(String[] args) {
agent = agentsClient.createAgentVersion("my-agent", agentDefinition);
System.out.printf("Agent created (id: %s, version: %s)\n", agent.getId(), agent.getVersion());

AgentReference agentReference = new AgentReference(agent.getName())
.setVersion(agent.getVersion());
AgentEndpointConfig endpointConfig = new AgentEndpointConfig()
.setVersionSelector(new VersionSelector().setVersionSelectionRules(Collections.singletonList(
new FixedRatioVersionSelectionRule(100).setAgentVersion(agent.getVersion()))))
.setProtocolConfiguration(new ProtocolConfiguration().setResponses(new ResponsesProtocolConfiguration()));
agentsClient.updateAgentDetails(agent.getName(),
new UpdateAgentDetailsOptions().setAgentEndpoint(endpointConfig));

OpenAIClient openAIClient = builder.buildAgentScopedOpenAIClient(agent.getName());

// Create a conversation
Conversation conversation = conversationService.create();
conversationId = conversation.id();
System.out.println("Created conversation: " + conversationId);

// Create a response using the conversation
Response response = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
Response response = openAIClient.responses().create(
ResponseCreateParams.builder()
.conversation(conversationId)
.input("Hi, how can you help me?"));
.input("Hi, how can you help me?")
.build());

// Process and display the response
System.out.println("\n=== Agent Response ===");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@

package com.azure.ai.agents;

import com.azure.ai.agents.models.AgentReference;
import com.azure.ai.agents.models.AzureCreateResponseOptions;
import com.azure.ai.agents.models.AgentEndpointConfig;
import com.azure.ai.agents.models.AgentVersionDetails;
import com.azure.ai.agents.models.FixedRatioVersionSelectionRule;
import com.azure.ai.agents.models.MemorySearchPreviewTool;
import com.azure.ai.agents.models.MemoryStoreDefaultDefinition;
import com.azure.ai.agents.models.MemoryStoreDefaultOptions;
import com.azure.ai.agents.models.MemoryStoreDetails;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.ai.agents.models.ProtocolConfiguration;
import com.azure.ai.agents.models.ResponsesProtocolConfiguration;
import com.azure.ai.agents.models.UpdateAgentDetailsOptions;
import com.azure.ai.agents.models.VersionSelector;
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
Expand Down Expand Up @@ -43,7 +48,6 @@ public static void main(String[] args) {
AgentsClient agentsClient = builder.buildAgentsClient();
BetaMemoryStoresClient memoryStoresClient = builder.beta().buildBetaMemoryStoresClient();
ConversationService conversationService = builder.buildOpenAIClient().conversations();
ResponsesClient responsesClient = builder.buildResponsesClient();

String memoryStoreName = "my_memory_store";
String agentName = "MyAgent";
Expand Down Expand Up @@ -74,18 +78,25 @@ public static void main(String[] args) {
agent = agentsClient.createAgentVersion(agentName, agentDefinition);
System.out.printf("Agent created (id: %s, version: %s)\n", agent.getId(), agent.getVersion());

AgentReference agentReference = new AgentReference(agent.getName()).setVersion(agent.getVersion());
AgentEndpointConfig endpointConfig = new AgentEndpointConfig()
.setVersionSelector(new VersionSelector().setVersionSelectionRules(Collections.singletonList(
new FixedRatioVersionSelectionRule(100).setAgentVersion(agent.getVersion()))))
.setProtocolConfiguration(new ProtocolConfiguration().setResponses(new ResponsesProtocolConfiguration()));
agentsClient.updateAgentDetails(agent.getName(),
new UpdateAgentDetailsOptions().setAgentEndpoint(endpointConfig));

OpenAIClient openAIClient = builder.buildAgentScopedOpenAIClient(agent.getName());

Conversation conversation = conversationService.create();
firstConversationId = conversation.id();
System.out.println("Created conversation (id: " + firstConversationId + ")");


Response response = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
Response response = openAIClient.responses().create(
ResponseCreateParams.builder()
.conversation(firstConversationId)
.input("I prefer dark roast coffee"));
.input("I prefer dark roast coffee")
.build());
System.out.println("Response output: " + getResponseText(response));

System.out.println("Waiting for memories to be stored...");
Expand All @@ -95,11 +106,11 @@ public static void main(String[] args) {
followUpConversationId = newConversation.id();
System.out.println("Created new conversation (id: " + followUpConversationId + ")");

Response followUpResponse = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agentReference),
Response followUpResponse = openAIClient.responses().create(
ResponseCreateParams.builder()
.conversation(followUpConversationId)
.input("Please order my usual coffee"));
.input("Please order my usual coffee")
.build());
System.out.println("Response output: " + getResponseText(followUpResponse));

System.out.println("Sample completed successfully.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

package com.azure.ai.agents;

import com.azure.ai.agents.models.AgentReference;
import com.azure.ai.agents.models.AzureCreateResponseOptions;
import com.azure.ai.agents.models.AgentEndpointConfig;
import com.azure.ai.agents.models.AgentVersionDetails;
import com.azure.ai.agents.models.FixedRatioVersionSelectionRule;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.ai.agents.models.ProtocolConfiguration;
import com.azure.ai.agents.models.ResponsesProtocolConfiguration;
import com.azure.ai.agents.models.UpdateAgentDetailsOptions;
import com.azure.ai.agents.models.VersionSelector;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.conversations.items.ItemCreateParams;
import com.openai.models.conversations.items.ItemListPage;
Expand All @@ -17,6 +22,8 @@
import com.openai.models.responses.ResponseCreateParams;
import com.openai.services.blocking.ConversationService;

import java.util.Collections;

/**
* This sample how multiple agents can consume a centralized context source (conversation) and provide different responses
* based on it.
Expand All @@ -35,7 +42,6 @@ public static void main(String[] args) {
.serviceVersion(AgentsServiceVersion.getLatest())
.endpoint(endpoint);
AgentsClient agentsClient = builder.buildAgentsClient();
ResponsesClient responsesClient = builder.buildResponsesClient();
ConversationService conversationsClient = builder.buildOpenAIClient().conversations();

// Setting up the conversation with initial messages
Expand All @@ -50,13 +56,26 @@ public static void main(String[] args) {
AgentVersionDetails agent1 = createPromptAgent(agentsClient, model, "weather-agent-1");
AgentVersionDetails agent2 = createPromptAgent(agentsClient, model, "weather-agent-2");

AgentReference agent1Reference = new AgentReference(agent1.getName()).setVersion(agent1.getVersion());
AgentReference agent2Reference = new AgentReference(agent2.getName()).setVersion(agent2.getVersion());
AgentEndpointConfig agent1EndpointConfig = new AgentEndpointConfig()
.setVersionSelector(new VersionSelector().setVersionSelectionRules(Collections.singletonList(
new FixedRatioVersionSelectionRule(100).setAgentVersion(agent1.getVersion()))))
.setProtocolConfiguration(new ProtocolConfiguration().setResponses(new ResponsesProtocolConfiguration()));
agentsClient.updateAgentDetails(agent1.getName(),
new UpdateAgentDetailsOptions().setAgentEndpoint(agent1EndpointConfig));

AgentEndpointConfig agent2EndpointConfig = new AgentEndpointConfig()
.setVersionSelector(new VersionSelector().setVersionSelectionRules(Collections.singletonList(
new FixedRatioVersionSelectionRule(100).setAgentVersion(agent2.getVersion()))))
.setProtocolConfiguration(new ProtocolConfiguration().setResponses(new ResponsesProtocolConfiguration()));
agentsClient.updateAgentDetails(agent2.getName(),
new UpdateAgentDetailsOptions().setAgentEndpoint(agent2EndpointConfig));
OpenAIClient agent1Client = builder.buildAgentScopedOpenAIClient(agent1.getName());
OpenAIClient agent2Client = builder.buildAgentScopedOpenAIClient(agent2.getName());

// Get response from agent1
Response response = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agent1Reference),
ResponseCreateParams.builder().conversation(conversation.id()));
Response response = agent1Client.responses().create(ResponseCreateParams.builder()
.conversation(conversation.id())
.build());
System.out.println("Agent response from: " + agent1.getName());
System.out.println("\tResponse: " + response.output().get(0).asMessage().content().get(0).asOutputText().text());

Expand All @@ -66,9 +85,9 @@ public static void main(String[] args) {
printConversationItems(conversationsClient, conversation.id(), 3);

// Get follow-up response from agent1
Response followUpResponse = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agent1Reference),
ResponseCreateParams.builder().conversation(conversation.id()));
Response followUpResponse = agent1Client.responses().create(ResponseCreateParams.builder()
.conversation(conversation.id())
.build());
System.out.println("Agent response from: " + agent1.getName());
System.out.println("\tResponse: " + followUpResponse.output().get(0).asMessage().content().get(0).asOutputText().text());

Expand All @@ -77,9 +96,9 @@ public static void main(String[] args) {
"Provide suggestions opposite of what historical data indicates.", EasyInputMessage.Role.SYSTEM);
printConversationItems(conversationsClient, conversation.id(), 4);

Response newMessageThread = responsesClient.createAzureResponse(
new AzureCreateResponseOptions().setAgentReference(agent2Reference),
ResponseCreateParams.builder().conversation(conversation.id()));
Response newMessageThread = agent2Client.responses().create(ResponseCreateParams.builder()
.conversation(conversation.id())
.build());
System.out.println("Agent response from: " + agent2.getName());
System.out.println("\tResponse: " + newMessageThread.output().get(0).asMessage().content().get(0).asOutputText().text());
}
Expand Down
Loading