Skip to content

Langchain Notes

j-sawn edited this page Mar 19, 2025 · 5 revisions

Langchain Notes

Installation and Setup (MAIN)

  • Langchain can just be downloaded through pip
pip install langchain
  • When doing this in a docker container, remember run
source ./app/venv/bin/activate

Installation and Setup (PromptTemplate)

Testing Exercise

Requirements

  • You would need a Docker container that can run the Python Virtual Environment
  • You would also need a Docker container that has the following: (the instructions cover this but you can save time by using our modelworks containers from Week 4)
    • Ollama
    • Gradio
    • Langchain
  • You should be able to run two instances of Docker from two terminals

Part 1

Using your terminal run

ollama serve

Part 2

Then, in a new terminal, run the following:

docker exec modelworks-container bash

use cd to move to the dir you want to work in then run

python3 -m venv ./app/venv

source ./app/venv/bin/activate

pip install langchain langchain-community ollama gradio

touch ./app/app.py

open the app.py file and write the following inside (see below)

import gradio as gr
from langchain_community.llms import Ollama

# Load the DeepSeek R1 1.5B model in Ollama
llm = Ollama(model="deepseek-r1:1.5b")

# Function to interact with the model
def chat_with_model(prompt):
    response = llm.invoke(prompt)  # Invoke the model with user input
    return response

# Create a Gradio interface
demo = gr.Interface(
    fn=chat_with_model,
    inputs="text",
    outputs="text",
    title="DeepSeek R1 1.5B Chatbot",
    description="Chat with DeepSeek-R1:1.5B model running on Ollama using LangChain and Gradio."
)

# Launch Gradio app on a specific port
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)

save that then in the same directory run

python ./app/app.py

open a browser and go to http://localhost:7860.

The result should look like this: image

Useful Information

  • If you want to make the app.py file from the command line:
    • Type vi ./app/app.py
    • Press i to insert code
    • Copy in the necessary code
    • Press Esc, type :wq, then Enter
  • Otherwise, you might want to do the following if you want to test files that are already in your system but you are using a non-Linux device:
    • docker cp "[INSERT LOCAL PATH TO YOUR FILE YOU WANT TO RUN]" [INSERT CONTAINER NAME]:[INSERT DIRECTORY OF CONTAINER YOU WANT TO SAVE THE FILE IN]

Clone this wiki locally