ChatVertexAI
Note: This is separate from the Google PaLM integration. Google has chosen to offer an enterprise version of PaLM through GCP, and this supports the models made available through there.
By default, Google Cloud does not use customer data to train its foundation models as part of Google Cloud`s AI/ML Privacy Commitment. More details about how Google processes data can also be found in Googleβs Customer Data Processing Addendum (CDPA).
To use Google Cloud Vertex AI
PaLM you must have the
google-cloud-aiplatform
Python package installed and either: - Have
credentials configured for your environment (gcloud, workload identity,
etcβ¦) - Store the path to a service account JSON file as the
GOOGLE_APPLICATION_CREDENTIALS environment variable
This codebase uses the google.auth
library which first looks for the
application credentials variable mentioned above, and then looks for
system-level auth.
For more information, see: - https://cloud.google.com/docs/authentication/application-default-credentials#GAC - https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth
!pip install -U google-cloud-aiplatform
from langchain.chat_models import ChatVertexAI
from langchain.prompts import ChatPromptTemplate
system = "You are a helpful assistant who translate English to French"
human = "Translate this sentence from English to French. I love programming."
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chat = ChatVertexAI()
chain = prompt | chat
chain.invoke({})
AIMessage(content=" J'aime la programmation.")
If we want to construct a simple chain that takes user specified parameters:
system = (
"You are a helpful assistant that translates {input_language} to {output_language}."
)
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
chain.invoke(
{
"input_language": "English",
"output_language": "Japanese",
"text": "I love programming",
}
)
AIMessage(content=' γγγ°γ©γγ³γ°γ倧ε₯½γγ§γ')
Code generation chat modelsβ
You can now leverage the Codey API for code chat within Vertex AI. The model name is: - codechat-bison: for code assistance
chat = ChatVertexAI(
model_name="codechat-bison", max_output_tokens=1000, temperature=0.5
)
message = chat.invoke("Write a Python function to identify all prime numbers")
print(message.content)
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def find_prime_numbers(n):
prime_numbers = []
for i in range(2, n + 1):
if is_prime(i):
prime_numbers.append(i)
return prime_numbers
print(find_prime_numbers(100))
```
Output:
```
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
```
Asynchronous callsβ
We can make asynchronous calls via the Runnables Async Interface
# for running these examples in the notebook:
import asyncio
import nest_asyncio
nest_asyncio.apply()
system = (
"You are a helpful assistant that translates {input_language} to {output_language}."
)
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
asyncio.run(
chain.ainvoke(
{
"input_language": "English",
"output_language": "Sanskrit",
"text": "I love programming",
}
)
)
AIMessage(content=' Why do you love programming?')
Streaming callsβ
We can also stream outputs via the stream
method:
import sys
prompt = ChatPromptTemplate.from_messages(
[("human", "List out the 5 most populous countries in the world")]
)
chat = ChatVertexAI()
chain = prompt | chat
for chunk in chain.stream({}):
sys.stdout.write(chunk.content)
sys.stdout.flush()
The five most populous countries in the world are:
1. China (1.4 billion)
2. India (1.3 billion)
3. United States (331 million)
4. Indonesia (273 million)
5. Pakistan (220 million)