Skip to content

Commit

Permalink
docs(aiplatform): Change all samples to use inline initialization Goo…
Browse files Browse the repository at this point in the history
  • Loading branch information
meteatamel authored and amanda-tarafa committed May 9, 2024
1 parent dea3a71 commit 61e8c78
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 291 deletions.
30 changes: 10 additions & 20 deletions aiplatform/api/AIPlatform.Samples/AudioInputSummarization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using Google.Cloud.AIPlatform.V1;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class AudioInputSummarization
Expand All @@ -38,31 +37,22 @@ public async Task<string> SummarizeAudio(
Provide chapter titles with timestamps, be concise and short, no need to provide chapter summaries.
Do not make up any information that is not part of the audio and do not be verbose.";

var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
var generateContentRequest = new GenerateContentRequest
{
new()
{
Text = prompt
},
new()
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Contents =
{
FileData = new()
new Content
{
MimeType = "audio/mp3",
FileUri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"
Role = "USER",
Parts =
{
new Part { Text = prompt },
new Part { FileData = new() { MimeType = "audio/mp3", FileUri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3" } }
}
}
}
});

var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}"
};
generateContentRequest.Contents.Add(content);

GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

Expand Down
30 changes: 10 additions & 20 deletions aiplatform/api/AIPlatform.Samples/AudioInputTranscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using Google.Cloud.AIPlatform.V1;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class AudioInputTranscription
Expand All @@ -38,31 +37,22 @@ public async Task<string> TranscribeAudio(
string prompt = @"Can you transcribe this interview, in the format of timecode, speaker, caption.
Use speaker A, speaker B, etc. to identify speakers.";

var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
var generateContentRequest = new GenerateContentRequest
{
new()
{
Text = prompt
},
new()
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Contents =
{
FileData = new()
new Content
{
MimeType = "audio/mp3",
FileUri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"
Role = "USER",
Parts =
{
new Part { Text = prompt },
new Part { FileData = new() { MimeType = "audio/mp3", FileUri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3" } }
}
}
}
});

var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}"
};
generateContentRequest.Contents.Add(content);

GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

Expand Down
40 changes: 13 additions & 27 deletions aiplatform/api/AIPlatform.Samples/GeminiQuickstart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using Google.Api.Gax.Grpc;
using Google.Cloud.AIPlatform.V1;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -37,31 +36,7 @@ public async Task<string> GenerateContent(
Endpoint = $"{location}-aiplatform.googleapis.com"
}.Build();

// Prompt
string prompt = "What's in this photo";
string imageUri = "gs://generativeai-downloads/images/scones.jpg";

// Initialize request argument(s)
var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
{
new()
{
Text = prompt
},
new()
{
FileData = new()
{
MimeType = "image/png",
FileUri = imageUri
}
}
});

// Initialize content request
var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Expand All @@ -71,9 +46,20 @@ public async Task<string> GenerateContent(
TopP = 1,
TopK = 32,
MaxOutputTokens = 2048
},
Contents =
{
new Content
{
Role = "USER",
Parts =
{
new Part { Text = "What's in this photo?" },
new Part { FileData = new() { MimeType = "image/png", FileUri = "gs://generativeai-downloads/images/scones.jpg" } }
}
}
}
};
generateContentRequest.Contents.Add(content);

// Make the request, returning a streaming response
using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);
Expand Down
19 changes: 4 additions & 15 deletions aiplatform/api/AIPlatform.Samples/MultiTurnChatSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public ChatSession(string modelPath, string location)
{
_modelPath = modelPath;

// Create a prediction service client.
_predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{location}-aiplatform.googleapis.com"
Expand All @@ -75,25 +74,18 @@ public ChatSession(string modelPath, string location)
_contents = new List<Content>();
}


public async Task<string> SendMessageAsync(string prompt)
{
// Initialize the content with the prompt.
var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
{
new()
Role = "USER",
Parts =
{
Text = prompt
new Part { Text = prompt }
}
});
};
_contents.Add(content);


