Skip to main content

Google Cloud Platform Vertex AI PaLM

注意:这与Google PaLM集成是分开的。Google选择通过GCP提供PaLM的企业版本,并支持通过该版本提供的模型。

默认情况下,Google Cloud不使用客户数据来训练其基础模型,这是Google Cloud AI/ML隐私承诺的一部分。有关Google如何处理数据的更多详细信息也可以在Google的客户数据处理附加协议(CDPA)中找到。

要使用Vertex AI PaLM,您必须安装google-cloud-aiplatform Python包,并满足以下条件之一:

  • 配置了您的环境的凭据(gcloud、工作负载身份等)
  • 将服务帐号JSON文件的路径存储为GOOGLE_APPLICATION_CREDENTIALS环境变量

此代码库使用google.auth库,该库首先查找上述应用凭据变量,然后查找系统级身份验证。

有关更多信息,请参见:

#!pip install google-cloud-aiplatform
from langchain.chat_models import ChatVertexAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
chat = ChatVertexAI()
messages = [
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(
content="Translate this sentence from English to French. I love programming."
),
]
chat(messages)
    AIMessage(content='Sure, here is the translation of the sentence "I love programming" from English to French:\n\nJ\'aime programmer.', additional_kwargs={}, example=False)

您可以使用MessagePromptTemplate来使用模板。您可以从一个或多个MessagePromptTemplates构建一个ChatPromptTemplate。您可以使用ChatPromptTemplateformat_prompt方法,该方法返回一个PromptValue,您可以将其转换为字符串或消息对象,具体取决于您是否希望将格式化的值用作输入到llm或chat模型。

为了方便起见,模板上公开了一个from_template方法。如果您要使用此模板,它将如下所示:

template = (
"You are a helpful assistant that translates {input_language} to {output_language}."
)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)

# get a chat completion from the formatted messages
chat(
chat_prompt.format_prompt(
input_language="English", output_language="French", text="I love programming."
).to_messages()
)
    AIMessage(content='Sure, here is the translation of "I love programming" in French:\n\nJ\'aime programmer.', additional_kwargs={}, example=False)

现在您可以在Vertex AI中使用Codey API进行代码聊天。模型名称是:

  • codechat-bison:用于代码辅助
chat = ChatVertexAI(model_name="codechat-bison")
messages = [
HumanMessage(
content="How do I create a python function to identify all prime numbers?"
)
]
chat(messages)
    AIMessage(content='The following Python function can be used to identify all prime numbers up to a given integer:\n\n```\ndef is_prime(n):\n  """\n  Determines whether the given integer is prime.\n\n  Args:\n    n: The integer to be tested for primality.\n\n  Returns:\n    True if n is prime, False otherwise.\n  """\n\n  # Check if n is divisible by 2.\n  if n % 2 == 0:\n    return False\n\n  # Check if n is divisible by any integer from 3 to the square root', additional_kwargs={}, example=False)