-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential breaking change due to renaming Renaming the property from Apply this diff to reintroduce the obsolete + /// <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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <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/> | ||||||||||||||||||||||||||||||||||||||||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace The use of 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
Suggested change
|
||||||||||||||||||
/// </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; } | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initialize the The XML comment indicates that Apply this diff to set the default value: public global::Cohere.CitationOptionsMode? Mode { get; set; }
+ = global::Cohere.CitationOptionsMode.Accurate; Committable suggestion
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
/// <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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) | ||||||||||||||
|
@@ -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; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle invalid string values during deserialization Returning Consider throwing a return global::Cohere.CitationOptionsModeExtensions.ToEnum(stringValue)
- ?? default;
+ ?? throw new global::System.Text.Json.JsonException($"Invalid value '{stringValue}' for CitationOptionsMode."); Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
} | ||||||||||||||
case global::System.Text.Json.JsonTokenType.Number: | ||||||||||||||
{ | ||||||||||||||
var numValue = reader.GetInt32(); | ||||||||||||||
return (global::Cohere.Chatv2RequestCitationMode)numValue; | ||||||||||||||
return (global::Cohere.CitationOptionsMode)numValue; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate numeric values before casting to enum Casting 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
Suggested change
|
||||||||||||||
} | ||||||||||||||
default: | ||||||||||||||
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | ||||||||||||||
|
@@ -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 |
---|---|---|
|
@@ -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) | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure robust handling of unrecognized string values When converting 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;
|
||
} | ||
|
||
break; | ||
} | ||
case global::System.Text.Json.JsonTokenType.Number: | ||
{ | ||
var numValue = reader.GetInt32(); | ||
return (global::Cohere.Chatv2RequestCitationMode)numValue; | ||
return (global::Cohere.CitationOptionsMode)numValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate integer value before casting to enum Casting 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;
|
||
} | ||
default: | ||
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | ||
|
@@ -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)); | ||
|
@@ -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)); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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 changesIntroducing a new parameter to the
Chatv2Async
method in theICohereApi
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.