import%20marimo%0A%0A__generated_with%20%3D%20%220.13.7%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Build%20Smarter%20Data%20Science%20Workflows%20with%20DeepSeek%20and%20LangChain%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Using%20DeepSeek%20Chat%20Models%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20DEEPSEEK_API_KEY%20%3D%20os.environ%5B%22DEEPSEEK_API_KEY%22%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20langchain_core.messages%20import%20HumanMessage%2C%20SystemMessage%0A%20%20%20%20from%20langchain_deepseek%20import%20ChatDeepSeek%0A%0A%20%20%20%20%23%20Initialize%20the%20chat%20model%0A%20%20%20%20llm%20%3D%20ChatDeepSeek(%0A%20%20%20%20%20%20%20%20model%3D%22deepseek-chat%22%2C%20%20%23%20Can%20also%20use%20%22deepseek-reasoner%22%0A%20%20%20%20%20%20%20%20temperature%3D0%2C%20%20%23%200%20for%20more%20deterministic%20responses%0A%20%20%20%20%20%20%20%20max_tokens%3DNone%2C%20%20%23%20None%20means%20model%20default%0A%20%20%20%20%20%20%20%20timeout%3DNone%2C%20%20%23%20API%20request%20timeout%0A%20%20%20%20%20%20%20%20max_retries%3D2%2C%20%20%23%20Retry%20failed%20requests%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Create%20a%20conversation%20with%20system%20and%20user%20messages%0A%20%20%20%20messages%20%3D%20%5B%0A%20%20%20%20%20%20%20%20SystemMessage(%0A%20%20%20%20%20%20%20%20%20%20%20%20content%3D%22You%20are%20a%20data%20scientist%20who%20writes%20efficient%20Python%20code%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20HumanMessage(%0A%20%20%20%20%20%20%20%20%20%20%20%20content%3D%22Given%20a%20DataFrame%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20%23%20Generate%20a%20response%0A%20%20%20%20response%20%3D%20llm.invoke(messages)%0A%20%20%20%20print(response.content)%0A%20%20%20%20return%20HumanMessage%2C%20SystemMessage%2C%20llm%2C%20messages%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22You%20can%20also%20use%20asynchronous%20operations%20for%20handling%20multiple%20requests%20without%20blocking%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Aasync%20def%20_(llm%2C%20messages)%3A%0A%20%20%20%20async%20def%20generate_async()%3A%0A%20%20%20%20%20%20%20%20response%20%3D%20await%20llm.ainvoke(messages)%0A%20%20%20%20%20%20%20%20return%20response.content%0A%0A%20%20%20%20%23%20In%20async%20context%0A%20%20%20%20async_result%20%3D%20await%20generate_async()%0A%20%20%20%20print(async_result)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Building%20Chains%20with%20DeepSeek%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(llm)%3A%0A%20%20%20%20from%20langchain_core.prompts%20import%20ChatPromptTemplate%0A%0A%20%20%20%20%23%20Create%20a%20structured%20prompt%20template%0A%20%20%20%20prompt%20%3D%20ChatPromptTemplate.from_messages(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22system%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22You%20are%20a%20data%20scientist%20who%20writes%20efficient%20%7Blanguage%7D%20code%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22human%22%2C%20%22%7Binput%7D%22)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Build%20the%20chain%0A%20%20%20%20chain%20%3D%20prompt%20%7C%20llm%0A%0A%20%20%20%20%23%20Execute%20the%20chain%0A%20%20%20%20result%20%3D%20chain.invoke(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22language%22%3A%20%22SQL%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22input%22%3A%20%22Given%20a%20table%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20print(result.content)%0A%20%20%20%20return%20ChatPromptTemplate%2C%20prompt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Streaming%20Responses%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(llm%2C%20prompt)%3A%0A%20%20%20%20from%20langchain_core.output_parsers%20import%20StrOutputParser%0A%0A%20%20%20%20streamed_chain%20%3D%20prompt%20%7C%20llm%20%7C%20StrOutputParser()%0A%0A%20%20%20%20for%20chunk%20in%20streamed_chain.stream(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22language%22%3A%20%22SQL%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22input%22%3A%20%22Given%20a%20table%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20print(chunk%2C%20end%3D%22%22%2C%20flush%3DTrue)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Structured%20Output%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ChatPromptTemplate%2C%20llm)%3A%0A%20%20%20%20from%20typing%20import%20List%0A%20%20%20%20from%20langchain_core.pydantic_v1%20import%20BaseModel%2C%20Field%0A%0A%20%20%20%20%23%20Define%20the%20output%20schema%0A%20%20%20%20class%20ApplicantProfile(BaseModel)%3A%0A%20%20%20%20%20%20%20%20first_name%3A%20str%0A%20%20%20%20%20%20%20%20last_name%3A%20str%0A%20%20%20%20%20%20%20%20experience_years%3A%20int%0A%20%20%20%20%20%20%20%20primary_skill%3A%20List%5Bstr%5D%0A%0A%20%20%20%20%23%20Bind%20the%20Pydantic%20model%20to%20the%20LLM%20for%20structured%20output%0A%20%20%20%20structured_llm%20%3D%20llm.with_structured_output(ApplicantProfile)%0A%0A%20%20%20%20%23%20Create%20a%20chain%0A%20%20%20%20prompt_structured%20%3D%20ChatPromptTemplate.from_messages(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22system%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22You%20are%20a%20helpful%20asssitant.%20Provide%20your%20output%20in%20the%20requested%20structured%20format.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22human%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Extract%20name%2C%20years%20of%20experience%2C%20and%20primary%20skill%20from%20%7Bjob_description%7D.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20chain_structured%20%3D%20prompt_structured%20%7C%20structured_llm%0A%0A%20%20%20%20%23%20Get%20structured%20output%0A%20%20%20%20job_description%20%3D%20%22Khuyen%20Tran%20is%20a%20data%20scientist%20with%205%20years%20of%20experience%2C%20skilled%20in%20Python%20and%20machine%20learning.%22%0A%20%20%20%20profile%20%3D%20chain_structured.invoke(%7B%22job_description%22%3A%20job_description%7D)%0A%20%20%20%20print(f%22First%20name%3A%20%7Bprofile.first_name%7D%22)%0A%20%20%20%20print(f%22Last%20name%3A%20%7Bprofile.last_name%7D%22)%0A%20%20%20%20print(f%22Years%20of%20experience%3A%20%7Bprofile.experience_years%7D%22)%0A%20%20%20%20print(f%22Primary%20skills%3A%20%7Bprofile.primary_skill%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Running%20DeepSeek%20Locally%20with%20Ollama%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(HumanMessage%2C%20SystemMessage)%3A%0A%20%20%20%20from%20langchain_ollama%20import%20ChatOllama%0A%0A%20%20%20%20local_deepseek_ollama%20%3D%20ChatOllama(%0A%20%20%20%20%20%20%20%20model%3D%22deepseek-r1%3A1.5b%22%2C%20temperature%3D0.7%2C%20base_url%3D%22http%3A%2F%2Flocalhost%3A11434%22%0A%20%20%20%20)%0A%20%20%20%20response_local%20%3D%20local_deepseek_ollama.invoke(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20SystemMessage(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20content%3D%22You%20are%20a%20data%20scientist%20who%20writes%20efficient%20Python%20code%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20HumanMessage(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20content%3D%22Given%20a%20DataFrame%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20print(%22Response%20from%20local%20DeepSeek%20(via%20Ollama)%3A%22)%0A%20%20%20%20print(response_local.content)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ff3f5bb979b1b927421143114774934e2bf1a5deeb6533943868f37821637fe4