// Create a request to generate content.
var generateContentRequest = new GenerateContentRequest
{
Model = _modelPath,
Expand All @@ -108,13 +100,10 @@ public async Task<string> SendMessageAsync(string prompt)
};
generateContentRequest.Contents.AddRange(_contents);

// Make a non-streaming request, get a response.
GenerateContentResponse response = await _predictionServiceClient.GenerateContentAsync(generateContentRequest);

// Save the content from the response.
_contents.Add(response.Candidates[0].Content);

// Return the text
return response.Candidates[0].Content.Parts[0].Text;
}
}
Expand Down
39 changes: 12 additions & 27 deletions aiplatform/api/AIPlatform.Samples/MultimodalAllInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using Google.Cloud.AIPlatform.V1;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class MultimodalAllInput
Expand All @@ -44,37 +43,23 @@ public async Task<string> AnswerFromMultimodalInput(
+ "Provide a timestamp.\n"
+ "- What is the context of the moment and what does the narrator say about it?";

var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
var generateContentRequest = new GenerateContentRequest
{
new()
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Contents =
{
Text = prompt
},
new()
{
FileData = new() {
MimeType = "video/mp4",
FileUri = "gs://cloud-samples-data/generative-ai/video/behind_the_scenes_pixel.mp4"
}
},
new()
{
FileData = new() {
MimeType = "image/png",
FileUri = "gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png"
new Content
{
Role = "USER",
Parts =
{
new Part { Text = prompt },
new Part { FileData = new() { MimeType = "video/mp4", FileUri = "gs://cloud-samples-data/generative-ai/video/behind_the_scenes_pixel.mp4" } },
new Part { FileData = new() { MimeType = "image/png", FileUri = "gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png" } }
}
}
}
});

var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}"
};
generateContentRequest.Contents.Add(content);

GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

Expand Down
59 changes: 13 additions & 46 deletions aiplatform/api/AIPlatform.Samples/MultimodalMultiImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Google.Api.Gax.Grpc;
using Google.Cloud.AIPlatform.V1;
using Google.Protobuf;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -33,13 +32,11 @@ public async Task<string> GenerateContent(
string model = "gemini-1.0-pro-vision"
)
{
// Create client
var predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{location}-aiplatform.googleapis.com"
}.Build();

// Images
ByteString colosseum = await ReadImageFileAsync(
"https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark1.png");

Expand All @@ -49,60 +46,30 @@ public async Task<string> GenerateContent(
ByteString christRedeemer = await ReadImageFileAsync(
"https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark3.png");

// Initialize request argument(s)
var content = new Content
{
Role = "USER"
};
content.Parts.AddRange(new List<Part>()
var generateContentRequest = new GenerateContentRequest
{
new()
{
InlineData = new()
{
MimeType = "image/png",
Data = colosseum

}
},
new()
{
Text = "city: Rome, Landmark: the Colosseum"
},
new()
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Contents =
{
InlineData = new()
new Content
{
MimeType = "image/png",
Data = forbiddenCity
}
},
new()
{
Text = "city: Beijing, Landmark: Forbidden City"
},
new()
{
InlineData = new()
{
MimeType = "image/png",
Data = christRedeemer
Role = "USER",
Parts =
{
new Part { InlineData = new() { MimeType = "image/png", Data = colosseum }},
new Part { Text = "city: Rome, Landmark: the Colosseum" },
new Part { InlineData = new() { MimeType = "image/png", Data = forbiddenCity }},
new Part { Text = "city: Beijing, Landmark: Forbidden City"},
new Part { InlineData = new() { MimeType = "image/png", Data = christRedeemer }}
}
}
}
});

var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}"
};
generateContentRequest.Contents.Add(content);

// Make the request, returning a streaming response
using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);

StringBuilder fullText = new();

// Read streaming responses from server until complete
AsyncResponseStream<GenerateContentResponse> responseStream = response.GetResponseStream();
await foreach (GenerateContentResponse responseItem in responseStream)
{
Expand Down
Loading

0 comments on commit 61e8c78

Please sign in to comment.