DEV Community

parmarjatin4911@gmail.com
parmarjatin4911@gmail.com

Posted on

Langchain Vertex AI

Langchain Vertex AI

Python package: google-cloud-aiplatform
Environment setup:
    Credentials (gcloud, workload identity)
    Or GOOGLE_APPLICATION_CREDENTIALS environment variable
Enter fullscreen mode Exit fullscreen mode

Install Command:

pip install langchain google-cloud-aiplatform

Documentation:

Application Default Credentials
Google Auth Library
Enter fullscreen mode Exit fullscreen mode




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()

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay