Providers: Added ability to get typed object from ChatResponse
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 1.1k
- Forks
- 141
- Avg merge
- 2d 18m
- Merged PRs (30d)
- 2
Description
public async Task<T> Ask<T>(string prompt)
{
string jsonStructure = Utils.GenerateJsonStructure<T>();
string expandedPrompt = $"{prompt}\nPlease provide the response in the following json format: {jsonStructure}";
var response = await Ask(expandedPrompt);
//sometimes gemini add some weird stuff around the json
if (response.StartsWith("```json"))
{
response = response.Substring(7);
response = response.Substring(0, response.Length - 4);
}
try
{
return JsonSerializer.Deserialize<T>(response);
}
catch (Exception e)
{
throw new Exception("Failed to generate a valid response from the Gemini API.");
}
}
public static string GenerateJsonStructure<T>()
{
var properties = typeof(T).GetProperties();
var jsonStructure = new StringBuilder();
jsonStructure.AppendLine("{");
foreach (var property in properties)
{
jsonStructure.AppendLine($" \"{property.Name}\": \"{property.PropertyType.Name}\",");
}
jsonStructure.Remove(jsonStructure.Length - 3, 1); // Remove the trailing comma
jsonStructure.AppendLine("}");
return jsonStructure.ToString();
}
that version is pretty brittle
looks like this is using gemeni directly for the large context
but no longer is working at it refuses to return pure json without commentary
3:28
OpenAI can be asked to return the response in json format.
But in general, of course, I like your approach, I could implement this as an Extension to ChatResponse, which casts the message object to a strict type.
Something like ChatResponse.As()
But need to test this with different providers, some may ignore formatting/requests to output or output it without ```json
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating ChatResponse and the provider implementations, then review how response message content is currently exposed. Compare the proposed ChatResponse.As() approach with the provider-specific formatting behavior described in the issue, and verify the result across different providers, including responses with commentary or code fences.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- ai, api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100