Skip to content

Walterb/response image url async #15

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 4 commits into from
Jun 26, 2025
Merged
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
13 changes: 6 additions & 7 deletions samples/chat-completion-async/java/sample.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ import com.openai.credential.BearerTokenCredential; <%
public class Main {
public static void main(String[] args) {

String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>;
<%if (useTokenCredentials) { %>
<%} else {%>
String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>;
<%}%>
String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>;
<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIOkHttpClientAsync.Builder clientBuilder = OpenAIOkHttpClientAsync.builder();
<% if (!useTokenCredentials) { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(azureOpenaiKey));
.credential(AzureApiKeyCredential.create(apiKey));
<%
} else { %>
clientBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import com.openai.credential.BearerTokenCredential; <%
public class Main {
public static void main(String[] args) {

String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>;
<%if (useTokenCredentials) { %>
<%} else {%>
String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>;
<%}%>
String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>;
<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIOkHttpClientAsync.Builder clientBuilder = OpenAIOkHttpClientAsync.builder();
<% if (useTokenCredentials) { %>
Expand All @@ -31,7 +30,7 @@ public class Main {
} else { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(azureOpenaiKey));
.credential(AzureApiKeyCredential.create(apiKey));
<%} %>
OpenAIClient client = clientBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import com.openai.credential.BearerTokenCredential; <%
public class Main {
public static void main(String[] args) {

String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>;
<%if (useTokenCredentials) { %>
<%} else {%>
String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>;
<%}%>
String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>;
<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>



OpenAIOkHttpClientAsync.Builder clientBuilder = OpenAIOkHttpClientAsync.builder();
<% if (useTokenCredentials) { %>
Expand All @@ -33,7 +34,7 @@ public class Main {
} else { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(azureOpenaiKey));
.credential(AzureApiKeyCredential.create(apiKey));
<%} %>
OpenAIClientAsync client = clientBuilder.build();

Expand Down
13 changes: 6 additions & 7 deletions samples/chat-completion-streaming/java/sample.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import com.openai.credential.BearerTokenCredential; <%
public class Main {
public static void main(String[] args) {

String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>;
<%if (useTokenCredentials) { %>
<%} else {%>
String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>;
<%}%>
String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>;
<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
<% if (useTokenCredentials) { %>
Expand All @@ -33,7 +32,7 @@ public class Main {
} else { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(azureOpenaiKey));
.credential(AzureApiKeyCredential.create(apiKey));
<%} %>
OpenAIClient client = clientBuilder.build();

Expand Down
13 changes: 6 additions & 7 deletions samples/chat-completion/java/sample.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ import com.openai.credential.BearerTokenCredential; <%
public class Main {
public static void main(String[] args) {

String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>;
<%if (useTokenCredentials) { %>
<%} else {%>
String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>;
<%}%>
String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>;
<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
<% if (!useTokenCredentials) { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(azureOpenaiKey));
.credential(AzureApiKeyCredential.create(apiKey));
<%
} else { %>
clientBuilder
Expand Down
37 changes: 37 additions & 0 deletions samples/responses-image-input-async/csharp/sample.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= csharp.usings(
{ namespace: "System.ClientModel", condition: !useTokenCredentials },
"System.ClientModel.Primitives",
{ namespace: "Azure.Identity", condition: useTokenCredentials },
"Azure.AI.OpenAI",
"OpenAI.Responses",
) %>

<%= csharp.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint) %>;
<% if (!useTokenCredentials) { %>
<%= csharp.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey) %>;
<% } %>
<%= csharp.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName) %>;

<% if (useTokenCredentials) { %>
AzureOpenAIClient azureClient = new(new(endpoint), new DefaultAzureCredential());
<% } else { %>
AzureOpenAIClient azureClient = new(new Uri(endpoint), new ApiKeyCredential(apiKey));
<% } %>
OpenAIResponseClient client = azureClient.GetOpenAIResponseClient(deploymentName);

Uri imageUri = new("https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Microsoft_logo.svg/512px-Microsoft_logo.svg.png");
ResponseContentPart imagePart = ResponseContentPart.CreateInputImagePart(imageUri);
ResponseContentPart textPart = ResponseContentPart.CreateInputTextPart("Describe this image");

List<ResponseContentPart> contentParts = [imagePart, textPart];
ResponseItem inputItem = ResponseItem.CreateUserMessageItem(contentParts);


var responseResult = await client.CreateResponseAsync(
inputItems:
[
ResponseItem.CreateSystemMessageItem("You are a helpful assistant that describes images"),
ResponseItem.CreateUserMessageItem(contentParts)
]);

Console.WriteLine($"{responseResult.Value.Id}: {((MessageResponseItem)responseResult.Value.OutputItems[0]).Content[0].Text}");
35 changes: 35 additions & 0 deletions samples/responses-image-input-async/csharp/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
template: sample.cs.template
type: csharp
dependencies:
- name: OpenAI
version: 2.1.0
- name: Azure.AI.OpenAI
version: 2.1.0
- name: Azure.Identity
version: 1.14.0
input:
- name: useTokenCredentials
type: boolean
default: true
description: "Use token credentials for authentication"
- name: endpoint
type: string
required: false
description: "Endpoint URL for the OpenAI API"
- name: apiKey
type: string
required: false
description: "API key for authentication"
- name: deploymentName
type: string
required: true
description: "Model to use for chat completion"
- name: useEnvVars
type: boolean
required: false
default: false
description: "Use environment variables for configuration"
- name: extraParams
type: object
required: false
description: "Additional parameters for the request"
101 changes: 101 additions & 0 deletions samples/responses-image-input-async/go/sample.go.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

<%= go.includes(
"context",
"fmt",
"os",
{ module: "github.com/Azure/azure-sdk-for-go/sdk/azidentity", condition: useTokenCredentials },
"github.com/openai/openai-go",
"github.com/openai/openai-go/azure",
"github.com/openai/openai-go/responses",
) %>

func main() {
<%= go.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint) %>
<%= go.valueOrEnvironment(useEnvVars, "api_version", "AZURE_OPENAI_API_VERSION", apiVersion) %>
<% if (!useTokenCredentials) { %>
<%= go.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey) %>
<% } %>

<%= go.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName) %>

<% if (useTokenCredentials) { %>
token_credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
fmt.Println("Error creating credential:", err)
os.Exit(1)
}
client := openai.NewClient(
azure.WithEndpoint(endpoint, api_version),
azure.WithTokenCredential(token_credential),
)
<% } else { %>
client := openai.NewClient(
azure.WithEndpoint(endpoint, api_version),
azure.WithAPIKey(apiKey),
)
<% } %>

// Create channels to receive the result or error
done := make(chan *responses.Response)
errChan := make(chan error)

// Launch goroutine for the async call
go func() {
defer close(done)
defer close(errChan)

imageURL := "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Microsoft_logo.svg/512px-Microsoft_logo.svg.png"

resp, err := client.Responses.New(
context.TODO(),
responses.ResponseNewParams{
Model: deploymentName,
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: []responses.ResponseInputItemUnionParam{
{
OfInputMessage: &responses.ResponseInputItemMessageParam{
Role: "user",
Content: []responses.ResponseInputContentUnionParam{
{
OfInputText: &responses.ResponseInputTextParam{
Text: "Describe this image.",
},
},
{
OfInputImage: &responses.ResponseInputImageParam{
ImageURL: openai.String(imageURL),
},
},
},
},
},
},
},
},
)

if err != nil {
errChan <- err
return
}
done <- resp
}()

select {
case resp := <-done:
// Print the text content from the output
for _, output := range resp.Output {
if output.Type == "message" {
for _, content := range output.Content {
if content.Type == "output_text" {
fmt.Fprintf(os.Stderr, "Model's description of the image: %s\n", content.Text)
}
}
}
}
case err := <-errChan:
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return
}
}
38 changes: 38 additions & 0 deletions samples/responses-image-input-async/go/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
template: sample.go.template
type: go
dependencies:
- name: github.com/Azure/azure-sdk-for-go/sdk/azidentity
version: v1.10.0
- name: github.com/openai/openai-go
version: v1.1.0
input:
- name: useTokenCredentials
type: boolean
default: true
description: "Use token credentials for authentication"
- name: endpoint
type: string
required: false
description: "Endpoint URL for the OpenAI API"
- name: apiVersion
type: string
required: false
default: "2025-03-01-preview"
description: "API version to use for the request"
- name: apiKey
type: string
required: false
description: "API key for authentication"
- name: deploymentName
type: string
required: true
description: "Model to use for chat completion"
- name: useEnvVars
type: boolean
required: false
default: false
description: "Use environment variables for configuration"
- name: extraParams
type: object
required: false
description: "Additional parameters for the request"
Loading