Langchain Vertex AI
Python package: google-cloud-aiplatform
Environment setup:
Credentials (gcloud, workload identity)
Or GOOGLE_APPLICATION_CREDENTIALS environment variable
Install Command:
pip install langchain google-cloud-aiplatform
Documentation:
Application Default Credentials
Google Auth Library
Code 1
from langchain.llms import VertexAI
llm = VertexAI()
print(llm("What are some of the pros and cons of Python as a programming language?"))
Code 2
from langchain.prompts import PromptTemplate
template = "Question: {question}\nAnswer: Let's think step by step."
prompt = PromptTemplate.from_template(template)
chain = prompt | llm
question = "Who was the president in the year Justin Beiber was born?"
print(chain.invoke({"question": question}))
Code 3
llm = VertexAI(model_name="code-bison", max_output_tokens=1000, temperature=0.3)
question = "Write a python function that checks if a string is a valid email address"
print(llm(question))
Code 4
result = llm.generate([question])
result.generations
Code 5
import asyncio
asyncio.run(llm.agenerate([question]))
Code 6
import sys
for chunk in llm.stream(question):
sys.stdout.write(chunk)
sys.stdout.flush()
Top comments (0)