Basic prompting¶
In [1]:
from dotenv import load_dotenv
load_dotenv()
Out[1]:
True
In [18]:
import os
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
from langchain.messages import HumanMessage
from pydantic import BaseModel
DEEPSEEK_MODEL = "deepseek-chat"
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="openai",
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url=DEEPSEEK_BASE_URL,
max_tokens = max_tokens,
**kwargs,
)
In [ ]:
agent = create_agent(model=deepseek_model())
In [ ]:
question = HumanMessage(content="What's the capital of the moon?")
response = agent.invoke(
{"messages": [question]}
)
print(response['messages'][1].content)
In [ ]:
system_prompt = "You are a science fiction writer, create a capital city at the users request."
scifi_agent = create_agent(
model=deepseek_model(temperature = 1),
system_prompt=system_prompt
)
response = scifi_agent.invoke(
{"messages": [question]}
)
print(response['messages'][1].content)
In [ ]:
for i in response['messages']:
print("")
print(i.content)
Few-shot examples¶
In [ ]:
system_prompt = """
You are a science fiction writer, create a space capital city at the users request.
User: What is the capital of mars?
Scifi Writer: Marsialis
User: What is the capital of Venus?
Scifi Writer: Venusovia
"""
scifi_agent = create_agent(
model=deepseek_model(),
system_prompt=system_prompt
)
response = scifi_agent.invoke(
{"messages": [question]}
)
print(response['messages'][1].content)
Structured prompts¶
In [ ]:
system_prompt = """
You are a science fiction writer, create a space capital city at the users request.
Please keep to the below structure.
Name: The name of the capital city
Location: Where it is based
Vibe: 2-3 words to describe its vibe
Economy: Main industries
"""
scifi_agent = create_agent(
model=deepseek_model(),
system_prompt=system_prompt
)
response = scifi_agent.invoke(
{"messages": [question]}
)
print(response['messages'][1].content)
Structured output¶
In [19]:
model = deepseek_model()
print(model)
client=<openai.resources.chat.completions.completions.Completions object at 0x13c431260> async_client=<openai.resources.chat.completions.completions.AsyncCompletions object at 0x13c430e20> root_client=<openai.OpenAI object at 0x13c431370> root_async_client=<openai.AsyncOpenAI object at 0x13c431150> model_name='deepseek-chat' model_kwargs={} openai_api_key=SecretStr('**********') openai_api_base='https://api.deepseek.com' max_tokens=200
In [20]:
response_model=model.invoke("what is 1+1?")
print(response_model.content)
1 + 1 = 2.
In [21]:
class CapitalInfo(BaseModel):
name: str
location: str
vibe: str
economy: str
agent = create_agent(
model=model,
system_prompt="You are a science fiction writer, create a capital city at the users request.",
response_format=CapitalInfo
)
question = HumanMessage(content="What is the capital of The Moon?")
response = agent.invoke(
{"messages": [question]}
)
response["structured_response"]
Out[21]:
CapitalInfo(name='Lunar Prime', location='Mare Imbrium, near the lunar equator', vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.')
In [38]:
print(response["structured_response"])
name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'
In [22]:
response["structured_response"].name
Out[22]:
'Lunar Prime'
In [54]:
[HumanMessage(content='What is the capital of The Moon?', additional_kwargs={}, response_metadata={}, id='331e6f86-9685-43a9-8e81-0839b8ee97a9'), AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0', tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}), ToolMessage(content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'", name='CapitalInfo', id='74264dc7-3811-488b-895f-d1ccbf2a32b5', tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755')]
In [72]:
from pprint import pprint
#print(response['messages'][1])
#print("")
#print(response['messages'][1].response_metadata)
#print(response['messages'][1].tool_calls)
print(type(response['messages'][1]))
#pprint(response['messages'][1].__dict__)
pprint(response['messages'][1].model_dump(), sort_dicts=False)
<class 'langchain_core.messages.ai.AIMessage'>
{'content': '',
'additional_kwargs': {'refusal': None},
'response_metadata': {'token_usage': {'completion_tokens': 166,
'prompt_tokens': 336,
'total_tokens': 502,
'completion_tokens_details': None,
'prompt_tokens_details': {'audio_tokens': None,
'cached_tokens': 0},
'prompt_cache_hit_tokens': 0,
'prompt_cache_miss_tokens': 336},
'model_provider': 'openai',
'model_name': 'deepseek-v4-flash',
'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402',
'id': '13a53314-195a-430c-8800-c94c3aa06826',
'finish_reason': 'tool_calls',
'logprobs': None},
'type': 'ai',
'name': None,
'id': 'lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0',
'tool_calls': [{'name': 'CapitalInfo',
'args': {'name': 'Lunar Prime',
'location': 'Mare Imbrium, near the lunar equator',
'vibe': 'A gleaming metropolis of pressurized domes '
'and towering spires, built into the rim of '
'a ancient impact crater. Citizens move '
'through transparent sky-bridges, and the '
'city hums with the soft thrum of artificial '
'gravity generators and air recyclers.',
'economy': 'Helium-3 mining exports, lunar tourism, '
'zero-gravity manufacturing, and the '
'headquarters of the Lunar Colonial '
'Authority.'},
'id': 'call_00_0Cs1R6mTFiU7QsDerh007755',
'type': 'tool_call'}],
'invalid_tool_calls': [],
'usage_metadata': {'input_tokens': 336,
'output_tokens': 166,
'total_tokens': 502,
'input_token_details': {'cache_read': 0},
'output_token_details': {}}}
In [55]:
print(response['messages'])
[HumanMessage(content='What is the capital of The Moon?', additional_kwargs={}, response_metadata={}, id='331e6f86-9685-43a9-8e81-0839b8ee97a9'), AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0', tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}), ToolMessage(content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'", name='CapitalInfo', id='74264dc7-3811-488b-895f-d1ccbf2a32b5', tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755')]
In [67]:
pprint(response['messages'])
[HumanMessage(content='What is the capital of The Moon?', additional_kwargs={}, response_metadata={}, id='331e6f86-9685-43a9-8e81-0839b8ee97a9'),
AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0', tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}),
ToolMessage(content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'", name='CapitalInfo', id='74264dc7-3811-488b-895f-d1ccbf2a32b5', tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755')]
In [68]:
for msg in response["messages"]:
pprint(msg)
print()
HumanMessage(content='What is the capital of The Moon?', additional_kwargs={}, response_metadata={}, id='331e6f86-9685-43a9-8e81-0839b8ee97a9')
AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0', tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})
ToolMessage(content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'", name='CapitalInfo', id='74264dc7-3811-488b-895f-d1ccbf2a32b5', tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755')
In [59]:
print(*response['messages'], sep="\n")
content='What is the capital of The Moon?' additional_kwargs={} response_metadata={} id='331e6f86-9685-43a9-8e81-0839b8ee97a9'
content='' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None} id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0' tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}] invalid_tool_calls=[] usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}
content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'" name='CapitalInfo' id='74264dc7-3811-488b-895f-d1ccbf2a32b5' tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755'
In [69]:
pprint(response)
{'messages': [HumanMessage(content='What is the capital of The Moon?', additional_kwargs={}, response_metadata={}, id='331e6f86-9685-43a9-8e81-0839b8ee97a9'),
AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 166, 'prompt_tokens': 336, 'total_tokens': 502, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 336}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '13a53314-195a-430c-8800-c94c3aa06826', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e2006-f9fe-7112-8db4-b972b1444ecb-0', tool_calls=[{'name': 'CapitalInfo', 'args': {'name': 'Lunar Prime', 'location': 'Mare Imbrium, near the lunar equator', 'vibe': 'A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', 'economy': 'Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'}, 'id': 'call_00_0Cs1R6mTFiU7QsDerh007755', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 336, 'output_tokens': 166, 'total_tokens': 502, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}),
ToolMessage(content="Returning structured response: name='Lunar Prime' location='Mare Imbrium, near the lunar equator' vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.' economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.'", name='CapitalInfo', id='74264dc7-3811-488b-895f-d1ccbf2a32b5', tool_call_id='call_00_0Cs1R6mTFiU7QsDerh007755')],
'structured_response': CapitalInfo(name='Lunar Prime', location='Mare Imbrium, near the lunar equator', vibe='A gleaming metropolis of pressurized domes and towering spires, built into the rim of a ancient impact crater. Citizens move through transparent sky-bridges, and the city hums with the soft thrum of artificial gravity generators and air recyclers.', economy='Helium-3 mining exports, lunar tourism, zero-gravity manufacturing, and the headquarters of the Lunar Colonial Authority.')}
In [39]:
capital_info = response["structured_response"]
capital_name = capital_info.name
capital_location = capital_info.location
print(f"{capital_name} is a city located at {capital_location}")
Lunar Prime is a city located at Mare Imbrium, near the lunar equator
In [ ]: