Text input¶
DeepSeek API models: https://api-docs.deepseek.com/quick_start/pricing
In [1]:
from dotenv import load_dotenv
load_dotenv()
Out[1]:
True
In [2]:
import os
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
from langchain.messages import HumanMessage, AIMessage
from langchain.tools import tool
from typing import Dict, Any
from tavily import TavilyClient
from pprint import pprint
DEEPSEEK_MODEL = "deepseek-chat"
DEEPSEEK_BASE_URL = "https://api.deepseek.com"
def deepseek_model(model: str = DEEPSEEK_MODEL, max_tokens=1000, **kwargs):
return init_chat_model(
model=model,
# DeepSeek uses LangChain's OpenAI-compatible transport.
model_provider="openai",
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url=DEEPSEEK_BASE_URL,
max_tokens=max_tokens,
**kwargs,
)
In [3]:
agent = create_agent(
model=deepseek_model(),
system_prompt="You are a science fiction writer, create a capital city at the users request.",
)
In [ ]:
question = HumanMessage(content=[
{"type": "text", "text": "What is the capital of The Moon?"}
])
response = agent.invoke(
{"messages": [question]}
)
print(response['messages'][-1].content)
Image input¶
In [4]:
from ipywidgets import FileUpload
from IPython.display import display
uploader = FileUpload(accept='.png', multiple=False)
display(uploader)
FileUpload(value=(), accept='.png', description='Upload')
In [6]:
print(uploader.value)
({'name': 'i1.png', 'type': 'image/png', 'size': 5769586, 'content': <memory at 0x1184bd840>, 'last_modified': datetime.datetime(2026, 4, 22, 6, 29, 19, 414000, tzinfo=datetime.timezone.utc)},)
In [7]:
print(uploader.value[0])
{'name': 'i1.png', 'type': 'image/png', 'size': 5769586, 'content': <memory at 0x1184bd840>, 'last_modified': datetime.datetime(2026, 4, 22, 6, 29, 19, 414000, tzinfo=datetime.timezone.utc)}
In [ ]:
import base64
# Get the first (and only) uploaded file dict
uploaded_file = uploader.value[0]
# This is a memoryview
content_mv = uploaded_file["content"]
# Convert memoryview -> bytes
img_bytes = bytes(content_mv) # or content_mv.tobytes()
# Now base64 encode
img_b64 = base64.b64encode(img_bytes).decode("utf-8")
question = HumanMessage(content=[
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{img_b64}"}
}
])
In [ ]:
print(
"DeepSeek's current public chat API is text/tool-call oriented and does not expose "
"an image input model in the official API docs. Keep this upload/base64 cell for "
"practice, but use a multimodal provider if you need image understanding."
)
Audio input¶
In [ ]:
import sounddevice as sd
from scipy.io.wavfile import write
import base64
import io
import time
from tqdm import tqdm
# Recording settings
duration = 5 # seconds
sample_rate = 44100
print("Recording...")
audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1)
# Progress bar for the duration
for _ in tqdm(range(duration * 10)): # update 10× per second
time.sleep(0.1)
sd.wait()
print("Done.")
# Write WAV to an in-memory buffer
buf = io.BytesIO()
write(buf, sample_rate, audio)
wav_bytes = buf.getvalue()
aud_b64 = base64.b64encode(wav_bytes).decode("utf-8")
In [ ]:
print(
"DeepSeek's current public chat API is text/tool-call oriented and does not expose "
"an audio input model in the official API docs. Keep the recording cell for practice, "
"but use a multimodal audio provider if you need audio understanding."
)
In [ ]: