Jack tileman
Jack tileman

Reputation: 871

Check if a prompt is valid - Vertex AI

We are using Google App engine standard F1 instance , python 3.12. A user inputs a string, we convert it to a prompt and get output from GenAI. We are following the example here.

from google import genai
from google.genai import types
import base64

def generate():
  client = genai.Client(
      vertexai=True,
      project="openbarn-ai-module",
      location="us-central1",
  )


  model = "gemini-2.0-flash-001"
  contents = [
    types.Content(
      role="user",
      parts=[
        types.Part.from_text(text="""adkfjsadfafsd""")
      ]
    )
  ]
  generate_content_config = types.GenerateContentConfig(
    temperature = 1,
    top_p = 0.95,
    max_output_tokens = 8192,
    response_modalities = ["TEXT"],
    safety_settings = [types.SafetySetting(
      category="HARM_CATEGORY_HATE_SPEECH",
      threshold="OFF"
    ),types.SafetySetting(
      category="HARM_CATEGORY_DANGEROUS_CONTENT",
      threshold="OFF"
    ),types.SafetySetting(
      category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
      threshold="OFF"
    ),types.SafetySetting(
      category="HARM_CATEGORY_HARASSMENT",
      threshold="OFF"
    )],
  )

  for chunk in client.models.generate_content_stream(
    model = model,
    contents = contents,
    config = generate_content_config,
    ):
    print(chunk.text, end="")

generate()

The string "adkfjsadfafsd" is garbage that was input by the user. If the user wrote a valid prompt - for example "How does AI work", then the code works as it should. But how can we check if a given string is a valid prompt? Is there a way to confirm before calling generate_content_stream if the prompt is a valid one?

Upvotes: 0

Views: 24

Answers (0)

Related Questions