-
-
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 CohereApi.Chat with safetyMode parameter and type changes #28
Conversation
WalkthroughThe changes involve updates to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ChatAPI
participant ChatRequest
participant JSONConverter
User->>ChatAPI: Call ChatAsync(safetyMode)
ChatAPI->>ChatRequest: Create request with safetyMode
ChatRequest->>JSONConverter: Serialize request
JSONConverter-->>ChatAPI: Return serialized request
ChatAPI-->>User: Return chat response
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 6
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (10)
- src/libs/Cohere/Generated/Cohere.CohereApi.Chat.g.cs (5 hunks)
- src/libs/Cohere/Generated/Cohere.Models.ChatRequest.g.cs (4 hunks)
- src/libs/Cohere/Generated/Cohere.Models.ChatRequestPromptTruncation.g.cs (1 hunks)
- src/libs/Cohere/Generated/Cohere.Models.ChatRequestSafetyMode.g.cs (1 hunks)
- src/libs/Cohere/Generated/Cohere.Models.ResponseFormat.g.cs (1 hunks)
- src/libs/Cohere/Generated/JsonConverters.ChatRequestSafetyMode.g.cs (1 hunks)
- src/libs/Cohere/Generated/JsonConverters.ChatRequestSafetyModeNullable.g.cs (1 hunks)
- src/libs/Cohere/Generated/JsonSerializerContext.g.cs (1 hunks)
- src/libs/Cohere/Generated/JsonSerializerContextTypes.g.cs (1 hunks)
- src/libs/Cohere/openapi.yaml (8 hunks)
Files skipped from review due to trivial changes (2)
- src/libs/Cohere/Generated/Cohere.Models.ChatRequestPromptTruncation.g.cs
- src/libs/Cohere/Generated/Cohere.Models.ResponseFormat.g.cs
Additional comments not posted (35)
src/libs/Cohere/Generated/JsonSerializerContext.g.cs (2)
94-94
: LGTM!The addition of
ChatRequestSafetyModeJsonConverter
is straightforward and follows the existing pattern.
95-95
: LGTM!The addition of
ChatRequestSafetyModeNullableJsonConverter
is straightforward and follows the existing pattern.src/libs/Cohere/Generated/Cohere.Models.ChatRequest.g.cs (5)
20-20
: LGTM!The update to the
Model
property documentation to reflect the new default valuecommand-r-plus-08-2024
is straightforward.
67-69
: LGTM!The reformatting of the
prompt_truncation
property documentation improves readability and understanding of the compatibility details.
235-235
: LGTM!The update to the
ForceSingleStep
property documentation enhances clarity by specifying the supported models and their release dates.
245-254
: LGTM!The addition of the
SafetyMode
property is well-documented and follows the existing pattern.
Line range hint
333-333
: LGTM!The addition of the
SafetyMode
property to the class constructor is consistent with the rest of the properties.src/libs/Cohere/Generated/Cohere.CohereApi.Chat.g.cs (5)
116-116
: LGTM!The update to the
model
parameter documentation to reflect the new default valuecommand-r-plus-08-2024
is straightforward.
148-150
: LGTM!The reformatting of the
promptTruncation
parameter documentation improves readability and understanding of the compatibility details.
269-275
: LGTM!The addition of the
safetyMode
parameter is well-documented and follows the existing pattern.
304-304
: LGTM!The addition of the
safetyMode
parameter to the method signature is consistent with the rest of the parameters.
333-333
: LGTM!The addition of the
SafetyMode
property to the class constructor is consistent with the rest of the properties.src/libs/Cohere/Generated/JsonSerializerContextTypes.g.cs (15)
1140-1141
: Verify the correctness and consistency of the type change forType281
.The type has been changed from
GenerateRequestTruncate?
toGenerateRequest?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type281
:
1168-1169
: Verify the correctness and consistency of the type change forType288
.The type has been changed from
ClassifyRequest?
toSystem.OneOf<string, Cohere.RerankDocument>?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type288
:
1152-1153
: Verify the correctness and consistency of the type change forType284
.The type has been changed from
EmbedRequestTruncate?
toEmbedRequest?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type284
:
1124-1125
: Verify the correctness and consistency of the type change forType277
.The type has been changed from
Chatv2Request?
toChatRequestSafetyMode?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type277
:
1136-1137
: Verify the correctness and consistency of the type change forType280
.The type has been changed from
GenerateRequest?
toChatv2RequestCitationMode?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type280
:Verification successful
Type change for
Type280
is isolated and consistent.The type change from
GenerateRequest?
toChatv2RequestCitationMode?
forType280
does not affect any other part of the codebase, as there are no other occurrences ofType280
. This change is isolated and should not cause any issues.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type280` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type280'Length of output: 603
Script:
#!/bin/bash # Comprehensive search for all occurrences of `Type280` in the codebase to verify its usage. rg --type cs 'Type280'Length of output: 159
1148-1149
: Verify the correctness and consistency of the type change forType283
.The type has been changed from
EmbedRequest?
toGenerateRequestReturnLikelihoods?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type283
:Verification successful
No references found for
Type283
in the codebase.The property
Type283
with the typeGenerateRequestReturnLikelihoods?
is not used elsewhere in the codebase, suggesting that the type change does not impact other parts of the code. This indicates no immediate issues with the change.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type283` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type283'Length of output: 607
Script:
#!/bin/bash # Description: Search for all occurrences of `Type283` in the codebase to verify its usage. # Test: Find all references to `Type283`. Expect: Consistent usage with the new type. rg --type cs 'Type283'Length of output: 166
1128-1129
: Verify the correctness and consistency of the type change forType278
.The type has been changed from
System.Collections.Generic.IList<Tool2>?
toChatv2Request?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type278
:Verification successful
No impact from the type change for
Type278
due to lack of usage.The property
Type278
is only declared in theJsonSerializerContextTypes.g.cs
file and is not used elsewhere in the codebase. This suggests that the type change does not currently affect other parts of the code.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type278` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type278'Length of output: 623
Script:
#!/bin/bash # Description: Search for all occurrences of `Type278` in the codebase to verify its usage. # Test: Find all instances of `Type278` to ensure the new type is used correctly. rg --type cs 'Type278'Length of output: 147
1132-1133
: Verify the correctness and consistency of the type change forType279
.The type has been changed from
Chatv2RequestCitationMode?
toSystem.Collections.Generic.IList<Tool2>?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type279
:Verification successful
The type change for
Type279
is consistent and appropriate.The change to
System.Collections.Generic.IList<global::Cohere.Tool2>?
aligns with other usages ofTool2
in the codebase, indicating that the modification is consistent with the overall data model. No further issues were found regarding this change.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type279` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type279'Length of output: 635
Script:
#!/bin/bash # Description: Search for the definition and usage of `Tool2` to understand its role in the codebase. # Test: Find the definition of `Tool2` and its usage. rg --type cs 'class Tool2' -A 5 rg --type cs 'Tool2' -A 5Length of output: 20808
1156-1157
: Verify the correctness and consistency of the type change forType285
.The type has been changed from
RerankRequest?
toEmbedRequestTruncate?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type285
:Verification successful
No references found for
Type285
in the codebase.The change from
RerankRequest?
toEmbedRequestTruncate?
does not impact other parts of the codebase asType285
is not referenced elsewhere. Ensure that this property is meant to be unused or verify if it should be utilized in other parts of the code.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type285` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type285'Length of output: 596
Script:
#!/bin/bash # Description: Search for references to `Type285` in the codebase to verify its usage. # Test: Find all references to `Type285` to ensure consistent usage of the new type. rg --type cs 'Type285'Length of output: 154
1164-1165
: Verify the correctness and consistency of the type change forType287
.The type has been changed from
System.OneOf<string, Cohere.RerankDocument>?
toSystem.Collections.Generic.IList<System.OneOf<string, Cohere.RerankDocument>>?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type287
:Verification successful
No issues found with the type change for
Type287
.The property
Type287
is not referenced elsewhere in the codebase, suggesting it may be newly introduced or not yet integrated. The change to a list type does not impact existing functionality. Ensure that any future usage aligns with the new type.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type287` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type287'Length of output: 693
Script:
#!/bin/bash # Description: Search for references to `Type287` in the codebase to verify its usage. # Test: Find all references to `Type287` to ensure the change to a list type is handled correctly. rg --type cs 'Type287'Length of output: 220
1180-1181
: Verify the correctness and consistency of the type change forType291
.The type has been changed from
CreateDatasetRequest?
toClassifyRequestTruncate?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type291
:Verification successful
Type change for
Type291
is isolated and consistent.The type change from
CreateDatasetRequest?
toClassifyRequestTruncate?
forType291
is isolated to its definition and does not affect other parts of the codebase, as there are no other occurrences ofType291
. This change is consistent and does not require further updates.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type291` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type291'Length of output: 606
Script:
#!/bin/bash # Description: Search for all occurrences of `Type291` in the codebase to verify its usage. # Test: Find all usages of `Type291` to ensure consistency with the new type. rg --type cs 'Type291'Length of output: 157
1176-1177
: Verify the correctness and consistency of the type change forType290
.The type has been changed from
ClassifyRequestTruncate?
toSystem.Collections.Generic.IList<ClassifyExample>?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type290
:
1144-1145
: Verify the correctness and consistency of the type change forType282
.The type has been changed from
GenerateRequestReturnLikelihoods?
toGenerateRequestTruncate?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type282
:
1160-1161
: Verify the correctness and consistency of the type change forType286
.The type has been changed from
System.Collections.Generic.IList<System.OneOf<string, Cohere.RerankDocument>>?
toRerankRequest?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type286
:Verification successful
The type change for
Type286
is consistent and appropriate.The change from
System.Collections.Generic.IList<System.OneOf<string, Cohere.RerankDocument>>?
toRerankRequest?
aligns with the usage ofRerankRequest
in the API, indicating that it is a suitable and consistent update. The absence of additional references toType286
suggests it is used internally or in a generated context.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type286` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type286'Length of output: 662
Script:
#!/bin/bash # Search for the usage of `RerankRequest` to understand its role in the codebase. rg --type cs -A 5 $'RerankRequest' # Search for any references to `Type286` in other parts of the codebase to confirm its usage and consistency. rg --type cs -A 5 $'Type286'Length of output: 5644
1172-1173
: Verify the correctness and consistency of the type change forType289
.The type has been changed from
System.Collections.Generic.IList<ClassifyExample>?
toClassifyRequest?
. Ensure that this change is appropriate and consistent with the rest of the codebase.Run the following script to verify the usage of
Type289
:Verification successful
The type change for
Type289
is consistent and appropriate.The change from
System.Collections.Generic.IList<ClassifyExample>?
toClassifyRequest?
aligns with the usage ofClassifyRequest
in the codebase, particularly in API-related operations. This refactoring likely aims to encapsulate the request data more effectively. No inconsistencies or issues were found related to this change.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `Type289` in the codebase. # Test: Search for the property usage. Expect: Consistent usage of the new type. rg --type cs -A 5 $'Type289'Length of output: 635
Script:
#!/bin/bash # Description: Search for references to `ClassifyRequest` to gather more context on its usage. # Test: Search for references to `ClassifyRequest`. rg --type cs 'ClassifyRequest'Length of output: 5465
src/libs/Cohere/openapi.yaml (8)
35-63
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
72-92
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
101-128
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
137-137
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
146-165
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
174-182
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
192-200
: LGTM!The addition of the
safety_mode
parameter is a significant enhancement, allowing users to select from various safety instructions. The description provides clear guidance on its usage.
6939-6939
: LGTM!The changes improve the readability and clarity of the descriptions, which is beneficial for developers using the API.
public override void Write( | ||
global::System.Text.Json.Utf8JsonWriter writer, | ||
global::Cohere.ChatRequestSafetyMode value, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer)); | ||
|
||
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(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.
Move null check to method parameter validation.
The null check for the writer can be moved to the method parameter validation for better readability.
Apply this diff to move the null check to the method parameter validation:
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Cohere.ChatRequestSafetyMode value,
global::System.Text.Json.JsonSerializerOptions options)
{
if (writer == null) throw new global::System.ArgumentNullException(nameof(writer));
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value));
}
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.
public override void Write( | |
global::System.Text.Json.Utf8JsonWriter writer, | |
global::Cohere.ChatRequestSafetyMode value, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer)); | |
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value)); | |
} | |
public override void Write( | |
global::System.Text.Json.Utf8JsonWriter writer, | |
global::Cohere.ChatRequestSafetyMode value, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
if (writer == null) throw new global::System.ArgumentNullException(nameof(writer)); | |
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value)); | |
} |
public override global::Cohere.ChatRequestSafetyMode Read( | ||
ref global::System.Text.Json.Utf8JsonReader reader, | ||
global::System.Type typeToConvert, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case global::System.Text.Json.JsonTokenType.String: | ||
{ | ||
var stringValue = reader.GetString(); | ||
if (stringValue != null) | ||
{ | ||
return global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(stringValue) ?? default; | ||
} | ||
|
||
break; | ||
} | ||
case global::System.Text.Json.JsonTokenType.Number: | ||
{ | ||
var numValue = reader.GetInt32(); | ||
return (global::Cohere.ChatRequestSafetyMode)numValue; | ||
} | ||
default: | ||
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | ||
} | ||
|
||
return default; |
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.
Improve readability and exception handling.
The switch
statement can be replaced with a more concise pattern matching. The default
case should throw a JsonException
instead of ArgumentOutOfRangeException
for better context.
Apply this diff to improve readability and exception handling:
public override global::Cohere.ChatRequestSafetyMode Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
return reader.TokenType switch
{
global::System.Text.Json.JsonTokenType.String => global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(reader.GetString()) ?? default,
global::System.Text.Json.JsonTokenType.Number => (global::Cohere.ChatRequestSafetyMode)reader.GetInt32(),
_ => throw new global::System.Text.Json.JsonException($"Unexpected token parsing ChatRequestSafetyMode. Expected String or Number, got {reader.TokenType}."),
};
}
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.
public override global::Cohere.ChatRequestSafetyMode Read( | |
ref global::System.Text.Json.Utf8JsonReader reader, | |
global::System.Type typeToConvert, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
switch (reader.TokenType) | |
{ | |
case global::System.Text.Json.JsonTokenType.String: | |
{ | |
var stringValue = reader.GetString(); | |
if (stringValue != null) | |
{ | |
return global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(stringValue) ?? default; | |
} | |
break; | |
} | |
case global::System.Text.Json.JsonTokenType.Number: | |
{ | |
var numValue = reader.GetInt32(); | |
return (global::Cohere.ChatRequestSafetyMode)numValue; | |
} | |
default: | |
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | |
} | |
return default; | |
public override global::Cohere.ChatRequestSafetyMode Read( | |
ref global::System.Text.Json.Utf8JsonReader reader, | |
global::System.Type typeToConvert, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
return reader.TokenType switch | |
{ | |
global::System.Text.Json.JsonTokenType.String => global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(reader.GetString()) ?? default, | |
global::System.Text.Json.JsonTokenType.Number => (global::Cohere.ChatRequestSafetyMode)reader.GetInt32(), | |
_ => throw new global::System.Text.Json.JsonException($"Unexpected token parsing ChatRequestSafetyMode. Expected String or Number, got {reader.TokenType}."), | |
}; | |
} |
public override void Write( | ||
global::System.Text.Json.Utf8JsonWriter writer, | ||
global::Cohere.ChatRequestSafetyMode? value, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer)); | ||
|
||
if (value == null) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else | ||
{ | ||
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.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.
Move null check to method parameter validation.
The null check for the writer can be moved to the method parameter validation for better readability.
Apply this diff to move the null check to the method parameter validation:
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Cohere.ChatRequestSafetyMode? value,
global::System.Text.Json.JsonSerializerOptions options)
{
if (writer == null) throw new global::System.ArgumentNullException(nameof(writer));
if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value.Value));
}
}
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.
public override void Write( | |
global::System.Text.Json.Utf8JsonWriter writer, | |
global::Cohere.ChatRequestSafetyMode? value, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer)); | |
if (value == null) | |
{ | |
writer.WriteNullValue(); | |
} | |
else | |
{ | |
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value.Value)); | |
} | |
} | |
public override void Write( | |
global::System.Text.Json.Utf8JsonWriter writer, | |
global::Cohere.ChatRequestSafetyMode? value, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
if (writer == null) throw new global::System.ArgumentNullException(nameof(writer)); | |
if (value == null) | |
{ | |
writer.WriteNullValue(); | |
} | |
else | |
{ | |
writer.WriteStringValue(global::Cohere.ChatRequestSafetyModeExtensions.ToValueString(value.Value)); | |
} | |
} |
public override global::Cohere.ChatRequestSafetyMode? Read( | ||
ref global::System.Text.Json.Utf8JsonReader reader, | ||
global::System.Type typeToConvert, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case global::System.Text.Json.JsonTokenType.String: | ||
{ | ||
var stringValue = reader.GetString(); | ||
if (stringValue != null) | ||
{ | ||
return global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(stringValue); | ||
} | ||
|
||
break; | ||
} | ||
case global::System.Text.Json.JsonTokenType.Number: | ||
{ | ||
var numValue = reader.GetInt32(); | ||
return (global::Cohere.ChatRequestSafetyMode)numValue; | ||
} | ||
default: | ||
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | ||
} | ||
|
||
return default; |
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.
Improve readability and exception handling.
The switch
statement can be replaced with a more concise pattern matching. The default
case should throw a JsonException
instead of ArgumentOutOfRangeException
for better context.
Apply this diff to improve readability and exception handling:
public override global::Cohere.ChatRequestSafetyMode? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
return reader.TokenType switch
{
global::System.Text.Json.JsonTokenType.String => global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(reader.GetString()),
global::System.Text.Json.JsonTokenType.Number => (global::Cohere.ChatRequestSafetyMode)reader.GetInt32(),
_ => throw new global::System.Text.Json.JsonException($"Unexpected token parsing ChatRequestSafetyMode. Expected String or Number, got {reader.TokenType}."),
};
}
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.
public override global::Cohere.ChatRequestSafetyMode? Read( | |
ref global::System.Text.Json.Utf8JsonReader reader, | |
global::System.Type typeToConvert, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
switch (reader.TokenType) | |
{ | |
case global::System.Text.Json.JsonTokenType.String: | |
{ | |
var stringValue = reader.GetString(); | |
if (stringValue != null) | |
{ | |
return global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(stringValue); | |
} | |
break; | |
} | |
case global::System.Text.Json.JsonTokenType.Number: | |
{ | |
var numValue = reader.GetInt32(); | |
return (global::Cohere.ChatRequestSafetyMode)numValue; | |
} | |
default: | |
throw new global::System.ArgumentOutOfRangeException(nameof(reader)); | |
} | |
return default; | |
public override global::Cohere.ChatRequestSafetyMode? Read( | |
ref global::System.Text.Json.Utf8JsonReader reader, | |
global::System.Type typeToConvert, | |
global::System.Text.Json.JsonSerializerOptions options) | |
{ | |
return reader.TokenType switch | |
{ | |
global::System.Text.Json.JsonTokenType.String => global::Cohere.ChatRequestSafetyModeExtensions.ToEnum(reader.GetString()), | |
global::System.Text.Json.JsonTokenType.Number => (global::Cohere.ChatRequestSafetyMode)reader.GetInt32(), | |
_ => throw new global::System.Text.Json.JsonException($"Unexpected token parsing ChatRequestSafetyMode. Expected String or Number, got {reader.TokenType}."), | |
}; | |
} |
public enum ChatRequestSafetyMode | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
CONTEXTUAL, | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
STRICT, | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
NONE, |
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.
Add meaningful descriptions to enum values.
The enum values have empty summaries. Add meaningful descriptions to improve documentation.
Apply this diff to add meaningful descriptions:
/// <summary>
/// Contextual safety mode.
/// </summary>
CONTEXTUAL,
/// <summary>
/// Strict safety mode.
/// </summary>
STRICT,
/// <summary>
/// No safety mode.
/// </summary>
NONE,
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.
public enum ChatRequestSafetyMode | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
CONTEXTUAL, | |
/// <summary> | |
/// | |
/// </summary> | |
STRICT, | |
/// <summary> | |
/// | |
/// </summary> | |
NONE, | |
public enum ChatRequestSafetyMode | |
{ | |
/// <summary> | |
/// Contextual safety mode. | |
/// </summary> | |
CONTEXTUAL, | |
/// <summary> | |
/// Strict safety mode. | |
/// </summary> | |
STRICT, | |
/// <summary> | |
/// No safety mode. | |
/// </summary> | |
NONE, |
public static class ChatRequestSafetyModeExtensions | ||
{ | ||
/// <summary> | ||
/// Converts an enum to a string. | ||
/// </summary> | ||
public static string ToValueString(this ChatRequestSafetyMode value) | ||
{ | ||
return value switch | ||
{ | ||
ChatRequestSafetyMode.CONTEXTUAL => "CONTEXTUAL", | ||
ChatRequestSafetyMode.STRICT => "STRICT", | ||
ChatRequestSafetyMode.NONE => "NONE", | ||
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, null), | ||
}; | ||
} | ||
/// <summary> | ||
/// Converts an string to a enum. | ||
/// </summary> | ||
public static ChatRequestSafetyMode? ToEnum(string value) | ||
{ | ||
return value switch | ||
{ | ||
"CONTEXTUAL" => ChatRequestSafetyMode.CONTEXTUAL, | ||
"STRICT" => ChatRequestSafetyMode.STRICT, | ||
"NONE" => ChatRequestSafetyMode.NONE, | ||
_ => null, | ||
}; | ||
} |
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.
Improve error handling in ToEnum
method.
The ToEnum
method should throw an exception for invalid values instead of returning null.
Apply this diff to improve error handling:
public static ChatRequestSafetyMode ToEnum(string value)
{
return value switch
{
"CONTEXTUAL" => ChatRequestSafetyMode.CONTEXTUAL,
"STRICT" => ChatRequestSafetyMode.STRICT,
"NONE" => ChatRequestSafetyMode.NONE,
_ => throw new global::System.ArgumentException($"Invalid value for ChatRequestSafetyMode: {value}"),
};
}
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.
public static class ChatRequestSafetyModeExtensions | |
{ | |
/// <summary> | |
/// Converts an enum to a string. | |
/// </summary> | |
public static string ToValueString(this ChatRequestSafetyMode value) | |
{ | |
return value switch | |
{ | |
ChatRequestSafetyMode.CONTEXTUAL => "CONTEXTUAL", | |
ChatRequestSafetyMode.STRICT => "STRICT", | |
ChatRequestSafetyMode.NONE => "NONE", | |
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, null), | |
}; | |
} | |
/// <summary> | |
/// Converts an string to a enum. | |
/// </summary> | |
public static ChatRequestSafetyMode? ToEnum(string value) | |
{ | |
return value switch | |
{ | |
"CONTEXTUAL" => ChatRequestSafetyMode.CONTEXTUAL, | |
"STRICT" => ChatRequestSafetyMode.STRICT, | |
"NONE" => ChatRequestSafetyMode.NONE, | |
_ => null, | |
}; | |
} | |
public static class ChatRequestSafetyModeExtensions | |
{ | |
/// <summary> | |
/// Converts an enum to a string. | |
/// </summary> | |
public static string ToValueString(this ChatRequestSafetyMode value) | |
{ | |
return value switch | |
{ | |
ChatRequestSafetyMode.CONTEXTUAL => "CONTEXTUAL", | |
ChatRequestSafetyMode.STRICT => "STRICT", | |
ChatRequestSafetyMode.NONE => "NONE", | |
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, null), | |
}; | |
} | |
/// <summary> | |
/// Converts an string to a enum. | |
/// </summary> | |
public static ChatRequestSafetyMode ToEnum(string value) | |
{ | |
return value switch | |
{ | |
"CONTEXTUAL" => ChatRequestSafetyMode.CONTEXTUAL, | |
"STRICT" => ChatRequestSafetyMode.STRICT, | |
"NONE" => ChatRequestSafetyMode.NONE, | |
_ => throw new global::System.ArgumentException($"Invalid value for ChatRequestSafetyMode: {value}"), | |
}; | |
} |
Summary by CodeRabbit
New Features
safetyMode
parameter in theChatAsync
method for enhanced control over safety instructions.SafetyMode
property in theChatRequest
class to manage safety instructions in prompts.ChatRequestSafetyMode
and nullable types.Documentation