1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What does calling invoke() on a chat model do?
Sends a request to the model and waits for the complete response all at once.
When should you use invoke instead of stream?
When you need the full result immediately; with no intermediate output.
What does calling stream() on a chat model return?
An iterator that yields output chunks as they are produced.
Why is stream preferred for chat interfaces?
Because it matters that the user sees the response right away instead of waiting for generation to finish; which improves UX by showing the model 'thinking'.
What does a reasoning content block represent when streaming?
The model's chain of thought (CoT) — the model 'thinking out loud' before answering; e.g. Claude's extended thinking feature.
What does a tool_call_chunk block let you do; and why is this critical for agents?
It lets you receive a tool call progressively — first the name; then the arguments — which is critical for agents.
What is the difference between a stream chunk and a streaming event from astream_events?
A chunk is just a piece of text; while a streaming event is a structured; semantic record of what happened at a given moment in the model's execution lifecycle.
What two benefits does astream_events() provide?
It simplifies filtering based on event types and other metadata; and it aggregates the full message in the background.
When does LangChain automatically enable 'auto-streaming' for a chat model called via invoke()?
When it detects that the overall application is being streamed; such as when a LangGraph agent graph is run in a streaming mode.
What LangChain callback event is triggered while a chat model is being auto-streamed inside an invoke() call?
The on_llm_new_token event in LangChain's callback system.
What does batch() do?
Sends multiple requests in a single batch for efficient processing.
Why might you use batch instead of calling invoke in a loop?
It's faster and cheaper than calling invoke in a loop; since the model can optimize processing of parallel requests.
What does batch() return by default?
Only the final output for the entire batch.
How does batch_as_completed() differ from batch()?
batch_as_completed() streams the output for each individual input as it finishes generating; rather than waiting for the whole batch like batch() does.
What role does each message's 'role' field serve in a list of messages passed to a chat model?
Models use it to indicate who sent the message in the conversation.
python for chunk in model.stream('What color is the sky?'): for block in chunk.content_blocks: if block['type'] == 'reasoning' and (reasoning := block.get('reasoning')): print(f'Reasoning: {reasoning}') elif block['type'] == '________': print(f'Tool call chunk: {block}') elif block['type'] == 'text': print(block['text']) else: …
python for chunk in model.stream('What color is the sky?'): for block in chunk.content_blocks: if block['type'] == 'reasoning' and (reasoning := block.get('reasoning')): print(f'Reasoning: {reasoning}') elif block['type'] == 'tool_call_chunk': print(f'Tool call chunk: {block}') elif block['type'] == 'text': print(block['text']) else: ...
python async for event in model.________('Hello'): if event['event'] == 'on_chat_model_start': print(f'Input: {event['data']['input']}') elif event['event'] == 'on_chat_model_stream': print(f'Token: {event['data']['chunk'].text}') elif event['event'] == 'on_chat_model_end': print(f'Full message: {event['data']['output'].text}') else: pass
python async for event in model.astream_events('Hello'): if event['event'] == 'on_chat_model_start': print(f'Input: {event['data']['input']}') elif event['event'] == 'on_chat_model_stream': print(f'Token: {event['data']['chunk'].text}') elif event['event'] == 'on_chat_model_end': print(f'Full message: {event['data']['output'].text}') else: pass
python for response in model.________([ 'Why do parrots talk?'; 'why does husky look like a wolf?'; 'What is cloud computing' ]): print(response)
python for response in model.batch_as_completed([ 'Why do parrots talk?'; 'why does husky look like a wolf?'; 'What is cloud computing' ]): print(response)
python from langchain.messages import ________; ________; ________ conversation = [ SystemMessage('You are a helpful assistant that translates English to French.'); HumanMessage('Translate: I love programming.'); AIMessage('J'adore la programmation.'); HumanMessage('Translate: I love building applications.') ] response = model.invoke(conversation)
python from langchain.messages import HumanMessage; AIMessage; SystemMessage conversation = [ SystemMessage('You are a helpful assistant that translates English to French.'); HumanMessage('Translate: I love programming.'); AIMessage('J'adore la programmation.'); HumanMessage('Translate: I love building applications.') ] response = model.invoke(conversation)
python full = None # None | AIMessageChunk for chunk in model.stream('What color is the sky?'): full = chunk if full is None else full + chunk print(full.________)
python full = None # None | AIMessageChunk for chunk in model.stream('What color is the sky?'): full = chunk if full is None else full + chunk print(full.text)