Skip to content

Commit 1172af2

Browse files
authored
[Firebase AI] Add the test json files (#1282)
* [Firebase AI] Add the test json files * Remove Imagen tests * Remove extra logging
1 parent 3263623 commit 1172af2

31 files changed

+628
-14
lines changed

firebaseai/testapp/Assets/Firebase/Sample/FirebaseAI/UIHandlerAutomated.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Firebase.Sample.FirebaseAI {
2727
using System.Threading.Tasks;
2828
using Google.MiniJSON;
2929
using UnityEngine;
30-
using UnityEngine.Video;
30+
using UnityEngine.Networking;
3131
using System.IO;
3232
#if INCLUDE_FIREBASE_AUTH
3333
using Firebase.Auth;
@@ -443,7 +443,7 @@ async Task TestEnumSchemaResponse(Backend backend) {
443443
generationConfig: new GenerationConfig(
444444
responseMimeType: "text/x.enum",
445445
responseSchema: Schema.Enum(new string[] { enumValue })));
446-
446+
447447
var response = await model.GenerateContentAsync(
448448
"Hello, I am testing setting the response schema to an enum.");
449449

@@ -459,7 +459,7 @@ async Task TestAnyOfSchemaResponse(Backend backend) {
459459
Schema.AnyOf(new[] { Schema.Int(), Schema.String() }),
460460
minItems: 2,
461461
maxItems: 6)));
462-
462+
463463
var response = await model.GenerateContentAsync(
464464
"Hello, I am testing setting the response schema with an array, cause you give me some random values.");
465465

@@ -653,7 +653,7 @@ async Task TestYoutubeLink(Backend backend) {
653653
async Task TestGenerateImage(Backend backend) {
654654
var model = GetFirebaseAI(backend).GetGenerativeModel("gemini-2.0-flash-exp",
655655
generationConfig: new GenerationConfig(
656-
responseModalities: new [] { ResponseModality.Text, ResponseModality.Image })
656+
responseModalities: new[] { ResponseModality.Text, ResponseModality.Image })
657657
);
658658

659659
GenerateContentResponse response = await model.GenerateContentAsync(
@@ -732,17 +732,40 @@ async Task TestReadSecureFile() {
732732

733733
// The url prefix to use when fetching test data to use from the separate GitHub repo.
734734
readonly string testDataUrl =
735-
"https://raw.githubusercontent.com/FirebaseExtended/vertexai-sdk-test-data/3737ae1fe9c5ecbd55abdeabc273ef4f392cbf19/mock-responses/";
735+
"https://raw.githubusercontent.com/FirebaseExtended/vertexai-sdk-test-data/47becf9101d11ea3c568bf60b12f1c8ed9fb684e/mock-responses/";
736736
readonly HttpClient httpClient = new();
737737

738+
private Task<string> LoadStreamingAsset(string fullPath) {
739+
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
740+
UnityWebRequest request = UnityWebRequest.Get(fullPath);
741+
request.SendWebRequest().completed += (_) => {
742+
if (request.result == UnityWebRequest.Result.Success) {
743+
tcs.SetResult(request.downloadHandler.text);
744+
} else {
745+
tcs.SetResult(null);
746+
}
747+
};
748+
return tcs.Task;
749+
}
750+
738751
// Gets the Json test data from the given filename, potentially downloading from a GitHub repo.
739752
private async Task<Dictionary<string, object>> GetJsonTestData(string filename) {
740-
// TODO: Check if the file is available locally first
753+
string jsonString = null;
754+
// First, try to load the file from StreamingAssets
755+
string localPath = Path.Combine(Application.streamingAssetsPath, "TestData", filename);
756+
if (localPath.StartsWith("jar") || localPath.StartsWith("http")) {
757+
// Special case to access StreamingAsset content on Android
758+
jsonString = await LoadStreamingAsset(localPath);
759+
} else if (File.Exists(localPath)) {
760+
jsonString = File.ReadAllText(localPath);
761+
}
741762

742-
var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, testDataUrl + filename));
743-
response.EnsureSuccessStatusCode();
744-
745-
string jsonString = await response.Content.ReadAsStringAsync();
763+
if (string.IsNullOrEmpty(jsonString)) {
764+
var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, testDataUrl + filename));
765+
response.EnsureSuccessStatusCode();
766+
767+
jsonString = await response.Content.ReadAsStringAsync();
768+
}
746769

