Mastering LangChain: Open-Source Models & Prompt Engineering (Part 2) A technical tutorial on LangChain demonstrates integrating open-source models via Hugging Face and structuring production prompt templates, including code for connecting to hosted endpoints and local pipelines using models such as Mistral-7B-Instruct-v0.2 and Llama-2-7b-chat-hf. In Part 1, we established the high-level roadmap of LangChain — exploring the architectural shift toward chat models and breaking down the foundational RAG pipeline. Now, we dive directly into code implementation. Before assembling end-to-end chains or building multi-step agents, we must master two foundational pillars: integrating open-source models and structuring production prompt templates . While proprietary APIs like OpenAI or Anthropic offer convenience, open-source models provide full data privacy, fine-tuning flexibility, and infrastructure control. The largest central hub for discovering, testing, and downloading open-source LLMs is Hugging Face . When integrating these models into your LangChain stack, you have two primary options: For implementing the below-given codes, you need to get HuggingFace API keys. After you get the API key, copy it and store it in a .env file in the project folder where you will be storing all your LangChain tutorial program files. Please refer to the tutorial link on how to get HuggingFace API keys: https://www.geeksforgeeks.org/artificial-intelligence/how-to-access-huggingface-api-key/ https://www.geeksforgeeks.org/artificial-intelligence/how-to-access-huggingface-api-key/ Also, I have not provided the program outputs for this blog, since I want my readers to actually try the code snippets in their systems. Here is how you connect both hosted endpoints and local inference pipelines inside LangChain: python import osfrom langchain huggingface import HuggingFaceEndpoint, HuggingFacePipelinefrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline 1. Using Hugging Face Inference API Serverless / Dedicated Endpoint os.environ "HUGGINGFACEHUB API TOKEN" = "your hf api token"endpoint llm = HuggingFaceEndpoint repo id="mistralai/Mistral-7B-Instruct-v0.2", task="text-generation", max new tokens=256, temperature=0.2 2. Running Locally using Transformers Pipelinemodel id = "meta-llama/Llama-2-7b-chat-hf"tokenizer = AutoTokenizer.from pretrained model id model = AutoModelForCausalLM.from pretrained model id, device map="auto" pipe = pipeline "text-generation", model=model, tokenizer=tokenizer, max new tokens=256, temperature=0.3 local llm = HuggingFacePipeline pipeline=pipe A prompt is the input instruction or query provided to a language model to guide its output. In modern conversational architectures, prompts are categorized across distinct roles to establish context, personas, and system boundaries. Note: We will focus on the LCEL and the formation of chains in langchain in detail in the upcoming blogs. In this blog, I only want you to focus on the prompt part only. You can run the code snippets for now if you wish to do so. Unlike raw end-user prompts which are typed directly by users or system prompts which define high-level personas and safety guardrails , a developer prompt programmatically blends application logic, formatting constraints, and task rules with dynamic user data before sending the payload to the LLM. Implementation: Production-Grade Customer Support Triage This implementation demonstrates a developer instruction pipeline that ingests a raw customer support ticket, extracts a concise 2-sentence summary, classifies the urgency into strict enum levels LOW, MEDIUM & HIGH , and returns a validated Pydantic object python import osfrom langchain core.prompts import PromptTemplatefrom langchain core.output parsers import StrOutputParserfrom langchain openai import ChatOpenAI 1. Initialize modelmodel = ChatOpenAI model="gpt-4o-mini", temperature=0 2. Define developer instruction promptdeveloper prompt = PromptTemplate.from template """Summarize the following customer support ticket in exactly 2 sentences.Flag the urgency level strictly as LOW, MEDIUM, or HIGH.Ticket Details:{ticket text}Output Format:Summary: