Angeben von ResponseSchema beim Generieren von Inhalten mit dem Vertex AI SDK (Gemini)C#

Ein Treffpunkt für C#-Programmierer
Guest
 Angeben von ResponseSchema beim Generieren von Inhalten mit dem Vertex AI SDK (Gemini)

Post by Guest »

Ich versuche, JSON-Inhalte mithilfe des Vertex AI SDK (Schnittstelle zu Gemini-Modellen) zu generieren, wobei ich die strukturierte Ausgabe („ResponseSchema“) darüber übergebe, wie das Antwort-JSON erstellt werden soll.
Strukturierte Ausgabe:

Code: Select all

"Comp = {'rank': int, 'score': int, 'propertyId': int, 'address': str, 'proximity': str, 'price': str, 'livingArea': str, 'lotSize': str, 'yearBuilt': int, 'bedrooms': int, 'bathrooms': int, 'notes': str}\r\nReturn: list[Comp]"
C#-Code:

Code: Select all

{
var prompts = args.Prompts;
var systemPrompts = BuildSystemPrompts(args);

try
{
var predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{GeminiLocation}-aiplatform.googleapis.com",
ChannelCredentials = GetGoogleCredentials()
}.Build();
GenerateContentRequest request = new()
{
Model = $"projects/{GeminiProjectId}/locations/{GeminiLocation}/publishers/google/models/{GeminiModelId}",
Contents = { BuildGeminiPromptContents(prompts) },
SystemInstruction = new Content()
{
Parts = { systemPrompts.Select(prompt => new Part { Text = prompt }) }
},
GenerationConfig = new GenerationConfig
{
CandidateCount = 1,
Temperature = Temperature,
MaxOutputTokens = args.MaxOutputTokens ?? MaxOutputTokens,
TopP = TopP,
Seed = Seed,
ResponseMimeType = args.Output == GenAiOutput.Json ? "application/json" : "text/plain",
ResponseSchema = args.OutputSchema != null ?
OpenApiSchema.Parser.ParseFrom(BinaryData.FromBytes(Encoding.UTF8.GetBytes(args.OutputSchema))) :
null
}
};
var response = await predictionServiceClient.GenerateContentAsync(request);
var message = response?.Candidates?.FirstOrDefault()?.Content?.Parts?.FirstOrDefault()?.Text ?? "No Response";

Logging.Model.Instance.LogInformation(LoggingCategory, null, false, $"Gemini Request: Prompts: {string.Join(", ", args.Prompts)}, Message: {message}");
return new GenAiResponse { Message = message };
}
catch (Exception ex)
{
Logging.Model.Instance.LogError(LoggingCategory, ex);
return null;
}
}
Wenn ich diesen Code ausführe, wird die folgende Ausnahme ausgelöst:

Google.Protobuf.InvalidProtocolBufferException: 'Protokollnachricht enthielt ein Tag mit einem ungültigen Drahttyp.'

Ich sehe online keine Beispiele dafür, wie das ResponseSchema in C# richtig angegeben wird. Hat jemand eine Anleitung für mich?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post