-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenerateTextFromText.cs
62 lines (53 loc) · 2.07 KB
/
GenerateTextFromText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Google.Api.Gax.Grpc;
using Google.Cloud.AIPlatform.V1;
public class GenerateTextFromText
{
const string ProjectId = "genai-atamel";
const string Location = "us-central1";
const string Publisher = "google";
const string Model_Gemini_1_0 = "gemini-1.0-pro-002";
const string Model_Gemini_1_5 = "gemini-1.5-pro-preview-0409";
const string Model = Model_Gemini_1_5;
public async static Task Generate()
{
// Create client
var predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{Location}-aiplatform.googleapis.com"
}.Build();
// Prompt
string prompt = "Why is the sky blue?";
Console.WriteLine($"Prompt: {prompt}");
// Initialize request argument(s)
var content = new Content
{
Role = "USER"
};
content.Parts.Add(new Part()
{
Text = prompt
});
var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{ProjectId}/locations/{Location}/publishers/{Publisher}/models/{Model}",
GenerationConfig = new GenerationConfig
{
Temperature = 0.4f,
TopP = 1,
TopK = 32,
MaxOutputTokens = 2048
}
};
generateContentRequest.Contents.Add(content);
// Make the request, returning a streaming response
using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);
Console.Write($"Response: ");
// Read streaming responses from server until complete
AsyncResponseStream<GenerateContentResponse> responseStream = response.GetResponseStream();
while (await responseStream.MoveNextAsync())
{
GenerateContentResponse responseItem = responseStream.Current;
Console.WriteLine(responseItem.Candidates[0].Content.Parts[0].Text);
}
}
}