-
Notifications
You must be signed in to change notification settings - Fork 2.2k
OpenAPI Tool samples and tests #48315
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/SampleUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.agents; | ||
|
|
||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| public class SampleUtils { | ||
|
|
||
| /** | ||
| * Gets the path to a file in the sample resource folder. | ||
| * @param fileName the name of the file in the sample resource folder | ||
| * @return Path to the sample resource file | ||
| */ | ||
| public static Path getResourcePath(String fileName) { | ||
| try { | ||
| URL resourceUrl = SampleUtils.class.getClassLoader().getResource(fileName); | ||
| if (resourceUrl == null) { | ||
| throw new RuntimeException("Sample resource file not found: " + fileName); | ||
| } | ||
| return Paths.get(resourceUrl.toURI()); | ||
| } catch (URISyntaxException e) { | ||
| throw new RuntimeException("Invalid URI for sample resource: " + fileName, e); | ||
| } | ||
| } | ||
| } |
109 changes: 109 additions & 0 deletions
109
sdk/ai/azure-ai-agents/src/samples/java/com/azure/ai/agents/tools/OpenApiSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.agents.tools; | ||
|
|
||
| import com.azure.ai.agents.AgentsClient; | ||
| import com.azure.ai.agents.AgentsClientBuilder; | ||
| import com.azure.ai.agents.ConversationsClient; | ||
| import com.azure.ai.agents.ResponsesClient; | ||
| import com.azure.ai.agents.SampleUtils; | ||
| import com.azure.ai.agents.models.AgentReference; | ||
| import com.azure.ai.agents.models.AgentVersionDetails; | ||
| import com.azure.ai.agents.models.OpenApiAnonymousAuthDetails; | ||
| import com.azure.ai.agents.models.OpenApiFunctionDefinition; | ||
| import com.azure.ai.agents.models.OpenApiTool; | ||
| import com.azure.ai.agents.models.PromptAgentDefinition; | ||
| import com.azure.core.util.BinaryData; | ||
| import com.azure.core.util.Configuration; | ||
| import com.azure.identity.DefaultAzureCredentialBuilder; | ||
| import com.openai.models.conversations.Conversation; | ||
| import com.openai.models.conversations.items.ItemCreateParams; | ||
| import com.openai.models.responses.EasyInputMessage; | ||
| import com.openai.models.responses.Response; | ||
| import com.openai.models.responses.ResponseCreateParams; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This sample demonstrates how to create an agent with an OpenAPI tool that calls | ||
| * an external API defined by an OpenAPI specification file. | ||
| * | ||
| * <p>Before running the sample, set these environment variables:</p> | ||
| * <ul> | ||
| * <li>FOUNDRY_PROJECT_ENDPOINT - The Azure AI Project endpoint.</li> | ||
| * <li>FOUNDRY_MODEL_DEPLOYMENT_NAME - The model deployment name.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Also place an OpenAPI spec JSON file at {@code src/samples/resources/assets/httpbin_openapi.json}.</p> | ||
| */ | ||
| public class OpenApiSample { | ||
| public static void main(String[] args) throws Exception { | ||
| String endpoint = Configuration.getGlobalConfiguration().get("FOUNDRY_PROJECT_ENDPOINT"); | ||
| String model = Configuration.getGlobalConfiguration().get("FOUNDRY_MODEL_DEPLOYMENT_NAME"); | ||
|
|
||
| AgentsClientBuilder builder = new AgentsClientBuilder() | ||
| .credential(new DefaultAzureCredentialBuilder().build()) | ||
| .endpoint(endpoint); | ||
|
|
||
| AgentsClient agentsClient = builder.buildAgentsClient(); | ||
| ResponsesClient responsesClient = builder.buildResponsesClient(); | ||
| ConversationsClient conversationsClient = builder.buildConversationsClient(); | ||
|
|
||
|
|
||
| // Load the OpenAPI spec from a JSON file | ||
| Map<String, BinaryData> spec = OpenApiFunctionDefinition.readSpecFromFile( | ||
| SampleUtils.getResourcePath("assets/httpbin_openapi.json")); | ||
|
|
||
| OpenApiFunctionDefinition toolDefinition = new OpenApiFunctionDefinition( | ||
| "httpbin_get", | ||
| spec, | ||
| new OpenApiAnonymousAuthDetails()) | ||
| .setDescription("Get request metadata from an OpenAPI endpoint."); | ||
|
|
||
| PromptAgentDefinition agentDefinition = new PromptAgentDefinition(model) | ||
| .setInstructions("Use the OpenAPI tool for HTTP request metadata.") | ||
| .setTools(Arrays.asList(new OpenApiTool(toolDefinition))); | ||
|
|
||
| AgentVersionDetails agentVersion = agentsClient.createAgentVersion("openapi-agent", agentDefinition); | ||
| System.out.println("Agent: " + agentVersion.getName() + ", version: " + agentVersion.getVersion()); | ||
|
|
||
| // Create a conversation and add a user message | ||
| Conversation conversation = conversationsClient.getConversationService().create(); | ||
| conversationsClient.getConversationService().items().create( | ||
| ItemCreateParams.builder() | ||
| .conversationId(conversation.id()) | ||
| .addItem(EasyInputMessage.builder() | ||
| .role(EasyInputMessage.Role.USER) | ||
| .content("Use the OpenAPI tool and summarize the returned URL and origin in one sentence.") | ||
| .build()) | ||
| .build()); | ||
|
|
||
| try { | ||
| AgentReference agentReference = new AgentReference(agentVersion.getName()) | ||
| .setVersion(agentVersion.getVersion()); | ||
|
|
||
| ResponseCreateParams.Builder options = ResponseCreateParams.builder() | ||
| .maxOutputTokens(300L); | ||
|
|
||
| Response response = responsesClient.createWithAgentConversation( | ||
| agentReference, conversation.id(), options); | ||
|
|
||
| String text = response.output().stream() | ||
| .filter(item -> item.isMessage()) | ||
| .map(item -> item.asMessage().content() | ||
| .get(item.asMessage().content().size() - 1) | ||
| .asOutputText() | ||
| .text()) | ||
jpalvarezl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .reduce((first, second) -> second) | ||
| .orElse("<no message output>"); | ||
|
|
||
| System.out.println("Status: " + response.status().map(Object::toString).orElse("unknown")); | ||
| System.out.println("Response: " + text); | ||
| } finally { | ||
| agentsClient.deleteAgentVersion(agentVersion.getName(), agentVersion.getVersion()); | ||
| System.out.println("Agent deleted"); | ||
| } | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
sdk/ai/azure-ai-agents/src/samples/resources/assets/httpbin_openapi.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "openapi": "3.0.0", | ||
| "info": { | ||
| "title": "httpbin", | ||
| "version": "1.0.0", | ||
| "description": "A simple HTTP request/response service" | ||
| }, | ||
| "servers": [ | ||
| { | ||
| "url": "https://httpbin.org" | ||
| } | ||
| ], | ||
| "paths": { | ||
| "/get": { | ||
| "get": { | ||
| "operationId": "httpbin_get", | ||
| "summary": "Returns the GET request data including headers, origin IP, and URL", | ||
| "responses": { | ||
| "200": { | ||
| "description": "Successful response", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "args": { "type": "object" }, | ||
| "headers": { "type": "object" }, | ||
| "origin": { "type": "string" }, | ||
| "url": { "type": "string" } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
93 changes: 93 additions & 0 deletions
93
sdk/ai/azure-ai-agents/src/test/java/com/azure/ai/agents/OpenAPIToolTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.agents; | ||
|
|
||
| import com.azure.ai.agents.models.AgentReference; | ||
| import com.azure.ai.agents.models.AgentVersionDetails; | ||
| import com.azure.ai.agents.models.OpenApiAnonymousAuthDetails; | ||
| import com.azure.ai.agents.models.OpenApiFunctionDefinition; | ||
| import com.azure.ai.agents.models.OpenApiTool; | ||
| import com.azure.ai.agents.models.PromptAgentDefinition; | ||
| import com.azure.core.http.HttpClient; | ||
| import com.azure.core.util.BinaryData; | ||
| import com.openai.models.conversations.Conversation; | ||
| import com.openai.models.conversations.items.ItemCreateParams; | ||
| import com.openai.models.responses.EasyInputMessage; | ||
| import com.openai.models.responses.Response; | ||
| import com.openai.models.responses.ResponseCreateParams; | ||
| import com.openai.models.responses.ResponseStatus; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
|
|
||
| import static com.azure.ai.agents.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class OpenAPIToolTests extends ClientTestBase { | ||
|
|
||
| private static final String AGENT_NAME = "openapi-tool-test-agent-java"; | ||
|
|
||
| @ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS) | ||
| @MethodSource("com.azure.ai.agents.TestUtils#getTestParameters") | ||
| public void openApiToolEndToEnd(HttpClient httpClient, AgentsServiceVersion serviceVersion) throws IOException { | ||
| AgentsClient agentsClient = getAgentsSyncClient(httpClient, serviceVersion); | ||
| ResponsesClient responsesClient = getResponsesSyncClient(httpClient, serviceVersion); | ||
| ConversationsClient conversationsClient = getConversationsSyncClient(httpClient, serviceVersion); | ||
|
|
||
| // Load the OpenAPI spec from test resources | ||
| Map<String, BinaryData> spec | ||
| = OpenApiFunctionDefinition.readSpecFromFile(TestUtils.getTestResourcePath("assets/httpbin_openapi.json")); | ||
|
|
||
| OpenApiFunctionDefinition toolDefinition | ||
| = new OpenApiFunctionDefinition("httpbin_get", spec, new OpenApiAnonymousAuthDetails()) | ||
| .setDescription("Get request metadata from an OpenAPI endpoint."); | ||
|
|
||
| PromptAgentDefinition agentDefinition | ||
| = new PromptAgentDefinition("gpt-4o").setInstructions("Use the OpenAPI tool for HTTP request metadata.") | ||
| .setTools(Arrays.asList(new OpenApiTool(toolDefinition))); | ||
|
|
||
| AgentVersionDetails agentVersion = agentsClient.createAgentVersion(AGENT_NAME, agentDefinition); | ||
| assertNotNull(agentVersion); | ||
| assertNotNull(agentVersion.getId()); | ||
| assertEquals(AGENT_NAME, agentVersion.getName()); | ||
|
|
||
| try { | ||
| // Create a conversation and add a user message | ||
| Conversation conversation = conversationsClient.getConversationService().create(); | ||
| assertNotNull(conversation); | ||
| assertNotNull(conversation.id()); | ||
|
|
||
| conversationsClient.getConversationService() | ||
| .items() | ||
| .create(ItemCreateParams.builder() | ||
| .conversationId(conversation.id()) | ||
| .addItem(EasyInputMessage.builder() | ||
| .role(EasyInputMessage.Role.USER) | ||
| .content("Use the OpenAPI tool and summarize the returned URL and origin in one sentence.") | ||
| .build()) | ||
| .build()); | ||
|
|
||
| // Create a response using the agent with the conversation | ||
| AgentReference agentReference | ||
| = new AgentReference(agentVersion.getName()).setVersion(agentVersion.getVersion()); | ||
|
|
||
| ResponseCreateParams.Builder options = ResponseCreateParams.builder().maxOutputTokens(300L); | ||
|
|
||
| Response response = responsesClient.createWithAgentConversation(agentReference, conversation.id(), options); | ||
|
|
||
| assertNotNull(response); | ||
| assertTrue(response.id().startsWith("resp")); | ||
| assertTrue(response.status().isPresent()); | ||
| assertEquals(ResponseStatus.COMPLETED, response.status().get()); | ||
| assertFalse(response.output().isEmpty()); | ||
| assertTrue(response.output().stream().anyMatch(item -> item.isMessage())); | ||
| } finally { | ||
| // Clean up | ||
| agentsClient.deleteAgentVersion(agentVersion.getName(), agentVersion.getVersion()); | ||
| } | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
sdk/ai/azure-ai-agents/src/test/resources/assets/httpbin_openapi.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "openapi": "3.0.0", | ||
| "info": { | ||
| "title": "httpbin", | ||
| "version": "1.0.0", | ||
| "description": "A simple HTTP request/response service" | ||
| }, | ||
| "servers": [ | ||
| { | ||
| "url": "https://httpbin.org" | ||
| } | ||
| ], | ||
| "paths": { | ||
| "/get": { | ||
| "get": { | ||
| "operationId": "httpbin_get", | ||
| "summary": "Returns the GET request data including headers, origin IP, and URL", | ||
| "responses": { | ||
| "200": { | ||
| "description": "Successful response", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "args": { "type": "object" }, | ||
| "headers": { "type": "object" }, | ||
| "origin": { "type": "string" }, | ||
| "url": { "type": "string" } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.