In [ ]:
from langchain_google_genai import ChatGoogleGenerativeAI
model = ChatGoogleGenerativeAI(model="gemini-2.5-flash-lite")
response = model.invoke("What's the capital of the Moon?")
print(response.content)
Initialising and invoking an agent¶
Streaming Output¶
In [47]:
i = 1
for token, metadata in agent.stream(
{"messages": [HumanMessage(content="Tell me all about Luna City, the capital of the Moon")]},
stream_mode="messages"
):
if (i==1):
print(metadata, end="", flush=True)
print()
print("\n")
i += 1
if token.content: # Check if there's actual content
print(token.content, end="", flush=True) # Print token
{'langgraph_step': 1, 'langgraph_node': 'model', 'langgraph_triggers': ('branch:to:model',), 'langgraph_path': ('__pregel_pull', 'model'), 'langgraph_checkpoint_ns': 'model:ee3e5314-fe80-289e-2137-f6ef43df22a0', 'checkpoint_ns': 'model:ee3e5314-fe80-289e-2137-f6ef43df22a0', 'ls_provider': 'deepseek', 'ls_model_name': 'deepseek-v4-flash', 'ls_model_type': 'chat', 'ls_temperature': None, 'ls_max_tokens': 500, 'ls_integration': 'langchain_chat_model'}
This is a fantastic question, but it comes with an important clarification right at the start: **Luna City does not exist in reality.** There is no capital city on the Moon. The only human presence on the lunar surface has been a handful of robotic landers and the short-term stays of the Apollo astronauts from 1969 to 1972.
However, "Luna City" (or sometimes "Lunar City") is a classic and powerful concept in science fiction, representing humanity's next great leap. So, I'll
In [ ]:
for token, metadata in agent.stream(
{
"messages": [
{"role": "user", "content": "My name is Tony."},
{"role": "assistant", "content": "Nice to meet you, Tony."},
{"role": "user", "content": "What is my name? And list 10 things you can do for me"}
]
}
):
i = 1;
if (i==1):
print(metadata, end="", flush=True)
i++
if token.content: # Check if there's actual content
print(token.content, end="", flush=True) # Print token
In [56]:
from dotenv import load_dotenv
load_dotenv
import os,json
from pprint import pprint
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage, AIMessage
from langchain.agents import create_agent
DEEPSEEK_MODEL = "deepseek-v4-flash"
DEEPSEEK_BASE_URL = "https://api.deepseek.com"
def deepseek_model(model:str = DEEPSEEK_MODEL, max_tokens = 200, **kwargs):
return init_chat_model(
model = model,
model_provider = "deepseek",
max_tokens = max_tokens,
base_url = DEEPSEEK_BASE_URL,
api_key = os.environ["DEEPSEEK_API_KEY"],
**kwargs
)
model = deepseek_model(temperature = 1)
model
Out[56]:
ChatDeepSeek(client=<openai.resources.chat.completions.completions.Completions object at 0x112dc4850>, async_client=<openai.resources.chat.completions.completions.AsyncCompletions object at 0x112dc4650>, root_client=<openai.OpenAI object at 0x112d56050>, root_async_client=<openai.AsyncOpenAI object at 0x112dc4c50>, model_name='deepseek-v4-flash', temperature=1.0, model_kwargs={}, openai_api_key=SecretStr('**********'), openai_api_base='https://api.deepseek.com', max_tokens=500, api_key=SecretStr('**********'), api_base='https://api.deepseek.com/v1')
In [57]:
input = "what is 1+1?"
response = model.invoke(input)
response.content
Out[57]:
'The answer is 2.'
In [60]:
agent = create_agent(model)
messages = [
{"role":"user",
"content": "what is 1+1?"},
{"role":"assistant",
"content":"2"},
{"role":"user",
"content":"how about times 5?"}
]
user_input = {"messages":messages}
response = agent.invoke(user_input)
response
Out[60]:
{'messages': [HumanMessage(content='what is 1+1?', additional_kwargs={}, response_metadata={}, id='4e5f15e0-be3e-4f2f-8f30-1100429e414d'),
AIMessage(content='2', additional_kwargs={}, response_metadata={}, id='05e0206d-4417-4bff-baa3-819181ff20fa', tool_calls=[], invalid_tool_calls=[]),
HumanMessage(content='how about times 5?', additional_kwargs={}, response_metadata={}, id='25cd5f07-344f-4204-95a3-d80c1420b3ee'),
AIMessage(content='If you take the result of 1+1 (which is 2) and multiply it by 5, you get **10**.', additional_kwargs={'refusal': None, 'reasoning_content': 'We need to interpret the user\'s query: "how about times 5?" Given the previous exchange where user asked "what is 1+1?" and I answered "2", now user says "how about times 5?" Likely referring to the result of 1+1 (which is 2) multiplied by 5. So 2 * 5 = 10. Alternatively, could be asking (1+1) times 5, i.e., 2*5=10. So answer is 10.'}, response_metadata={'token_usage': {'completion_tokens': 138, 'prompt_tokens': 22, 'total_tokens': 160, 'completion_tokens_details': {'accepted_prediction_tokens': None, 'audio_tokens': None, 'reasoning_tokens': 109, 'rejected_prediction_tokens': None}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 22}, 'model_provider': 'deepseek', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '934adc85-0c5d-491d-849e-03a7a5dd44a8', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019e1fb8-b03b-7452-a7ec-27bdf102328c-0', tool_calls=[], invalid_tool_calls=[], usage_metadata={'input_tokens': 22, 'output_tokens': 138, 'total_tokens': 160, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 109}})]}
In [64]:
response['messages'][-1].content
Out[64]:
'If you take the result of 1+1 (which is 2) and multiply it by 5, you get **10**.'
In [65]:
len(response['messages'])
Out[65]:
4
In [66]:
for i in response['messages']:
print(i.content)
what is 1+1? 2 how about times 5? If you take the result of 1+1 (which is 2) and multiply it by 5, you get **10**.
In [77]:
messages1 = [{"role":"user","content":"Tell me all about Luna City, the capital of the Moon within 10 words"}];
user_input = {"messages":messages1}
for token, metadata in agent.stream(
user_input,
stream_mode="messages"
):
if token.content:
print(token.content, end="", flush=True)
Luna City is a fictional lunar capital featured in sci-fi.