I want to bind the configuration from a JSON file to an instance of HttpApiClientOptions
. However, I encountered some issues when binding the Body
property. After defining it as Dictionary<string, object>?
, the Body
property correctly contains the model
and temperature
key-value pairs, but the value of messages
is empty, and I'm unable to retrieve {"role":"user", "content":"hello!"}
from it.
C# Code:
var config = new ConfigurationBuilder()
.AddJsonFile(configFilePath, false, false)
.Build();
//...
config.GetSection("apiClientOptions").GetSection(apiClientName).Bind(apiClientOptions);
HttpApiClientOptions Class:
public class HttpApiClientOptions : BaseApiClientOptions
{
public string Url { get; set; }
public Dictionary<string, string>? Headers { get; set; }
public Dictionary<string, string>? Params { get; set; }
public Dictionary<string, object>? Body { get; set; }
}
JSON Configuration:
{
"url": "https://example.com",
"headers": {
"Authorization": "Bearer sk-"
},
"params": {},
"body": {"model": "gpt-3.5-turbo", "messages": [{"role":"user", "content":"hello!"}], "temperature": 0.7}
}
I attempted to define Body
as JObject
or JsonElement
, but this approach also failed to retrieve values from the first level of nesting.
The reason I did not map the entire Body
node to a specific type is that the contents and nesting depth under the Body
node are uncertain. Is there any way to make the program read all the information inside the body
node from the JSON file during configuration?