Migrate to PaLM API from Azure OpenAI

This page outlines the steps required to migrate to Vertex AI PaLM API from Microsoft Azure OpenAI.

Objectives

The PaLM API is a fully managed cloud-based service that lets you create and train generative models using the Google Cloud console. It provides access to large language models (LLMs), which you can use to create a variety of applications, including chatbots, content generators, and creative tools.

Prerequisites

To migrate an OpenAI service from Microsoft Azure Open AI to Vertex AI PaLM API, you must first create a Google Cloud project and development environment. For more information, see Set up a project and a development environment.

Migrate to PaLM API

Use the following topics to learn how to migrate to PaLM API from an OpenAI project in Microsoft Azure.

Use equivalent PaLM API parameters

The following are some common Azure OpenAI parameters and their equivalent parameters in the PaLM API:

OpenAI Parameters PaLM API Parameters Description Valid values
prompt prompt A prompt is a natural language request submitted to a language model to receive a response back. Prompts can contain questions, instructions, contextual information, examples, and text for the model to complete or continue. Text
temperature temperature The temperature is used for sampling during response generation, which occurs when topP and topK are applied. Temperature controls the degree of randomness in token selection. Lower temperatures are good for prompts that require a less open-ended or creative response, while higher temperatures can lead to more diverse or creative results. A temperature of 0 means that the highest probability tokens are always selected. In this case, responses for a given prompt are mostly deterministic, but a small amount of variation is still possible.

If the model returns a response that's too generic, too short, or the model gives a fallback response, try increasing the temperature.

0.01.0

max_tokens maxOutputTokens Maximum number of tokens that can be generated in the response. A token is approximately four characters. 100 tokens correspond to roughly 60-80 words.

Specify a lower value for shorter responses and a higher value for potentially longer responses.

1-8192 (OpenAI)

18192 (PaLM API)

Not available topK Top-K changes how the model selects tokens for output. A top-K of 1 means the next selected token is the most probable among all tokens in the model's vocabulary (also called greedy decoding), while a top-K of 3 means that the next token is selected from among the three most probable tokens by using temperature.

For each token selection step, the top-K tokens with the highest probabilities are sampled. Then tokens are further filtered based on top-P with the final token selected using temperature sampling.

Specify a lower value for less random responses and a higher value for more random responses.

140

top_p topP Top-P changes how the model selects tokens for output. Tokens are selected from the most (see top-K) to least probable until the sum of their probabilities equals the top-P value. For example, if tokens A, B, and C have a probability of 0.3, 0.2, and 0.1 and the top-P value is 0.5, then the model will select either A or B as the next token by using temperature and excludes C as a candidate.

Specify a lower value for less random responses and a higher value for more random responses.

0.01.0

Use the equivalent PaLM API model

The following table describes the foundation models available.

Type Description OpenAI endpoints PaLM API LLM endpoints
Text Fine-tuned to follow natural language instructions and suitable for a variety of language tasks. gpt-3.5-turbo or gpt-4 text-bison@002
Chat Fine-tuned for multi-turn conversation use cases. gpt-3.5-turbo or gpt-4 chat-bison@002
Embedding Fine-tuned to return model embeddings for text inputs. text-embedding-ada-002 textembedding-gecko@003

Install, import, and authenticate Generative AI on Vertex AI

Use the Vertex AI SDK for Python to install, import, and authenticate Generative AI on Vertex AI. The following shows you the equivalent methods for Vertex AI SDK for Python and Azure OpenAI.

Install Vertex AI PaLM API

Azure OpenAI

$ pip install --upgrade openai

Vertex AI PaLM API

$ pip install google-cloud-aiplatform

Import Vertex AI PaLM API

Azure OpenAI

import openai

Vertex AI PaLM API

from vertexai.preview.language_models import TextGenerationModel

Authenticate Vertex AI PaLM API

Azure OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")

Vertex AI PaLM API

from google.colab import auth as google_auth
google_auth.authenticate_user()

Vertex AI PaLM API and Azure comparisons and sample code

Generate text with the Vertex AI SDK for Python

Azure OpenAI

from openai import OpenAI

client = OpenAI()

response = client.completions.create(prompt="Hello",
    max_tokens=256,
    temperature=0.3,
    model="gpt-4")

print(f"Response from Model: {response['choices'][0]['text']}")

Vertex AI PaLM API

from vertexai.preview.language_models import TextGenerationModel

model = TextGenerationModel.from_pretrained("text-bison@002")

response = model.predict(
    "Hello",
    max_output_tokens=256,
    temperature=0.3,)

print(f"Response from Model: {response.text}")

Use chat completion with the Vertex AI SDK for Python

Azure OpenAI

from openai import OpenAI

client = OpenAI()

parameters = {
    "model":"gpt-4",
    "temperature": 0.2,
    "max_tokens": 256,
    "top_p": 0.95}

chat = client.chat.completions.create(
    messages=[
      {"role": "system", "content": "My name is Miles. You are an astronomer, knowledgeable about the solar system."},
      {"role": "user", "name":"example_user", "content": "How many planets are there in the solar system?"}
      ],
    **parameters)

response = chat['choices'][0]
print(f"Response from Azure OpenAI Model: {response.text}")

Vertex AI PaLM API

from vertexai.preview.language_models import ChatModel

chat_model = ChatModel.from_pretrained("chat-bison@002")

parameters = {
    "temperature": 0.2,
    "max_output_tokens": 256,
    "top_p": 0.95}

chat = chat_model.start_chat(context="My name is Miles. You are an astronomer, knowledgeable about the solar system.")

response = chat.send_message(
    "How many planets are there in the solar system?",
    **parameters)

print(f"Response from Google GenAI Model: {response.text}")

Use text embedding with the Vertex AI SDK for Python

Azure OpenAI

import openai

embeddings = openai.Embedding.create(
  deployment_id="text-embedding-ada-002",
  #engine="text-embedding-ada-002",
  input="What is life?"
)["data"][0]["embedding"]

print(f'Length of Embedding Vector: {len(embeddings)}')

Vertex AI PaLM API

from vertexai.preview.language_models import TextEmbeddingModel

model = TextEmbeddingModel.from_pretrained("textembedding-gecko@003")
embeddings = model.get_embeddings(["What is life?"])

for embedding in embeddings:
  vector = embedding.values

print(f'Length of Embedding Vector: {len(vector)}')

What's next