name: firestore-palm-gen-text
version: 0.1.10
specVersion: v1beta

displayName: Language Tasks with PaLM API

description: >-
  Performs AI/ML tasks on text, customizable with prompt engineering, using PaLM
  API and Firestore.

icon: icon.png

tags:
  [
    ai,
    palm,
    generative-ai,
    text-ai,
    language-ai,
    large-language-models,
    llm,
    nlp,
    google-ai,
  ]

license: Apache-2.0 # https://spdx.org/licenses/

sourceUrl: https://github.com/GoogleCloudPlatform/firebase-extensions/tree/main/firestore-palm-gen-text
releaseNotesUrl: https://github.com/GoogleCloudPlatform/firebase-extensions/tree/main/firestore-palm-gen-text/CHANGELOG.md

author:
  authorName: Google Cloud
  url: https://cloud.google.com

contributors:
  - authorName: Invertase
    email: oss@invertase.io
    url: https://github.com/invertase

billingRequired: true

apis:
  - apiName: aiplatform.googleapis.com
    reason: For access to the PaLM API if this provider is chosen.

roles:
  - role: datastore.user
    reason:
      Allows this extension to access Cloud Firestore to read and process added
      messages.

  - role: aiplatform.user
    reason:
      Allows this extension to access the PaLM API via Vertex AI if this
      provider is chosen.

resources:
  - name: generateText
    type: firebaseextensions.v1beta.function
    description: >-
      Listens to Firestore data writes to generate conversations.
    properties:
      location: ${LOCATION}
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${COLLECTION_NAME}/{summaryId}
      runtime: 'nodejs18'

