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

Add IChatClient implementation to AnthropicApi #71

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

stephentoub
Copy link
Contributor

@stephentoub stephentoub commented Oct 28, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new chat client interface for improved handling of chat messages and completions.
    • Added support for streaming chat completions, allowing real-time updates during interactions.
    • Enhanced JSON serialization capabilities with new properties and classes for better data handling.
  • Bug Fixes

    • Updated package references to ensure compatibility and remove deprecated dependencies.
  • Documentation

    • Improved structure and clarity in the JSON serialization context to facilitate easier integration and usage.

Copy link

coderabbitai bot commented Oct 28, 2024

Walkthrough

This pull request introduces modifications to the src/libs/Anthropic project, including updates to the project file and the addition of new classes and methods for chat functionality. A property to suppress build warnings is added, along with new package references for asynchronous interfaces and AI abstractions. A file implementing the IChatClient interface is introduced, providing methods for chat message handling, including synchronous and asynchronous completions. Additionally, updates to the JsonSerializerContextTypes class enhance JSON serialization capabilities with new properties and a class for tool parameter schemas.

Changes

File Path Change Summary
src/libs/Anthropic/Anthropic.csproj Added <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>, added package references for Microsoft.Bcl.AsyncInterfaces and Microsoft.Extensions.AI.Abstractions, removed Tiktoken, and updated System.Text.Json version.
src/libs/Anthropic/Extensions/AnthropicApi.ChatClient.cs Added AnthropicApi class implementing IChatClient, with methods for metadata retrieval, message completion, and streaming.
src/libs/Anthropic/JsonSerializerContextTypes.AdditionalTypes.cs Added properties DictionaryStringObject and ToolParameterJsonSchema to JsonSerializerContextTypes, and introduced ToolParameterJsonSchema class with properties for improved JSON serialization.

Possibly related PRs

🐰 In the garden of code, I hop and play,
New features bloom, brightening the day.
With chat clients chatting, and schemas refined,
In this world of bytes, joyfully entwined.
So here’s to the changes, both big and small,
A rabbit's delight in the coding hall! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@CLAassistant
Copy link

CLAassistant commented Oct 28, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 12

🧹 Outside diff range and nitpick comments (1)
src/libs/Anthropic/JsonSerializerContextTypes.AdditionalTypes.cs (1)

26-34: Add XML documentation for new properties

The new properties are missing meaningful XML documentation. This documentation is important for IntelliSense and API documentation generation.

Consider adding descriptive documentation:

    /// <summary>
-   /// 
+   /// Gets or sets a dictionary that can store arbitrary string-keyed object values.
    /// </summary>
    public Dictionary<string, object?>? DictionaryStringObject { get; set; }

    /// <summary>
-   /// 
+   /// Gets or sets the JSON schema for tool parameters used in chat completions.
    /// </summary>
    public ToolParameterJsonSchema? ToolParameterJsonSchema { get; set; }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b17a1d2 and 2e48659.

📒 Files selected for processing (3)
  • src/libs/Anthropic/Anthropic.csproj (2 hunks)
  • src/libs/Anthropic/Extensions/AnthropicApi.ChatClient.cs (1 hunks)
  • src/libs/Anthropic/JsonSerializerContextTypes.AdditionalTypes.cs (2 hunks)
🔇 Additional comments (8)
src/libs/Anthropic/JsonSerializerContextTypes.AdditionalTypes.cs (2)

1-5: LGTM: Warning suppressions are appropriate

The warning suppressions for CA1002 and CA2227 are justified as this is a serialization context where mutable collections are necessary for proper JSON deserialization.


37-59: Enhance documentation and verify schema structure

The class needs proper XML documentation to explain its purpose and relationship to JSON Schema standards. Additionally, let's verify the schema structure matches OpenAPI specifications.

Add descriptive documentation:

    /// <summary>
-   /// 
+   /// Represents a JSON Schema structure for tool parameters following the OpenAPI specification.
    /// </summary>
    public sealed class ToolParameterJsonSchema
    {
        /// <summary>
-       /// 
+       /// Gets or sets the type of the parameter. Defaults to "object".
        /// </summary>
        [JsonPropertyName("type")]
        public string Type { get; set; } = "object";

        /// <summary>
-       /// 
+       /// Gets or sets the list of required property names.
        /// </summary>
        [JsonPropertyName("required")]
        public List<string> Required { get; set; } = [];

        /// <summary>
-       /// 
+       /// Gets or sets the dictionary of property schemas.
        /// </summary>
        [JsonPropertyName("properties")]
        public Dictionary<string, JsonElement> Properties { get; set; } = [];
    }

