<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lokendra Singh on Medium]]></title>
        <description><![CDATA[Stories by Lokendra Singh on Medium]]></description>
        <link>https://medium.com/@lokendrasolanki60?source=rss-7210f3a23c97------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kRwB8hDbIf-HLgoUpcSNVA.jpeg</url>
            <title>Stories by Lokendra Singh on Medium</title>
            <link>https://medium.com/@lokendrasolanki60?source=rss-7210f3a23c97------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:07:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lokendrasolanki60/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Build a Gen AI App with the DeepSeek-R1 Model Using LangChain and Ollama]]></title>
            <link>https://medium.com/@lokendrasolanki60/build-a-gen-ai-app-with-the-deepseek-r1-model-using-langchain-and-ollama-6875b0321bd5?source=rss-7210f3a23c97------2</link>
            <guid isPermaLink="false">https://medium.com/p/6875b0321bd5</guid>
            <category><![CDATA[deepseek]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[ollama]]></category>
            <dc:creator><![CDATA[Lokendra Singh]]></dc:creator>
            <pubDate>Sat, 15 Feb 2025 15:38:00 GMT</pubDate>
            <atom:updated>2025-02-15T15:38:00.613Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*4EYGhD_2u3YHI7jTdc1gcQ.jpeg" /></figure><p>In this article, we will be building a GenAi app using the deepseek open-source model, which is capable of generating code and other stuff based on our requirements. In building this app, we will be using open-source tools like Langchain (a framework for AI workflows), Ollama, and DeepSeek-R1.</p><h3>Why are we using DeepSeek-R1, Langchain, and Ollama?</h3><ol><li><strong>DeepSeek-R1:</strong> A highly capable open-source LLM excelling in reasoning, coding, and creative tasks.</li><li><strong>Langchain: </strong>Simplifies chaining AI components (models, prompts, databases) into cohesive workflows.</li><li><strong>Ollama:</strong> It lets you run LLMs like DeepSeek-R1, Llama3.3, and Phi-4 locally on your machine, even without a GPU.</li></ol><h3>Step 1. Download Ollama from the below link</h3><ul><li><a href="https://ollama.com/download">https://ollama.com/download</a></li></ul><h3>Step 2. Now Download the DeepSeek-R1 Model with Ollama</h3><pre>ollama run deepseek-r1:1.5b</pre><h3>Step 3. Install the below library to run the model</h3><pre>pip install streamlit<br>pip install langchain_core<br>pip install langchain_community<br>pip install langchain_ollama</pre><p>Now let’s start writing code step-wise:</p><h3>1. LLM Initialization</h3><pre>llm_engine = ChatOllama(<br>    model=selected_model,<br>    base_url=&quot;http://localhost:11434&quot;,<br>    temperature=0.3,<br>    streaming=True<br>)</pre><ul><li><strong>ChatOllama: </strong>A client to interact with the local LLM hosted at localhost:11434.</li><li><strong>selected_model: </strong>The LLM you’ve chosen (e.g., DeepSeek).</li><li><strong>temperature=0.3:</strong> Controls randomness (0 = deterministic, 1 = creative).</li><li><strong>streaming=True: </strong>Enables real-time response streaming.</li></ul><h3>2. System Prompt Configuration</h3><pre>system_prompt = SystemMessagePromptTemplate.from_template(<br>    &quot;You are an expert AI coding assistant...&quot;<br>)</pre><ul><li>Defines the initial behavior of the AI (concise, correct, and with debugging tips).</li><li>Acts like giving context to the model before the conversation starts.</li></ul><h3>3. Session State Management</h3><pre>if &quot;message_log&quot; not in st.session_state:<br>    st.session_state.message_log = [{&quot;role&quot;: &quot;ai&quot;, &quot;content&quot;: &quot;Hi! I&#39;m DeepSeek...&quot;}]</pre><ul><li>st.session_state: Maintains chat history across interactions.</li><li>The first message is a greeting from the AI if no prior chat exists.</li></ul><h3>4. Display Chat History</h3><pre>def display_chat_history():<br>    for message in st.session_state.message_log:<br>        with st.chat_message(message[&quot;role&quot;]):<br>            st.markdown(message[&quot;content&quot;])</pre><ul><li>Iterates through all past messages (both user and AI).</li><li>Displays them with appropriate roles (user or AI) using st.chat_message().</li></ul><h3>5. Prompt Chain Building</h3><pre>def build_prompt_chain():<br>    prompt_sequence = [system_prompt]<br>    for msg in st.session_state.message_log:<br>        if msg[&quot;role&quot;] == &quot;user&quot;:<br>            prompt_sequence.append(HumanMessagePromptTemplate.from_template(msg[&quot;content&quot;]))<br>        elif msg[&quot;role&quot;] == &quot;ai&quot;:<br>            prompt_sequence.append(AIMessagePromptTemplate.from_template(msg[&quot;content&quot;]))<br>    return ChatPromptTemplate.from_messages(prompt_sequence)</pre><ul><li><strong>Initialization:</strong> The function starts by adding a system_prompt to the prompt sequence to set the AI’s behavior.</li><li><strong>Conversation History Processing: </strong>It loops through<strong> st.session_state.message_log</strong>, adding user messages with HumanMessagePromptTemplate and AI responses with AIMessagePromptTemplate.</li><li><strong>Prompt Chain Creation:</strong> Finally, it returns a ChatPromptTemplate containing the system prompt and full conversation history for context-aware AI responses.</li></ul><h3>6. Generating AI Response with Streaming</h3><pre>def generate_response():<br>    prompt_chain = build_prompt_chain()<br>    processing_pipeline = prompt_chain | llm_engine | StrOutputParser()<br>    return processing_pipeline.stream({})</pre><ul><li><strong>Chains together: </strong>Prompt, LLM, and Output Parser</li><li><strong>stream(): </strong>Streams the response chunk-by-chunk, enabling real-time typing effects.</li></ul><p>Below is the combined code in below code. I have added streamlit and html&amp;css code to see the the app on the UI.</p><pre>import streamlit as st<br>from langchain_ollama import ChatOllama<br>from langchain_core.output_parsers import StrOutputParser<br>from langchain_core.prompts import (<br>    SystemMessagePromptTemplate,<br>    HumanMessagePromptTemplate,<br>    AIMessagePromptTemplate,<br>    ChatPromptTemplate<br>)<br>import warnings<br>import sys<br># Custom CSS styling for the app <br>st.markdown(&quot;&quot;&quot;<br>&lt;style&gt;<br>    /* Existing styles */<br>    .main {<br>        background-color: #1a1a1a;<br>        color: #ffffff;<br>    }<br>    .sidebar .sidebar-content {<br>        background-color: #2d2d2d;<br>    }<br>    .stTextInput textarea {<br>        color: #ffffff !important;<br>    }<br>    /* Add these new styles for select box */<br>    .stSelectbox div[data-baseweb=&quot;select&quot;] {<br>        color: white !important;<br>        background-color: #3d3d3d !important;<br>    }<br>    .stSelectbox svg {<br>        fill: white !important;<br>    }<br>    .stSelectbox option {<br>        background-color: #2d2d2d !important;<br>        color: white !important;<br>    }<br>    /* For dropdown menu items */<br>    div[role=&quot;listbox&quot;] div {<br>        background-color: #2d2d2d !important;<br>        color: white !important;<br>    }<br>&lt;/style&gt;<br>&quot;&quot;&quot;, unsafe_allow_html=True)<br>st.title(&quot;🧠 DeepSeek Code Companion build by Lokendra&quot;)<br>st.caption(&quot;🚀 Your AI Pair Programmer with Debugging Superpowers&quot;)<br># Sidebar configuration<br>with st.sidebar:<br>    st.header(&quot;⚙️ Configuration&quot;)<br>    selected_model = st.selectbox(<br>        &quot;Choose Model&quot;,<br>        [&quot;deepseek-r1:1.5b&quot;, &quot;deepseek-r1:3b&quot;, &quot;deepseek-r1:7b&quot;, &quot;deepseek-r1:8b&quot;],<br>        index=0<br>    )<br>    st.divider()<br>    st.markdown(&quot;### Model Capabilities&quot;)<br>    st.markdown(&quot;&quot;&quot;<br>    - 🐍 Code genaration<br>    - 🐞 Debugging Assistant<br>    - 📝 Documentation<br>    &quot;&quot;&quot;)<br>    st.divider()<br>    st.markdown(&quot;### 🔗 Resources&quot;)<br>    st.markdown(&quot;&quot;&quot;<br>    - [Ollama Documentation](https://ollama.ai/)<br>    - [LangChain Guides](https://python.langchain.com/)<br>    - [DeepSeek GitHub](https://github.com/deepseek-ai)<br>    &quot;&quot;&quot;)<br># main code logic starts from hare<br># Initialize chat engine with selected model(Deepseek)<br>llm_engine = ChatOllama(<br>    model=selected_model,<br>    base_url=&quot;http://localhost:11434&quot;,<br>    temperature=0.3,<br>    streaming=True<br>)<br><br>system_prompt = SystemMessagePromptTemplate.from_template(<br>    &quot;You are an expert AI coding assistant. Provide concise, correct solutions &quot;<br>    &quot;with strategic print statements for debugging. Always respond in English.&quot;<br>)<br># Session state management<br>if &quot;message_log&quot; not in st.session_state:<br>    st.session_state.message_log = [{<br>        &quot;role&quot;: &quot;ai&quot;, <br>        &quot;content&quot;: &quot;Hi! I&#39;m DeepSeek. How can I help you code today? 💻&quot;<br>    }]<br># Display chat messages<br>def display_chat_history():<br>    for message in st.session_state.message_log:<br>        with st.chat_message(message[&quot;role&quot;]):<br>            st.markdown(message[&quot;content&quot;])<br># Build the prompt chain<br>def build_prompt_chain():<br>    prompt_sequence = [system_prompt]<br>    for msg in st.session_state.message_log:<br>        if msg[&quot;role&quot;] == &quot;user&quot;:<br>            prompt_sequence.append(HumanMessagePromptTemplate.from_template(msg[&quot;content&quot;]))<br>        elif msg[&quot;role&quot;] == &quot;ai&quot;:<br>            prompt_sequence.append(AIMessagePromptTemplate.from_template(msg[&quot;content&quot;]))<br>    return ChatPromptTemplate.from_messages(prompt_sequence)<br><br>def generate_response():<br>    prompt_chain = build_prompt_chain()<br>    processing_pipeline = prompt_chain | llm_engine | StrOutputParser()<br>    return processing_pipeline.stream({})<br><br>display_chat_history()<br><br>user_query = st.chat_input(&quot;Type your coding question here...&quot;)<br>if user_query:<br>   <br>    st.session_state.message_log.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_query})<br>   <br>    with st.chat_message(&quot;user&quot;):<br>        st.markdown(user_query)<br>   <br>    with st.chat_message(&quot;ai&quot;):<br>        response_placeholder = st.empty()<br>        response_placeholder.markdown(&#39;&lt;div class=&quot;typing-indicator&quot;&gt;Generating&lt;span class=&quot;dot&quot;&gt;&lt;/span&gt;&lt;span class=&quot;dot&quot;&gt;&lt;/span&gt;&lt;span class=&quot;dot&quot;&gt;&lt;/span&gt;&lt;/div&gt;&#39;, unsafe_allow_html=True)<br> <br>    full_response = &quot;&quot;<br>    for chunk in generate_response():<br>        full_response += chunk<br>        response_placeholder.markdown(full_response + &quot;▌&quot;)<br>    # Update final response<br>    response_placeholder.markdown(full_response)<br>    # Add assistant response to history<br>    st.session_state.message_log.append({&quot;role&quot;: &quot;ai&quot;, &quot;content&quot;: full_response})</pre><p>To run the above code, you have to use the below command:</p><pre>streamlit run app.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U5zTqw91aqXcv87xnNEOWg.png" /><figcaption>example image</figcaption></figure><p>Now you are ready to ask any type of question from your genAi application without using the internet like below:</p><h3>Conclusion</h3><p>By using DeepSeek-R1, Langchain, and Ollama, we’ve built a powerful Gen AI application capable of generating intelligent responses. This architecture can be extended to support complex workflows, real-time data processing, and advanced NLP tasks.</p><p>For the code for this article, you can check out my <a href="https://github.com/lokendrasolanki/GenAi-with-Deepseek-r1">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6875b0321bd5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Downgrade and Upgrade python version on windows using pyenv Version Manager]]></title>
            <link>https://medium.com/@lokendrasolanki60/downgrade-and-upgrade-python-version-on-windows-using-pyenv-version-manager-a5dda7631624?source=rss-7210f3a23c97------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5dda7631624</guid>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Lokendra Singh]]></dc:creator>
            <pubDate>Sun, 05 May 2024 21:11:16 GMT</pubDate>
            <atom:updated>2024-05-05T21:13:51.833Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*zIRb1qgmAE3BxWaJ-9fS6Q.jpeg" /></figure><p><a href="https://github.com/pyenv/pyenv">pyenv</a> lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.</p><p>First of all, you have to install pyenv in your window system to install it i am using pip.</p><pre># check python version<br>python --version<br># Python 3.12.0<br><br># Install pyenv using pip<br>pip install pyenv-win <br> <br># Set environment variables<br>pyenv init</pre><p>I have python 3.12.0 version in my system i wanted to downgrade it into python 3.10.0 for switching version,</p><pre># Switch python version<br>pyenv global 3.10.0<br><br># check python version<br>python --version<br># Python 3.10.0</pre><p>Same way we can also upgrade python version by using pyenv.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5dda7631624" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get duplicate records from spark dataframe — pyspark]]></title>
            <link>https://medium.com/@lokendrasolanki60/get-duplicate-records-from-spark-dataframe-pyspark-4c401771cc2?source=rss-7210f3a23c97------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c401771cc2</guid>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[pyspark]]></category>
            <dc:creator><![CDATA[Lokendra Singh]]></dc:creator>
            <pubDate>Sun, 16 Apr 2023 20:42:42 GMT</pubDate>
            <atom:updated>2023-04-16T20:42:42.204Z</atom:updated>
            <content:encoded><![CDATA[<h3>Get duplicate records from spark dataframe — pyspark</h3><p>This is the most common requirement when you are working with CDC data to get duplicate record according to natural keys to find out CDC records.</p><p>Let&#39;s took some data and create a spark dataframe.</p><pre>data = [(&#39;EMP001&#39;,&#39;Loki&#39;,&#39;CS&#39;,&#39;M&#39;,30000,&#39;N&#39;),<br>        (&#39;EMP001&#39;,&#39;Loki&#39;,&#39;IT&#39;,&#39;M&#39;,40000,&#39;Y&#39;),<br>      (&#39;EMP002&#39;,&#39;Robert&#39;,&#39;EE&#39;,&#39;M&#39;,40000,&#39;Y&#39;),<br>         (&#39;EMP003&#39;,&#39;william&#39;,&#39;IT&#39;,&#39;M&#39;,40000,&#39;Y&#39;),<br>         (&#39;EMP004&#39;,&#39;Rana&#39;,&#39;ME&#39;,&#39;M&#39;,230000,&#39;Y&#39;),<br>        (&#39;EMP005&#39;,&#39;Luke&#39;,&#39;IT&#39;,&#39;M&#39;,55000,&#39;N&#39;),<br>        (&#39;EMP005&#39;,&#39;Luke&#39;,&#39;EC&#39;,&#39;M&#39;,60000,&#39;Y&#39;),]<br><br>columns = [&quot;empid&quot;,&quot;name&quot;,&quot;department&quot;,&quot;gender&quot;,&quot;salary&quot;,&quot;current_version&quot;]<br>df1 = spark.createDataFrame(data=data, schema = columns)</pre><blockquote>Let’s take a look in to the data in data we have natural keys which is <strong>empid </strong>and <strong>name </strong>and there us one more column which is maintaining the version of data which is <strong>current_version</strong>.</blockquote><p>We have to identify that in our data which records and are duplicate and also we will get CDC records from a dataframe. It will work in both cases.</p><pre>import pyspark.sql.functions as F<br>natural_key_cols = [&quot;empid&quot;,&quot;name&quot;]<br><br>duplicated_natural_keys = df1.<br>groupby(natural_key_cols).<br>count().<br>filter(F.col(&#39;count&#39;) &gt; 1).<br>drop(F.col(&#39;count&#39;))<br><br>df1.join(F.broadcast(duplicated_natural_keys), natural_key_cols).<br>orderBy(natural_key_cols).display()</pre><p>In Above code we can se first we have to findout duplicate natural keys which we can get using group with those natural keys. After that we will join duplicate key dataframe with our actual dataframe and we will get our result as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*fwziB5SNBnff-gfRE5qJbw.png" /></figure><p>You can find above code on <a href="https://github.com/lokendrasolanki/duplicate_record_pyspark/blob/main/duplicate_record.py">github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c401771cc2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>