params:
  - param: PALM_API_PROVIDER
    label: Palm API Provider
    description: >-
      There are two services which provide access to the PaLM API. Which would
      you like to use? If Vertex AI is selected, the service will be
      automatically enabled. If Generative Language is selected, you can provide
      an API key obtained through MakerSuite or your GCP console, or use
      Application Default Credentials if the Generative Language AI is enabled
      in your google cloud project.
    type: select
    options:
      - label: Vertex AI
        value: vertex
      - label: Generative Language AI for Developers
        value: generative
    required: true
    default: vertex
    immutable: false

  - param: API_KEY
    label: API Key (Generative Language AI for Developers Provider ONLY)
    description: >-
      If you selected Generative AI for Developers as your PaLM API provider,
      you can optionally choose to provide an API key. If you do not provide an
      API key, the extension will use Application Default Credentials, but will
      need the service enabled in GCP.
    type: secret
    required: false
    immutable: false

  - param: COLLECTION_NAME
    label: Collection Path
    description: >-
      Path to the Firestore collection where text will be generated.
    type: string
    validationRegex: '^[^/]+(/[^/]+/[^/]+)*$'
    validationErrorMessage: Must be a valid Cloud Firestore Collection
    default: generate
    required: true
    immutable: false

  - param: PROMPT
    label: Prompt
    description: >-
      Prompt. Use {{ handlebars }} for variable substitution from the created or
      updated doc.
    type: string
    required: true
    immutable: false

  - param: VARIABLE_FIELDS
    label: Variable fields
    description: >-
      A comma separated list of fields to substitute as variables in the prompt.
    type: string
    required: false
    immutable: false
    validationRegex: ^[a-zA-Z0-9_]+(,[a-zA-Z0-9_]+)*$
    validationErrorMessage: >-
      Field names may only use upper and lowercase letters from A to Z,
      underscores, or numbers, and may only be separated by commas. Trailing
      commas and empty variable names are not allowed.

  - param: RESPONSE_FIELD
    label: Response Field
    description: >-
      The field in the message document into which to put the response.This is
      the name of the field in the Firestore document where you want the
      extension to store the response from the PaLM API. There is a token limit
      for the response field. This means responses may be truncated if they
      exceed a certain length.  The standard limit is approximately 4096
      characters, but this can vary depending on language and context. For more
      information, check [here](https://ai.google.dev/models/palm).
    type: string
    default: output
    required: true
    immutable: false

  - param: LOCATION
    label: Cloud Functions location
    description: >-
      Where do you want to deploy the functions created for this extension? For
      help selecting a location, refer to the [location selection
      guide](https://firebase.google.com/docs/functions/locations).
    type: select
    options:
      - label: Iowa (us-central1)
        value: us-central1
      - label: South Carolina (us-east1)
        value: us-east1
      - label: Northern Virginia (us-east4)
        value: us-east4
      - label: Los Angeles (us-west2)
        value: us-west2
      - label: Salt Lake City (us-west3)
        value: us-west3
      - label: Las Vegas (us-west4)
        value: us-west4
      - label: Warsaw (europe-central2)
        value: europe-central2
      - label: Belgium (europe-west1)
        value: europe-west1
      - label: London (europe-west2)
        value: europe-west2
      - label: Frankfurt (europe-west3)
        value: europe-west3
      - label: Zurich (europe-west6)
        value: europe-west6
      - label: Hong Kong (asia-east2)
        value: asia-east2
      - label: Tokyo (asia-northeast1)
        value: asia-northeast1
      - label: Osaka (asia-northeast2)
        value: asia-northeast2
      - label: Seoul (asia-northeast3)
        value: asia-northeast3
      - label: Mumbai (asia-south1)
        value: asia-south1
      - label: Jakarta (asia-southeast2)
        value: asia-southeast2
      - label: Montreal (northamerica-northeast1)
        value: northamerica-northeast1
      - label: Sao Paulo (southamerica-east1)
        value: southamerica-east1
      - label: Sydney (australia-southeast1)
        value: australia-southeast1
    required: true
    immutable: true

  - param: MODEL
    label: Language model
    description: >-
      Which language model do you want to use?
    type: select
    options:
      - label: text-bison
        value: text-bison
    required: true
    immutable: false

  - param: TEMPERATURE
    label: Temperature
    description: >-
      Controls the randomness of the output. Values can range over [0,1],
      inclusive. A value closer to 1 will produce responses that are more
      varied, while a value closer to 0 will typically result in less surprising
      responses from the model.
    type: string
    validationRegex: ^(?:0*(?:\.\d+)?|1(\.0*)?)$
    validationErrorMessage:
      Please specify a decimal representation of a number between 0 and 1.
    required: false
    immutable: false

  - param: TOP_P
    label: Nucleus sampling probability
    description: >-
      If specified, nucleus sampling will be used as the decoding strategy.
      Nucleus sampling considers the smallest set of tokens whose probability
      sum is at least a fixed value. Enter a value between 0 and 1.
    type: string
    validationRegex: ^(?:0*(?:\.\d+)?|1(\.0*)?)$
    validationErrorMessage:
      Please specify a decimal representation of a number between 0 and 1.
    required: false
    immutable: false

  - param: TOP_K
    label: Sampling strategy parameter
    description: >-
      If specified, top-k sampling will be used as the decoding strategy. Top-k
      sampling considers the set of topK most probable tokens.
    type: string
    validationRegex: ^[1-9][0-9]*
    validationErrorMessage: Please specify a positive integer.
    required: false
    immutable: false

  - param: CANDIDATE_COUNT
    label: Candidate count
    description: >-
      The default value is one. When set to an integer higher than one,
      additional candidate responses, up to the specified number, will be stored
      in Firestore under the 'candidates' field. Note this is only available as
      a feature if you  selected the Generative Language API for Developers as
      your Palm API provider.
    type: string
    validationRegex: ^[1-9][0-9]*
    validationErrorMessage: Please specify a positive integer.
    default: 1
    required: false
    immutable: false

  - param: CANDIDATES_FIELD
    label: Candidates field
    description: >-
      The field in the message document into which to put the other candidate
      responses if the candidate count parameter is greater than one. Note this
      is only available as a feature if you  selected the Generative Language
      API for Developers as your Palm API provider.
    type: string
    default: candidates
    required: false
    immutable: false

  - param: MAX_OUTPUT_TOKENS
    label: Maximum number of tokens
    description: >-
      If you have selected the Vertex AI service as your PaLM API provider, this
      parameter will be used to set the max_tokens parameter in the Vertex API
      request. It should be an integer in the range [1,1024]. The default value
      for the extension is 1024.
    type: string
    validationRegex: ^(102[0-4]|10[01][0-9]|100[0-9]|[1-9][0-9]{0,2})$
    validationErrorMessage: Please specify an integer in the range [1,1024]
    required: false
    default: 1024
    immutable: false

  - param: DEROGATORY_THRESHOLD
    label: Derogatory Content Threshold
    description: >-
      Threshold for derogatory content. Specify what probability level of
      derogatory content is blocked by the PaLM provider. This threshold is
      applicable only to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: TOXICITY_THRESHOLD
    label: Toxicity Threshold
    description: >-
      Threshold for toxic content. Specify what probability level of toxic
      content is blocked by the PaLM provider. This threshold is applicable only
      to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: SEXUAL_THRESHOLD
    label: Sexual Content Threshold
    description: >-
      Threshold for sexual content. Specify what probability level of sexual
      content is blocked by the PaLM provider. This threshold is currently
      applicable only to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: VIOLENCE_THRESHOLD
    label: Violent Content Threshold
    description: >-
      Threshold for violent content. Specify what probability of violent content
      is blocked by the PaLM provider. This threshold is currently applicable
      only to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: MEDICAL_THRESHOLD
    label: Medical Content Threshold
    description: >-
      Threshold for medical content. Specify what probability level of medical
      content is blocked by the PaLM provider. This threshold is applicable only
      to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: DANGEROUS_THRESHOLD
    label: Dangerous Content Threshold
    description: >-
      Threshold for dangerous content. Specify what probability level of
      dangerous content is blocked by the PaLM provider. This threshold is
      applicable only to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false

  - param: UNSPECIFIED_THRESHOLD
    label: Unspecified Harm Threshold
    description: >-
      Threshold for non-specific harmful content. Specify what probability level
      of non-specific harmful content is blocked by the PaLM provider. This
      threshold is applicable only to the Generative Language PaLM API.
    type: select
    options:
      - label: Default
        value: HARM_BLOCK_THRESHOLD_UNSPECIFIED
      - label: Block low and above
        value: BLOCK_LOW_AND_ABOVE
      - label: Block medium and above
        value: BLOCK_MEDIUM_AND_ABOVE
      - label: Block only high
        value: BLOCK_ONLY_HIGH
      - label: Block none
        value: BLOCK_NONE
    required: false
    default: HARM_BLOCK_THRESHOLD_UNSPECIFIED
    immutable: false
