Skip to content

Adds support for OpenAI Content Parts. Closes #1174 #1175

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 1 commit into from
May 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace DevProxy.Abstractions.LanguageModel;

public interface ILanguageModelChatCompletionMessage
{
string Content { get; set; }
object Content { get; set; }
string Role { get; set; }
}
6 changes: 3 additions & 3 deletions dev-proxy-abstractions/LanguageModel/OllamaModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class OllamaLanguageModelChatCompletionResponse : OllamaResponse
public OllamaLanguageModelChatCompletionMessage Message { get; set; } = new();
public override string? Response
{
get => Message.Content;
get => Message.Content.ToString();
set
{
if (value is null)
Expand Down Expand Up @@ -118,7 +118,7 @@ public override OpenAIResponse ConvertToOpenAIResponse()
Index = 0,
Message = new()
{
Content = Message.Content,
Content = Message.Content.ToString() ?? string.Empty,
Role = Message.Role
}
}],
Expand Down Expand Up @@ -152,7 +152,7 @@ public override OpenAIResponse ConvertToOpenAIResponse()

public class OllamaLanguageModelChatCompletionMessage : ILanguageModelChatCompletionMessage
{
public string Content { get; set; } = string.Empty;
public object Content { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;

public override bool Equals(object? obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace DevProxy.Abstractions.LanguageModel;

public class OpenAIContentPartJsonConverter : JsonConverter<object>
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert == typeof(object);
}

public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
else if (reader.TokenType == JsonTokenType.StartArray)
{
var contentParts = new List<object>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}

if (reader.TokenType == JsonTokenType.StartObject)
{
using JsonDocument doc = JsonDocument.ParseValue(ref reader);

var root = doc.RootElement;
if (root.TryGetProperty("type", out var typeProp))
{
var contentType = typeProp.GetString() switch
{
"text" => typeof(OpenAITextContentPart),
"image" => typeof(OpenAIImageContentPart),
"audio" => typeof(OpenAIAudioContentPart),
"file" => typeof(OpenAIFileContentPart),
_ => null
};
if (contentType is not null)
{
var contentPart = JsonSerializer.Deserialize(doc.RootElement.GetRawText(), contentType, options);
if (contentPart is not null)
{
contentParts.Add(contentPart);
}
}
}
}
}
return contentParts.ToArray();
}
return null;
}

public override void Write(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
{
if (value is string str)
{
writer.WriteStringValue(str);
}
else
{
JsonSerializer.Serialize(writer, value, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;

namespace DevProxy.Abstractions.LanguageModel;

Expand Down Expand Up @@ -170,7 +170,9 @@ private async Task<bool> IsEnabledInternalAsync()
Temperature = options?.Temperature
};

var response = await _httpClient.PostAsJsonAsync(url, payload);
// var payloadString = JsonSerializer.Serialize(payload, ProxyUtils.JsonSerializerOptions);

var response = await _httpClient.PostAsJsonAsync(url, payload, ProxyUtils.JsonSerializerOptions);
_logger.LogDebug("Response: {response}", response.StatusCode);

if (!response.IsSuccessStatusCode)
Expand Down
80 changes: 67 additions & 13 deletions dev-proxy-abstractions/LanguageModel/OpenAIModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class OpenAIError
public string? Message { get; set; }
}

public abstract class OpenAIResponse: ILanguageModelCompletionResponse
public abstract class OpenAIResponse : ILanguageModelCompletionResponse
{
public long Created { get; set; }
public OpenAIError? Error { get; set; }
Expand Down Expand Up @@ -106,9 +106,63 @@ public class OpenAICompletionResponseChoice : OpenAIResponseChoice
public string Text { get; set; } = string.Empty;
}

public class OpenAIChatCompletionMessage: ILanguageModelChatCompletionMessage
#region content parts

public abstract class OpenAIContentPart
{
public string Content { get; set; } = string.Empty;
public string? Type { get; set; }
}

public class OpenAITextContentPart : OpenAIContentPart
{
public string? Text { get; set; }
}

public class OpenAIImageContentPartUrl
{
public string? Detail { get; set; } = "auto";
public string? Url { get; set; }
}

public class OpenAIImageContentPart : OpenAIContentPart
{
[JsonPropertyName("image_url")]
public OpenAIImageContentPartUrl? Url { get; set; }
}

public class OpenAIAudioContentPartInputAudio
{
public string? Data { get; set; }
public string? Format { get; set; }
}

public class OpenAIAudioContentPart : OpenAIContentPart
{
[JsonPropertyName("input_audio")]
public OpenAIAudioContentPartInputAudio? InputAudio { get; set; }
}

public class OpenAIFileContentPartFile
{
[JsonPropertyName("file_data")]
public string? Data { get; set; }
[JsonPropertyName("file_id")]
public string? Id { get; set; }
[JsonPropertyName("filename")]
public string? Name { get; set; }
}

public class OpenAIFileContentPart : OpenAIContentPart
{
public OpenAIFileContentPartFile? File { get; set; }
}

#endregion

public class OpenAIChatCompletionMessage : ILanguageModelChatCompletionMessage
{
[JsonConverter(typeof(OpenAIContentPartJsonConverter))]
public object Content { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;

public override bool Equals(object? obj)
Expand Down Expand Up @@ -204,13 +258,13 @@ public class OpenAIFineTuneRequest : OpenAIRequest
}

public class OpenAIFineTuneResponse : OpenAIResponse
{
{
[JsonPropertyName("fine_tuned_model")]
public string? FineTunedModel { get; set; }
public string Status { get; set; } = string.Empty;
public string? Organization { get; set; }
public long CreatedAt { get; set; }
public long UpdatedAt { get; set; }
public string? FineTunedModel { get; set; }
public string Status { get; set; } = string.Empty;
public string? Organization { get; set; }
public long CreatedAt { get; set; }
public long UpdatedAt { get; set; }
[JsonPropertyName("training_file")]
public string TrainingFile { get; set; } = string.Empty;
[JsonPropertyName("validation_file")]
Expand All @@ -233,16 +287,16 @@ public class OpenAIImageRequest : OpenAIRequest
}

public class OpenAIImageResponse : OpenAIResponse
{
public OpenAIImageData[]? Data { get; set; }
{
public OpenAIImageData[]? Data { get; set; }
public override string? Response => null; // Image responses don't have a text response
}

public class OpenAIImageData
{
public string? Url { get; set; }
public string? Url { get; set; }
[JsonPropertyName("b64_json")]
public string? Base64Json { get; set; }
public string? Base64Json { get; set; }
[JsonPropertyName("revised_prompt")]
public string? RevisedPrompt { get; set; }
}
7 changes: 7 additions & 0 deletions dev-proxy-plugins/Mocks/OpenAIMockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public override async Task RegisterAsync()

private async Task OnRequestAsync(object sender, ProxyRequestArgs e)
{
if (UrlsToWatch is null ||
!e.HasRequestUrlMatch(UrlsToWatch))
{
Logger.LogRequest("URL not matched", MessageType.Skipped, new LoggingContext(e.Session));
return;
}

var request = e.Session.HttpClient.Request;
if (request.Method is null ||
!request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase) ||
Expand Down