-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Fix MistralAI function calling and add image content support (#…
…9844) ### Motivation and Context Tool messages were being rejected as bad requests Close #9806 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
- Loading branch information
1 parent
5e7049b
commit 6dc7559
Showing
13 changed files
with
569 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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
13 changes: 13 additions & 0 deletions
13
dotnet/src/Connectors/Connectors.MistralAI/Client/ContentChunk.cs
This file contains 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,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.MistralAI.Client; | ||
|
||
[JsonDerivedType(typeof(TextChunk))] | ||
[JsonDerivedType(typeof(ImageUrlChunk))] | ||
internal abstract class ContentChunk(ContentChunkType type) | ||
{ | ||
[JsonPropertyName("type")] | ||
public string Type { get; set; } = type.ToString(); | ||
} |
62 changes: 62 additions & 0 deletions
62
dotnet/src/Connectors/Connectors.MistralAI/Client/ContentChunkType.cs
This file contains 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,62 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.MistralAI.Client; | ||
|
||
internal readonly struct ContentChunkType : IEquatable<ContentChunkType> | ||
{ | ||
public static ContentChunkType Text { get; } = new("text"); | ||
|
||
public static ContentChunkType ImageUrl { get; } = new("image_url"); | ||
|
||
public string Type { get; } | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="ContentChunkType"/> instance with the provided type. | ||
/// </summary> | ||
/// <param name="type">The label to associate with this <see cref="ContentChunkType"/>.</param> | ||
[JsonConstructor] | ||
public ContentChunkType(string type) | ||
{ | ||
Verify.NotNullOrWhiteSpace(type, nameof(type)); | ||
this.Type = type!; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a value indicating whether two <see cref="ContentChunkType"/> instances are equivalent, as determined by a | ||
/// case-insensitive comparison of their labels. | ||
/// </summary> | ||
/// <param name="left"> the first <see cref="ContentChunkType"/> instance to compare </param> | ||
/// <param name="right"> the second <see cref="ContentChunkType"/> instance to compare </param> | ||
/// <returns> true if left and right are both null or have equivalent labels; false otherwise </returns> | ||
public static bool operator ==(ContentChunkType left, ContentChunkType right) | ||
=> left.Equals(right); | ||
|
||
/// <summary> | ||
/// Returns a value indicating whether two <see cref="ContentChunkType"/> instances are not equivalent, as determined by a | ||
/// case-insensitive comparison of their labels. | ||
/// </summary> | ||
/// <param name="left"> the first <see cref="ContentChunkType"/> instance to compare </param> | ||
/// <param name="right"> the second <see cref="ContentChunkType"/> instance to compare </param> | ||
/// <returns> false if left and right are both null or have equivalent labels; true otherwise </returns> | ||
public static bool operator !=(ContentChunkType left, ContentChunkType right) | ||
=> !left.Equals(right); | ||
|
||
/// <inheritdoc/> | ||
public override bool Equals([NotNullWhen(true)] object? obj) | ||
=> obj is ContentChunkType otherRole && this == otherRole; | ||
|
||
/// <inheritdoc/> | ||
public bool Equals(ContentChunkType other) | ||
=> string.Equals(this.Type, other.Type, StringComparison.OrdinalIgnoreCase); | ||
|
||
/// <inheritdoc/> | ||
public override int GetHashCode() | ||
=> StringComparer.OrdinalIgnoreCase.GetHashCode(this.Type); | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() => this.Type; | ||
} |
11 changes: 11 additions & 0 deletions
11
dotnet/src/Connectors/Connectors.MistralAI/Client/ImageUrlChunk.cs
This file contains 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,11 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.MistralAI.Client; | ||
internal class ImageUrlChunk(Uri imageUrl) : ContentChunk(ContentChunkType.ImageUrl) | ||
{ | ||
[JsonPropertyName("image_url")] | ||
public string ImageUrl { get; set; } = imageUrl.ToString(); | ||
} |
This file contains 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 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
Oops, something went wrong.