Introduction
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. For the code for this article, you can check out my GitHub.
Why are we using DeepSeek-R1, Langchain, and Ollama?
- DeepSeek-R1: A highly capable open-source LLM excelling in reasoning, coding, and creative tasks.
- Langchain: Simplifies chaining AI components (models, prompts, databases) into cohesive workflows.
- Ollama: It lets you run LLMs like DeepSeek-R1, Llama3.3, and Phi-4 locally on your machine, even without a GPU.
Step 1. Download Ollama from the below link
Step 2. Now Download the DeepSeek-R1 Model with Ollama
Step 3. Install the below library to run the model
Now let's start writing code step-wise:
1. LLM Initialization
- ChatOllama: A client to interact with the local LLM hosted at localhost:11434.
- selected_model: The LLM you’ve chosen (e.g., DeepSeek).
- temperature=0.3: Controls randomness (0 = deterministic, 1 = creative).
- streaming=True: Enables real-time response streaming.
2. System Prompt Configuration
- Defines the initial behavior of the AI (concise, correct, and with debugging tips).
- Acts like giving context to the model before the conversation starts.
3. Session State Management
- st.session_state: Maintains chat history across interactions.
- The first message is a greeting from the AI if no prior chat exists.
4. Display Chat History
- Iterates through all past messages (both user and AI).
- Displays them with appropriate roles (user or AI) using st.chat_message().
5. Prompt Chain Building
- Initialization: The function starts by adding a system_prompt to the prompt sequence to set the AI's behavior.
- Conversation History Processing: It loops through st.session_state.message_log, adding user messages with HumanMessagePromptTemplate and AI responses with AIMessagePromptTemplate.
- Prompt Chain Creation: Finally, it returns a ChatPromptTemplate containing the system prompt and full conversation history for context-aware AI responses.
6. Generating AI Response with Streaming
- Chains together: Prompt, LLM, and Output Parser
- stream(): Streams the response chunk-by-chunk, enabling real-time typing effects.
Below is the combined code in below code. I have added streamlit and html&css code to see the the app on the UI.
To run the above code, you have to use the below command:
Now you are ready to ask any type of question from your genAi application without using the internet like below:
![Localhost]()
Conclusion
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.