Let's verify the schema structure matches OpenAPI standards:

✅ Verification successful

Based on the OpenAPI specification and codebase analysis, I can now provide the final response:

Documentation suggestions approved, schema structure verified

The ToolParameterJsonSchema class correctly follows the OpenAPI specification for tool input schemas. The class structure with type, required, and properties fields matches the schema structure used in the Anthropic API for tool definitions. The suggested documentation improvements are appropriate and helpful.

  • The default value of "object" for the Type property aligns with the OpenAPI schema requirements for tool input definitions
  • The Properties dictionary using JsonElement matches the flexible schema structure needed for tool parameters
  • The Required list properly tracks mandatory properties as specified in the OpenAPI spec
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for similar schema structures in the codebase
rg -l "JsonSchema" | xargs rg -A 5 "type.*=.*\"object\".*required.*properties"

# Check if CSharpToJsonSchema is used correctly
ast-grep --pattern 'using CSharpToJsonSchema;'

Length of output: 303


Script:

#!/bin/bash
# Let's check how CSharpToJsonSchema is used in the test file
rg -A 10 "CSharpToJsonSchema" src/tests/Anthropic.IntegrationTests/WeatherFunctions.cs

# Also check for any other JsonSchema related classes/implementations
ast-grep --pattern 'class $_ {
  $$$
  Type = "object"
  $$$
}'

# Let's see if there are any OpenAPI related files or schemas
fd -e json -e yaml openapi

Length of output: 403


Script:

#!/bin/bash
# Let's examine the OpenAPI spec to verify the schema structure
cat src/libs/Anthropic/openapi.yaml

# Let's also check how this class is used in the codebase
rg -A 5 "ToolParameterJsonSchema" 

# Check if there are any other similar schema classes
ast-grep --pattern 'class $_ {
  $$$
  [JsonPropertyName("type")]
  public string Type { get; set; }
  $$$
}'

Length of output: 36891

src/libs/Anthropic/Anthropic.csproj (2)

20-21: Consider package version stability and token counting strategy.

  1. The new packages are using pre-release versions:

    • Microsoft.Bcl.AsyncInterfaces (9.0.0-rc.2)
    • Microsoft.Extensions.AI.Abstractions (9.0.0-preview.9)

    Consider the stability implications for production use.

  2. The removal of Tiktoken package suggests a change in token counting strategy. Please verify that token counting functionality is handled appropriately in the new implementation.

#!/bin/bash
# Search for token counting related code
rg -g "*.cs" -i "token.*count|count.*token"

# Check if there are any other files still referencing Tiktoken
rg -l "Tiktoken"

6-6: Verify cross-framework compatibility before suppressing TFM warnings.

While suppressing TFM support warnings can clean up the build output, it might mask potential compatibility issues across the different target frameworks (net4.6.2, net8.0, netstandard2.0). Please ensure that:

  1. All APIs used are available across all target frameworks
  2. Appropriate #if directives are in place where needed
✅ Verification successful

The previous search didn't yield results for conditional compilation, but we should check for usage of modern APIs and verify the project configuration to make an informed decision about the TFM warning suppression.


Based on the analysis of the codebase, I can now provide a final response:

TFM warning suppression is appropriate given the source-generated compatibility layer

The warning suppression is justified because:

  1. The code primarily uses System.Text.Json which is properly handled across frameworks through explicit package references and version control (9.0.0-rc.2.24473.5)
  2. Framework-specific dependencies are properly segregated in conditional ItemGroups
  3. The codebase uses source generation (as seen in Generated/ files) which provides compile-time verification of cross-framework compatibility
  4. Microsoft.Bcl.AsyncInterfaces package is included to ensure async interface support across frameworks
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for framework-specific code paths
rg -g "*.cs" "#if\s+(NET|NETSTANDARD|NETFRAMEWORK)"

Length of output: 1667


Script:

#!/bin/bash
# Let's check for any framework-specific API usage patterns
rg -g "*.cs" "(?:Task|IAsyncEnumerable|System\.Text\.Json|Microsoft\.Extensions\.AI)"

