tryAGI / LangChain

C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
https://tryagi.github.io/LangChain/
MIT License
507 stars 78 forks source link

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

Open HavenDV opened 4 months ago

HavenDV commented 4 months ago
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