Skip to content

Commit

Permalink
fix: Fixed Streaming issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 24, 2024
1 parent 48dbf5e commit f6261ff
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 597 deletions.
20 changes: 6 additions & 14 deletions src/helpers/FixOpenApiSpec/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
using Microsoft.OpenApi.Readers;

var path = args[0];
var text = await File.ReadAllTextAsync(path);
var jsonOrYaml = await File.ReadAllTextAsync(path);

text = text.Replace("description: *run_temperature_description", "description: empty");
text = text.Replace("description: &run_temperature_description ", "description: ");
var openApiDocument = new OpenApiStringReader().Read(jsonOrYaml, out var diagnostics);

text = text.Replace("description: *run_top_p_description", "description: empty");
text = text.Replace("description: &run_top_p_description ", "description: ");
openApiDocument.Components.Schemas["MessageStreamEvent"]!.Discriminator.Mapping["error"] = "#/components/schemas/ErrorEvent";

var openApiDocument = new OpenApiStringReader().Read(text, out var diagnostics);
openApiDocument.Components.Schemas["ParallelToolCalls"]!.Default = null;
openApiDocument.Components.Schemas["ParallelToolCalls"]!.Nullable = true;

openApiDocument.Components.Schemas["CreateEmbeddingRequest"]!.Properties["dimensions"].Nullable = true;

text = openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
_ = new OpenApiStringReader().Read(text, out diagnostics);
jsonOrYaml = openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
_ = new OpenApiStringReader().Read(jsonOrYaml, out diagnostics);

if (diagnostics.Errors.Count > 0)
{
Expand All @@ -30,4 +22,4 @@
Environment.Exit(1);
}

await File.WriteAllTextAsync(path, text);
await File.WriteAllTextAsync(path, jsonOrYaml);
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public enum MessageStreamEventDiscriminatorType
///
/// </summary>
Ping,
/// <summary>
///
/// </summary>
Error,
}

/// <summary>
Expand All @@ -57,6 +61,7 @@ public static string ToValueString(this MessageStreamEventDiscriminatorType valu
MessageStreamEventDiscriminatorType.ContentBlockDelta => "content_block_delta",
MessageStreamEventDiscriminatorType.ContentBlockStop => "content_block_stop",
MessageStreamEventDiscriminatorType.Ping => "ping",
MessageStreamEventDiscriminatorType.Error => "error",
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, null),
};
}
Expand All @@ -74,6 +79,7 @@ public static string ToValueString(this MessageStreamEventDiscriminatorType valu
"content_block_delta" => MessageStreamEventDiscriminatorType.ContentBlockDelta,
"content_block_stop" => MessageStreamEventDiscriminatorType.ContentBlockStop,
"ping" => MessageStreamEventDiscriminatorType.Ping,
"error" => MessageStreamEventDiscriminatorType.Error,
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,49 @@ public class MessageStreamEventJsonConverter : global::System.Text.Json.Serializ
start = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.MessageDeltaEvent? delta = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageDelta)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.MessageDeltaEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.MessageDeltaEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.MessageDeltaEvent)}");
delta = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.MessageStopEvent? stop = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStop)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.MessageStopEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.MessageStopEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.MessageStopEvent)}");
stop = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.ContentBlockStartEvent? contentBlockStart = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.ContentBlockStart)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.ContentBlockStartEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.ContentBlockStartEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.ContentBlockStartEvent)}");
contentBlockStart = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.ContentBlockDeltaEvent? contentBlockDelta = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.ContentBlockDelta)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.ContentBlockDeltaEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.ContentBlockDeltaEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.ContentBlockDeltaEvent)}");
contentBlockDelta = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.ContentBlockStopEvent? contentBlockStop = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.ContentBlockStop)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.ContentBlockStopEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.ContentBlockStopEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.ContentBlockStopEvent)}");
contentBlockStop = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.PingEvent? ping = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.Ping)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.PingEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.PingEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.PingEvent)}");
ping = global::System.Text.Json.JsonSerializer.Deserialize(ref reader, typeInfo);
}
global::Anthropic.ErrorEvent? error = default;
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.MessageStart)
if (discriminator?.Type == global::Anthropic.MessageStreamEventDiscriminatorType.Error)
{
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(global::Anthropic.ErrorEvent), options) as global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Anthropic.ErrorEvent> ??
throw new global::System.InvalidOperationException($"Cannot get type info for {nameof(global::Anthropic.ErrorEvent)}");
Expand Down
10 changes: 5 additions & 5 deletions src/libs/Anthropic/generate.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
dotnet tool install --global autosdk.cli --prerelease
rm -rf Generated
curl -o openapi.yaml https://raw.githubusercontent.com/davidmigloz/langchain_dart/main/packages/anthropic_sdk_dart/oas/anthropic_openapi_curated.yaml
#dotnet run --project ../../helpers/FixOpenApiSpec openapi.yaml
#if [ $? -ne 0 ]; then
# echo "Failed, exiting..."
# exit 1
#fi
dotnet run --project ../../helpers/FixOpenApiSpec openapi.yaml
if [ $? -ne 0 ]; then
echo "Failed, exiting..."
exit 1
fi
autosdk generate openapi.yaml \
--namespace Anthropic \
--clientClassName AnthropicApi \
Expand Down
Loading

0 comments on commit f6261ff

Please sign in to comment.