I have a "Trial Bonus" for VertexAI, meaning I have a free $300 to use services for.
I'm coding a C# Unity project in VScode, and have ways to use LLM API keys to utilize them in it.
What API keys for high level LLM models can I get from VertexAI?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To utilize high-level Large Language Models (LLMs) on Vertex AI within your C# Unity project in VS Code, you can access the following models via Vertex AI's APIs. Below is a step-by-step guide to get started, including model availability, authentication, and API usage:
Available Vertex AI LLM Models
Vertex AI provides access to PaLM series models (Google's LLMs) and other specialized models. Key models include:
PaLM 2 (Gemini Series)
Gemini Pro: High-performance model for complex tasks.
Gemini Enterprise: Enterprise-grade model with advanced capabilities.
Gemini Code: Specialized for code generation (e.g., code-bison, code-gecko, code-fox).
Legacy PaLM 1 Models
Text-bison: A versatile text-generation model (good for beginners).
Save the downloaded JSON file (contains your API credentials).
Step 2: Authenticate in Your C# Code
Use the service account credentials to authenticate API requests. You’ll need to:
Install the Google Cloud C# Library (if available) or use raw HTTP requests.
For simplicity, use the Google.Apis.Auth package to handle authentication.
bash
dotnet add package Google.Apis.Auth
Load Credentials and Generate an OAuth Token:
csharp
using Google.Apis.Auth.OAuth2;
using System.IO;
// Load the service account JSON file
var credentialPath = "path/to/your-service-account-file.json";
var credential = GoogleCredential.FromFile(credentialPath)
.CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });
// Get an access token for API requests
var accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
Step 3: Make API Calls to Vertex AI
Use HTTP requests to interact with Vertex AI models. Here’s an example for PaLM 2 (Gemini Pro):
API Endpoint
http
POST https://generativelanguage.googleapis.com/v1beta/models/{model}:generateText
Example C# Code
csharp
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
public async Task<string> GenerateTextAsync(string model, string prompt)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
var request = new
{
prompt = new[] { new { text = prompt } },
temperature = 0.7, // Adjust creativity (0-1)
maxOutputTokens = 256
};
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(
$"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateText",
content);
var responseString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<dynamic>(responseString).candidates[0].content;
}
Parameters
model: Specify the model name (e.g., gemini-pro).
prompt: Your input text (e.g., "Explain quantum computing in simple terms").
Step 4: Integrate into Unity
Place the service account JSON file in your Unity project (e.g., Assets/Resources).
Use the above code in a C# script (e.g., LLMController.cs).
Call GenerateTextAsync from your Unity code and handle the response.
Important Notes
Cost Management:
PaLM 2 models (e.g., Gemini Pro) are costlier than legacy models. Monitor usage via the Google Cloud Console.
Quotas:
The trial might have API call limits. Increase quotas if needed.
For code generation, use code-bison or code-gecko.
For simple tasks, use text-bison (PaLM 1).
Example Workflow in Unity
csharp
// Example usage in a Unity MonoBehaviour
public class LLMExample : MonoBehaviour
{
private async void Start()
{
string model = "models/gemini-pro"; // Use the correct model ID
string response = await GenerateTextAsync(model, "Tell me a joke about AI.");
Debug.Log(response);
}
}
This setup allows you to leverage Vertex AI’s advanced LLMs within your C# Unity project while staying within your $300 trial credit. Adjust the model and parameters based on your project’s needs!