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:Update citation handling in Cohere API with new CitationOptions class #51

Merged
merged 1 commit into from
Sep 17, 2024
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
9 changes: 4 additions & 5 deletions src/libs/Cohere/Generated/Cohere.CohereApi.Chatv2.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ partial void ProcessChatv2ResponseContent(
/// <param name="documents">
/// A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
/// </param>
/// <param name="citationMode">
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
/// <param name="citationOptions">
/// Options for controlling citation generation.
/// </param>
/// <param name="responseFormat">
/// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R](https://docs.cohere.com/docs/command-r), [Command R+](https://docs.cohere.com/docs/command-r-plus) and newer models.<br/>
Expand Down Expand Up @@ -173,7 +172,7 @@ partial void ProcessChatv2ResponseContent(
global::System.Collections.Generic.IList<global::Cohere.ChatMessage2> messages,
global::System.Collections.Generic.IList<global::Cohere.Tool2>? tools = default,
global::System.Collections.Generic.IList<global::Cohere.OneOf<string, global::Cohere.Document>>? documents = default,
global::Cohere.Chatv2RequestCitationMode? citationMode = default,
global::Cohere.CitationOptions? citationOptions = default,
global::Cohere.ResponseFormat2? responseFormat = default,
global::Cohere.Chatv2RequestSafetyMode? safetyMode = default,
int? maxTokens = default,
Expand All @@ -192,7 +191,7 @@ partial void ProcessChatv2ResponseContent(
Messages = messages,
Tools = tools,
Documents = documents,
CitationMode = citationMode,
CitationOptions = citationOptions,
ResponseFormat = responseFormat,
SafetyMode = safetyMode,
MaxTokens = maxTokens,
Expand Down
7 changes: 3 additions & 4 deletions src/libs/Cohere/Generated/Cohere.ICohereApi.Chatv2.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public partial interface ICohereApi
/// <param name="documents">
/// A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
/// </param>
/// <param name="citationMode">
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
/// <param name="citationOptions">
/// Options for controlling citation generation.
/// </param>
/// <param name="responseFormat">
/// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R](https://docs.cohere.com/docs/command-r), [Command R+](https://docs.cohere.com/docs/command-r-plus) and newer models.<br/>
Expand Down Expand Up @@ -93,7 +92,7 @@ public partial interface ICohereApi
global::System.Collections.Generic.IList<global::Cohere.ChatMessage2> messages,
global::System.Collections.Generic.IList<global::Cohere.Tool2>? tools = default,
global::System.Collections.Generic.IList<global::Cohere.OneOf<string, global::Cohere.Document>>? documents = default,
global::Cohere.Chatv2RequestCitationMode? citationMode = default,
global::Cohere.CitationOptions? citationOptions = default,
Copy link

Choose a reason for hiding this comment

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

Adding citationOptions to the interface method may cause breaking changes

Introducing a new parameter to the Chatv2Async method in the ICohereApi interface can break existing implementations. All implementers of this interface will need to update their method signatures accordingly.

Consider providing an overload of the method or introducing a new method to maintain backward compatibility.

global::Cohere.ResponseFormat2? responseFormat = default,
global::Cohere.Chatv2RequestSafetyMode? safetyMode = default,
int? maxTokens = default,
Expand Down
8 changes: 3 additions & 5 deletions src/libs/Cohere/Generated/Cohere.Models.Chatv2Request.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public sealed partial class Chatv2Request
public global::System.Collections.Generic.IList<global::Cohere.OneOf<string, global::Cohere.Document>>? Documents { get; set; }

/// <summary>
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
/// Options for controlling citation generation.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("citation_mode")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::Cohere.JsonConverters.Chatv2RequestCitationModeJsonConverter))]
public global::Cohere.Chatv2RequestCitationMode? CitationMode { get; set; }
[global::System.Text.Json.Serialization.JsonPropertyName("citation_options")]
public global::Cohere.CitationOptions? CitationOptions { get; set; }
Comment on lines +42 to +45
Copy link

Choose a reason for hiding this comment

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

Potential breaking change due to renaming CitationMode to CitationOptions

Renaming the property from CitationMode to CitationOptions may break existing clients that rely on CitationMode. To maintain backward compatibility, consider marking CitationMode as obsolete rather than removing it immediately.

Apply this diff to reintroduce the obsolete CitationMode property:

+     /// <summary>
+     /// Deprecated. Use CitationOptions instead.
+     /// </summary>
+     [Obsolete("Use CitationOptions instead.")]
+     [global::System.Text.Json.Serialization.JsonPropertyName("citation_mode")]
+     public global::Cohere.Chatv2RequestCitationMode? CitationMode { get; set; }
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
/// Options for controlling citation generation.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("citation_mode")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::Cohere.JsonConverters.Chatv2RequestCitationModeJsonConverter))]
public global::Cohere.Chatv2RequestCitationMode? CitationMode { get; set; }
[global::System.Text.Json.Serialization.JsonPropertyName("citation_options")]
public global::Cohere.CitationOptions? CitationOptions { get; set; }
/// <summary>
/// Options for controlling citation generation.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("citation_options")]
public global::Cohere.CitationOptions? CitationOptions { get; set; }
/// <summary>
/// Deprecated. Use CitationOptions instead.
/// </summary>
[Obsolete("Use CitationOptions instead.")]
[global::System.Text.Json.Serialization.JsonPropertyName("citation_mode")]
public global::Cohere.Chatv2RequestCitationMode? CitationMode { get; set; }


/// <summary>
/// Configuration for forcing the model output to adhere to the specified format. Supported on [Command R](https://docs.cohere.com/docs/command-r), [Command R+](https://docs.cohere.com/docs/command-r-plus) and newer models.<br/>
Expand Down
25 changes: 25 additions & 0 deletions src/libs/Cohere/Generated/Cohere.Models.CitationOptions.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#nullable enable

namespace Cohere
{
/// <summary>
/// Options for controlling citation generation.
/// </summary>
public sealed partial class CitationOptions
{
/// <summary>
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

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

Replace <br/> with <para> in XML documentation comments

The use of <br/> in XML documentation comments is not standard. To properly format line breaks or separate paragraphs, consider using the <para> element instead. This ensures compatibility with XML documentation tools and proper rendering of the comments.

Apply this diff to correct the documentation:

 /// <summary>
-/// Defaults to `"accurate"`.<br/>
-/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
+/// Defaults to `"accurate"`.
+/// <para>
+/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results, or no results.
+/// </para>
 /// </summary>
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
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
/// <summary>
/// Defaults to `"accurate"`.
/// <para>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results, or no results.
/// </para>
/// </summary>

/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("mode")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::Cohere.JsonConverters.CitationOptionsModeJsonConverter))]
public global::Cohere.CitationOptionsMode? Mode { get; set; }
Copy link

Choose a reason for hiding this comment

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

Initialize the Mode property to its default value

The XML comment indicates that Mode defaults to "accurate", but currently, it's nullable and not assigned a default value. This could lead to null values and unexpected behavior if Mode is not explicitly set elsewhere. Consider initializing Mode to CitationOptionsMode.Accurate to ensure it aligns with the documented default.

Apply this diff to set the default value:

 public global::Cohere.CitationOptionsMode? Mode { get; set; }
+    = global::Cohere.CitationOptionsMode.Accurate;
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 global::Cohere.CitationOptionsMode? Mode { get; set; }
public global::Cohere.CitationOptionsMode? Mode { get; set; }
= global::Cohere.CitationOptionsMode.Accurate;


/// <summary>
/// Additional properties that are not explicitly defined in the schema
/// </summary>
[global::System.Text.Json.Serialization.JsonExtensionData]
public global::System.Collections.Generic.IDictionary<string, object> AdditionalProperties { get; set; } = new global::System.Collections.Generic.Dictionary<string, object>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Cohere
/// Defaults to `"accurate"`.<br/>
/// Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
/// </summary>
public enum Chatv2RequestCitationMode
public enum CitationOptionsMode
{
/// <summary>
///
Expand All @@ -26,31 +26,31 @@ public enum Chatv2RequestCitationMode
/// <summary>
/// Enum extensions to do fast conversions without the reflection.
/// </summary>
public static class Chatv2RequestCitationModeExtensions
public static class CitationOptionsModeExtensions
{
/// <summary>
/// Converts an enum to a string.
/// </summary>
public static string ToValueString(this Chatv2RequestCitationMode value)
public static string ToValueString(this CitationOptionsMode value)
{
return value switch
{
Chatv2RequestCitationMode.FAST => "FAST",
Chatv2RequestCitationMode.ACCURATE => "ACCURATE",
Chatv2RequestCitationMode.OFF => "OFF",
CitationOptionsMode.FAST => "FAST",
CitationOptionsMode.ACCURATE => "ACCURATE",
CitationOptionsMode.OFF => "OFF",
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, null),
};
}
/// <summary>
/// Converts an string to a enum.
/// </summary>
public static Chatv2RequestCitationMode? ToEnum(string value)
public static CitationOptionsMode? ToEnum(string value)
{
return value switch
{
"FAST" => Chatv2RequestCitationMode.FAST,
"ACCURATE" => Chatv2RequestCitationMode.ACCURATE,
"OFF" => Chatv2RequestCitationMode.OFF,
"FAST" => CitationOptionsMode.FAST,
"ACCURATE" => CitationOptionsMode.ACCURATE,
"OFF" => CitationOptionsMode.OFF,
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Cohere.JsonConverters
{
/// <inheritdoc />
public sealed class Chatv2RequestCitationModeJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Cohere.Chatv2RequestCitationMode>
public sealed class CitationOptionsModeJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Cohere.CitationOptionsMode>
{
/// <inheritdoc />
public override global::Cohere.Chatv2RequestCitationMode Read(
public override global::Cohere.CitationOptionsMode Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand All @@ -18,15 +18,15 @@ public sealed class Chatv2RequestCitationModeJsonConverter : global::System.Text
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::Cohere.Chatv2RequestCitationModeExtensions.ToEnum(stringValue) ?? default;
return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue) ?? default;
Copy link

Choose a reason for hiding this comment

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

Handle invalid string values during deserialization

Returning default when ToEnum(stringValue) is null may lead to silent failures or unexpected default values if the input string does not match any enum member.

Consider throwing a JsonException to explicitly indicate an invalid value:

 return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue)
-        ?? default;
+        ?? throw new global::System.Text.Json.JsonException($"Invalid value '{stringValue}' for CitationOptionsMode.");
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
return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue) ?? default;
return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue)
?? throw new global::System.Text.Json.JsonException($"Invalid value '{stringValue}' for CitationOptionsMode.");

}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::Cohere.Chatv2RequestCitationMode)numValue;
return (global::Cohere.CitationOptionsMode)numValue;
Copy link

Choose a reason for hiding this comment

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

Validate numeric values before casting to enum

Casting numValue directly to CitationOptionsMode without validation might result in invalid enum values if numValue doesn't correspond to a defined member.

Consider adding a validation check:

+ if (!System.Enum.IsDefined(typeof(global::Cohere.CitationOptionsMode), numValue))
+ {
+     throw new global::System.Text.Json.JsonException($"Invalid numeric value '{numValue}' for CitationOptionsMode.");
+ }
  return (global::Cohere.CitationOptionsMode)numValue;
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
return (global::Cohere.CitationOptionsMode)numValue;
if (!System.Enum.IsDefined(typeof(global::Cohere.CitationOptionsMode), numValue))
{
throw new global::System.Text.Json.JsonException($"Invalid numeric value '{numValue}' for CitationOptionsMode.");
}
return (global::Cohere.CitationOptionsMode)numValue;

}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
Expand All @@ -38,12 +38,12 @@ public sealed class Chatv2RequestCitationModeJsonConverter : global::System.Text
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Cohere.Chatv2RequestCitationMode value,
global::Cohere.CitationOptionsMode value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

writer.WriteStringValue(global::Cohere.Chatv2RequestCitationModeExtensions.ToValueString(value));
writer.WriteStringValue(global::Cohere.CitationOptionsModeExtensions.ToValueString(value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Cohere.JsonConverters
{
/// <inheritdoc />
public sealed class Chatv2RequestCitationModeNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Cohere.Chatv2RequestCitationMode?>
public sealed class CitationOptionsModeNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Cohere.CitationOptionsMode?>
{
/// <inheritdoc />
public override global::Cohere.Chatv2RequestCitationMode? Read(
public override global::Cohere.CitationOptionsMode? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand All @@ -18,15 +18,15 @@ public sealed class Chatv2RequestCitationModeNullableJsonConverter : global::Sys
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::Cohere.Chatv2RequestCitationModeExtensions.ToEnum(stringValue);
return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue);
Copy link

Choose a reason for hiding this comment

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

Ensure robust handling of unrecognized string values

When converting stringValue to CitationOptionsMode using ToEnum(stringValue), consider handling invalid or unrecognized strings to prevent exceptions or unintended behavior.

Apply this diff to enhance error handling:

 return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue);
+// Handle unrecognized string values
+if (!Enum.TryParse<global::Cohere.CitationOptionsMode>(stringValue, out var mode))
+{
+    throw new ArgumentException($"Invalid CitationOptionsMode value: {stringValue}");
+}
+return mode;

Committable suggestion was skipped due to low confidence.

}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::Cohere.Chatv2RequestCitationMode)numValue;
return (global::Cohere.CitationOptionsMode)numValue;
Copy link

Choose a reason for hiding this comment

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

Validate integer value before casting to enum

Casting numValue directly to CitationOptionsMode without validation may result in invalid enum values. Consider validating numValue to ensure it corresponds to a defined CitationOptionsMode.

Apply this diff to add validation:

 return (global::Cohere.CitationOptionsMode)numValue;
+// Validate that numValue corresponds to a defined enum value
+if (!Enum.IsDefined(typeof(global::Cohere.CitationOptionsMode), numValue))
+{
+    throw new ArgumentException($"Invalid CitationOptionsMode value: {numValue}");
+}
+return (global::Cohere.CitationOptionsMode)numValue;

Committable suggestion was skipped due to low confidence.

}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
Expand All @@ -38,7 +38,7 @@ public sealed class Chatv2RequestCitationModeNullableJsonConverter : global::Sys
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Cohere.Chatv2RequestCitationMode? value,
global::Cohere.CitationOptionsMode? value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));
Expand All @@ -49,7 +49,7 @@ public override void Write(
}
else
{
writer.WriteStringValue(global::Cohere.Chatv2RequestCitationModeExtensions.ToValueString(value.Value));
writer.WriteStringValue(global::Cohere.CitationOptionsModeExtensions.ToValueString(value.Value));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Cohere/Generated/JsonSerializerContext.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Cohere
typeof(global::Cohere.JsonConverters.ToolMessage2RoleNullableJsonConverter),
typeof(global::Cohere.JsonConverters.Tool2TypeJsonConverter),
typeof(global::Cohere.JsonConverters.Tool2TypeNullableJsonConverter),
typeof(global::Cohere.JsonConverters.CitationOptionsModeJsonConverter),
typeof(global::Cohere.JsonConverters.CitationOptionsModeNullableJsonConverter),
typeof(global::Cohere.JsonConverters.ResponseFormatType2JsonConverter),
typeof(global::Cohere.JsonConverters.ResponseFormatType2NullableJsonConverter),
typeof(global::Cohere.JsonConverters.ChatFinishReasonJsonConverter),
Expand Down Expand Up @@ -95,8 +97,6 @@ namespace Cohere
typeof(global::Cohere.JsonConverters.ChatRequestCitationQualityNullableJsonConverter),
typeof(global::Cohere.JsonConverters.ChatRequestSafetyModeJsonConverter),
typeof(global::Cohere.JsonConverters.ChatRequestSafetyModeNullableJsonConverter),
typeof(global::Cohere.JsonConverters.Chatv2RequestCitationModeJsonConverter),
typeof(global::Cohere.JsonConverters.Chatv2RequestCitationModeNullableJsonConverter),
typeof(global::Cohere.JsonConverters.Chatv2RequestSafetyModeJsonConverter),
typeof(global::Cohere.JsonConverters.Chatv2RequestSafetyModeNullableJsonConverter),
typeof(global::Cohere.JsonConverters.GenerateRequestTruncateJsonConverter),
Expand Down
Loading
Loading