# Also check the csproj file content to understand the framework targets and dependencies
cat src/libs/Anthropic/Anthropic.csproj

Length of output: 191896

src/libs/Anthropic/Extensions/AnthropicApi.ChatClient.cs (4)

31-34: Ensure correct role mapping

When assigning Role in responseMessage, ensure that all possible MessageRole values are correctly mapped to ChatRole. Currently, only MessageRole.Assistant and defaulting to ChatRole.User are handled. If other roles exist, they should be mapped appropriately.


74-75: Specify JsonSerializerOptions when deserializing

When deserializing block.ToolUse!.Input, ensure that the correct JsonSerializerOptions are used. This prevents potential deserialization issues due to missing or incorrect settings.


84-91: Handle unknown stop reasons gracefully

In the switch expression for FinishReason, unhandled StopReason values default to creating a new ChatFinishReason with response.StopReason.ToString()!. Ensure that this is the intended behavior and that it won't cause issues if response.StopReason has unexpected values.


215-215: ⚠️ Potential issue

Fix invalid dictionary initialization

The expression (responseMessage.AdditionalProperties ??= [])[...] uses [], which is invalid. Initialize the dictionary properly.

Modification:

-(responseMessage.AdditionalProperties ??= [])[nameof(response.StopSequence)] = response.StopSequence;
+(responseMessage.AdditionalProperties ??= new Dictionary<string, object?>())[nameof(response.StopSequence)] = response.StopSequence;

Likely invalid or redundant comment.


public partial class AnthropicApi : IChatClient
{
private static readonly JsonElement s_defaultParameterSchema = JsonDocument.Parse("{}").RootElement;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Initialize static readonly JsonElement correctly

The static readonly field s_defaultParameterSchema is initialized using JsonDocument.Parse("{}").RootElement;. Since JsonDocument implements IDisposable, the JsonDocument should be disposed to avoid potential memory leaks.

You can modify the initialization as follows:

-private static readonly JsonElement s_defaultParameterSchema = JsonDocument.Parse("{}").RootElement;
+private static readonly JsonElement s_defaultParameterSchema;
+
+static AnthropicApi()
+{
+    using JsonDocument doc = JsonDocument.Parse("{}");
+    s_defaultParameterSchema = doc.RootElement.Clone();
+}
📝 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
private static readonly JsonElement s_defaultParameterSchema = JsonDocument.Parse("{}").RootElement;
private static readonly JsonElement s_defaultParameterSchema;
static AnthropicApi()
{
using JsonDocument doc = JsonDocument.Parse("{}");
s_defaultParameterSchema = doc.RootElement.Clone();
}

Comment on lines +41 to +77
if (response.Content.Value1 is string stringContents)
{
responseMessage.Contents.Add(new TextContent(stringContents));
}
else if (response.Content.Value2 is IList<Block> blocks)
{
foreach (var block in blocks)
{
if (block.IsText)
{
responseMessage.Contents.Add(new TextContent(block.Text!.Text) { RawRepresentation = block.Text });
}
else if (block.IsImage)
{
responseMessage.Contents.Add(new ImageContent(
block.Image!.Source.Data,
block.Image!.Source.MediaType switch
{
ImageBlockSourceMediaType.ImagePng => "image/png",
ImageBlockSourceMediaType.ImageGif => "image/gif",
ImageBlockSourceMediaType.ImageWebp => "image/webp",
_ => "image/jpeg",
})
{
RawRepresentation = block.Image
});
}
else if (block.IsToolUse)
{
responseMessage.Contents.Add(new FunctionCallContent(
block.ToolUse!.Id,
block.ToolUse!.Name,
block.ToolUse!.Input is JsonElement e ? (IDictionary<string, object?>?)e.Deserialize(
JsonSerializerContext.Options.GetTypeInfo(typeof(Dictionary<string, object?>))) :
null));
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add null checks to prevent NullReferenceException

The use of the null-forgiving operator ! on block.Text, block.Image, and block.ToolUse can lead to NullReferenceException if these properties are unexpectedly null. Instead, add null checks or handle potential null values safely.

Example modification:

-if (block.IsText)
+if (block.IsText && block.Text is not null)

Apply similar checks for block.Image and block.ToolUse.

Committable suggestion was skipped due to low confidence.

@HavenDV HavenDV merged commit 896715e into tryAGI:main Oct 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants