Git Product home page Git Product logo

generative-quote's Introduction

Generative AI .NET 8 Sample

This sample application is written using the .NET 8 minimal Web API to demonstrate calling the Vertex Generative AI API with the Gemini model.

Using Vertex AI Studio in the Google Cloud Consle you can easily design and test prompts. You can also quickly generate code using the <> GET CODE button in the upper right.

alt text for image

Prerequisites

Update your GCP project ID

You can find your project id in the GCP console under the project settings. Update your appsettings.json:

{
  "projectId": "YOUR_GCP_PROJECT_ID_HERE"
}

Generating a .NET wrapper for the Vertex AI API

Unfortunately, there is not a Vertex SDK for .NET yet. Thankfully all GCP APIs have a REST API which makes it easy for us to wrap with C# using the .NET HttpClient.

Authenticating with Google Cloud APIs

We are going to use the GCP Application Default Credentials which provides an easy abstraction to authenticate with GCP APIs. When running locally it will look for an environment variable named GOOGLE_APPLICATION_CREDENTIALS which contains the path to a JSON file with your GCP credentials. When you deploy to GCP (i.e. Cloud Run, GKE, GCE, etc...) the SDK will automatically authenticate using the metadata server, so you don't have to pass in any credentials.

Use these instructions to generate your credentials file. For example on linux follow these steps:

gcloud auth application-default login 

export GOOGLE_APPLICATION_CREDENTIALS=~/.config/gcloud/application_default_credentials.json

The code to get the required access token is in the GetAccessTokenAsync() method of VertexAIModelGenerator.cs:

        using var client = new HttpClient();

        // ... 

        var accessToken = await GetAccessTokenAsync();
        
        client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

        // ...

    private async Task<string> GetAccessTokenAsync()
    {
        // Use Application Default Credentials
        var credential = await GoogleCredential.GetApplicationDefaultAsync();

        // Create credential scoped to GCP APIs
        var scoped = credential.CreateScoped(
            new[] { "https://www.googleapis.com/auth/cloud-platform" });

        var accessToken = await scoped.UnderlyingCredential.GetAccessTokenForRequestAsync();

        return accessToken;
    }

BONUS: Use Duet to help generate the .NET Wrapper

As an added bonus you can use Duet AI assistance with Google's Cloud Code Extension for VS Code.

generative-quote's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.