AI Hints
Home›LangChain Tutorial›Models and Messages
LangChain · Lesson 4 of 120

Your First Model Call

Init_chat_model.

⏱ 2 min read▶ Video at 3:03🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

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.

Your First Model Call
From the video: Your First Model Call
  • init_chat_model
  • invoke
  • Get a reply

Hello, model

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_model

model = 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.

What comes back

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_model

model = 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")
Output
AIMessage
Hello to you!
7 tokens in

Summary

Init chat model creates a model, invoke sends a message, and the reply is an AI message with text and usage. Next, the different kinds of messages.

Key takeaways
  • init_chat_model
  • model.invoke(...)
  • reply.text
Next up: Messages — system, human, AI