Skip to content
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

feat: Use official OpenAPI spec. #78

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Discord](https://img.shields.io/discord/1115206893015662663?label=Discord&logo=discord&logoColor=white&color=d82679)](https://discord.gg/Ca2xhfBf3v)

## Features 🔥
- Fully generated C# SDK based on [OpenAPI specification](https://raw.githubusercontent.com/davidmigloz/langchain_dart/main/packages/anthropic_sdk_dart/oas/anthropic_openapi_curated.yaml) using [OpenApiGenerator](https://github.com/HavenDV/OpenApiGenerator)
- Fully generated C# SDK based on [official OpenAPI specification](https://raw.githubusercontent.com/anthropics/anthropic-sdk-typescript/refs/heads/main/.stats.yml) using [AutoSDK](https://github.com/HavenDV/OpenApiGenerator)
- Automatic releases of new preview versions if there was an update to the OpenAPI specification
- Source generator to define tools natively through C# interfaces
- All modern .NET features - nullability, trimming, NativeAOT, etc.
Expand Down
1 change: 1 addition & 0 deletions src/helpers/FixOpenApiSpec/FixOpenApiSpec.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoSDK" Version="0.27.1-dev.3" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Consider upgrading to the stable version 0.27.0 instead of using pre-release version 0.27.1-dev.3

The current implementation uses AutoSDK version 0.27.1-dev.3, which is a pre-release version. The latest stable version available on NuGet is 0.27.0. Using a pre-release version in a production environment could introduce instability or breaking changes. Unless there's a specific feature in 0.27.1-dev.3 that's required, it's recommended to use the stable version 0.27.0.

🔗 Analysis chain

Confirm the appropriateness of using a pre-release version of AutoSDK

The package version 0.27.1-dev.3 of AutoSDK appears to be a development or pre-release version. Consider whether it's appropriate to depend on a pre-release package, as it might introduce instability or unexpected changes. If possible, evaluate using the latest stable release.

You can run the following script to check for the latest stable version of AutoSDK:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Fetch the latest stable version of 'AutoSDK' from NuGet.

# Fetch all available versions of 'AutoSDK' and filter out pre-release versions
curl -s "https://api.nuget.org/v3-flatcontainer/autosdk/index.json" | jq -r '.versions[]' | grep -v -E '(-)'

# Get the highest stable version
LATEST_VERSION=$(curl -s "https://api.nuget.org/v3-flatcontainer/autosdk/index.json" | jq -r '.versions[]' | grep -v -E '(-)' | sort -V | tail -n 1)
echo "Latest stable version of AutoSDK is: $LATEST_VERSION"

Length of output: 400

<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
</ItemGroup>

Expand Down
71 changes: 54 additions & 17 deletions src/helpers/FixOpenApiSpec/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
using AutoSDK.Helpers;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;

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

if (OpenApi31Support.IsOpenApi31(jsonOrYaml))
{
jsonOrYaml = OpenApi31Support.ConvertToOpenApi30(jsonOrYaml);
}

var openApiDocument = new OpenApiStringReader().Read(jsonOrYaml, out var diagnostics);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling after parsing the OpenAPI document

After parsing the OpenAPI document with OpenApiStringReader().Read, it's important to check diagnostics.Errors for any parsing errors before modifying the document. This ensures that issues with the initial OpenAPI specification are caught early and prevents potential exceptions when manipulating an invalid document.

You can implement error handling as follows:

if (diagnostics.Errors.Count > 0)
{
    foreach (var error in diagnostics.Errors)
    {
        Console.WriteLine(error.Message);
    }
    Environment.Exit(1);
}

Place this code after line 17, before modifying openApiDocument.

openApiDocument.Components.Schemas["TextBlock"].Properties["type"].Enum = new List<IOpenApiAny>
{
new OpenApiString("text"),
};
openApiDocument.Components.Schemas["ImageBlock"].Properties["type"].Enum = new List<IOpenApiAny>
openApiDocument.Servers.Clear();
openApiDocument.Servers.Add(new OpenApiServer
{
new OpenApiString("image"),
};
openApiDocument.Components.Schemas["ToolUseBlock"]!.Properties["type"].Enum = new List<IOpenApiAny>
Url = "https://api.anthropic.com/v1",
});

openApiDocument.Components.SecuritySchemes.Clear();
openApiDocument.Components.SecuritySchemes.Add("ApiKeyAuth", new OpenApiSecurityScheme
{
new OpenApiString("tool_use"),
};
openApiDocument.Components.Schemas["ToolResultBlock"]!.Properties["type"].Enum = new List<IOpenApiAny>
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Name = "x-api-key",
});

openApiDocument.SecurityRequirements.Clear();
openApiDocument.SecurityRequirements.Add(new OpenApiSecurityRequirement
{
new OpenApiString("tool_result"),
};
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ApiKeyAuth",
},
},
new List<string>()
}
});

openApiDocument.Components.Schemas["TextBlock"].Required.Add("type");
openApiDocument.Components.Schemas["ImageBlock"].Required.Add("type");
openApiDocument.Components.Schemas["ToolUseBlock"].Required.Add("type");
openApiDocument.Components.Schemas["ToolResultBlock"].Required.Add("type");
// openApiDocument.Components.Schemas["TextBlock"].Properties["type"].Enum = new List<IOpenApiAny>
// {
// new OpenApiString("text"),
// };
// openApiDocument.Components.Schemas["ImageBlock"].Properties["type"].Enum = new List<IOpenApiAny>
// {
// new OpenApiString("image"),
// };
// openApiDocument.Components.Schemas["ToolUseBlock"]!.Properties["type"].Enum = new List<IOpenApiAny>
// {
// new OpenApiString("tool_use"),
// };
// openApiDocument.Components.Schemas["ToolResultBlock"]!.Properties["type"].Enum = new List<IOpenApiAny>
// {
// new OpenApiString("tool_result"),
// };
//
// openApiDocument.Components.Schemas["TextBlock"].Required.Add("type");
// openApiDocument.Components.Schemas["ImageBlock"].Required.Add("type");
// openApiDocument.Components.Schemas["ToolUseBlock"].Required.Add("type");
// openApiDocument.Components.Schemas["ToolResultBlock"].Required.Add("type");

jsonOrYaml = openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
_ = new OpenApiStringReader().Read(jsonOrYaml, out diagnostics);
Expand Down
51 changes: 35 additions & 16 deletions src/libs/Anthropic/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public static class StringExtensions
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static Message AsUserMessage(this string content)
public static InputMessage AsUserMessage(this string content)
{
return new Message
return new InputMessage
{
Role = MessageRole.User,
Role = InputMessageRole.User,
Content = content,
};
}
Expand All @@ -24,11 +24,11 @@ public static Message AsUserMessage(this string content)
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static Message AsAssistantMessage(this string content)
public static InputMessage AsAssistantMessage(this string content)
{
return new Message
return new InputMessage
{
Role = MessageRole.Assistant,
Role = InputMessageRole.Assistant,
Content = content,
};
}
Expand All @@ -39,16 +39,16 @@ public static Message AsAssistantMessage(this string content)
/// <param name="content"></param>
/// <param name="toolUse"></param>
/// <returns></returns>
public static Message AsToolCall(this string content, ToolUseBlock toolUse)
public static InputMessage AsToolCall(this string content, ResponseToolUseBlock toolUse)
{
toolUse = toolUse ?? throw new ArgumentNullException(nameof(toolUse));

return new Message
return new InputMessage
{
Role = MessageRole.User,
Content = new List<Block>
Role = InputMessageRole.User,
Content = new List<ContentVariant2Item2>
{
new ToolResultBlock
new RequestToolResultBlock
{
ToolUseId = toolUse.Id,
Content = content,
Expand All @@ -62,15 +62,34 @@ public static Message AsToolCall(this string content, ToolUseBlock toolUse)
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static Message AsRequestMessage(this Message message)
public static InputMessage AsInputMessage(this Message message)
{
message = message ?? throw new ArgumentNullException(nameof(message));

return new Message
return new InputMessage
{
Content = message.Content,
Role = message.Role,
StopSequence = message.StopSequence,
Content = message.Content.Select(x =>
{
if (x.IsText)
{
return new ContentVariant2Item2(new RequestTextBlock
{
Text = x.Text!.Text,
});
}
if (x.IsToolUse)
{
return new ContentVariant2Item2(new RequestToolUseBlock
{
Id = x.ToolUse!.Id,
Input = x.ToolUse.Input,
Name = x.ToolUse!.Name,
});
}

return new ContentVariant2Item2();
}).ToList(),
Role = InputMessageRole.Assistant,
Comment on lines +65 to +92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle Unrecognized Content Types in 'AsInputMessage' Method

In the AsInputMessage method, if a content item is neither Text nor ToolUse, it returns an empty ContentVariant2Item2, which may lead to unexpected behavior. Consider adding explicit handling or logging for unrecognized content types to improve robustness.

+ else
+ {
+     // Handle unknown content types
+     throw new InvalidOperationException("Unrecognized content type in message.");
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static InputMessage AsInputMessage(this Message message)
{
message = message ?? throw new ArgumentNullException(nameof(message));
return new Message
return new InputMessage
{
Content = message.Content,
Role = message.Role,
StopSequence = message.StopSequence,
Content = message.Content.Select(x =>
{
if (x.IsText)
{
return new ContentVariant2Item2(new RequestTextBlock
{
Text = x.Text!.Text,
});
}
if (x.IsToolUse)
{
return new ContentVariant2Item2(new RequestToolUseBlock
{
Id = x.ToolUse!.Id,
Input = x.ToolUse.Input,
Name = x.ToolUse!.Name,
});
}
return new ContentVariant2Item2();
}).ToList(),
Role = InputMessageRole.Assistant,
public static InputMessage AsInputMessage(this Message message)
{
message = message ?? throw new ArgumentNullException(nameof(message));
return new InputMessage
{
Content = message.Content.Select(x =>
{
if (x.IsText)
{
return new ContentVariant2Item2(new RequestTextBlock
{
Text = x.Text!.Text,
});
}
if (x.IsToolUse)
{
return new ContentVariant2Item2(new RequestToolUseBlock
{
Id = x.ToolUse!.Id,
Input = x.ToolUse.Input,
Name = x.ToolUse!.Name,
});
}
else
{
// Handle unknown content types
throw new InvalidOperationException("Unrecognized content type in message.");
}
}).ToList(),
Role = InputMessageRole.Assistant,

};
}

Expand Down
68 changes: 68 additions & 0 deletions src/libs/Anthropic/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Anthropic;

public partial class InputMessage
{
/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static implicit operator InputMessage(string content)
{
return ToInputMessage(content);
}

/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static InputMessage ToInputMessage(string content)
{
return new InputMessage
{
Role = InputMessageRole.User,
Content = content,
};
}

/// <summary>
/// Returns the message content as a simple text.
/// </summary>
/// <returns></returns>
public string AsSimpleText()
{
if (Content.IsValue1)
{
return Content.Value1!;
}
if (Content.IsValue2)
{
return string.Join(Environment.NewLine, Content.Value2!
.Select(x =>
{
if (x.IsText)
{
return x.Text!.Text;
}
if (x.IsImage)
{
return "Image";
//return x.Image!.Source.Data;
}
if (x.IsToolUse)
{
return x.ToolUse!.Name;
}
if (x.IsToolResult)
{
return x.ToolResult!.ToolUseId;
}

return string.Empty;
}));
}

return string.Empty;
}
}
67 changes: 13 additions & 54 deletions src/libs/Anthropic/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,25 @@ namespace Anthropic;

public partial class Message
{
/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static implicit operator Message(string content)
{
return ToMessage(content);
}

/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static Message ToMessage(string content)
{
return new Message
{
Role = MessageRole.User,
Content = content,
};
}

/// <summary>
/// Returns the message content as a simple text.
/// </summary>
/// <returns></returns>
public string AsSimpleText()
{
if (Content.IsValue1)
{
return Content.Value1!;
}
if (Content.IsValue2)
{
return string.Join(Environment.NewLine, Content.Value2!
.Select(x =>
return string.Join(Environment.NewLine, Content
.Select(x =>
{
if (x.IsText)
{
return x.Text!.Text;
}
if (x.IsToolUse)
{
if (x.IsText)
{
return x.Text!.Text;
}
if (x.IsImage)
{
return x.Image!.Source.Data;
}
if (x.IsToolUse)
{
return x.ToolUse!.Name;
}
if (x.IsToolResult)
{
return x.ToolResult!.ToolUseId;
}

return string.Empty;
}));
}

return string.Empty;
return x.ToolUse!.Name;
}

return string.Empty;
}));
}
}
Loading