747770
return Json.Deserialize(jsonString) as Dictionary<string, object>;
748771
}
@@ -857,7 +880,7 @@ async Task InternalTestBasicReplyShort() {
857880
async Task InternalTestCitations() {
858881
Dictionary<string, object> json = await GetVertexJsonTestData("unary-success-citations.json");
859882
GenerateContentResponse response = GenerateContentResponse.FromJson(json, FirebaseAI.Backend.InternalProvider.VertexAI);
860-
883+
861884
ValidateTextPart(response, "Some information cited from an external source");
862885

863886
CitationMetadata? metadata = response.Candidates.First().CitationMetadata;
@@ -1047,7 +1070,7 @@ async Task InternalTestBasicResponseLongUsageMetadata() {
10471070
// Test that parsing a basic short reply from Google AI endpoint works as expected.
10481071
// https://github.com/FirebaseExtended/vertexai-sdk-test-data/blob/main/mock-responses/googleai/unary-success-basic-reply-short.txt
10491072
async Task InternalTestGoogleAIBasicReplyShort() {
1050-
Dictionary<string, object> json = await GetGoogleAIJsonTestData("unary-success-basic-reply-short.txt"); //
1073+
Dictionary<string, object> json = await GetGoogleAIJsonTestData("unary-success-basic-reply-short.json"); //
10511074
GenerateContentResponse response = GenerateContentResponse.FromJson(json, FirebaseAI.Backend.InternalProvider.GoogleAI);
10521075

10531076
ValidateTextPart(response, "Google's headquarters, also known as the Googleplex, is located in **Mountain View, California**.\n");
@@ -1073,12 +1096,12 @@ async Task InternalTestGoogleAIBasicReplyShort() {
10731096
// Test parsing a Google AI format response with citations.
10741097
// Based on: https://github.com/FirebaseExtended/vertexai-sdk-test-data/blob/main/mock-responses/googleai/unary-success-citations.txt
10751098
async Task InternalTestGoogleAICitations() {
1076-
Dictionary<string, object> json = await GetGoogleAIJsonTestData("unary-success-citations.txt");
1099+
Dictionary<string, object> json = await GetGoogleAIJsonTestData("unary-success-citations.json");
10771100
GenerateContentResponse response = GenerateContentResponse.FromJson(json, FirebaseAI.Backend.InternalProvider.GoogleAI);
10781101

10791102
// Validate Text Part (check start and end)
10801103
string expectedStart = "Okay, let's break down quantum mechanics.";
1081-
string expectedEnd = "foundation for many technologies, including:\n";
1104+
string expectedEnd = "area of physics!";
10821105
Assert("Candidate count", response.Candidates.Count() == 1);
10831106
Candidate candidate = response.Candidates.First();
10841107
AssertEq("Content role", candidate.Content.Role, "model");

firebaseai/testapp/Assets/StreamingAssets.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firebaseai/testapp/Assets/StreamingAssets/TestData.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firebaseai/testapp/Assets/StreamingAssets/TestData/googleai.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"candidates": [
3+
{
4+
"content": {
5+
"parts": [
6+
{
7+
"text": "Google's headquarters, also known as the Googleplex, is located in **Mountain View, California**.\n"
8+
}
9+
],
10+
"role": "model"
11+
},
12+
"finishReason": "STOP",
13+
"safetyRatings": [
14+
{
15+
"category": "HARM_CATEGORY_HATE_SPEECH",
16+
"probability": "NEGLIGIBLE"
17+
},
18+
{
19+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
20+
"probability": "NEGLIGIBLE"
21+
},
22+
{
23+
"category": "HARM_CATEGORY_HARASSMENT",
24+
"probability": "NEGLIGIBLE"
25+
},
26+
{
27+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
28+
"probability": "NEGLIGIBLE"
29+
}
30+
],
31+
"avgLogprobs": -0.048741644079034981
32+
}
33+
],
34+
"usageMetadata": {
35+
"promptTokenCount": 7,
36+
"candidatesTokenCount": 22,
37+
"totalTokenCount": 29,
38+
"promptTokensDetails": [
39+
{
40+
"modality": "TEXT",
41+
"tokenCount": 7
42+
}
43+
],
44+
"candidatesTokensDetails": [
45+
{
46+
"modality": "TEXT",
47+
"tokenCount": 22
48+
}
49+
]
50+
},
51+
"modelVersion": "gemini-2.0-flash"
52+
}

firebaseai/testapp/Assets/StreamingAssets/TestData/googleai/unary-success-basic-reply-short.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"candidates": [
3+
{
4+
"content": {
5+
"parts": [
6+
{
7+
"text": "Okay, let's break down quantum mechanics. It's a challenging but fascinating area of physics!"
8+
}
9+
],
10+
"role": "model"
11+
},
12+
"finishReason": "STOP",
13+
"safetyRatings": [
14+
{
15+
"category": "HARM_CATEGORY_HATE_SPEECH",
16+
"probability": "NEGLIGIBLE"
17+
},
18+
{
19+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
20+
"probability": "NEGLIGIBLE"
21+
},
22+
{
23+
"category": "HARM_CATEGORY_HARASSMENT",
24+
"probability": "NEGLIGIBLE"
25+
},
26+
{
27+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
28+
"probability": "NEGLIGIBLE"
29+
}
30+
],
31+
"citationMetadata": {
32+
"citationSources": [
33+
{
34+
"startIndex": 548,
35+
"endIndex": 690,
36+
"uri": "https://www.example.com/some-citation-1",
37+
"license": "mit"
38+
},
39+
{
40+
"startIndex": 1240,
41+
"endIndex": 1407,
42+
"uri": "https://www.example.com/some-citation-1"
43+
},
44+
{
45+
"startIndex": 1942,
46+
"endIndex": 2149
47+
},
48+
{
49+
"startIndex": 2036,
50+
"endIndex": 2175
51+
}
52+
]
53+
},
54+
"avgLogprobs": -0.30887421148582783
55+
}
56+
],
57+
"usageMetadata": {
58+
"promptTokenCount": 15,
59+
"candidatesTokenCount": 1667,
60+
"totalTokenCount": 1682,
61+
"promptTokensDetails": [
62+
{
63+
"modality": "TEXT",
64+
"tokenCount": 15
65+
}
66+
],
67+
"candidatesTokensDetails": [
68+
{
69+
"modality": "TEXT",
70+
"tokenCount": 1667
71+
}
72+
]
73+
},
74+
"modelVersion": "gemini-2.0-flash"
75+
}

firebaseai/testapp/Assets/StreamingAssets/TestData/googleai/unary-success-citations.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firebaseai/testapp/Assets/StreamingAssets/TestData/vertexai.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"candidates": [
3+
{
4+
"finishReason": "SAFETY",
5+
"safetyRatings": [
6+
{
7+
"category": "HARM_CATEGORY_HATE_SPEECH",
8+
"probability": "NEGLIGIBLE",
9+
"probabilityScore": 0.3984375,
10+
"severity": "HARM_SEVERITY_LOW",
11+
"severityScore": 0.21582031
12+
},
13+
{
14+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
15+
"probability": "NEGLIGIBLE",
16+
"probabilityScore": 0.14941406,
17+
"severity": "HARM_SEVERITY_NEGLIGIBLE",
18+
"severityScore": 0.02331543
19+
},
20+
{
21+
"category": "HARM_CATEGORY_HARASSMENT",
22+
"probability": "LOW",
23+
"blocked": true,
24+
"probabilityScore": 0.61328125,
25+
"severity": "HARM_SEVERITY_LOW",
26+
"severityScore": 0.31835938
27+
},
28+
{
29+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
30+
"probability": "NEGLIGIBLE",
31+
"probabilityScore": 0.13476563,
32+
"severity": "HARM_SEVERITY_NEGLIGIBLE",
33+
"severityScore": 0.12109375
34+
}
35+
]
36+
}
37+
],
38+
"usageMetadata": {
39+
"promptTokenCount": 8,
40+
"totalTokenCount": 8
41+
},
42+
"modelVersion": "gemini-1.5-flash"
43+
}

firebaseai/testapp/Assets/StreamingAssets/TestData/vertexai/unary-failure-finish-reason-safety-no-content.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"predictions": [
3+
{
4+
"raiFilteredReason": "Unable to show generated images. All images were filtered out because they violated Vertex AI's usage guidelines. You will not be charged for blocked images. Try rephrasing the prompt. If you think this was an error, send feedback. Support codes: 39322892, 29310472"
5+
}
6+
]
7+
}

firebaseai/testapp/Assets/StreamingAssets/TestData/vertexai/unary-failure-generate-images-all-filtered.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)