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
- Google Cloud Account: Create a Google Cloud account if you donβt have one.
- Create a Project: Set up a new project in the Google Cloud Console.
- Enable APIs: Enable the Generative AI API for your project.
- Install the Google Cloud Client Library:
pip install google-cloud-aiplatform
- 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)
Explanation
- Initialization: The script initializes the AI Platform with your project ID and location.
-
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 liketemperature
(which controls the randomness of the output) andmax_output_tokens
(the maximum length of the generated output).
- It defines a function
- 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
andmax_output_tokens
). - Make sure your environment is properly authenticated with Google Cloud to allow access to the API.
Top comments (0)