I've experimented with RAG style systems using OpenAPI REST endpoints. Recently at a hackathon for my current employer I got to use LangChain and it's pretty nice.
While following the LangChain tutorial my Google Gemini Pro API key was not enough to get things working. The error message said something about providing google cloud credentials and I didn't want to go that far. A bit of googling led to this solution.
In my Juypter Notebook, I install the python packages
%pip install langchain langchain-core langchain-community langchain-google-genai pillow
Then used langchain_google_genai.ChatGoogleGenerativeAI
like so...
import getpass
import os
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
# env
os.environ["LANGCHAIN_TRACING_V2"] = "false"
os.environ["GOOGLE_API_KEY"] = "KEY_HERE"
# template
prompt_template = ChatPromptTemplate.from_messages(
[
("system", "translate the following into {language}"),
("user", "{text}")
]
)
# chain
model = ChatGoogleGenerativeAI(model="gemini-pro", convert_system_message_to_human=True)
parser = StrOutputParser()
chain = prompt_template | model | parser
# print
print(chain.invoke({"language": "italian", "text": "hi"}))
I wanted to save these learnings for later when I dig into LangGraph.