Skip to content

Providers: Added ability to get typed object from ChatResponse #247

Open
@HavenDV

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions