Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f364876
Pick up changes to tool naming
trangevi Aug 13, 2024
afb3c25
Test/sample fixes
trangevi Aug 13, 2024
e5a9b77
TSP commit
trangevi Aug 14, 2024
65a55c8
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Aug 14, 2024
bf629a5
Update with recent typespec changes. Re-record tests
trangevi Aug 23, 2024
bab6b9d
Add embedding client and associated changes
trangevi Aug 29, 2024
7071138
Add samples. Regenerate apiview
trangevi Aug 30, 2024
762f437
Add test for user agent
trangevi Sep 16, 2024
e04113e
Updates to flatten Choices array
trangevi Sep 23, 2024
1db2dfc
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Sep 25, 2024
2b0bcd7
Pick up changes from tsp
trangevi Sep 25, 2024
c2a1f72
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Sep 25, 2024
c3d996e
Finally got tool streaming working!
trangevi Oct 3, 2024
b8fe531
regenerate after changes
trangevi Oct 3, 2024
b417e4e
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Oct 3, 2024
9f4fb2f
up
Oct 8, 2024
a6cb31f
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Oct 9, 2024
4c8ed0d
Merge fixes. Removing test recordings
trangevi Oct 10, 2024
920be7a
Rename sample files
trangevi Oct 10, 2024
b600294
Add handling to pass both auth headers in every request
trangevi Oct 10, 2024
7e20f3e
Add handling for image files for chat completions
trangevi Oct 11, 2024
652596c
Regen code and api
trangevi Oct 11, 2024
cacdc03
Update spellchecker
trangevi Oct 11, 2024
02ef186
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Oct 11, 2024
37e36fb
Update snippets
trangevi Oct 11, 2024
df9b796
Update sample names/pointers
trangevi Oct 15, 2024
c19fb2d
Update test recordings asset pointer
trangevi Oct 15, 2024
b861fcf
Rename some enum values
trangevi Oct 18, 2024
bf2143d
Update Apiview
trangevi Oct 18, 2024
8215877
Merge branch 'main' into trangevi/ai-inference-sdk
trangevi Oct 18, 2024
6e961ca
Regenerated
trangevi Oct 18, 2024
d617623
Changelog update
trangevi Oct 21, 2024
c8c7759
Changelog update
trangevi Oct 22, 2024
3584458
Update hopefull release date
trangevi Oct 23, 2024
b4dc0e6
Remove extraParams. Add more samples
trangevi Oct 24, 2024
8a031ac
Missed change to apiview
trangevi Oct 24, 2024
176b9d7
Snippet fixes
trangevi Oct 24, 2024
3b1fb6f
Update release date
trangevi Oct 24, 2024
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
Prev Previous commit
Next Next commit
Add handling for image files for chat completions
Signed-off-by: trangevi <[email protected]>
  • Loading branch information
trangevi committed Oct 11, 2024
commit 7e20f3e0656dbaadd8ac4a57b9852ae0e2ebf7ae
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public ChatCompletionsClient(Uri endpoint, TokenCredential credential, AzureAIIn
Argument.AssertNotNull(credential, nameof(credential));
options ??= new AzureAIInferenceClientOptions();

// CUSTOM CODE NOTE: This mimics the TokenRequestContext internal to the BearerTokenAuthenticationPolicy used in the pipeline
TokenRequestContext context = new TokenRequestContext(AuthorizationScopes, null, null, null, isCaeEnabled: true, isProofOfPossessionEnabled: false, null, null, null);
var token = credential.GetToken(context, CancellationToken.None).Token;
options.AddPolicy(new AddApiKeyHeaderPolicy(token), HttpPipelinePosition.PerCall);

ClientDiagnostics = new ClientDiagnostics(options, true);
_tokenCredential = credential;
_pipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Runtime.InteropServices.ComTypes;

namespace Azure.AI.Inference
{
Expand Down Expand Up @@ -50,6 +51,21 @@ public ChatMessageImageContentItem(Stream stream, string mimeType, ChatMessageIm
: this(new ChatMessageImageUrl(stream, mimeType, detailLevel))
{ }

/// <summary>
/// Initializes a new instance of ChatMessageImageContentItem from a file pointer to an image
/// in a known format.
/// </summary>
/// <param name="imageFilePath"> The path to the image to use. </param>
/// <param name="mimeType"> The MIME type, e.g. <c>image/png</c>, matching the format of the image data. </param>
/// <param name="detailLevel"> The image detail level the model should use when evaluating the image. </param>
public ChatMessageImageContentItem(string imageFilePath, string mimeType, ChatMessageImageDetailLevel? detailLevel = null)
{
Stream fileStream = File.OpenRead(imageFilePath);

Type = "image_url";
ImageUrl = new ChatMessageImageUrl(fileStream, mimeType, detailLevel);
}

/// <summary> Initializes a new instance of <see cref="ChatMessageImageContentItem"/>. </summary>
/// <param name="imageUrl"> An internet location, which must be accessible to the model,from which the image may be retrieved. </param>
/// <exception cref="ArgumentNullException"> <paramref name="imageUrl"/> is null. </exception>
Expand Down
10 changes: 9 additions & 1 deletion sdk/ai/Azure.AI.Inference/tests/ChatCompletionsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public enum ImageTestSourceKind
UsingInternetLocation,
UsingStream,
UsingBinaryData,
UsingFilePath
}

public ChatCompletionsClientTest(bool isAsync) : base(isAsync)
public ChatCompletionsClientTest(bool isAsync) : base(isAsync, RecordedTestMode.Record)
{
TestDiagnostics = false;
JsonPathSanitizers.Add("$.messages[*].content[*].image_url.url");
Expand Down Expand Up @@ -460,8 +461,14 @@ Dictionary<string, string> arguments
[TestCase(ImageTestSourceKind.UsingInternetLocation)]
[TestCase(ImageTestSourceKind.UsingStream)]
[TestCase(ImageTestSourceKind.UsingBinaryData)]
[TestCase(ImageTestSourceKind.UsingFilePath)]
public async Task TestChatCompletionsWithImages(ImageTestSourceKind imageSourceKind)
{
if (imageSourceKind == ImageTestSourceKind.UsingFilePath && Mode == RecordedTestMode.Playback)
{
Assert.Inconclusive("Unable to run test with file path in playback mode.");
}

var aoaiEndpoint = new Uri(TestEnvironment.AoaiEndpoint);
var aoaiKey = new AzureKeyCredential(TestEnvironment.AoaiKey);

Expand All @@ -480,6 +487,7 @@ public async Task TestChatCompletionsWithImages(ImageTestSourceKind imageSourceK
ImageTestSourceKind.UsingInternetLocation => new(GetTestImageInternetUri(), ChatMessageImageDetailLevel.Low),
ImageTestSourceKind.UsingStream => new(GetTestImageStream("image/jpg"), "image/jpg", ChatMessageImageDetailLevel.Low),
ImageTestSourceKind.UsingBinaryData => new(GetTestImageData("image/jpg"), "image/jpg", ChatMessageImageDetailLevel.Low),
ImageTestSourceKind.UsingFilePath => new(TestEnvironment.TestImageJpgInputPath, "image/jpg", ChatMessageImageDetailLevel.Low),
_ => throw new ArgumentException(nameof(imageSourceKind)),
};

Expand Down