Let's talk to a model. LangChain's init chat model function creates a chat model from a short name. Then invoke sends it a message, and waits for the reply.
We import init chat model, and create a model. The name has two parts. The provider, google gen AI, and the model, Gemini flash lite. Temperature zero keeps answers steady. Invoke sends a question, and dot text gives the reply as plain text. Run it. The model answers in one sentence.
first_call.pypython
from langchain.chat_models import init_chat_modelmodel = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)reply = model.invoke("In one sentence, what does a bookshop do?")print(reply.text)
Output
A bookshop curates, sells, and provides access to a diverse collection of books to
foster reading, learning, and discovery in the community.
The reply isn't just text. It's an AI message object, with extra information attached. Run it. We see its type, AI message, the text, and a usage field, with how many tokens went in and out. We'll use that later to track cost.
reply_object.pypython
from langchain.chat_models import init_chat_modelmodel = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)reply = model.invoke("Say hello in three words.")print(type(reply).__name__)print(reply.text)print(reply.usage_metadata["input_tokens"], "tokens in")