DEV Community

Ravi
Ravi

Posted on

Sample python script: using Text-Bison model via GCP

To use Text-Bison from Google Cloud's Generative AI services, you'll typically need to set up a Google Cloud project, enable the necessary APIs, and install the relevant libraries. Below is a sample Python script demonstrating how to interact with the Text-Bison API to generate text.

Prerequisites

  1. Google Cloud Account: Create a Google Cloud account if you don’t have one.
  2. Create a Project: Set up a new project in the Google Cloud Console.
  3. Enable APIs: Enable the Generative AI API for your project.
  4. Install the Google Cloud Client Library:
   pip install google-cloud-aiplatform
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Authentication: Make sure you have set up authentication using a service account key.

Sample Python Script

from google.cloud import aiplatform

# Initialize the AI Platform with your project and location
project_id = 'your-project-id'
location = 'us-central1'  # or your specific region

aiplatform.init(project=project_id, location=location)

def generate_text(prompt):
    # Create a Text-Bison model instance
    model = aiplatform.Model("text-bison")

    # Call the model to generate text
    response = model.predict(
        instances=[{"prompt": prompt}],
        parameters={"temperature": 0.7, "max_output_tokens": 100}
    )

    return response.predictions[0]['text']

if __name__ == "__main__":
    prompt = "What are the benefits of using AI in healthcare?"
    generated_text = generate_text(prompt)
    print("Generated Text:", generated_text)
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Initialization: The script initializes the AI Platform with your project ID and location.
  2. Text Generation Function:
    • It defines a function generate_text that takes a prompt as input.
    • Inside the function, it creates a model instance for Text-Bison.
    • The predict method is called on the model with the prompt and optional parameters like temperature (which controls the randomness of the output) and max_output_tokens (the maximum length of the generated output).
  3. Execution: The script generates text based on a predefined prompt and prints the result.

Notes

  • Replace "your-project-id" with your actual Google Cloud project ID.
  • Adjust the parameters based on your needs (e.g., change temperature and max_output_tokens).
  • Make sure your environment is properly authenticated with Google Cloud to allow access to the API.

Top comments (0)