You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicasyncTask<T>Ask<T>(stringprompt){stringjsonStructure=Utils.GenerateJsonStructure<T>();stringexpandedPrompt=$"{prompt}\nPlease provide the response in the following json format: {jsonStructure}";varresponse=awaitAsk(expandedPrompt);//sometimes gemini add some weird stuff around the jsonif(response.StartsWith("```json")){response=response.Substring(7);response=response.Substring(0,response.Length-4);}try{returnJsonSerializer.Deserialize<T>(response);}catch(Exceptione){thrownewException("Failed to generate a valid response from the Gemini API.");}}publicstaticstringGenerateJsonStructure<T>(){varproperties=typeof(T).GetProperties();varjsonStructure=newStringBuilder();jsonStructure.AppendLine("{");foreach(varpropertyinproperties){jsonStructure.AppendLine($" \"{property.Name}\": \"{property.PropertyType.Name}\",");}jsonStructure.Remove(jsonStructure.Length-3,1);// Remove the trailing commajsonStructure.AppendLine("}");returnjsonStructure.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
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: