Skip to content

Commit

Permalink
[skip ci] add convenience method to response types
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Sep 19, 2024
1 parent 206e1d2 commit 5ab823d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Mscc.GenerativeAI/Types/Generative/GenerateAnswerResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Linq;
#endif
Expand Down Expand Up @@ -37,5 +38,14 @@ public string? Text
/// Output only. Feedback related to the input data used to answer the question, as opposed to model-generated response to the question.
/// </summary>
public PromptFeedback InputFeedback { get; set; }

/// <summary>
/// A convenience overload to easily access the responded text.
/// </summary>
/// <returns>The responded text information of first candidate.</returns>
public override string ToString()
{
return Text ?? String.Empty;
}
}
}
14 changes: 12 additions & 2 deletions src/Mscc.GenerativeAI/Types/Generative/GenerateContentResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
Expand All @@ -13,7 +14,7 @@ namespace Mscc.GenerativeAI
public class GenerateContentResponse
{
/// <summary>
/// Responded text information of first candidate.
/// A convenience property to get the responded text information of first candidate.
/// </summary>
[JsonIgnore]
public string? Text
Expand All @@ -26,7 +27,7 @@ FinishReason.Safety or
FinishReason.Recitation or
FinishReason.Other)
return string.Empty;
return Candidates?.FirstOrDefault()?.Content?.Parts?.FirstOrDefault()?.Text;
return Candidates?.FirstOrDefault()?.Content?.Parts.FirstOrDefault()?.Text;
}
}

Expand All @@ -44,5 +45,14 @@ FinishReason.Recitation or
/// Usage metadata about the response(s).
/// </summary>
public UsageMetadata? UsageMetadata { get; set; }

/// <summary>
/// A convenience overload to easily access the responded text.
/// </summary>
/// <returns>The responded text information of first candidate.</returns>
public override string ToString()
{
return Text ?? String.Empty;
}
}
}
10 changes: 10 additions & 0 deletions src/Mscc.GenerativeAI/Types/Generative/GenerateMessageResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Linq;
#endif
Expand Down Expand Up @@ -28,5 +29,14 @@ public class GenerateMessageResponse
/// This indicates which SafetyCategory(s) blocked a candidate from this response, the lowest HarmProbability that triggered a block, and the HarmThreshold setting for that category. This indicates the smallest change to the SafetySettings that would be necessary to unblock at least 1 response.
/// </summary>
public List<ContentFilter>? Filters { get; set; }

/// <summary>
/// A convenience overload to easily access the responded text.
/// </summary>
/// <returns>The responded text information of first candidate.</returns>
public override string ToString()
{
return Text ?? String.Empty;
}
}
}
10 changes: 10 additions & 0 deletions src/Mscc.GenerativeAI/Types/Generative/GenerateTextResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Linq;
#endif
Expand Down Expand Up @@ -31,5 +32,14 @@ public class GenerateTextResponse
/// Returns any safety feedback related to content filtering.
/// </summary>
public List<SafetyFeedback>? SafetyFeedback { get; set; }

/// <summary>
/// A convenience overload to easily access the responded text.
/// </summary>
/// <returns>The responded text information of first candidate.</returns>
public override string ToString()
{
return Text ?? String.Empty;
}
}
}

0 comments on commit 5ab823d

Please sign in to comment.