-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a converter similar to the Open AI one
- Loading branch information
Showing
4 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
LLama.SemanticKernel/ChatCompletion/ChatRequestSettingsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace LLamaSharp.SemanticKernel.ChatCompletion; | ||
|
||
/// <summary> | ||
/// JSON converter for <see cref="OpenAIRequestSettings"/> | ||
/// </summary> | ||
public class ChatRequestSettingsConverter : JsonConverter<ChatRequestSettings> | ||
{ | ||
/// <inheritdoc/> | ||
public override ChatRequestSettings? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var requestSettings = new ChatRequestSettings(); | ||
|
||
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) | ||
{ | ||
if (reader.TokenType == JsonTokenType.PropertyName) | ||
{ | ||
string? propertyName = reader.GetString(); | ||
|
||
if (propertyName is not null) | ||
{ | ||
// normalise property name to uppercase | ||
propertyName = propertyName.ToUpperInvariant(); | ||
} | ||
|
||
reader.Read(); | ||
|
||
switch (propertyName) | ||
{ | ||
case "TEMPERATURE": | ||
requestSettings.Temperature = reader.GetDouble(); | ||
break; | ||
case "TOPP": | ||
case "TOP_P": | ||
requestSettings.TopP = reader.GetDouble(); | ||
break; | ||
case "FREQUENCYPENALTY": | ||
case "FREQUENCY_PENALTY": | ||
requestSettings.FrequencyPenalty = reader.GetDouble(); | ||
break; | ||
case "PRESENCEPENALTY": | ||
case "PRESENCE_PENALTY": | ||
requestSettings.PresencePenalty = reader.GetDouble(); | ||
break; | ||
case "MAXTOKENS": | ||
case "MAX_TOKENS": | ||
requestSettings.MaxTokens = reader.GetInt32(); | ||
break; | ||
case "STOPSEQUENCES": | ||
case "STOP_SEQUENCES": | ||
requestSettings.StopSequences = JsonSerializer.Deserialize<IList<string>>(ref reader, options) ?? Array.Empty<string>(); | ||
break; | ||
case "RESULTSPERPROMPT": | ||
case "RESULTS_PER_PROMPT": | ||
requestSettings.ResultsPerPrompt = reader.GetInt32(); | ||
break; | ||
case "TOKENSELECTIONBIASES": | ||
case "TOKEN_SELECTION_BIASES": | ||
requestSettings.TokenSelectionBiases = JsonSerializer.Deserialize<IDictionary<int, int>>(ref reader, options) ?? new Dictionary<int, int>(); | ||
break; | ||
case "SERVICEID": | ||
case "SERVICE_ID": | ||
requestSettings.ServiceId = reader.GetString(); | ||
break; | ||
default: | ||
reader.Skip(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return requestSettings; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, ChatRequestSettings value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
writer.WriteNumber("temperature", value.Temperature); | ||
writer.WriteNumber("top_p", value.TopP); | ||
writer.WriteNumber("frequency_penalty", value.FrequencyPenalty); | ||
writer.WriteNumber("presence_penalty", value.PresencePenalty); | ||
if (value.MaxTokens is null) | ||
{ | ||
writer.WriteNull("max_tokens"); | ||
} | ||
else | ||
{ | ||
writer.WriteNumber("max_tokens", (decimal)value.MaxTokens); | ||
} | ||
writer.WritePropertyName("stop_sequences"); | ||
JsonSerializer.Serialize(writer, value.StopSequences, options); | ||
writer.WriteNumber("results_per_prompt", value.ResultsPerPrompt); | ||
writer.WritePropertyName("token_selection_biases"); | ||
JsonSerializer.Serialize(writer, value.TokenSelectionBiases, options); | ||
writer.WriteString("service_id", value.ServiceId); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters