├── LICENSE ├── Langchian Fundamentals ├── 02 Retrieval Augmented Generation │ ├── 02 Document Loaders and Splitters │ │ └── document_loaders_splitters.py │ ├── 01 Vector Stores │ │ └── vector_stores.py │ ├── 03 RAG Pipeline │ │ └── rag_pipeline.py │ ├── 04 Embedding Models │ │ └── embedding_models.py │ └── README.md ├── 01 Core Components │ ├── 02 Prompts │ │ └── prompts.py │ ├── 03 Memory │ │ └── memory.py │ ├── 04 Document Loaders │ │ └── document_loaders.py │ ├── 01 Chains │ │ └── chains.py │ └── README.md ├── 03 AI Agents │ ├── 02 Agent Types │ │ └── agent_types.py │ ├── 03 Agent Reasoning │ │ └── agent_reasoning.py │ ├── 04 Custom Agents │ │ └── custom_agents.py │ ├── 01 Tool Integration │ │ └── tool_integration.py │ └── README.md ├── 04 Advanced Features │ ├── 04 Integration with APIs │ │ └── api_integration.py │ ├── 02 Evaluation Metrics │ │ └── evaluation_metrics.py │ ├── 03 Agent Optimization │ │ └── agent_optimization.py │ ├── 01 Custom Chains │ │ └── custom_chains.py │ └── README.md └── 05 Retail Applications │ ├── 02 Recommendation Systems │ └── recommendation_systems.py │ ├── 03 Review Analysis │ └── review_analysis.py │ ├── 01 Chatbots │ └── chatbots.py │ ├── 04 Query Answering │ └── query_answering.py │ └── README.md ├── README.md └── Langchain Interview Questions └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Langchian Fundamentals/02 Retrieval Augmented Generation/02 Document Loaders and Splitters/document_loaders_splitters.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Document Loaders and Splitters] 2 | # Learn to process and split retail documents with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib pandas nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.text_splitter import CharacterTextSplitter 8 | import numpy as np 9 | import nltk 10 | 11 | def run_document_loaders_splitters_demo(): 12 | # %% [2. Synthetic Retail Document Data] 13 | document = Document( 14 | page_content="TechCorp Laptop Manual: The laptop features a 16GB RAM, Intel i7 processor, and 512GB SSD. It has a vibrant 15-inch display and a 10-hour battery life. Ideal for professionals and gamers.", 15 | metadata={"product": "TechCorp Laptop"} 16 | ) 17 | print("Synthetic Data: Retail document created") 18 | print(f"Document: {document.metadata['product']} - {document.page_content[:50]}...") 19 | 20 | # %% [3. Document Splitting] 21 | text_splitter = CharacterTextSplitter(chunk_size=50, chunk_overlap=10, separator=".") 22 | chunks = text_splitter.split_documents([document]) 23 | print("Document Splitter: Document chunks created") 24 | for i, chunk in enumerate(chunks): 25 | print(f"Chunk {i+1}: {chunk.page_content}") 26 | 27 | # %% [4. Visualization] 28 | chunk_lengths = [len(nltk.word_tokenize(chunk.page_content)) for chunk in chunks] 29 | plt.figure(figsize=(8, 4)) 30 | plt.bar(range(1, len(chunks) + 1), chunk_lengths, color='green') 31 | plt.title("Document Chunk Lengths") 32 | plt.xlabel("Chunk") 33 | plt.ylabel("Word Count") 34 | plt.savefig("document_loaders_splitters_output.png") 35 | print("Visualization: Chunk lengths saved as document_loaders_splitters_output.png") 36 | 37 | # %% [5. Interview Scenario: Document Loaders and Splitters] 38 | """ 39 | Interview Scenario: Document Loaders and Splitters 40 | Q: How do document splitters optimize RAG? 41 | A: Splitters break large documents into smaller chunks for efficient retrieval and processing by LLMs. 42 | Key: Balances chunk size with context retention. 43 | Example: CharacterTextSplitter(chunk_size=50, chunk_overlap=10) 44 | """ 45 | 46 | # Execute the demo 47 | if __name__ == "__main__": 48 | nltk.download('punkt', quiet=True) 49 | run_document_loaders_splitters_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/01 Core Components/02 Prompts/prompts.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Prompts] 2 | # Learn dynamic prompt engineering for retail queries with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.prompts import PromptTemplate 7 | from langchain.chains import LLMChain 8 | from langchain.llms import OpenAI 9 | import numpy as np 10 | import nltk 11 | 12 | def run_prompts_demo(): 13 | # %% [2. Synthetic Retail Query Data] 14 | queries = [ 15 | {"product": "TechCorp laptop", "question": "What are its features?"}, 16 | {"product": "TechCorp smartphone", "question": "How long is the battery life?"}, 17 | {"product": "TechCorp tablet", "question": "Is it good for students?"} 18 | ] 19 | print("Synthetic Data: Retail customer queries created") 20 | print(f"Queries: {queries}") 21 | 22 | # %% [3. Dynamic Prompt Engineering] 23 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 24 | prompt = PromptTemplate( 25 | input_variables=["product", "question"], 26 | template="You are a retail assistant. For the product {product}, answer: {question}" 27 | ) 28 | chain = LLMChain(llm=llm, prompt=prompt) 29 | 30 | responses = [chain.run(product=query["product"], question=query["question"]) for query in queries] 31 | print("Dynamic Prompts: Responses generated") 32 | for i, (query, response) in enumerate(zip(queries, responses)): 33 | print(f"Query {i+1}: {query['product']} - {query['question']}") 34 | print(f"Response: {response.strip()}") 35 | 36 | # %% [4. Visualization] 37 | response_lengths = [len(nltk.word_tokenize(resp)) for resp in responses] 38 | plt.figure(figsize=(8, 4)) 39 | plt.bar(range(1, len(queries) + 1), response_lengths, color='blue') 40 | plt.title("Prompt Response Lengths") 41 | plt.xlabel("Query") 42 | plt.ylabel("Word Count") 43 | plt.savefig("prompts_output.png") 44 | print("Visualization: Response lengths saved as prompts_output.png") 45 | 46 | # %% [5. Interview Scenario: Prompts] 47 | """ 48 | Interview Scenario: Prompts 49 | Q: What’s the role of prompt engineering in LangChain? 50 | A: Prompt engineering designs structured inputs to guide LLM responses, using templates for dynamic, context-specific queries. 51 | Key: Improves response relevance and consistency. 52 | Example: PromptTemplate(input_variables=["product", "question"], template="...") 53 | """ 54 | 55 | # Execute the demo 56 | if __name__ == "__main__": 57 | nltk.download('punkt', quiet=True) 58 | run_prompts_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/02 Retrieval Augmented Generation/01 Vector Stores/vector_stores.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Vector Stores] 2 | # Learn Faiss-based document retrieval for retail applications with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai faiss-cpu numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.vectorstores import FAISS 8 | from langchain.embeddings import OpenAIEmbeddings 9 | from sklearn.metrics.pairwise import cosine_similarity 10 | import numpy as np 11 | 12 | def run_vector_stores_demo(): 13 | # %% [2. Synthetic Retail Document Data] 14 | documents = [ 15 | Document(page_content="TechCorp Laptop: 16GB RAM, Intel i7, 512GB SSD.", metadata={"product": "Laptop"}), 16 | Document(page_content="TechCorp Smartphone: Long battery, vibrant display.", metadata={"product": "Smartphone"}), 17 | Document(page_content="TechCorp Tablet: Lightweight, 10-hour battery.", metadata={"product": "Tablet"}) 18 | ] 19 | query = "Find a laptop with good performance." 20 | print("Synthetic Data: Retail documents and query created") 21 | print(f"Documents: {[doc.metadata['product'] for doc in documents]}") 22 | print(f"Query: {query}") 23 | 24 | # %% [3. Faiss Vector Store] 25 | embeddings = OpenAIEmbeddings(api_key="your-openai-api-key") # Replace with your OpenAI API key 26 | vector_store = FAISS.from_documents(documents, embeddings) 27 | retrieved_docs = vector_store.similarity_search(query, k=2) 28 | print("Vector Store: Documents retrieved") 29 | for i, doc in enumerate(retrieved_docs): 30 | print(f"Retrieved {i+1}: {doc.metadata['product']} - {doc.page_content}") 31 | 32 | # %% [4. Visualization] 33 | query_embedding = embeddings.embed_query(query) 34 | doc_embeddings = [embeddings.embed_query(doc.page_content) for doc in documents] 35 | similarities = [cosine_similarity([query_embedding], [emb])[0][0] for emb in doc_embeddings] 36 | 37 | plt.figure(figsize=(8, 4)) 38 | plt.bar([doc.metadata['product'] for doc in documents], similarities, color='blue') 39 | plt.title("Document Similarity to Query") 40 | plt.xlabel("Product") 41 | plt.ylabel("Cosine Similarity") 42 | plt.savefig("vector_stores_output.png") 43 | print("Visualization: Document similarities saved as vector_stores_output.png") 44 | 45 | # %% [5. Interview Scenario: Vector Stores] 46 | """ 47 | Interview Scenario: Vector Stores 48 | Q: What’s the role of vector stores in RAG? 49 | A: Vector stores index document embeddings for efficient similarity-based retrieval, enhancing LLM responses. 50 | Key: Faiss enables fast nearest-neighbor search. 51 | Example: FAISS.from_documents(documents, embeddings) 52 | """ 53 | 54 | # Execute the demo 55 | if __name__ == "__main__": 56 | run_vector_stores_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/03 AI Agents/02 Agent Types/agent_types.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Agent Types] 2 | # Learn reactive, planning, and ReAct agents for retail tasks with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | from collections import Counter 9 | import numpy as np 10 | 11 | def run_agent_types_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | query = "Handle a customer request for TechCorp laptop stock and discount." 14 | print("Synthetic Data: Retail query created") 15 | print(f"Query: {query}") 16 | 17 | # %% [3. Agent Types Comparison] 18 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 19 | 20 | def mock_stock_check(product): 21 | return f"Stock for {product}: 10 units." 22 | 23 | def mock_discount_calculator(product): 24 | return f"Discount for {product}: 15% off." 25 | 26 | tools = [ 27 | Tool(name="StockCheck", func=mock_stock_check, description="Check product stock"), 28 | Tool(name="DiscountCalculator", func=mock_discount_calculator, description="Calculate product discount") 29 | ] 30 | 31 | # Reactive Agent (Zero-Shot ReAct) 32 | reactive_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 33 | reactive_response = reactive_agent.run(query) 34 | 35 | # Planning Agent (Simulated with more steps) 36 | planning_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 37 | planning_response = planning_agent.run(f"Plan and execute: {query}") 38 | 39 | print("Agent Types: Responses generated") 40 | print(f"Reactive Agent Response: {reactive_response}") 41 | print(f"Planning Agent Response: {planning_response}") 42 | 43 | # %% [4. Visualization] 44 | response_lengths = [ 45 | len(reactive_response.split()), 46 | len(planning_response.split()) 47 | ] 48 | plt.figure(figsize=(8, 4)) 49 | plt.bar(['Reactive', 'Planning'], response_lengths, color=['blue', 'green']) 50 | plt.title("Agent Response Lengths by Type") 51 | plt.xlabel("Agent Type") 52 | plt.ylabel("Word Count") 53 | plt.savefig("agent_types_output.png") 54 | print("Visualization: Response lengths saved as agent_types_output.png") 55 | 56 | # %% [5. Interview Scenario: Agent Types] 57 | """ 58 | Interview Scenario: Agent Types 59 | Q: What’s the difference between reactive and planning agents? 60 | A: Reactive agents respond directly to queries, while planning agents break tasks into steps for complex scenarios. 61 | Key: ReAct combines reasoning and action. 62 | Example: initialize_agent(tools, llm, agent="zero-shot-react-description") 63 | """ 64 | 65 | # Execute the demo 66 | if __name__ == "__main__": 67 | run_agent_types_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/03 AI Agents/03 Agent Reasoning/agent_reasoning.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Agent Reasoning] 2 | # Learn autonomous decision-making for customer support with LangChain agents. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | from collections import Counter 9 | import numpy as np 10 | 11 | def run_agent_reasoning_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | queries = [ 14 | "Customer asks if TechCorp laptop is in stock.", 15 | "Customer wants a discount on TechCorp smartphone.", 16 | "Customer needs help with TechCorp tablet warranty." 17 | ] 18 | print("Synthetic Data: Retail customer queries created") 19 | print(f"Queries: {queries}") 20 | 21 | # %% [3. Agent Reasoning] 22 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 23 | 24 | def mock_stock_check(query): 25 | return "Stock: 10 units available." 26 | 27 | def mock_discount_offer(query): 28 | return "Offer: 15% discount applied." 29 | 30 | def mock_warranty_info(query): 31 | return "Warranty: 1-year coverage." 32 | 33 | tools = [ 34 | Tool(name="StockCheck", func=mock_stock_check, description="Check product stock"), 35 | Tool(name="DiscountOffer", func=mock_discount_offer, description="Offer a discount"), 36 | Tool(name="WarrantyInfo", func=mock_warranty_info, description="Provide warranty details") 37 | ] 38 | 39 | agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 40 | responses = [agent.run(query) for query in queries] 41 | print("Agent Reasoning: Responses generated") 42 | for i, (query, response) in enumerate(zip(queries, responses)): 43 | print(f"Query {i+1}: {query}") 44 | print(f"Response: {response}") 45 | 46 | # %% [4. Visualization] 47 | tool_calls = [response.split(":")[0].split()[-1] for response in responses] 48 | tool_counts = Counter(tool_calls) 49 | 50 | plt.figure(figsize=(8, 4)) 51 | plt.bar(tool_counts.keys(), tool_counts.values(), color='purple') 52 | plt.title("Agent Decision Tool Usage") 53 | plt.xlabel("Tool") 54 | plt.ylabel("Count") 55 | plt.savefig("agent_reasoning_output.png") 56 | print("Visualization: Tool usage saved as agent_reasoning_output.png") 57 | 58 | # %% [5. Interview Scenario: Agent Reasoning] 59 | """ 60 | Interview Scenario: Agent Reasoning 61 | Q: How does agent reasoning work for retail tasks? 62 | A: Agents reason by selecting tools based on query context, using LLMs to plan actions autonomously. 63 | Key: ReAct framework enhances decision-making. 64 | Example: initialize_agent(tools, llm, agent="zero-shot-react-description") 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_agent_reasoning_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/03 AI Agents/04 Custom Agents/custom_agents.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Custom Agents] 2 | # Learn to build retail-specific agents with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | from collections import Counter 9 | import numpy as np 10 | 11 | def run_custom_agents_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | queries = [ 14 | "Check inventory for TechCorp laptop.", 15 | "Suggest a product for a student.", 16 | "Process a return for TechCorp smartphone." 17 | ] 18 | print("Synthetic Data: Retail queries created") 19 | print(f"Queries: {queries}") 20 | 21 | # %% [3. Custom Agent for Retail] 22 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 23 | 24 | def mock_inventory_check(query): 25 | return "Inventory: 10 laptops available." 26 | 27 | def mock_product_suggestion(query): 28 | return "Suggestion: TechCorp Tablet, ideal for students." 29 | 30 | def mock_return_process(query): 31 | return "Return: Processed for TechCorp smartphone." 32 | 33 | tools = [ 34 | Tool(name="InventoryCheck", func=mock_inventory_check, description="Check product inventory"), 35 | Tool(name="ProductSuggestion", func=mock_product_suggestion, description="Suggest a product"), 36 | Tool(name="ReturnProcess", func=mock_return_process, description="Process a product return") 37 | ] 38 | 39 | custom_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 40 | responses = [custom_agent.run(query) for query in queries] 41 | print("Custom Agent: Responses generated") 42 | for i, (query, response) in enumerate(zip(queries, responses)): 43 | print(f"Query {i+1}: {query}") 44 | print(f"Response: {response}") 45 | 46 | # %% [4. Visualization] 47 | tool_calls = [response.split(":")[0].split()[-1] for response in responses] 48 | tool_counts = Counter(tool_calls) 49 | 50 | plt.figure(figsize=(8, 4)) 51 | plt.bar(tool_counts.keys(), tool_counts.values(), color='orange') 52 | plt.title("Custom Agent Tool Usage") 53 | plt.xlabel("Tool") 54 | plt.ylabel("Count") 55 | plt.savefig("custom_agents_output.png") 56 | print("Visualization: Tool usage saved as custom_agents_output.png") 57 | 58 | # %% [5. Interview Scenario: Custom Agents] 59 | """ 60 | Interview Scenario: Custom Agents 61 | Q: How do you build a custom agent for retail? 62 | A: Define task-specific tools and initialize an agent with a reasoning framework like ReAct for retail scenarios. 63 | Key: Tailor tools to domain needs. 64 | Example: initialize_agent(tools=[Tool(name="InventoryCheck", ...)], llm, ...) 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_custom_agents_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/04 Advanced Features/04 Integration with APIs/api_integration.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Integration with APIs] 2 | # Learn to connect LangChain with retail APIs for task automation. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | from collections import Counter 9 | import numpy as np 10 | 11 | def run_api_integration_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | queries = [ 14 | "Check TechCorp laptop price via API.", 15 | "Fetch TechCorp smartphone specs via API.", 16 | "Get TechCorp tablet availability via API." 17 | ] 18 | print("Synthetic Data: Retail queries created") 19 | print(f"Queries: {queries}") 20 | 21 | # %% [3. API Integration] 22 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 23 | 24 | def mock_price_api(query): 25 | return "Price: $999 for TechCorp laptop." 26 | 27 | def mock_specs_api(query): 28 | return "Specs: 8GB RAM, 128GB storage for TechCorp smartphone." 29 | 30 | def mock_availability_api(query): 31 | return "Availability: In stock for TechCorp tablet." 32 | 33 | tools = [ 34 | Tool(name="PriceAPI", func=mock_price_api, description="Fetch product price via API"), 35 | Tool(name="SpecsAPI", func=mock_specs_api, description="Fetch product specs via API"), 36 | Tool(name="AvailabilityAPI", func=mock_availability_api, description="Check product availability via API") 37 | ] 38 | 39 | agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 40 | responses = [agent.run(query) for query in queries] 41 | print("API Integration: Responses generated") 42 | for i, (query, response) in enumerate(zip(queries, responses)): 43 | print(f"Query {i+1}: {query}") 44 | print(f"Response: {response}") 45 | 46 | # %% [4. Visualization] 47 | api_calls = [response.split(":")[0].split()[-1] for response in responses] 48 | api_counts = Counter(api_calls) 49 | 50 | plt.figure(figsize=(8, 4)) 51 | plt.bar(api_counts.keys(), api_counts.values(), color='blue') 52 | plt.title("API Call Frequencies") 53 | plt.xlabel("API") 54 | plt.ylabel("Count") 55 | plt.savefig("api_integration_output.png") 56 | print("Visualization: API call frequencies saved as api_integration_output.png") 57 | 58 | # %% [5. Interview Scenario: Integration with APIs] 59 | """ 60 | Interview Scenario: Integration with APIs 61 | Q: How does LangChain integrate with external APIs? 62 | A: LangChain agents use tools to call APIs, enabling dynamic data retrieval for retail tasks. 63 | Key: Tools map queries to API functions. 64 | Example: Tool(name="PriceAPI", func=mock_price_api, description="...") 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_api_integration_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/03 AI Agents/01 Tool Integration/tool_integration.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Tool Integration] 2 | # Learn to integrate tools for retail tasks with LangChain agents. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | from collections import Counter 9 | import numpy as np 10 | 11 | def run_tool_integration_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | queries = [ 14 | "Check stock for TechCorp laptop.", 15 | "Calculate discount for TechCorp smartphone.", 16 | "Search for TechCorp tablet reviews." 17 | ] 18 | print("Synthetic Data: Retail queries created") 19 | print(f"Queries: {queries}") 20 | 21 | # %% [3. Tool Integration] 22 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 23 | 24 | def mock_stock_check(product): 25 | return f"Stock for {product}: 10 units available." 26 | 27 | def mock_discount_calculator(product): 28 | return f"Discount for {product}: 15% off." 29 | 30 | def mock_search_reviews(product): 31 | return f"Reviews for {product}: Mostly positive, 4.5/5 rating." 32 | 33 | tools = [ 34 | Tool(name="StockCheck", func=mock_stock_check, description="Check product stock"), 35 | Tool(name="DiscountCalculator", func=mock_discount_calculator, description="Calculate product discount"), 36 | Tool(name="SearchReviews", func=mock_search_reviews, description="Search product reviews") 37 | ] 38 | 39 | agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) 40 | responses = [agent.run(query) for query in queries] 41 | print("Tool Integration: Agent responses generated") 42 | for i, (query, response) in enumerate(zip(queries, responses)): 43 | print(f"Query {i+1}: {query}") 44 | print(f"Response: {response}") 45 | 46 | # %% [4. Visualization] 47 | tool_calls = [response.split(":")[0].split()[-1] for response in responses] # Extract tool name 48 | tool_counts = Counter(tool_calls) 49 | 50 | plt.figure(figsize=(8, 4)) 51 | plt.bar(tool_counts.keys(), tool_counts.values(), color='blue') 52 | plt.title("Tool Call Frequencies") 53 | plt.xlabel("Tool") 54 | plt.ylabel("Count") 55 | plt.savefig("tool_integration_output.png") 56 | print("Visualization: Tool call frequencies saved as tool_integration_output.png") 57 | 58 | # %% [5. Interview Scenario: Tool Integration] 59 | """ 60 | Interview Scenario: Tool Integration 61 | Q: How do agents use tools in LangChain? 62 | A: Agents use tools to perform specific tasks, selected based on query context via reasoning. 63 | Key: Tools are defined with functions and descriptions. 64 | Example: Tool(name="StockCheck", func=mock_stock_check, description="...") 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_tool_integration_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/05 Retail Applications/02 Recommendation Systems/recommendation_systems.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Recommendation Systems] 2 | # Learn to build product recommendation systems using embeddings with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai faiss-cpu numpy matplotlib sklearn 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.vectorstores import FAISS 8 | from langchain.embeddings import OpenAIEmbeddings 9 | from sklearn.metrics.pairwise import cosine_similarity 10 | import numpy as np 11 | 12 | def run_recommendation_systems_demo(): 13 | # %% [2. Synthetic Retail Product Data] 14 | products = [ 15 | Document(page_content="TechCorp Laptop: 16GB RAM, Intel i7, 512GB SSD, ideal for gaming.", metadata={"product": "Laptop"}), 16 | Document(page_content="TechCorp Smartphone: Long battery, vibrant display, great for media.", metadata={"product": "Smartphone"}), 17 | Document(page_content="TechCorp Tablet: Lightweight, 10-hour battery, perfect for students.", metadata={"product": "Tablet"}) 18 | ] 19 | user_query = "Recommend a product for a student." 20 | print("Synthetic Data: Retail products and user query created") 21 | print(f"Products: {[doc.metadata['product'] for doc in products]}") 22 | print(f"User Query: {user_query}") 23 | 24 | # %% [3. Recommendation System] 25 | embeddings = OpenAIEmbeddings(api_key="your-openai-api-key") # Replace with your OpenAI API key 26 | vector_store = FAISS.from_documents(products, embeddings) 27 | recommended_docs = vector_store.similarity_search(user_query, k=2) 28 | 29 | print("Recommendation System: Products recommended") 30 | for i, doc in enumerate(recommended_docs): 31 | print(f"Recommendation {i+1}: {doc.metadata['product']} - {doc.page_content}") 32 | 33 | # %% [4. Visualization] 34 | query_embedding = embeddings.embed_query(user_query) 35 | product_embeddings = [embeddings.embed_query(doc.page_content) for doc in products] 36 | similarities = [cosine_similarity([query_embedding], [emb])[0][0] for emb in product_embeddings] 37 | 38 | plt.figure(figsize=(8, 4)) 39 | plt.bar([doc.metadata['product'] for doc in products], similarities, color='green') 40 | plt.title("Product Recommendation Similarities") 41 | plt.xlabel("Product") 42 | plt.ylabel("Cosine Similarity") 43 | plt.savefig("recommendation_systems_output.png") 44 | print("Visualization: Recommendation similarities saved as recommendation_systems_output.png") 45 | 46 | # %% [5. Interview Scenario: Recommendation Systems] 47 | """ 48 | Interview Scenario: Recommendation Systems 49 | Q: How do embeddings power recommendation systems? 50 | A: Embeddings convert product descriptions into vectors, enabling similarity-based matching for personalized recommendations. 51 | Key: Cosine similarity identifies relevant products. 52 | Example: FAISS.from_documents(products, embeddings) 53 | """ 54 | 55 | # Execute the demo 56 | if __name__ == "__main__": 57 | run_recommendation_systems_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/05 Retail Applications/03 Review Analysis/review_analysis.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Review Analysis] 2 | # Learn sentiment and topic extraction from retail reviews with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib pandas nltk scikit-learn 5 | import matplotlib.pyplot as plt 6 | from langchain.llms import OpenAI 7 | from langchain.prompts import PromptTemplate 8 | from langchain.chains import LLMChain 9 | from collections import Counter 10 | import numpy as np 11 | import nltk 12 | 13 | def run_review_analysis_demo(): 14 | # %% [2. Synthetic Retail Review Data] 15 | reviews = [ 16 | "The TechCorp laptop is fast and reliable, great for gaming!", 17 | "TechCorp smartphone has poor battery life, but nice display.", 18 | "TechCorp tablet is lightweight, but the app selection is limited." 19 | ] 20 | print("Synthetic Data: Retail reviews created") 21 | print(f"Reviews: {reviews}") 22 | 23 | # %% [3. Sentiment and Topic Extraction] 24 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 25 | 26 | sentiment_prompt = PromptTemplate( 27 | input_variables=["review"], 28 | template="You are a retail analyst. Determine the sentiment (Positive, Negative, Neutral) of this review: {review}" 29 | ) 30 | sentiment_chain = LLMChain(llm=llm, prompt=sentiment_prompt) 31 | 32 | topic_prompt = PromptTemplate( 33 | input_variables=["review"], 34 | template="Extract the main topic (e.g., performance, battery, design) of this review: {review}" 35 | ) 36 | topic_chain = LLMChain(llm=llm, prompt=topic_prompt) 37 | 38 | sentiments = [sentiment_chain.run(review=review).strip() for review in reviews] 39 | topics = [topic_chain.run(review=review).strip() for review in reviews] 40 | 41 | print("Review Analysis: Sentiment and topics extracted") 42 | for i, (review, sentiment, topic) in enumerate(zip(reviews, sentiments, topics)): 43 | print(f"Review {i+1}: {review}") 44 | print(f"Sentiment: {sentiment}, Topic: {topic}") 45 | 46 | # %% [4. Visualization] 47 | sentiment_counts = Counter(sentiments) 48 | plt.figure(figsize=(8, 4)) 49 | plt.bar(sentiment_counts.keys(), sentiment_counts.values(), color='blue') 50 | plt.title("Sentiment Distribution") 51 | plt.xlabel("Sentiment") 52 | plt.ylabel("Count") 53 | plt.savefig("review_analysis_output.png") 54 | print("Visualization: Sentiment distribution saved as review_analysis_output.png") 55 | 56 | # %% [5. Interview Scenario: Review Analysis] 57 | """ 58 | Interview Scenario: Review Analysis 59 | Q: How is sentiment extracted from reviews? 60 | A: Sentiment is extracted using LLMs to classify reviews as Positive, Negative, or Neutral based on text content. 61 | Key: Prompt engineering ensures accurate classification. 62 | Example: PromptTemplate(...template="Determine the sentiment...") 63 | """ 64 | 65 | # Execute the demo 66 | if __name__ == "__main__": 67 | nltk.download('punkt', quiet=True) 68 | run_review_analysis_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/02 Retrieval Augmented Generation/03 RAG Pipeline/rag_pipeline.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to RAG Pipeline] 2 | # Learn to enhance LLM responses with RAG for retail queries using LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai faiss-cpu numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.vectorstores import FAISS 8 | from langchain.embeddings import OpenAIEmbeddings 9 | from langchain.llms import OpenAI 10 | from langchain.chains import RetrievalQA 11 | import numpy as np 12 | import nltk 13 | 14 | def run_rag_pipeline_demo(): 15 | # %% [2. Synthetic Retail Document Data and Query] 16 | documents = [ 17 | Document(page_content="TechCorp Laptop: 16GB RAM, Intel i7, 512GB SSD, ideal for gaming.", metadata={"product": "Laptop"}), 18 | Document(page_content="TechCorp Smartphone: Long battery, vibrant display.", metadata={"product": "Smartphone"}), 19 | Document(page_content="TechCorp Tablet: Lightweight, 10-hour battery.", metadata={"product": "Tablet"}) 20 | ] 21 | query = "What’s the best product for gaming?" 22 | print("Synthetic Data: Retail documents and query created") 23 | print(f"Documents: {[doc.metadata['product'] for doc in documents]}") 24 | print(f"Query: {query}") 25 | 26 | # %% [3. RAG Pipeline] 27 | embeddings = OpenAIEmbeddings(api_key="your-openai-api-key") # Replace with your OpenAI API key 28 | vector_store = FAISS.from_documents(documents, embeddings) 29 | llm = OpenAI(api_key="your-openai-api-key") 30 | rag_chain = RetrievalQA.from_chain_type( 31 | llm=llm, 32 | chain_type="stuff", 33 | retriever=vector_store.as_retriever(search_kwargs={"k": 2}) 34 | ) 35 | 36 | rag_response = rag_chain.run(query) 37 | non_rag_response = llm(query) # Direct LLM response without RAG 38 | print("RAG Pipeline: Responses generated") 39 | print(f"RAG Response: {rag_response.strip()}") 40 | print(f"Non-RAG Response: {non_rag_response.strip()}") 41 | 42 | # %% [4. Visualization] 43 | response_lengths = [ 44 | len(nltk.word_tokenize(rag_response)), 45 | len(nltk.word_tokenize(non_rag_response)) 46 | ] 47 | plt.figure(figsize=(8, 4)) 48 | plt.bar(['RAG', 'Non-RAG'], response_lengths, color=['blue', 'red']) 49 | plt.title("RAG vs Non-RAG Response Lengths") 50 | plt.xlabel("Response Type") 51 | plt.ylabel("Word Count") 52 | plt.savefig("rag_pipeline_output.png") 53 | print("Visualization: Response lengths saved as rag_pipeline_output.png") 54 | 55 | # %% [5. Interview Scenario: RAG Pipeline] 56 | """ 57 | Interview Scenario: RAG Pipeline 58 | Q: How does RAG improve LLM responses? 59 | A: RAG retrieves relevant documents to provide context, improving accuracy and relevance over standalone LLMs. 60 | Key: Combines retrieval with generation. 61 | Example: RetrievalQA.from_chain_type(llm=llm, retriever=vector_store.as_retriever()) 62 | """ 63 | 64 | # Execute the demo 65 | if __name__ == "__main__": 66 | nltk.download('punkt', quiet=True) 67 | run_rag_pipeline_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/05 Retail Applications/01 Chatbots/chatbots.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Chatbots] 2 | # Learn to build conversational agents for retail customer support with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.chains import LLMChain 7 | from langchain.prompts import PromptTemplate 8 | from langchain.memory import ConversationBufferMemory 9 | from langchain.llms import OpenAI 10 | import numpy as np 11 | import nltk 12 | 13 | def run_chatbots_demo(): 14 | # %% [2. Synthetic Retail Conversation Data] 15 | conversation = [ 16 | "What are the features of the TechCorp laptop?", 17 | "Is it good for gaming?", 18 | "What’s the price?" 19 | ] 20 | print("Synthetic Data: Retail customer conversation created") 21 | print(f"Conversation: {conversation}") 22 | 23 | # %% [3. Chatbot with Memory] 24 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 25 | prompt = PromptTemplate( 26 | input_variables=["history", "query"], 27 | template="You are a retail assistant. Given the conversation history:\n{history}\nAnswer: {query}" 28 | ) 29 | memory = ConversationBufferMemory(return_messages=True) 30 | chatbot = LLMChain(llm=llm, prompt=prompt, memory=memory) 31 | 32 | responses = [] 33 | for query in conversation: 34 | response = chatbot.run(query=query) 35 | responses.append(response) 36 | memory.save_context({"query": query}, {"output": response}) 37 | 38 | print("Chatbot: Responses generated") 39 | for i, (query, response) in enumerate(zip(conversation, responses)): 40 | print(f"Query {i+1}: {query}") 41 | print(f"Response: {response.strip()}") 42 | 43 | # %% [4. Visualization] 44 | response_lengths = [len(nltk.word_tokenize(resp)) for resp in responses] 45 | history_lengths = [len(nltk.word_tokenize(memory.buffer_as_str)) for _ in range(len(conversation))] 46 | 47 | plt.figure(figsize=(8, 4)) 48 | x = np.arange(len(conversation)) 49 | plt.plot(x, response_lengths, marker='o', label='Response Length', color='blue') 50 | plt.plot(x, history_lengths, marker='x', label='History Length', color='green') 51 | plt.xticks(x, [f"Query {i+1}" for i in range(len(conversation))]) 52 | plt.title("Chatbot Response and History Lengths") 53 | plt.xlabel("Query") 54 | plt.ylabel("Word Count") 55 | plt.legend() 56 | plt.savefig("chatbots_output.png") 57 | print("Visualization: Response and history lengths saved as chatbots_output.png") 58 | 59 | # %% [5. Interview Scenario: Chatbots] 60 | """ 61 | Interview Scenario: Chatbots 62 | Q: How do chatbots use memory in LangChain? 63 | A: Chatbots use memory to retain conversation history, ensuring context-aware responses for coherent interactions. 64 | Key: ConversationBufferMemory stores query-response pairs. 65 | Example: ConversationBufferMemory(return_messages=True) 66 | """ 67 | 68 | # Execute the demo 69 | if __name__ == "__main__": 70 | nltk.download('punkt', quiet=True) 71 | run_chatbots_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/01 Core Components/03 Memory/memory.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Memory] 2 | # Learn contextual conversation history with LangChain memory for retail interactions. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.chains import LLMChain 7 | from langchain.prompts import PromptTemplate 8 | from langchain.memory import ConversationBufferMemory 9 | from langchain.llms import OpenAI 10 | import numpy as np 11 | import nltk 12 | 13 | def run_memory_demo(): 14 | # %% [2. Synthetic Retail Conversation Data] 15 | conversation = [ 16 | "Tell me about TechCorp laptops.", 17 | "Which one is best for gaming?", 18 | "What’s the price of that model?" 19 | ] 20 | print("Synthetic Data: Retail customer conversation created") 21 | print(f"Conversation: {conversation}") 22 | 23 | # %% [3. Conversational Memory] 24 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 25 | prompt = PromptTemplate( 26 | input_variables=["history", "query"], 27 | template="You are a retail assistant. Given the conversation history:\n{history}\nAnswer the query: {query}" 28 | ) 29 | memory = ConversationBufferMemory(return_messages=True) 30 | chain = LLMChain(llm=llm, prompt=prompt, memory=memory) 31 | 32 | responses = [] 33 | for query in conversation: 34 | response = chain.run(query=query) 35 | responses.append(response) 36 | memory.save_context({"query": query}, {"output": response}) 37 | 38 | print("Memory: Conversational responses generated") 39 | for i, (query, response) in enumerate(zip(conversation, responses)): 40 | print(f"Query {i+1}: {query}") 41 | print(f"Response: {response.strip()}") 42 | 43 | # %% [4. Visualization] 44 | history_lengths = [len(nltk.word_tokenize(memory.buffer_as_str)) for _ in range(len(conversation))] 45 | response_lengths = [len(nltk.word_tokenize(resp)) for resp in responses] 46 | 47 | plt.figure(figsize=(8, 4)) 48 | x = np.arange(len(conversation)) 49 | plt.plot(x, history_lengths, marker='o', label='History Length', color='blue') 50 | plt.plot(x, response_lengths, marker='x', label='Response Length', color='green') 51 | plt.xticks(x, [f"Query {i+1}" for i in range(len(conversation))]) 52 | plt.title("Conversation History and Response Lengths") 53 | plt.xlabel("Query") 54 | plt.ylabel("Word Count") 55 | plt.legend() 56 | plt.savefig("memory_output.png") 57 | print("Visualization: History and response lengths saved as memory_output.png") 58 | 59 | # %% [5. Interview Scenario: Memory] 60 | """ 61 | Interview Scenario: Memory 62 | Q: How does memory enhance conversational agents in LangChain? 63 | A: Memory stores conversation history, enabling context-aware responses for coherent interactions. 64 | Key: Types like ConversationBufferMemory retain query-response pairs. 65 | Example: ConversationBufferMemory(return_messages=True) 66 | """ 67 | 68 | # Execute the demo 69 | if __name__ == "__main__": 70 | nltk.download('punkt', quiet=True) 71 | run_memory_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/04 Advanced Features/02 Evaluation Metrics/evaluation_metrics.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Evaluation Metrics] 2 | # Learn BLEU, ROUGE, and custom metrics for retail response quality with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk rouge-score 5 | import matplotlib.pyplot as plt 6 | from langchain.llms import OpenAI 7 | from nltk.translate.bleu_score import sentence_bleu 8 | from rouge_score import rouge_scorer 9 | import numpy as np 10 | 11 | def run_evaluation_metrics_demo(): 12 | # %% [2. Synthetic Retail Query and Reference Data] 13 | queries = [ 14 | "Describe the TechCorp laptop features.", 15 | "Explain the TechCorp smartphone battery.", 16 | "Detail the TechCorp tablet use case." 17 | ] 18 | references = [ 19 | "The TechCorp laptop has 16GB RAM, Intel i7, and 512GB SSD.", 20 | "The TechCorp smartphone offers a long-lasting battery with vibrant display.", 21 | "The TechCorp tablet is lightweight, ideal for students and professionals." 22 | ] 23 | print("Synthetic Data: Retail queries and references created") 24 | print(f"Queries: {queries}") 25 | 26 | # %% [3. Response Generation and Evaluation] 27 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 28 | responses = [llm(query) for query in queries] 29 | 30 | bleu_scores = [] 31 | rouge_scores = [] 32 | scorer = rouge_scorer.RougeScorer(['rouge1', 'rougeL'], use_stemmer=True) 33 | 34 | for ref, resp in zip(references, responses): 35 | # BLEU Score 36 | ref_tokens = [ref.split()] 37 | resp_tokens = resp.split() 38 | bleu = sentence_bleu(ref_tokens, resp_tokens) 39 | bleu_scores.append(bleu) 40 | 41 | # ROUGE Score 42 | rouge = scorer.score(ref, resp) 43 | rouge_scores.append(rouge['rouge1'].fmeasure) 44 | 45 | print("Evaluation Metrics: Scores calculated") 46 | for i, (query, resp, bleu, rouge) in enumerate(zip(queries, responses, bleu_scores, rouge_scores)): 47 | print(f"Query {i+1}: {query}") 48 | print(f"Response: {resp.strip()}") 49 | print(f"BLEU: {bleu:.2f}, ROUGE-1: {rouge:.2f}") 50 | 51 | # %% [4. Visualization] 52 | plt.figure(figsize=(8, 4)) 53 | x = np.arange(len(queries)) 54 | plt.bar(x - 0.2, bleu_scores, 0.4, label='BLEU', color='blue') 55 | plt.bar(x + 0.2, rouge_scores, 0.4, label='ROUGE-1', color='red') 56 | plt.xticks(x, [f"Query {i+1}" for i in range(len(queries))]) 57 | plt.title("BLEU and ROUGE Scores") 58 | plt.xlabel("Query") 59 | plt.ylabel("Score") 60 | plt.legend() 61 | plt.savefig("evaluation_metrics_output.png") 62 | print("Visualization: Scores saved as evaluation_metrics_output.png") 63 | 64 | # %% [5. Interview Scenario: Evaluation Metrics] 65 | """ 66 | Interview Scenario: Evaluation Metrics 67 | Q: What are BLEU and ROUGE used for? 68 | A: BLEU measures n-gram overlap, ROUGE evaluates recall and precision for text similarity, both used to assess response quality. 69 | Key: Metrics quantify LLM performance. 70 | Example: sentence_bleu([ref.split()], resp.split()) 71 | """ 72 | 73 | # Execute the demo 74 | if __name__ == "__main__": 75 | nltk.download('punkt', quiet=True) 76 | run_evaluation_metrics_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/01 Core Components/04 Document Loaders/document_loaders.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Document Loaders] 2 | # Learn to process retail data with LangChain document loaders. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib pandas nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.chains import LLMChain 8 | from langchain.prompts import PromptTemplate 9 | from langchain.llms import OpenAI 10 | import numpy as np 11 | import nltk 12 | 13 | def run_document_loaders_demo(): 14 | # %% [2. Synthetic Retail Document Data] 15 | documents = [ 16 | Document( 17 | page_content="TechCorp Laptop Manual: Features include a 16GB RAM, Intel i7 processor, and 512GB SSD.", 18 | metadata={"product": "TechCorp Laptop"} 19 | ), 20 | Document( 21 | page_content="TechCorp Smartphone Review: Excellent battery life, vibrant display, but average camera.", 22 | metadata={"product": "TechCorp Smartphone"} 23 | ), 24 | Document( 25 | page_content="TechCorp Tablet Guide: Lightweight design, 10-hour battery, ideal for students.", 26 | metadata={"product": "TechCorp Tablet"} 27 | ) 28 | ] 29 | print("Synthetic Data: Retail documents created") 30 | print(f"Documents: {[doc.metadata['product'] for doc in documents]}") 31 | 32 | # %% [3. Document Processing] 33 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 34 | prompt = PromptTemplate( 35 | input_variables=["content"], 36 | template="You are a retail assistant. Summarize the document: {content}" 37 | ) 38 | chain = LLMChain(llm=llm, prompt=prompt) 39 | 40 | summaries = [chain.run(content=doc.page_content) for doc in documents] 41 | print("Document Loaders: Summaries generated") 42 | for i, (doc, summary) in enumerate(zip(documents, summaries)): 43 | print(f"Document {i+1}: {doc.metadata['product']}") 44 | print(f"Summary: {summary.strip()}") 45 | 46 | # %% [4. Visualization] 47 | document_lengths = [len(nltk.word_tokenize(doc.page_content)) for doc in documents] 48 | summary_lengths = [len(nltk.word_tokenize(summary)) for summary in summaries] 49 | 50 | plt.figure(figsize=(8, 4)) 51 | x = np.arange(len(documents)) 52 | plt.bar(x - 0.2, document_lengths, 0.4, label='Document Length', color='blue') 53 | plt.bar(x + 0.2, summary_lengths, 0.4, label='Summary Length', color='green') 54 | plt.xticks(x, [doc.metadata['product'] for doc in documents]) 55 | plt.title("Document and Summary Lengths") 56 | plt.xlabel("Product") 57 | plt.ylabel("Word Count") 58 | plt.legend() 59 | plt.savefig("document_loaders_output.png") 60 | print("Visualization: Document and summary lengths saved as document_loaders_output.png") 61 | 62 | # %% [5. Interview Scenario: Document Loaders] 63 | """ 64 | Interview Scenario: Document Loaders 65 | Q: How do document loaders process external data in LangChain? 66 | A: Document loaders parse text from various sources into Document objects, enabling LLM processing for tasks like summarization. 67 | Key: Support diverse formats like PDFs, CSVs, or raw text. 68 | Example: Document(page_content="...", metadata={...}) 69 | """ 70 | 71 | # Execute the demo 72 | if __name__ == "__main__": 73 | nltk.download('punkt', quiet=True) 74 | run_document_loaders_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/04 Advanced Features/03 Agent Optimization/agent_optimization.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Agent Optimization] 2 | # Learn to optimize agent performance and latency for retail tasks with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.agents import initialize_agent, Tool 7 | from langchain.llms import OpenAI 8 | import time 9 | import numpy as np 10 | 11 | def run_agent_optimization_demo(): 12 | # %% [2. Synthetic Retail Query Data] 13 | queries = [ 14 | "Check TechCorp laptop stock.", 15 | "Calculate TechCorp smartphone discount.", 16 | "Search TechCorp tablet reviews." 17 | ] 18 | print("Synthetic Data: Retail queries created") 19 | print(f"Queries: {queries}") 20 | 21 | # %% [3. Agent Optimization] 22 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 23 | 24 | def mock_stock_check(query): 25 | return "Stock: 10 units." 26 | 27 | def mock_discount_calculator(query): 28 | return "Discount: 15% off." 29 | 30 | def mock_search_reviews(query): 31 | return "Reviews: 4.5/5 rating." 32 | 33 | tools = [ 34 | Tool(name="StockCheck", func=mock_stock_check, description="Check product stock"), 35 | Tool(name="DiscountCalculator", func=mock_discount_calculator, description="Calculate discount"), 36 | Tool(name="SearchReviews", func=mock_search_reviews, description="Search reviews") 37 | ] 38 | 39 | # Non-Optimized Agent 40 | non_optimized_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) 41 | non_optimized_times = [] 42 | for query in queries: 43 | start = time.time() 44 | non_optimized_agent.run(query) 45 | non_optimized_times.append(time.time() - start) 46 | 47 | # Optimized Agent (less verbose, limited tool calls) 48 | optimized_agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False, max_iterations=3) 49 | optimized_times = [] 50 | for query in queries: 51 | start = time.time() 52 | optimized_agent.run(query) 53 | optimized_times.append(time.time() - start) 54 | 55 | print("Agent Optimization: Execution times recorded") 56 | for i, (query, non_opt_time, opt_time) in enumerate(zip(queries, non_optimized_times, optimized_times)): 57 | print(f"Query {i+1}: {query}") 58 | print(f"Non-Optimized: {non_opt_time:.2f}s, Optimized: {opt_time:.2f}s") 59 | 60 | # %% [4. Visualization] 61 | plt.figure(figsize=(8, 4)) 62 | x = np.arange(len(queries)) 63 | plt.bar(x - 0.2, non_optimized_times, 0.4, label='Non-Optimized', color='red') 64 | plt.bar(x + 0.2, optimized_times, 0.4, label='Optimized', color='green') 65 | plt.xticks(x, [f"Query {i+1}" for i in range(len(queries))]) 66 | plt.title("Agent Execution Times") 67 | plt.xlabel("Query") 68 | plt.ylabel("Time (s)") 69 | plt.legend() 70 | plt.savefig("agent_optimization_output.png") 71 | print("Visualization: Execution times saved as agent_optimization_output.png") 72 | 73 | # %% [5. Interview Scenario: Agent Optimization] 74 | """ 75 | Interview Scenario: Agent Optimization 76 | Q: How do you optimize agent performance? 77 | A: Limit iterations, reduce verbosity, and streamline tool calls to lower latency while maintaining accuracy. 78 | Key: Balances speed and quality. 79 | Example: initialize_agent(..., max_iterations=3, verbose=False) 80 | """ 81 | 82 | # Execute the demo 83 | if __name__ == "__main__": 84 | run_agent_optimization_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/04 Advanced Features/01 Custom Chains/custom_chains.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Custom Chains] 2 | # Learn to design tailored workflows for retail tasks with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.chains import LLMChain, SequentialChain 7 | from langchain.prompts import PromptTemplate 8 | from langchain.llms import OpenAI 9 | import numpy as np 10 | import nltk 11 | 12 | def run_custom_chains_demo(): 13 | # %% [2. Synthetic Retail Query Data] 14 | queries = [ 15 | "Describe the TechCorp laptop and suggest a use case.", 16 | "Explain the TechCorp smartphone features and recommend an accessory.", 17 | "Detail the TechCorp tablet and propose a target audience." 18 | ] 19 | print("Synthetic Data: Retail queries created") 20 | print(f"Queries: {queries}") 21 | 22 | # %% [3. Custom Chain] 23 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 24 | 25 | # Chain 1: Describe product 26 | description_prompt = PromptTemplate( 27 | input_variables=["query"], 28 | template="You are a retail assistant. Describe the product in the query: {query}" 29 | ) 30 | description_chain = LLMChain(llm=llm, prompt=description_prompt, output_key="description") 31 | 32 | # Chain 2: Suggest recommendation 33 | suggestion_prompt = PromptTemplate( 34 | input_variables=["description"], 35 | template="Based on the product description: {description}, suggest a use case or recommendation." 36 | ) 37 | suggestion_chain = LLMChain(llm=llm, prompt=suggestion_prompt, output_key="suggestion") 38 | 39 | custom_chain = SequentialChain( 40 | chains=[description_chain, suggestion_chain], 41 | input_variables=["query"], 42 | output_variables=["description", "suggestion"] 43 | ) 44 | 45 | responses = [custom_chain({"query": query}) for query in queries] 46 | print("Custom Chain: Responses generated") 47 | for i, (query, response) in enumerate(zip(queries, responses)): 48 | print(f"Query {i+1}: {query}") 49 | print(f"Description: {response['description'].strip()}") 50 | print(f"Suggestion: {response['suggestion'].strip()}") 51 | 52 | # %% [4. Visualization] 53 | description_lengths = [len(nltk.word_tokenize(resp["description"])) for resp in responses] 54 | suggestion_lengths = [len(nltk.word_tokenize(resp["suggestion"])) for resp in responses] 55 | 56 | plt.figure(figsize=(8, 4)) 57 | x = np.arange(len(queries)) 58 | plt.bar(x - 0.2, description_lengths, 0.4, label='Description Length', color='blue') 59 | plt.bar(x + 0.2, suggestion_lengths, 0.4, label='Suggestion Length', color='green') 60 | plt.xticks(x, [f"Query {i+1}" for i in range(len(queries))]) 61 | plt.title("Custom Chain Output Lengths") 62 | plt.xlabel("Query") 63 | plt.ylabel("Word Count") 64 | plt.legend() 65 | plt.savefig("custom_chains_output.png") 66 | print("Visualization: Output lengths saved as custom_chains_output.png") 67 | 68 | # %% [5. Interview Scenario: Custom Chains] 69 | """ 70 | Interview Scenario: Custom Chains 71 | Q: How do custom chains handle complex tasks? 72 | A: Custom chains combine multiple LLMChain instances in a SequentialChain for multi-step workflows tailored to specific tasks. 73 | Key: Modular design enhances flexibility. 74 | Example: SequentialChain(chains=[LLMChain(...), LLMChain(...)], ...) 75 | """ 76 | 77 | # Execute the demo 78 | if __name__ == "__main__": 79 | nltk.download('punkt', quiet=True) 80 | run_custom_chains_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/05 Retail Applications/04 Query Answering/query_answering.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Query Answering] 2 | # Learn to answer retail customer queries using RAG with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai faiss-cpu numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.vectorstores import FAISS 8 | from langchain.embeddings import OpenAIEmbeddings 9 | from langchain.llms import OpenAI 10 | from langchain.chains import RetrievalQA 11 | import numpy as np 12 | import nltk 13 | 14 | def run_query_answering_demo(): 15 | # %% [2. Synthetic Retail Document and Query Data] 16 | documents = [ 17 | Document(page_content="TechCorp Laptop: 16GB RAM, Intel i7, 512GB SSD, great for gaming.", metadata={"product": "Laptop"}), 18 | Document(page_content="TechCorp Smartphone: Long battery, vibrant display, ideal for media.", metadata={"product": "Smartphone"}), 19 | Document(page_content="TechCorp Tablet: Lightweight, 10-hour battery, perfect for students.", metadata={"product": "Tablet"}) 20 | ] 21 | queries = [ 22 | "What’s the best product for gaming?", 23 | "Which product has a long battery life?", 24 | "Is there a lightweight product?" 25 | ] 26 | print("Synthetic Data: Retail documents and queries created") 27 | print(f"Documents: {[doc.metadata['product'] for doc in documents]}") 28 | print(f"Queries: {queries}") 29 | 30 | # %% [3. RAG-Based Query Answering] 31 | embeddings = OpenAIEmbeddings(api_key="your-openai-api-key") # Replace with your OpenAI API key 32 | vector_store = FAISS.from_documents(documents, embeddings) 33 | llm = OpenAI(api_key="your-openai-api-key") 34 | rag_chain = RetrievalQA.from_chain_type( 35 | llm=llm, 36 | chain_type="stuff", 37 | retriever=vector_store.as_retriever(search_kwargs={"k": 2}) 38 | ) 39 | 40 | rag_responses = [rag_chain.run(query) for query in queries] 41 | non_rag_responses = [llm(query) for query in queries] 42 | 43 | print("Query Answering: Responses generated") 44 | for i, (query, rag_resp, non_rag_resp) in enumerate(zip(queries, rag_responses, non_rag_responses)): 45 | print(f"Query {i+1}: {query}") 46 | print(f"RAG Response: {rag_resp.strip()}") 47 | print(f"Non-RAG Response: {non_rag_resp.strip()}") 48 | 49 | # %% [4. Visualization] 50 | rag_lengths = [len(nltk.word_tokenize(resp)) for resp in rag_responses] 51 | non_rag_lengths = [len(nltk.word_tokenize(resp)) for resp in non_rag_responses] 52 | 53 | plt.figure(figsize=(8, 4)) 54 | x = np.arange(len(queries)) 55 | plt.bar(x - 0.2, rag_lengths, 0.4, label='RAG Response', color='blue') 56 | plt.bar(x + 0.2, non_rag_lengths, 0.4, label='Non-RAG Response', color='red') 57 | plt.xticks(x, [f"Query {i+1}" for i in range(len(queries))]) 58 | plt.title("RAG vs Non-RAG Response Lengths") 59 | plt.xlabel("Query") 60 | plt.ylabel("Word Count") 61 | plt.legend() 62 | plt.savefig("query_answering_output.png") 63 | print("Visualization: Response lengths saved as query_answering_output.png") 64 | 65 | # %% [5. Interview Scenario: Query Answering] 66 | """ 67 | Interview Scenario: Query Answering 68 | Q: How does RAG improve query answering? 69 | A: RAG retrieves relevant documents to provide context, improving accuracy and specificity of LLM responses. 70 | Key: Combines retrieval and generation. 71 | Example: RetrievalQA.from_chain_type(llm=llm, retriever=...) 72 | """ 73 | 74 | # Execute the demo 75 | if __name__ == "__main__": 76 | nltk.download('punkt', quiet=True) 77 | run_query_answering_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/01 Core Components/01 Chains/chains.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Chains] 2 | # Learn sequential workflows with LangChain chains for retail applications. 3 | 4 | # Setup: pip install langchain langchain-openai numpy matplotlib nltk 5 | import matplotlib.pyplot as plt 6 | from langchain.chains import LLMChain, SequentialChain 7 | from langchain.prompts import PromptTemplate 8 | from langchain.llms import OpenAI 9 | import numpy as np 10 | import nltk 11 | 12 | def run_chains_demo(): 13 | # %% [2. Synthetic Retail Query Data] 14 | queries = [ 15 | "What are the features of the TechCorp laptop?", 16 | "Compare TechCorp laptops and smartphones.", 17 | "Is the TechCorp laptop good for gaming?" 18 | ] 19 | print("Synthetic Data: Retail customer queries created") 20 | print(f"Queries: {queries}") 21 | 22 | # %% [3. Single LLMChain] 23 | llm = OpenAI(api_key="your-openai-api-key") # Replace with your OpenAI API key 24 | prompt = PromptTemplate( 25 | input_variables=["query"], 26 | template="You are a retail assistant. Answer the customer query: {query}" 27 | ) 28 | chain = LLMChain(llm=llm, prompt=prompt, output_key="response") 29 | 30 | single_chain_responses = [chain.run(query) for query in queries] 31 | print("Single LLMChain: Responses generated") 32 | for i, (query, response) in enumerate(zip(queries, single_chain_responses)): 33 | print(f"Query {i+1}: {query}") 34 | print(f"Response: {response.strip()}") 35 | 36 | # %% [4. SequentialChain] 37 | summary_prompt = PromptTemplate( 38 | input_variables=["response"], 39 | template="Summarize the following response in one sentence: {response}" 40 | ) 41 | summary_chain = LLMChain(llm=llm, prompt=summary_prompt, output_key="summary") 42 | 43 | sequential_chain = SequentialChain( 44 | chains=[chain, summary_chain], 45 | input_variables=["query"], 46 | output_variables=["response", "summary"] 47 | ) 48 | 49 | sequential_responses = [sequential_chain({"query": query}) for query in queries] 50 | print("SequentialChain: Responses and summaries generated") 51 | for i, (query, result) in enumerate(zip(queries, sequential_responses)): 52 | print(f"Query {i+1}: {query}") 53 | print(f"Response: {result['response'].strip()}") 54 | print(f"Summary: {result['summary'].strip()}") 55 | 56 | # %% [5. Visualization] 57 | response_lengths = [len(nltk.word_tokenize(resp["response"])) for resp in sequential_responses] 58 | summary_lengths = [len(nltk.word_tokenize(resp["summary"])) for resp in sequential_responses] 59 | 60 | plt.figure(figsize=(8, 4)) 61 | x = np.arange(len(queries)) 62 | plt.bar(x - 0.2, response_lengths, 0.4, label='Response Length', color='blue') 63 | plt.bar(x + 0.2, summary_lengths, 0.4, label='Summary Length', color='green') 64 | plt.xticks(x, [f"Query {i+1}" for i in range(len(queries))]) 65 | plt.title("Response and Summary Lengths") 66 | plt.xlabel("Query") 67 | plt.ylabel("Word Count") 68 | plt.legend() 69 | plt.savefig("chains_output.png") 70 | print("Visualization: Response and summary lengths saved as chains_output.png") 71 | 72 | # %% [6. Interview Scenario: Chains] 73 | """ 74 | Interview Scenario: Chains 75 | Q: How do LangChain chains work for LLM workflows? 76 | A: Chains combine LLMs with prompts to create sequential workflows, like LLMChain for single tasks or SequentialChain for multi-step processes. 77 | Key: Modular design enables complex task automation. 78 | Example: SequentialChain(chains=[LLMChain(...), LLMChain(...)], ...) 79 | """ 80 | 81 | # Execute the demo 82 | if __name__ == "__main__": 83 | nltk.download('punkt', quiet=True) 84 | run_chains_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/02 Retrieval Augmented Generation/04 Embedding Models/embedding_models.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Embedding Models] 2 | # Learn to use Hugging Face and OpenAI embeddings for retail RAG with LangChain. 3 | 4 | # Setup: pip install langchain langchain-openai langchain-huggingface faiss-cpu numpy matplotlib 5 | import matplotlib.pyplot as plt 6 | from langchain.docstore.document import Document 7 | from langchain.vectorstores import FAISS 8 | from langchain.embeddings import OpenAIEmbeddings, HuggingFaceEmbeddings 9 | from sklearn.metrics.pairwise import cosine_similarity 10 | import numpy as np 11 | 12 | def run_embedding_models_demo(): 13 | # %% [2. Synthetic Retail Document Data and Query] 14 | documents = [ 15 | Document(page_content="TechCorp Laptop: 16GB RAM, Intel i7, 512GB SSD.", metadata={"product": "Laptop"}), 16 | Document(page_content="TechCorp Smartphone: Long battery, vibrant display.", metadata={"product": "Smartphone"}), 17 | Document(page_content="TechCorp Tablet: Lightweight, 10-hour battery.", metadata={"product": "Tablet"}) 18 | ] 19 | query = "Find a product with a good battery." 20 | print("Synthetic Data: Retail documents and query created") 21 | print(f"Documents: {[doc.metadata['product'] for doc in documents]}") 22 | print(f"Query: {query}") 23 | 24 | # %% [3. Embedding Models Comparison] 25 | openai_embeddings = OpenAIEmbeddings(api_key="your-openai-api-key") # Replace with your OpenAI API key 26 | hf_embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") 27 | 28 | openai_vector_store = FAISS.from_documents(documents, openai_embeddings) 29 | hf_vector_store = FAISS.from_documents(documents, hf_embeddings) 30 | 31 | openai_retrieved = openai_vector_store.similarity_search(query, k=2) 32 | hf_retrieved = hf_vector_store.similarity_search(query, k=2) 33 | 34 | print("Embedding Models: Documents retrieved") 35 | print("OpenAI Embeddings:") 36 | for i, doc in enumerate(openai_retrieved): 37 | print(f"Retrieved {i+1}: {doc.metadata['product']} - {doc.page_content}") 38 | print("Hugging Face Embeddings:") 39 | for i, doc in enumerate(hf_retrieved): 40 | print(f"Retrieved {i+1}: {doc.metadata['product']} - {doc.page_content}") 41 | 42 | # %% [4. Visualization] 43 | query_openai_emb = openai_embeddings.embed_query(query) 44 | query_hf_emb = hf_embeddings.embed_query(query) 45 | doc_openai_embs = [openai_embeddings.embed_query(doc.page_content) for doc in documents] 46 | doc_hf_embs = [hf_embeddings.embed_query(doc.page_content) for doc in documents] 47 | 48 | openai_similarities = [cosine_similarity([query_openai_emb], [emb])[0][0] for emb in doc_openai_embs] 49 | hf_similarities = [cosine_similarity([query_hf_emb], [emb])[0][0] for emb in doc_hf_embs] 50 | 51 | plt.figure(figsize=(10, 4)) 52 | x = np.arange(len(documents)) 53 | plt.bar(x - 0.2, openai_similarities, 0.4, label='OpenAI Embeddings', color='blue') 54 | plt.bar(x + 0.2, hf_similarities, 0.4, label='Hugging Face Embeddings', color='green') 55 | plt.xticks(x, [doc.metadata['product'] for doc in documents]) 56 | plt.title("Embedding Model Similarity Comparison") 57 | plt.xlabel("Product") 58 | plt.ylabel("Cosine Similarity") 59 | plt.legend() 60 | plt.savefig("embedding_models_output.png") 61 | print("Visualization: Embedding similarities saved as embedding_models_output.png") 62 | 63 | # %% [5. Interview Scenario: Embedding Models] 64 | """ 65 | Interview Scenario: Embedding Models 66 | Q: How do embedding models impact RAG performance? 67 | A: Embeddings determine retrieval quality; OpenAI offers high accuracy, while Hugging Face models are cost-effective and open-source. 68 | Key: Model choice balances performance and cost. 69 | Example: HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") 70 | """ 71 | 72 | # Execute the demo 73 | if __name__ == "__main__": 74 | run_embedding_models_demo() -------------------------------------------------------------------------------- /Langchian Fundamentals/03 AI Agents/README.md: -------------------------------------------------------------------------------- 1 | # 🤖 AI Agents with LangChain 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | Matplotlib 8 |
9 |

Your guide to mastering AI agents with LangChain for AI/ML and retail-focused interviews

10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **AI Agents** subsection of the **LangChain Library Roadmap**! 🚀 This folder explores AI agents, autonomous systems that leverage tools, reasoning, and planning for retail tasks. Designed for hands-on learning and interview success, it builds on your prior roadmaps and supports your retail-themed projects (April 26, 2025). This section equips you with skills for retail AI roles using LangChain. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Tool Integration**: Use tools like search or APIs for retail tasks. 20 | - **Agent Types**: Reactive, planning, and ReAct agents. 21 | - **Agent Reasoning**: Autonomous decision-making for customer support. 22 | - **Custom Agents**: Build agents for retail scenarios. 23 | - **Hands-on Code**: Four `.py` files with examples using synthetic retail data. 24 | - **Interview Scenarios**: Key questions and answers for LangChain interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - AI Engineers building autonomous retail applications. 29 | - Machine Learning Engineers developing agent-based systems. 30 | - AI Researchers mastering LangChain’s agent capabilities. 31 | - Software Engineers deepening expertise in LangChain for retail. 32 | - Anyone preparing for AI/ML interviews in retail or tech. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This subsection covers four key AI agent components, each with a dedicated `.py` file: 37 | 38 | ### 🔧 Tool Integration (`tool_integration.py`) 39 | - Tool Usage 40 | - Retail Task Automation 41 | - Tool Call Visualization 42 | 43 | ### 🧩 Agent Types (`agent_types.py`) 44 | - Reactive, Planning, ReAct Agents 45 | - Agent Behavior Comparison 46 | - Behavior Visualization 47 | 48 | ### 🧠 Agent Reasoning (`agent_reasoning.py`) 49 | - Autonomous Decision-Making 50 | - Customer Support Scenarios 51 | - Decision Visualization 52 | 53 | ### 🛠️ Custom Agents (`custom_agents.py`) 54 | - Retail-Specific Agents 55 | - Scenario-Based Design 56 | - Agent Action Visualization 57 | 58 | ## 💡 Why Master AI Agents? 59 | 60 | AI agents are critical for intelligent automation: 61 | 1. **Retail Relevance**: Automate customer support and inventory tasks. 62 | 2. **Interview Relevance**: Tested in coding challenges (e.g., agent design). 63 | 3. **Autonomy**: Enable decision-making with minimal human input. 64 | 4. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles. 65 | 66 | ## 📆 Study Plan 67 | 68 | - **Week 1**: 69 | - Day 1-2: Tool Integration 70 | - Day 3-4: Agent Types 71 | - Day 5-6: Agent Reasoning 72 | - Day 7: Custom Agents 73 | - **Week 2**: 74 | - Day 1-7: Review `.py` files and practice interview scenarios. 75 | 76 | ## 🛠️ Setup Instructions 77 | 78 | 1. **Python Environment**: 79 | - Install Python 3.8+ and pip. 80 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 81 | - Install dependencies: `pip install langchain langchain-openai numpy matplotlib pandas`. 82 | 2. **API Keys**: 83 | - Obtain an OpenAI API key (replace `"your-openai-api-key"` in code). 84 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 85 | 3. **Datasets**: 86 | - Uses synthetic retail data (e.g., customer queries, inventory). 87 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets). 88 | - Note: `.py` files use simulated data to avoid file I/O constraints. 89 | 4. **Running Code**: 90 | - Run `.py` files (e.g., `python tool_integration.py`). 91 | - Use Google Colab or local setup with GPU support. 92 | - View outputs in terminal and Matplotlib visualizations (PNGs). 93 | - Check terminal for errors; ensure dependencies and API keys are set. 94 | 95 | ## 🏆 Practical Tasks 96 | 97 | 1. **Tool Integration**: 98 | - Integrate a mock API for retail tasks. 99 | - Visualize tool call frequencies. 100 | 2. **Agent Types**: 101 | - Compare reactive and planning agents. 102 | - Analyze agent behavior metrics. 103 | 3. **Agent Reasoning**: 104 | - Build an agent for customer support. 105 | - Visualize decision paths. 106 | 4. **Custom Agents**: 107 | - Design an agent for inventory queries. 108 | - Plot agent action outcomes. 109 | 110 | ## 💡 Interview Tips 111 | 112 | - **Common Questions**: 113 | - How do agents use tools in LangChain? 114 | - What’s the difference between reactive and planning agents? 115 | - How does agent reasoning work for retail tasks? 116 | - How do you build a custom agent? 117 | - **Tips**: 118 | - Explain tool integration with code (e.g., `tools=[Tool(...)]`). 119 | - Demonstrate agent types (e.g., `initialize_agent`). 120 | - Code tasks like custom agent design. 121 | - Discuss trade-offs (e.g., agent complexity vs. performance). 122 | - **Coding Tasks**: 123 | - Build an agent with a mock API tool. 124 | - Implement a ReAct agent for retail queries. 125 | - **Conceptual Clarity**: 126 | - Explain agent autonomy and tool usage. 127 | - Describe ReAct reasoning framework. 128 | 129 | ## 📚 Resources 130 | 131 | - [LangChain Documentation](https://python.langchain.com/docs/) 132 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 133 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 134 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 135 | 136 | ## 🤝 Contributions 137 | 138 | 1. Fork the repository. 139 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 140 | 3. Commit changes (`git commit -m 'Add some amazing content'`). 141 | 4. Push to the branch (`git push origin feature/amazing-addition`). 142 | 5. Open a Pull Request. 143 | 144 | --- 145 | 146 |
147 |

Happy Learning and Good Luck with Your Interviews! ✨

148 |
-------------------------------------------------------------------------------- /Langchian Fundamentals/04 Advanced Features/README.md: -------------------------------------------------------------------------------- 1 | # 🚀 Advanced Features of LangChain 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | Matplotlib 8 |
9 |

Your guide to mastering advanced LangChain features for AI/ML and retail-focused interviews

10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Advanced Features** subsection of the **LangChain Library Roadmap**! 🚀 This folder explores advanced LangChain capabilities, including custom chains, evaluation metrics, agent optimization, and API integration. Designed for hands-on learning and interview success, it builds on your prior roadmaps and supports your retail-themed projects (April 26, 2025). This section equips you with skills for retail AI roles using LangChain. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Custom Chains**: Design tailored workflows for complex retail tasks. 20 | - **Evaluation Metrics**: BLEU, ROUGE, and custom metrics for response quality. 21 | - **Agent Optimization**: Optimize agent performance and latency. 22 | - **Integration with APIs**: Connect LangChain with external retail APIs. 23 | - **Hands-on Code**: Four `.py` files with examples using synthetic retail data. 24 | - **Interview Scenarios**: Key questions and answers for LangChain interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - AI Engineers building sophisticated LLM applications. 29 | - Machine Learning Engineers optimizing LangChain workflows. 30 | - AI Researchers mastering advanced LangChain features. 31 | - Software Engineers deepening expertise in LangChain for retail. 32 | - Anyone preparing for AI/ML interviews in retail or tech. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This subsection covers four key advanced features, each with a dedicated `.py` file: 37 | 38 | ### 🔗 Custom Chains (`custom_chains.py`) 39 | - Tailored Workflows 40 | - Multi-Step Retail Tasks 41 | - Workflow Visualization 42 | 43 | ### 📊 Evaluation Metrics (`evaluation_metrics.py`) 44 | - BLEU and ROUGE Metrics 45 | - Custom Retail Metrics 46 | - Metric Visualization 47 | 48 | ### ⚡ Agent Optimization (`agent_optimization.py`) 49 | - Performance and Latency 50 | - Retail Agent Tuning 51 | - Performance Visualization 52 | 53 | ### 🌐 Integration with APIs (`api_integration.py`) 54 | - External Retail APIs 55 | - API-Driven Retail Tasks 56 | - API Call Visualization 57 | 58 | ## 💡 Why Master Advanced Features? 59 | 60 | Advanced LangChain features are critical for production-grade AI: 61 | 1. **Retail Relevance**: Enable complex workflows and API-driven tasks. 62 | 2. **Interview Relevance**: Tested in coding challenges (e.g., custom chain design). 63 | 3. **Performance**: Optimize accuracy and speed for real-world use. 64 | 4. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles. 65 | 66 | ## 📆 Study Plan 67 | 68 | - **Week 1**: 69 | - Day 1-2: Custom Chains 70 | - Day 3-4: Evaluation Metrics 71 | - Day 5-6: Agent Optimization 72 | - Day 7: Integration with APIs 73 | - **Week 2**: 74 | - Day 1-7: Review `.py` files and practice interview scenarios. 75 | 76 | ## 🛠️ Setup Instructions 77 | 78 | 1. **Python Environment**: 79 | - Install Python 3.8+ and pip. 80 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 81 | - Install dependencies: `pip install langchain langchain-openai numpy matplotlib pandas nltk rouge-score`. 82 | 2. **API Keys**: 83 | - Obtain an OpenAI API key (replace `"your-openai-api-key"` in code). 84 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 85 | 3. **Datasets**: 86 | - Uses synthetic retail data (e.g., customer queries, product data). 87 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets). 88 | - Note: `.py` files use simulated data to avoid file I/O constraints. 89 | 4. **Running Code**: 90 | - Run `.py` files (e.g., `python custom_chains.py`). 91 | - Use Google Colab or local setup with GPU support. 92 | - View outputs in terminal and Matplotlib visualizations (PNGs). 93 | - Check terminal for errors; ensure dependencies and API keys are set. 94 | 95 | ## 🏆 Practical Tasks 96 | 97 | 1. **Custom Chains**: 98 | - Build a chain for retail query processing. 99 | - Visualize workflow steps. 100 | 2. **Evaluation Metrics**: 101 | - Evaluate responses with BLEU and ROUGE. 102 | - Plot metric scores. 103 | 3. **Agent Optimization**: 104 | - Optimize an agent for customer support. 105 | - Analyze latency improvements. 106 | 4. **Integration with APIs**: 107 | - Connect to a mock retail API. 108 | - Visualize API call frequencies. 109 | 110 | ## 💡 Interview Tips 111 | 112 | - **Common Questions**: 113 | - How do custom chains handle complex tasks? 114 | - What are BLEU and ROUGE metrics used for? 115 | - How do you optimize agent performance? 116 | - How does LangChain integrate with external APIs? 117 | - **Tips**: 118 | - Explain custom chains with code (e.g., `SequentialChain`). 119 | - Demonstrate metric calculation (e.g., `RougeScorer`). 120 | - Code tasks like agent optimization or API integration. 121 | - Discuss trade-offs (e.g., chain complexity vs. latency). 122 | - **Coding Tasks**: 123 | - Implement a custom chain for retail tasks. 124 | - Calculate BLEU for response evaluation. 125 | - **Conceptual Clarity**: 126 | - Explain chain modularity and API integration. 127 | - Describe optimization techniques for agents. 128 | 129 | ## 📚 Resources 130 | 131 | - [LangChain Documentation](https://python.langchain.com/docs/) 132 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 133 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 134 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 135 | - [ROUGE Documentation](https://github.com/pltrdy/rouge) 136 | 137 | ## 🤝 Contributions 138 | 139 | 1. Fork the repository. 140 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 141 | 3. Commit changes (`git commit -m 'Add some amazing content'`). 142 | 4. Push to the branch (`git push origin feature/amazing-addition`). 143 | 5. Open a Pull Request. 144 | 145 | --- 146 | 147 |
148 |

Happy Learning and Good Luck with Your Interviews! ✨

149 |
-------------------------------------------------------------------------------- /Langchian Fundamentals/05 Retail Applications/README.md: -------------------------------------------------------------------------------- 1 | # 🛒 Retail Applications with LangChain 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | Faiss 8 | Matplotlib 9 |
10 |

Your guide to mastering retail applications with LangChain for AI/ML and retail-focused interviews

11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Retail Applications** subsection of the **LangChain Library Roadmap**! 🚀 This folder explores practical retail use cases, including chatbots, recommendation systems, review analysis, and query answering. Designed for hands-on learning and interview success, it builds on your prior roadmaps and supports your retail-themed projects (April 26, 2025). This section equips you with skills for retail AI roles using LangChain. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Chatbots**: Conversational agents for customer support with memory. 21 | - **Recommendation Systems**: Product recommendations using embeddings. 22 | - **Review Analysis**: Sentiment and topic extraction from reviews. 23 | - **Query Answering**: Answer customer queries using RAG. 24 | - **Hands-on Code**: Four `.py` files with examples using synthetic retail data. 25 | - **Interview Scenarios**: Key questions and answers for LangChain interviews. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - AI Engineers building retail-focused AI applications. 30 | - Machine Learning Engineers developing retail recommendation or analysis systems. 31 | - AI Researchers mastering LangChain for retail use cases. 32 | - Software Engineers deepening expertise in LangChain for retail. 33 | - Anyone preparing for AI/ML interviews in retail or tech. 34 | 35 | ## 🗺️ Learning Roadmap 36 | 37 | This subsection covers four key retail applications, each with a dedicated `.py` file: 38 | 39 | ### 🤖 Chatbots (`chatbots.py`) 40 | - Conversational Agents 41 | - Memory-Based Support 42 | - Interaction Visualization 43 | 44 | ### 🛍️ Recommendation Systems (`recommendation_systems.py`) 45 | - Embedding-Based Recommendations 46 | - Product Matching 47 | - Recommendation Visualization 48 | 49 | ### 📝 Review Analysis (`review_analysis.py`) 50 | - Sentiment Extraction 51 | - Topic Modeling 52 | - Sentiment Visualization 53 | 54 | ### ❓ Query Answering (`query_answering.py`) 55 | - RAG-Based Answers 56 | - Retail Query Handling 57 | - Response Visualization 58 | 59 | ## 💡 Why Master Retail Applications? 60 | 61 | Retail applications are critical for customer-focused AI: 62 | 1. **Retail Relevance**: Enhance customer support, sales, and insights. 63 | 2. **Interview Relevance**: Tested in coding challenges (e.g., chatbot design). 64 | 3. **Practicality**: Directly applicable to retail business needs. 65 | 4. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles. 66 | 67 | ## 📆 Study Plan 68 | 69 | - **Week 1**: 70 | - Day 1-2: Chatbots 71 | - Day 3-4: Recommendation Systems 72 | - Day 5-6: Review Analysis 73 | - Day 7: Query Answering 74 | - **Week 2**: 75 | - Day 1-7: Review `.py` files and practice interview scenarios. 76 | 77 | ## 🛠️ Setup Instructions 78 | 79 | 1. **Python Environment**: 80 | - Install Python 3.8+ and pip. 81 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 82 | - Install dependencies: `pip install langchain langchain-openai faiss-cpu numpy matplotlib pandas nltk scikit-learn`. 83 | 2. **API Keys**: 84 | - Obtain an OpenAI API key (replace `"your-openai-api-key"` in code). 85 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 86 | 3. **Datasets**: 87 | - Uses synthetic retail data (e.g., customer queries, reviews, products). 88 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets). 89 | - Note: `.py` files use simulated data to avoid file I/O constraints. 90 | 4. **Running Code**: 91 | - Run `.py` files (e.g., `python chatbots.py`). 92 | - Use Google Colab or local setup with GPU support. 93 | - View outputs in terminal and Matplotlib visualizations (PNGs). 94 | - Check terminal for errors; ensure dependencies and API keys are set. 95 | 96 | ## 🏆 Practical Tasks 97 | 98 | 1. **Chatbots**: 99 | - Build a customer support chatbot with memory. 100 | - Visualize conversation lengths. 101 | 2. **Recommendation Systems**: 102 | - Create a product recommendation system. 103 | - Plot recommendation similarities. 104 | 3. **Review Analysis**: 105 | - Analyze sentiment in retail reviews. 106 | - Visualize sentiment distribution. 107 | 4. **Query Answering**: 108 | - Implement RAG for customer queries. 109 | - Compare RAG vs. non-RAG responses. 110 | 111 | ## 💡 Interview Tips 112 | 113 | - **Common Questions**: 114 | - How do chatbots use memory in LangChain? 115 | - How do embeddings power recommendation systems? 116 | - How is sentiment extracted from reviews? 117 | - How does RAG improve query answering? 118 | - **Tips**: 119 | - Explain chatbots with code (e.g., `ConversationBufferMemory`). 120 | - Demonstrate recommendation systems (e.g., `FAISS` embeddings). 121 | - Code tasks like sentiment analysis or RAG setup. 122 | - Discuss trade-offs (e.g., chatbot latency vs. context). 123 | - **Coding Tasks**: 124 | - Build a retail chatbot. 125 | - Implement a RAG-based query system. 126 | - **Conceptual Clarity**: 127 | - Explain memory in conversational agents. 128 | - Describe RAG’s role in query answering. 129 | 130 | ## 📚 Resources 131 | 132 | - [LangChain Documentation](https://python.langchain.com/docs/) 133 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 134 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 135 | - [Faiss Documentation](https://github.com/facebookresearch/faiss) 136 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 137 | 138 | ## 🤝 Contributions 139 | 140 | 1. Fork the repository. 141 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 142 | 3. Commit changes (`git commit -m 'Add some amazing content'`). 143 | 4. Push to the branch (`git push origin feature/amazing-addition`). 144 | 5. Open a Pull Request. 145 | 146 | --- 147 | 148 |
149 |

Happy Learning and Good Luck with Your Interviews! ✨

150 |
-------------------------------------------------------------------------------- /Langchian Fundamentals/02 Retrieval Augmented Generation/README.md: -------------------------------------------------------------------------------- 1 | # 📚 Retrieval-Augmented Generation (RAG) with LangChain 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | Faiss 8 | Matplotlib 9 |
10 |

Your guide to mastering Retrieval-Augmented Generation (RAG) with LangChain for AI/ML and retail-focused interviews

11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Retrieval-Augmented Generation (RAG)** subsection of the **LangChain Library Roadmap**! 🚀 This folder explores RAG, a technique to enhance LLM responses with external knowledge, using vector stores, document loaders, and embedding models. Designed for hands-on learning and interview success, it builds on your prior roadmaps and supports your retail-themed projects (April 26, 2025). This section equips you with skills for retail AI roles using LangChain. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Vector Stores**: Faiss for efficient document retrieval. 21 | - **Document Loaders and Splitters**: Process large retail documents. 22 | - **RAG Pipeline**: Combine retrieval and generation for accurate responses. 23 | - **Embedding Models**: Use Hugging Face or OpenAI embeddings. 24 | - **Hands-on Code**: Four `.py` files with examples using synthetic retail data. 25 | - **Interview Scenarios**: Key questions and answers for LangChain interviews. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - AI Engineers building knowledge-enhanced LLM applications. 30 | - Machine Learning Engineers developing RAG systems. 31 | - AI Researchers mastering LangChain’s retrieval capabilities. 32 | - Software Engineers deepening expertise in LangChain for retail. 33 | - Anyone preparing for AI/ML interviews in retail or tech. 34 | 35 | ## 🗺️ Learning Roadmap 36 | 37 | This subsection covers four key RAG components, each with a dedicated `.py` file: 38 | 39 | ### 📈 Vector Stores (`vector_stores.py`) 40 | - Faiss-Based Retrieval 41 | - Document Indexing 42 | - Retrieval Visualization 43 | 44 | ### 📄 Document Loaders and Splitters (`document_loaders_splitters.py`) 45 | - Document Processing 46 | - Text Splitting 47 | - Chunk Visualization 48 | 49 | ### 🔄 RAG Pipeline (`rag_pipeline.py`) 50 | - Retrieval and Generation 51 | - Retail Query Answering 52 | - Response Visualization 53 | 54 | ### 🧠 Embedding Models (`embedding_models.py`) 55 | - Hugging Face and OpenAI Embeddings 56 | - Embedding Comparison 57 | - Similarity Visualization 58 | 59 | ## 💡 Why Master RAG? 60 | 61 | RAG is critical for accurate, context-aware AI applications: 62 | 1. **Retail Relevance**: Enhances product query answers with manuals. 63 | 2. **Interview Relevance**: Tested in coding challenges (e.g., RAG setup). 64 | 3. **Accuracy**: Combines LLM generation with factual retrieval. 65 | 4. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles. 66 | 67 | ## 📆 Study Plan 68 | 69 | - **Week 1**: 70 | - Day 1-2: Vector Stores 71 | - Day 3-4: Document Loaders and Splitters 72 | - Day 5-6: RAG Pipeline 73 | - Day 7: Embedding Models 74 | - **Week 2**: 75 | - Day 1-7: Review `.py` files and practice interview scenarios. 76 | 77 | ## 🛠️ Setup Instructions 78 | 79 | 1. **Python Environment**: 80 | - Install Python 3.8+ and pip. 81 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 82 | - Install dependencies: `pip install langchain langchain-openai langchain-huggingface faiss-cpu numpy matplotlib pandas nltk`. 83 | 2. **API Keys**: 84 | - Obtain an OpenAI API key (replace `"your-openai-api-key"` in code). 85 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 86 | - Optional: Use Hugging Face models (`langchain-huggingface`). 87 | 3. **Datasets**: 88 | - Uses synthetic retail data (e.g., product manuals, queries). 89 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets). 90 | - Note: `.py` files use simulated data to avoid file I/O constraints. 91 | 4. **Running Code**: 92 | - Run `.py` files (e.g., `python vector_stores.py`). 93 | - Use Google Colab or local setup with GPU support. 94 | - View outputs in terminal and Matplotlib visualizations (PNGs). 95 | - Check terminal for errors; ensure dependencies and API keys are set. 96 | 97 | ## 🏆 Practical Tasks 98 | 99 | 1. **Vector Stores**: 100 | - Index retail documents with Faiss. 101 | - Visualize retrieval similarities. 102 | 2. **Document Loaders and Splitters**: 103 | - Process synthetic manuals. 104 | - Analyze chunk sizes. 105 | 3. **RAG Pipeline**: 106 | - Build a RAG system for product queries. 107 | - Compare RAG vs. non-RAG responses. 108 | 4. **Embedding Models**: 109 | - Compare Hugging Face and OpenAI embeddings. 110 | - Visualize embedding similarities. 111 | 112 | ## 💡 Interview Tips 113 | 114 | - **Common Questions**: 115 | - How does RAG improve LLM accuracy? 116 | - What’s the role of vector stores in RAG? 117 | - How do document splitters optimize retrieval? 118 | - How do embedding models impact RAG performance? 119 | - **Tips**: 120 | - Explain RAG with code (e.g., `FAISS.from_texts`). 121 | - Demonstrate document splitting (e.g., `CharacterTextSplitter`). 122 | - Code tasks like RAG pipeline setup. 123 | - Discuss trade-offs (e.g., retrieval speed vs. accuracy). 124 | - **Coding Tasks**: 125 | - Build a Faiss-based vector store. 126 | - Implement a RAG pipeline for retail queries. 127 | - **Conceptual Clarity**: 128 | - Explain RAG’s retrieval-generation synergy. 129 | - Describe embedding model trade-offs. 130 | 131 | ## 📚 Resources 132 | 133 | - [LangChain Documentation](https://python.langchain.com/docs/) 134 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 135 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 136 | - [Faiss Documentation](https://github.com/facebookresearch/faiss) 137 | - [Hugging Face Documentation](https://huggingface.co/docs) 138 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 139 | 140 | ## 🤝 Contributions 141 | 142 | 1. Fork the repository. 143 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 144 | 3. Commit changes (`git commit -m 'Add some amazing content'`). 145 | 4. Push to the branch (`git push origin feature/amazing-addition`). 146 | 5. Open a Pull Request. 147 | 148 | --- 149 | 150 |
151 |

Happy Learning and Good Luck with Your Interviews! ✨

152 |
-------------------------------------------------------------------------------- /Langchian Fundamentals/01 Core Components/README.md: -------------------------------------------------------------------------------- 1 | # 🛠️ Core Components of LangChain 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | NumPy 8 | Matplotlib 9 |
10 |

Your guide to mastering LangChain's core components for AI/ML and retail-focused interviews

11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Core Components** subsection of the **LangChain Library Roadmap**! 🚀 This folder dives into the foundational elements of the **LangChain** library, including chains, prompts, memory, and document loaders. Designed for hands-on learning and interview success, it builds on your prior roadmaps—**Python**, **TensorFlow.js**, **GenAI**, **JavaScript**, **Keras**, **Matplotlib**, **Pandas**, **NumPy**, **Computer Vision with OpenCV (cv2)**, **NLP with NLTK**, and **Hugging Face Transformers**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in retail AI roles using LangChain. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Chains**: Sequential workflows for LLM tasks (e.g., LLMChain, SequentialChain). 21 | - **Prompts**: Dynamic prompt engineering for retail queries. 22 | - **Memory**: Contextual conversation history for customer interactions. 23 | - **Document Loaders**: Process retail data like product manuals and reviews. 24 | - **Hands-on Code**: Four `.py` files with practical examples using synthetic retail data (e.g., customer queries, product descriptions). 25 | - **Interview Scenarios**: Key questions and answers to ace LangChain interviews. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - AI Engineers building LLM-powered retail applications. 30 | - Machine Learning Engineers creating workflows with LangChain. 31 | - AI Researchers mastering LangChain’s core components. 32 | - Software Engineers deepening expertise in LangChain for retail use cases. 33 | - Anyone preparing for AI/ML interviews in retail or tech. 34 | 35 | ## 🗺️ Learning Roadmap 36 | 37 | This subsection covers four key core components, each with a dedicated `.py` file: 38 | 39 | ### 🔗 Chains (`chains.py`) 40 | - Sequential Workflows 41 | - LLMChain and SequentialChain 42 | - Workflow Visualization 43 | 44 | ### 📝 Prompts (`prompts.py`) 45 | - Prompt Engineering 46 | - Dynamic Prompts for Retail 47 | - Response Visualization 48 | 49 | ### 🧠 Memory (`memory.py`) 50 | - Conversation History 51 | - Contextual Retail Interactions 52 | - Context Visualization 53 | 54 | ### 📚 Document Loaders (`document_loaders.py`) 55 | - Retail Data Processing 56 | - Text Extraction 57 | - Data Visualization 58 | 59 | ## 💡 Why Master Core Components? 60 | 61 | LangChain’s core components are essential for building intelligent AI applications, and here’s why they matter: 62 | 1. **Foundation**: Chains, prompts, memory, and loaders form the backbone of LangChain workflows. 63 | 2. **Retail Relevance**: Enable customer support, product queries, and review processing. 64 | 3. **Interview Relevance**: Tested in coding challenges (e.g., chain design, prompt engineering). 65 | 4. **Flexibility**: Support diverse retail tasks with modular components. 66 | 5. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles. 67 | 68 | This section is your roadmap to mastering LangChain’s core components for technical interviews—let’s dive in! 69 | 70 | ## 📆 Study Plan 71 | 72 | - **Week 1**: 73 | - Day 1-2: Chains 74 | - Day 3-4: Prompts 75 | - Day 5-6: Memory 76 | - Day 7: Document Loaders 77 | - **Week 2**: 78 | - Day 1-7: Review all `.py` files and practice interview scenarios. 79 | 80 | ## 🛠️ Setup Instructions 81 | 82 | 1. **Python Environment**: 83 | - Install Python 3.8+ and pip. 84 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 85 | - Install dependencies: `pip install langchain langchain-openai numpy matplotlib pandas nltk`. 86 | 2. **API Keys**: 87 | - Obtain an OpenAI API key for LLM access (replace `"your-openai-api-key"` in code). 88 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 89 | - Alternatively, use Hugging Face models with `langchain-huggingface` (`pip install langchain-huggingface huggingface_hub`). 90 | 3. **Datasets**: 91 | - Uses synthetic retail data (e.g., customer queries, product descriptions, reviews). 92 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets) (e.g., Amazon Reviews). 93 | - Note: `.py` files use simulated data to avoid file I/O constraints. 94 | 4. **Running Code**: 95 | - Run `.py` files in a Python environment (e.g., `python chains.py`). 96 | - Use Google Colab for convenience or local setup with GPU support for faster processing. 97 | - View outputs in terminal (console logs) and Matplotlib visualizations (saved as PNGs). 98 | - Check terminal for errors; ensure dependencies and API keys are configured. 99 | 100 | ## 🏆 Practical Tasks 101 | 102 | 1. **Chains**: 103 | - Build a chain for retail product queries. 104 | - Visualize chain response lengths. 105 | 2. **Prompts**: 106 | - Create dynamic prompts for customer support queries. 107 | - Analyze prompt response quality. 108 | 3. **Memory**: 109 | - Implement a conversational agent with context retention. 110 | - Visualize conversation history length. 111 | 4. **Document Loaders**: 112 | - Process synthetic retail manuals or reviews. 113 | - Visualize extracted text statistics. 114 | 115 | ## 💡 Interview Tips 116 | 117 | - **Common Questions**: 118 | - How do LangChain chains work for LLM workflows? 119 | - What’s the role of prompt engineering in LangChain? 120 | - How does memory enhance conversational agents? 121 | - How do document loaders process external data? 122 | - **Tips**: 123 | - Explain chains with code (e.g., `LLMChain` with `PromptTemplate`). 124 | - Demonstrate prompt engineering (e.g., `PromptTemplate` for retail queries). 125 | - Be ready to code tasks like memory implementation or document loading. 126 | - Discuss trade-offs (e.g., chain complexity vs. performance, memory size vs. latency). 127 | - **Coding Tasks**: 128 | - Implement a chain for retail query processing. 129 | - Create a dynamic prompt for customer support. 130 | - Build a conversational agent with memory. 131 | - **Conceptual Clarity**: 132 | - Explain how chains combine LLMs with prompts. 133 | - Describe the role of memory in maintaining context. 134 | 135 | ## 📚 Resources 136 | 137 | - [LangChain Documentation](https://python.langchain.com/docs/) 138 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 139 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 140 | - [NumPy Documentation](https://numpy.org/doc/) 141 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 142 | - [“Deep Learning with Python” by François Chollet](https://www.manning.com/books/deep-learning-with-python) 143 | 144 | ## 🤝 Contributions 145 | 146 | Love to collaborate? Here’s how! 🌟 147 | 1. Fork the repository. 148 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 149 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 150 | 4. Push to the branch (`git push origin feature/amazing-addition`). 151 | 5. Open a Pull Request. 152 | 153 | --- 154 | 155 |
156 |

Happy Learning and Good Luck with Your Interviews! ✨

157 |
-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🦜🔗 LangChain Library Roadmap - Interview Preparation 2 | 3 |
4 | Python Logo 5 | LangChain 6 | OpenAI 7 | Faiss 8 | NumPy 9 | Matplotlib 10 |
11 |

Your comprehensive guide to mastering the LangChain library for AI/ML and retail-focused interviews

12 | 13 | --- 14 | 15 | ## 📖 Introduction 16 | 17 | Welcome to the **LangChain Library Roadmap** for AI/ML and retail-focused interview preparation! 🚀 This roadmap dives deep into the **LangChain** library, a powerful framework for building applications powered by large language models (LLMs) with external tools, memory, and data retrieval. Covering all major **LangChain components** and retail applications, it’s designed for hands-on learning and interview success, building on your prior roadmaps—**Python**, **TensorFlow.js**, **GenAI**, **JavaScript**, **Keras**, **Matplotlib**, **Pandas**, **NumPy**, **Computer Vision with OpenCV (cv2)**, **NLP with NLTK**, and **Hugging Face Transformers**. Tailored to your retail-themed projects (April 26, 2025), this roadmap equips you with the skills to excel in advanced AI roles, whether tackling coding challenges or technical discussions. 18 | 19 | ## 🌟 What’s Inside? 20 | 21 | - **LangChain Components**: Chains, prompts, memory, and document loaders for LLM workflows. 22 | - **Retrieval-Augmented Generation (RAG)**: Knowledge-enhanced LLM responses with vector stores. 23 | - **AI Agents**: Autonomous agents with tools and reasoning for retail tasks. 24 | - **Advanced Features**: Custom chains, agent optimization, and evaluation metrics. 25 | - **Retail Applications**: Chatbots, recommendation systems, review analysis, and query answering. 26 | - **Hands-on Code**: Subsections with `.py` files using synthetic retail data (e.g., product reviews, customer queries). 27 | - **Interview Scenarios**: Key questions and answers to ace LangChain-related interviews. 28 | 29 | ## 🔍 Who Is This For? 30 | 31 | - AI Engineers building LLM-powered retail applications. 32 | - Machine Learning Engineers developing RAG systems or AI agents. 33 | - AI Researchers exploring LangChain’s capabilities with LLMs. 34 | - Software Engineers deepening expertise in LangChain for retail use cases. 35 | - Anyone preparing for AI/ML interviews in retail or tech. 36 | 37 | ## 🗺️ Learning Roadmap 38 | 39 | This roadmap is organized into subsections, each covering a key aspect of the LangChain library. Each subsection includes a dedicated folder with a `README.md` and `.py` files for practical demos. 40 | 41 | ### 🛠️ Core Components 42 | - **Chains**: Sequential workflows for LLM tasks (e.g., LLMChain, SequentialChain). 43 | - **Prompts**: Dynamic prompt engineering for retail queries. 44 | - **Memory**: Contextual conversation history for customer interactions. 45 | - **Document Loaders**: Process retail data (e.g., product manuals, reviews). 46 | 47 | ### 📚 Retrieval-Augmented Generation (RAG) 48 | - **Vector Stores**: Faiss or Chroma for document retrieval. 49 | - **Document Loaders and Splitters**: Handle large retail documents. 50 | - **RAG Pipeline**: Enhance LLM responses with external knowledge. 51 | - **Embedding Models**: Use Hugging Face or OpenAI embeddings. 52 | 53 | ### 🤖 AI Agents 54 | - **Tool Integration**: Use tools like search, calculators, or APIs for retail tasks. 55 | - **Agent Types**: Reactive, planning, and ReAct agents. 56 | - **Agent Reasoning**: Autonomous decision-making for customer support. 57 | - **Custom Agents**: Build agents for specific retail scenarios. 58 | 59 | ### 🚀 Advanced Features 60 | - **Custom Chains**: Design tailored workflows for complex tasks. 61 | - **Evaluation Metrics**: BLEU, ROUGE, and custom metrics for response quality. 62 | - **Agent Optimization**: Optimize agent performance and latency. 63 | - **Integration with APIs**: Connect LangChain with external retail APIs. 64 | 65 | ### 🛒 Retail Applications 66 | - **Chatbots**: Conversational agents for customer support with memory. 67 | - **Recommendation Systems**: Product recommendations using embeddings. 68 | - **Review Analysis**: Sentiment and topic extraction from reviews. 69 | - **Query Answering**: Answer customer queries using RAG. 70 | 71 | ## 💡 Why Master LangChain? 72 | 73 | LangChain is a cornerstone for building intelligent, context-aware AI applications, and here’s why it matters: 74 | 1. **Retail Relevance**: Powers customer support chatbots, personalized recommendations, and review analysis. 75 | 2. **Scalability**: Combines LLMs with external data and tools for robust applications. 76 | 3. **Interview Relevance**: Tested in coding challenges (e.g., RAG implementation, agent design). 77 | 4. **Flexibility**: Supports diverse use cases with chains, memory, and agents. 78 | 5. **Industry Demand**: A must-have for 6 LPA+ AI/ML roles in retail and tech. 79 | 80 | This roadmap is your guide to mastering LangChain for technical interviews and retail AI projects—let’s dive in! 81 | 82 | ## 📆 Study Plan 83 | 84 | - **Month 1**: 85 | - Week 1: Core Components (Chains, Prompts) 86 | - Week 2: Core Components (Memory, Document Loaders) 87 | - Week 3: Retrieval-Augmented Generation (Vector Stores, RAG Pipeline) 88 | - Week 4: Retrieval-Augmented Generation (Embedding Models, Document Splitters) 89 | - **Month 2**: 90 | - Week 1: AI Agents (Tool Integration, Agent Types) 91 | - Week 2: AI Agents (Agent Reasoning, Custom Agents) 92 | - Week 3: Advanced Features (Custom Chains, Evaluation Metrics) 93 | - Week 4: Advanced Features (Agent Optimization, API Integration) 94 | - **Month 3**: 95 | - Week 1: Retail Applications (Chatbots, Review Analysis) 96 | - Week 2: Retail Applications (Recommendation Systems, Query Answering) 97 | - Week 3: Review all subsections and practice coding tasks 98 | - Week 4: Prepare for interviews with scenarios and mock coding challenges 99 | 100 | ## 🛠️ Setup Instructions 101 | 102 | 1. **Python Environment**: 103 | - Install Python 3.8+ and pip. 104 | - Create a virtual environment: `python -m venv langchain_env; source langchain_env/bin/activate`. 105 | - Install dependencies: `pip install langchain langchain-openai faiss-cpu numpy matplotlib pandas scikit-learn`. 106 | 2. **API Keys**: 107 | - Obtain an OpenAI API key for LLM access (replace `"your-openai-api-key"` in code). 108 | - Set environment variable: `export OPENAI_API_KEY="your-openai-api-key"`. 109 | - Alternatively, use Hugging Face models with `langchain-huggingface` (`pip install langchain-huggingface huggingface_hub`). 110 | 3. **Datasets**: 111 | - Uses synthetic retail data (e.g., product descriptions, customer queries, reviews). 112 | - Optional: Download datasets from [Hugging Face Datasets](https://huggingface.co/datasets) (e.g., Amazon Reviews). 113 | - Note: `.py` files use simulated data to avoid file I/O constraints. 114 | 4. **Running Code**: 115 | - Run `.py` files in a Python environment (e.g., `python core_components.py`). 116 | - Use Google Colab for convenience or local setup with GPU support for faster processing. 117 | - View outputs in terminal (console logs) and Matplotlib visualizations (saved as PNGs). 118 | - Check terminal for errors; ensure dependencies and API keys are configured. 119 | 120 | ## 🏆 Practical Tasks 121 | 122 | 1. **Core Components**: 123 | - Build a chain to answer retail product queries. 124 | - Implement a conversational agent with memory for customer support. 125 | 2. **Retrieval-Augmented Generation (RAG)**: 126 | - Create a RAG system for product manual queries. 127 | - Use Faiss to retrieve relevant documents for customer questions. 128 | 3. **AI Agents**: 129 | - Design an agent to track orders using a mock API tool. 130 | - Build a planning agent for retail inventory queries. 131 | 4. **Advanced Features**: 132 | - Develop a custom chain for multi-step retail workflows. 133 | - Evaluate response quality with BLEU and ROUGE metrics. 134 | 5. **Retail Applications**: 135 | - Build a chatbot for customer queries with RAG and memory. 136 | - Create a recommendation system using product description embeddings. 137 | - Analyze sentiment in retail reviews. 138 | 139 | ## 💡 Interview Tips 140 | 141 | - **Common Questions**: 142 | - What is LangChain, and how does it enhance LLM applications? 143 | - How does RAG improve LLM response accuracy? 144 | - What are the differences between reactive and planning agents in LangChain? 145 | - How can LangChain be applied to retail use cases? 146 | - **Tips**: 147 | - Explain chains with code (e.g., `LLMChain` with `PromptTemplate`). 148 | - Demonstrate RAG with a vector store (e.g., `FAISS.from_texts`). 149 | - Be ready to code tasks like agent tool integration or review analysis. 150 | - Discuss trade-offs (e.g., RAG latency vs. accuracy, agent complexity vs. reliability). 151 | - **Coding Tasks**: 152 | - Implement a simple chain for retail queries. 153 | - Build a RAG system for product information. 154 | - Design an agent for customer support. 155 | - **Conceptual Clarity**: 156 | - Explain how LangChain integrates LLMs with external data and tools. 157 | - Describe the role of memory in maintaining conversational context. 158 | 159 | ## 📚 Resources 160 | 161 | - [LangChain Documentation](https://python.langchain.com/docs/) 162 | - [LangChain GitHub](https://github.com/langchain-ai/langchain) 163 | - [OpenAI API Documentation](https://platform.openai.com/docs/) 164 | - [Faiss Documentation](https://github.com/facebookresearch/faiss) 165 | - [Hugging Face Datasets Documentation](https://huggingface.co/docs/datasets/) 166 | - [NumPy Documentation](https://numpy.org/doc/) 167 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 168 | - [“Deep Learning with Python” by François Chollet](https://www.manning.com/books/deep-learning-with-python) 169 | 170 | ## 🤝 Contributions 171 | 172 | Love to collaborate? Here’s how! 🌟 173 | 1. Fork the repository. 174 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 175 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 176 | 4. Push to the branch (`git push origin feature/amazing-addition`). 177 | 5. Open a Pull Request. 178 | 179 | --- 180 | 181 |
182 |

Happy Learning and Good Luck with Your Interviews! ✨

183 |
-------------------------------------------------------------------------------- /Langchain Interview Questions/README.md: -------------------------------------------------------------------------------- 1 | # LangChain Interview Questions for AI/ML Roles (NLP and Generative AI) 2 | 3 | This README provides 170 LangChain interview questions tailored for AI/ML roles, focusing on natural language processing (NLP) and generative AI applications. The questions cover **core LangChain concepts** (e.g., chains, agents, memory, retrieval, tools) and their use in building, deploying, and optimizing LLM-powered applications. Questions are categorized by topic and divided into **Basic**, **Intermediate**, and **Advanced** levels to support candidates preparing for roles requiring LangChain in AI/ML workflows. 4 | 5 | ## LangChain Basics 6 | 7 | ### Basic 8 | 1. **What is LangChain, and how is it used in NLP applications?** 9 | LangChain is a framework for building applications with LLMs, enabling context-aware NLP tasks like chatbots. 10 | ```python 11 | from langchain.llms import OpenAI 12 | llm = OpenAI(model_name="text-davinci-003") 13 | response = llm("What is NLP?") 14 | ``` 15 | 16 | 2. **How do you install LangChain and its dependencies?** 17 | Installs LangChain via pip, typically with an LLM provider. 18 | ```python 19 | # Install LangChain and OpenAI 20 | !pip install langchain openai 21 | ``` 22 | 23 | 3. **What are the core components of LangChain?** 24 | Includes LLMs, chains, agents, memory, and retrievers for NLP tasks. 25 | ```python 26 | from langchain.chains import LLMChain 27 | from langchain.prompts import PromptTemplate 28 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize: {text}")) 29 | ``` 30 | 31 | 4. **How do you configure an LLM in LangChain for text generation?** 32 | Sets up an LLM with API keys and parameters. 33 | ```python 34 | from langchain.llms import OpenAI 35 | llm = OpenAI(api_key="your-api-key", temperature=0.7) 36 | ``` 37 | 38 | 5. **What is a PromptTemplate in LangChain, and how is it used?** 39 | Structures prompts for consistent NLP inputs. 40 | ```python 41 | from langchain.prompts import PromptTemplate 42 | prompt = PromptTemplate(input_variables=["topic"], template="Explain {topic} in simple terms.") 43 | ``` 44 | 45 | 6. **How do you save and load a LangChain chain for reuse?** 46 | Persists chain configurations for NLP applications. 47 | ```python 48 | chain.save("chain.json") 49 | from langchain.chains import load_chain 50 | loaded_chain = load_chain("chain.json") 51 | ``` 52 | 53 | #### Intermediate 54 | 7. **Write a function to create a simple LangChain LLMChain for text summarization.** 55 | Builds a chain for summarizing text inputs. 56 | ```python 57 | from langchain.chains import LLMChain 58 | from langchain.llms import OpenAI 59 | from langchain.prompts import PromptTemplate 60 | def create_summary_chain(): 61 | llm = OpenAI(model_name="text-davinci-003") 62 | prompt = PromptTemplate(input_variables=["text"], template="Summarize: {text}") 63 | return LLMChain(llm=llm, prompt=prompt) 64 | ``` 65 | 66 | 8. **How do you use LangChain to integrate with external APIs like OpenAI?** 67 | Configures API access for LLM calls. 68 | ```python 69 | import os 70 | os.environ["OPENAI_API_KEY"] = "your-api-key" 71 | from langchain.llms import OpenAI 72 | llm = OpenAI() 73 | ``` 74 | 75 | 9. **Explain the role of callbacks in LangChain for monitoring.** 76 | Tracks chain execution for debugging NLP tasks. 77 | ```python 78 | from langchain.callbacks import StdOutCallbackHandler 79 | chain = LLMChain(llm=llm, prompt=prompt, callbacks=[StdOutCallbackHandler()]) 80 | ``` 81 | 82 | 10. **How do you handle API rate limits in LangChain?** 83 | Uses retry mechanisms for robust LLM calls. 84 | ```python 85 | from langchain.llms import OpenAI 86 | llm = OpenAI(max_retries=3, retry_delay=2) 87 | ``` 88 | 89 | 11. **Write a function to generate text completions with LangChain.** 90 | Produces NLP outputs for user prompts. 91 | ```python 92 | def generate_completion(prompt_text, model_name="text-davinci-003"): 93 | llm = OpenAI(model_name=model_name) 94 | return llm(prompt_text) 95 | ``` 96 | 97 | 12. **How do you use LangChain to switch between different LLM providers?** 98 | Abstracts LLM interfaces for flexibility. 99 | ```python 100 | from langchain.llms import HuggingFaceHub 101 | llm = HuggingFaceHub(repo_id="google/flan-t5-base", huggingfacehub_api_token="your-token") 102 | ``` 103 | 104 | #### Advanced 105 | 13. **Implement a custom LangChain LLM wrapper for a local model.** 106 | Integrates a custom NLP model into LangChain. 107 | ```python 108 | from langchain.llms.base import LLM 109 | from typing import Optional, List 110 | class CustomLLM(LLM): 111 | model: object 112 | def __init__(self, model): 113 | super().__init__() 114 | self.model = model 115 | def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str: 116 | return self.model.generate(prompt) 117 | @property 118 | def _identifying_params(self) -> dict: 119 | return {"model": "custom"} 120 | ``` 121 | 122 | 14. **Write a function to optimize LangChain LLM calls for cost efficiency.** 123 | Batches requests to reduce API costs. 124 | ```python 125 | from langchain.llms import OpenAI 126 | def batch_llm_calls(prompts: List[str], model_name="text-davinci-003"): 127 | llm = OpenAI(model_name=model_name) 128 | return llm.generate(prompts) 129 | ``` 130 | 131 | 15. **How do you implement a custom prompt template with dynamic inputs?** 132 | Supports flexible NLP prompt structures. 133 | ```python 134 | from langchain.prompts import PromptTemplate 135 | def create_dynamic_prompt(inputs: List[str]): 136 | template = "Answer the following: {" + "}{".join(inputs) + "}" 137 | return PromptTemplate(input_variables=inputs, template=template) 138 | ``` 139 | 140 | 16. **Write a function to validate LangChain LLM outputs.** 141 | Ensures NLP responses meet quality criteria. 142 | ```python 143 | def validate_output(output: str, min_length: int = 10): 144 | if len(output.strip()) < min_length: 145 | raise ValueError("Output too short") 146 | return output 147 | ``` 148 | 149 | 17. **How do you use LangChain to handle multi-lingual NLP tasks?** 150 | Configures LLMs for language-specific prompts. 151 | ```python 152 | from langchain.llms import OpenAI 153 | llm = OpenAI(model_name="text-davinci-003") 154 | response = llm("Translate 'Hello' to Spanish.") 155 | ``` 156 | 157 | 18. **Implement a LangChain callback for logging execution time.** 158 | Monitors performance of NLP chains. 159 | ```python 160 | from langchain.callbacks.base import BaseCallbackHandler 161 | import time 162 | class TimeLogger(BaseCallbackHandler): 163 | def on_chain_start(self, serialized: dict, inputs: dict, **kwargs): 164 | self.start = time.time() 165 | def on_chain_end(self, outputs: dict, **kwargs): 166 | print(f"Chain took {time.time() - self.start} seconds") 167 | ``` 168 | 169 | ## Chains 170 | 171 | ### Basic 172 | 19. **What is a chain in LangChain, and how is it used in NLP?** 173 | Sequences prompts and LLM calls for complex tasks. 174 | ```python 175 | from langchain.chains import LLMChain 176 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 177 | ``` 178 | 179 | 20. **How do you create a sequential chain in LangChain?** 180 | Combines multiple chains for NLP workflows. 181 | ```python 182 | from langchain.chains import SimpleSequentialChain 183 | chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize: {text}")) 184 | chain2 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Explain: {summary}")) 185 | sequential_chain = SimpleSequentialChain(chains=[chain1, chain2]) 186 | ``` 187 | 188 | 21. **What is the difference between LLMChain and SequentialChain?** 189 | LLMChain is single-step; SequentialChain combines multiple steps. 190 | ```python 191 | llm_chain = LLMChain(llm=llm, prompt=prompt) 192 | ``` 193 | 194 | 22. **How do you pass inputs to a LangChain chain?** 195 | Provides variables to prompt templates. 196 | ```python 197 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 198 | response = chain.run(word="algorithm") 199 | ``` 200 | 201 | 23. **What is the role of output parsers in LangChain chains?** 202 | Structures LLM outputs for downstream NLP tasks. 203 | ```python 204 | from langchain.output_parsers import CommaSeparatedListOutputParser 205 | parser = CommaSeparatedListOutputParser() 206 | chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser) 207 | ``` 208 | 209 | 24. **How do you debug a LangChain chain?** 210 | Uses verbose mode to log execution. 211 | ```python 212 | chain = LLMChain(llm=llm, prompt=prompt, verbose=True) 213 | ``` 214 | 215 | #### Intermediate 216 | 25. **Write a function to create a LangChain chain for question answering.** 217 | Builds a chain for contextual NLP responses. 218 | ```python 219 | def create_qa_chain(): 220 | llm = OpenAI(model_name="text-davinci-003") 221 | prompt = PromptTemplate( 222 | input_variables=["question", "context"], 223 | template="Context: {context}\nQuestion: {question}\nAnswer:" 224 | ) 225 | return LLMChain(llm=llm, prompt=prompt) 226 | ``` 227 | 228 | 26. **How do you implement a chain with multiple prompts in LangChain?** 229 | Combines prompts for multi-step NLP tasks. 230 | ```python 231 | from langchain.chains import SequentialChain 232 | chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize: {text}"), output_key="summary") 233 | chain2 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Key points: {summary}"), output_key="points") 234 | chain = SequentialChain(chains=[chain1, chain2], input_variables=["text"], output_variables=["summary", "points"]) 235 | ``` 236 | 237 | 27. **Write a function to chain text generation and parsing.** 238 | Structures NLP outputs for usability. 239 | ```python 240 | from langchain.output_parsers import StructuredOutputParser 241 | def create_structured_chain(): 242 | parser = StructuredOutputParser.from_response_schemas([{"name": "answer", "type": "str"}]) 243 | prompt = PromptTemplate( 244 | template="Answer: {question}\n{format_instructions}", 245 | input_variables=["question"], 246 | partial_variables={"format_instructions": parser.get_format_instructions()} 247 | ) 248 | return LLMChain(llm=llm, prompt=prompt, output_parser=parser) 249 | ``` 250 | 251 | 28. **How do you use LangChain to create a conversational chain?** 252 | Maintains dialogue context for NLP chats. 253 | ```python 254 | from langchain.chains import ConversationChain 255 | from langchain.memory import ConversationBufferMemory 256 | conversation = ConversationChain(llm=llm, memory=ConversationBufferMemory()) 257 | ``` 258 | 259 | 29. **Implement a chain to handle batch processing in LangChain.** 260 | Processes multiple NLP inputs efficiently. 261 | ```python 262 | def batch_chain_processing(inputs: List[dict]): 263 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 264 | return chain.batch(inputs) 265 | ``` 266 | 267 | 30. **How do you handle errors in LangChain chains?** 268 | Uses try-except for robust execution. 269 | ```python 270 | try: 271 | response = chain.run(word="algorithm") 272 | except Exception as e: 273 | print(f"Chain error: {e}") 274 | ``` 275 | 276 | #### Advanced 277 | 31. **Write a function to implement a custom LangChain chain.** 278 | Defines specialized NLP workflows. 279 | ```python 280 | from langchain.chains.base import Chain 281 | class CustomChain(Chain): 282 | llm: object 283 | prompt: object 284 | @property 285 | def input_keys(self) -> List[str]: 286 | return ["input"] 287 | @property 288 | def output_keys(self) -> List[str]: 289 | return ["output"] 290 | def _call(self, inputs: dict) -> dict: 291 | response = self.llm(self.prompt.format(**inputs)) 292 | return {"output": response} 293 | ``` 294 | 295 | 32. **How do you optimize LangChain chains for low-latency NLP tasks?** 296 | Caches prompts and minimizes LLM calls. 297 | ```python 298 | from langchain.prompts import PromptTemplate 299 | prompt = PromptTemplate.from_template("Define {word}", cache=True) 300 | ``` 301 | 302 | 33. **Write a function to implement a parallel chain execution in LangChain.** 303 | Runs multiple NLP tasks concurrently. 304 | ```python 305 | from langchain.chains import LLMChain 306 | from concurrent.futures import ThreadPoolExecutor 307 | def parallel_chains(inputs: List[dict], chain: LLMChain): 308 | with ThreadPoolExecutor() as executor: 309 | results = list(executor.map(chain.run, inputs)) 310 | return results 311 | ``` 312 | 313 | 34. **How do you implement a chain with dynamic routing in LangChain?** 314 | Selects chains based on input type. 315 | ```python 316 | from langchain.chains.router import MultiPromptChain 317 | def create_router_chain(): 318 | prompt1 = PromptTemplate.from_template("Summarize: {text}") 319 | prompt2 = PromptTemplate.from_template("Translate: {text}") 320 | chain1 = LLMChain(llm=llm, prompt=prompt1) 321 | chain2 = LLMChain(llm=llm, prompt=prompt2) 322 | router = MultiPromptChain.from_chains( 323 | chains={"summarize": chain1, "translate": chain2}, 324 | default_chain=chain1 325 | ) 326 | return router 327 | ``` 328 | 329 | 35. **Implement a chain to handle multi-step reasoning in LangChain.** 330 | Breaks down complex NLP tasks. 331 | ```python 332 | from langchain.chains import SequentialChain 333 | def reasoning_chain(): 334 | chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Step 1: {input}"), output_key="step1") 335 | chain2 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Step 2: {step1}"), output_key="step2") 336 | return SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["step2"]) 337 | ``` 338 | 339 | 36. **How do you monitor chain performance in production?** 340 | Logs execution time and errors. 341 | ```python 342 | import logging 343 | logging.basicConfig(level=logging.INFO) 344 | def run_chain_with_monitoring(chain, inputs): 345 | start = time.time() 346 | try: 347 | result = chain.run(**inputs) 348 | logging.info(f"Chain completed in {time.time() - start}s") 349 | return result 350 | except Exception as e: 351 | logging.error(f"Chain failed: {e}") 352 | raise 353 | ``` 354 | 355 | ## Agents and Tools 356 | 357 | ### Basic 358 | 37. **What is a LangChain agent, and how is it used in NLP?** 359 | Combines LLMs with tools for dynamic tasks. 360 | ```python 361 | from langchain.agents import initialize_agent, Tool 362 | tools = [Tool(name="Search", func=lambda x: f"Searched: {x}", description="Search tool")] 363 | agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description") 364 | ``` 365 | 366 | 38. **How do you create a tool in LangChain for an agent?** 367 | Defines functions for NLP tasks like search. 368 | ```python 369 | from langchain.tools import Tool 370 | tool = Tool( 371 | name="Calculator", 372 | func=lambda x: str(eval(x)), 373 | description="Evaluates mathematical expressions" 374 | ) 375 | ``` 376 | 377 | 39. **What is the ReAct framework in LangChain agents?** 378 | Combines reasoning and acting for NLP decisions. 379 | ```python 380 | agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description") 381 | ``` 382 | 383 | 40. **How do you use LangChain agents for web search integration?** 384 | Integrates tools like SerpAPI for NLP queries. 385 | ```python 386 | from langchain.tools import SerpAPIWrapper 387 | search = SerpAPIWrapper(serpapi_api_key="your-key") 388 | tools = [Tool(name="Search", func=search.run, description="Web search")] 389 | ``` 390 | 391 | 41. **What is the role of the agent executor in LangChain?** 392 | Manages tool execution and LLM interactions. 393 | ```python 394 | from langchain.agents import AgentExecutor 395 | executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools) 396 | ``` 397 | 398 | 42. **How do you handle tool failures in LangChain agents?** 399 | Configures fallback behaviors. 400 | ```python 401 | agent = initialize_agent(tools, llm, handle_parsing_errors=True) 402 | ``` 403 | 404 | #### Intermediate 405 | 43. **Write a function to create a LangChain agent with custom tools.** 406 | Builds an agent for specific NLP tasks. 407 | ```python 408 | from langchain.agents import initialize_agent, Tool 409 | def create_custom_agent(llm): 410 | tools = [ 411 | Tool(name="Summarizer", func=lambda x: f"Summary: {x[:50]}...", description="Summarizes text") 412 | ] 413 | return initialize_agent(tools, llm, agent_type="zero-shot-react-description") 414 | ``` 415 | 416 | 44. **How do you implement a LangChain agent with memory?** 417 | Maintains context for conversational NLP. 418 | ```python 419 | from langchain.memory import ConversationBufferMemory 420 | agent = initialize_agent(tools, llm, memory=ConversationBufferMemory()) 421 | ``` 422 | 423 | 45. **Write a function to integrate a LangChain agent with a database tool.** 424 | Queries databases for NLP applications. 425 | ```python 426 | from langchain.tools import Tool 427 | import sqlite3 428 | def create_db_tool(): 429 | def query_db(query): 430 | conn = sqlite3.connect("example.db") 431 | cursor = conn.cursor() 432 | cursor.execute(query) 433 | return cursor.fetchall() 434 | return Tool(name="Database", func=query_db, description="Query SQLite database") 435 | ``` 436 | 437 | 46. **How do you use LangChain agents for multi-tool workflows?** 438 | Combines tools for complex NLP tasks. 439 | ```python 440 | tools = [ 441 | Tool(name="Search", func=lambda x: f"Searched: {x}", description="Search tool"), 442 | Tool(name="Calc", func=lambda x: str(eval(x)), description="Calculator") 443 | ] 444 | agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description") 445 | ``` 446 | 447 | 47. **Implement a LangChain agent to handle API-based tools.** 448 | Integrates external APIs for NLP. 449 | ```python 450 | from langchain.tools import Tool 451 | import requests 452 | def create_api_tool(): 453 | def call_api(query): 454 | response = requests.get(f"https://api.example.com?q={query}") 455 | return response.json() 456 | return Tool(name="API", func=call_api, description="Calls external API") 457 | ``` 458 | 459 | 48. **How do you debug LangChain agent decision-making?** 460 | Enables verbose logging for tool selection. 461 | ```python 462 | agent = initialize_agent(tools, llm, verbose=True) 463 | ``` 464 | 465 | #### Advanced 466 | 49. **Write a function to implement a custom LangChain agent.** 467 | Defines specialized agent logic for NLP. 468 | ```python 469 | from langchain.agents import AgentExecutor, BaseSingleActionAgent 470 | class CustomAgent(BaseSingleActionAgent): 471 | def plan(self, intermediate_steps, **kwargs): 472 | return {"tool": "Search", "tool_input": kwargs["input"]} 473 | def aplan(self, intermediate_steps, **kwargs): 474 | return self.plan(intermediate_steps, **kwargs) 475 | def create_custom_agent(llm, tools): 476 | agent = CustomAgent() 477 | return AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, llm=llm) 478 | ``` 479 | 480 | 50. **How do you optimize LangChain agents for complex NLP tasks?** 481 | Limits tool usage and caches results. 482 | ```python 483 | agent = initialize_agent(tools, llm, max_iterations=3, return_intermediate_steps=True) 484 | ``` 485 | 486 | 51. **Write a function to implement a multi-agent system in LangChain.** 487 | Coordinates multiple agents for NLP workflows. 488 | ```python 489 | def create_multi_agent_system(llm, tools): 490 | agent1 = initialize_agent(tools[:1], llm, agent_type="zero-shot-react-description") 491 | agent2 = initialize_agent(tools[1:], llm, agent_type="zero-shot-react-description") 492 | return [agent1, agent2] 493 | ``` 494 | 495 | 52. **How do you implement a LangChain agent with tool prioritization?** 496 | Weights tools for efficient NLP decisions. 497 | ```python 498 | from langchain.agents import initialize_agent 499 | def prioritized_agent(llm, tools): 500 | return initialize_agent(tools, llm, agent_type="react-description", tool_weights={"Search": 0.7, "Calc": 0.3}) 501 | ``` 502 | 503 | 53. **Write a function to handle tool timeouts in LangChain agents.** 504 | Ensures robust NLP tool execution. 505 | ```python 506 | import asyncio 507 | async def run_tool_with_timeout(tool, input, timeout=5): 508 | try: 509 | return await asyncio.wait_for(tool.func(input), timeout=timeout) 510 | except asyncio.TimeoutError: 511 | return "Tool timed out" 512 | ``` 513 | 514 | 54. **How do you monitor LangChain agent performance in production?** 515 | Logs tool usage and response times. 516 | ```python 517 | agent = initialize_agent(tools, llm, callbacks=[TimeLogger()]) 518 | ``` 519 | 520 | ## Memory 521 | 522 | ### Basic 523 | 55. **What is memory in LangChain, and how is it used in NLP?** 524 | Stores conversation history for contextual responses. 525 | ```python 526 | from langchain.memory import ConversationBufferMemory 527 | memory = ConversationBufferMemory() 528 | ``` 529 | 530 | 56. **How do you add memory to a LangChain chain?** 531 | Maintains dialogue context for NLP tasks. 532 | ```python 533 | from langchain.chains import ConversationChain 534 | conversation = ConversationChain(llm=llm, memory=ConversationBufferMemory()) 535 | ``` 536 | 537 | 57. **What is ConversationBufferMemory in LangChain?** 538 | Stores full conversation history for NLP chats. 539 | ```python 540 | memory = ConversationBufferMemory() 541 | memory.save_context({"input": "Hi"}, {"output": "Hello!"}) 542 | ``` 543 | 544 | 58. **How do you retrieve memory from a LangChain conversation?** 545 | Accesses stored dialogue for NLP context. 546 | ```python 547 | history = memory.load_memory_variables({})["history"] 548 | ``` 549 | 550 | 59. **What is the role of memory keys in LangChain?** 551 | Defines variables for memory storage. 552 | ```python 553 | memory = ConversationBufferMemory(memory_key="chat_history") 554 | ``` 555 | 556 | 60. **How do you clear memory in a LangChain conversation?** 557 | Resets conversation history for NLP tasks. 558 | ```python 559 | memory.clear() 560 | ``` 561 | 562 | #### Intermediate 563 | 61. **Write a function to create a LangChain chain with summary memory.** 564 | Summarizes conversation for efficient NLP context. 565 | ```python 566 | from langchain.memory import ConversationSummaryMemory 567 | def create_summary_conversation(): 568 | memory = ConversationSummaryMemory(llm=llm) 569 | return ConversationChain(llm=llm, memory=memory) 570 | ``` 571 | 572 | 62. **How do you implement token-limited memory in LangChain?** 573 | Limits memory to fit LLM token constraints. 574 | ```python 575 | from langchain.memory import ConversationTokenBufferMemory 576 | memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=1000) 577 | ``` 578 | 579 | 63. **Write a function to store conversation history in a database.** 580 | Persists NLP dialogue for later use. 581 | ```python 582 | import sqlite3 583 | def save_conversation_to_db(memory, db_path="chat.db"): 584 | conn = sqlite3.connect(db_path) 585 | cursor = conn.cursor() 586 | cursor.execute("CREATE TABLE IF NOT EXISTS chat (input TEXT, output TEXT)") 587 | history = memory.load_memory_variables({})["history"] 588 | cursor.execute("INSERT INTO chat VALUES (?, ?)", (history["input"], history["output"])) 589 | conn.commit() 590 | conn.close() 591 | ``` 592 | 593 | 64. **How do you use LangChain to implement entity-based memory?** 594 | Tracks key entities in NLP conversations. 595 | ```python 596 | from langchain.memory import ConversationEntityMemory 597 | memory = ConversationEntityMemory(llm=llm, entity_extraction_prompt="Extract entities from: {text}") 598 | ``` 599 | 600 | 65. **Implement a function to merge multiple memory contexts.** 601 | Combines dialogue histories for NLP tasks. 602 | ```python 603 | def merge_memories(memories: List[ConversationBufferMemory]): 604 | merged = ConversationBufferMemory() 605 | for mem in memories: 606 | history = mem.load_memory_variables({})["history"] 607 | merged.save_context(history["input"], history["output"]) 608 | return merged 609 | ``` 610 | 611 | 66. **How do you handle memory overflow in LangChain?** 612 | Truncates or summarizes history for NLP. 613 | ```python 614 | memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=500) 615 | ``` 616 | 617 | #### Advanced 618 | 67. **Write a function to implement a custom memory type in LangChain.** 619 | Defines specialized memory for NLP tasks. 620 | ```python 621 | from langchain.memory import BaseMemory 622 | class CustomMemory(BaseMemory): 623 | memory: dict = {} 624 | @property 625 | def memory_variables(self) -> List[str]: 626 | return ["custom_history"] 627 | def load_memory_variables(self, inputs: dict) -> dict: 628 | return {"custom_history": self.memory} 629 | def save_context(self, inputs: dict, outputs: dict): 630 | self.memory[inputs["input"]] = outputs["output"] 631 | ``` 632 | 633 | 68. **How do you implement memory with vector stores in LangChain?** 634 | Uses embeddings for efficient NLP context retrieval. 635 | ```python 636 | from langchain.memory import VectorStoreRetrieverMemory 637 | from langchain.vectorstores import FAISS 638 | from langchain.embeddings import OpenAIEmbeddings 639 | retriever = FAISS.from_texts([""], OpenAIEmbeddings()).as_retriever() 640 | memory = VectorStoreRetrieverMemory(retriever=retriever) 641 | ``` 642 | 643 | 69. **Write a function to compress conversation memory.** 644 | Summarizes history to reduce token usage. 645 | ```python 646 | def compress_memory(memory: ConversationBufferMemory): 647 | history = memory.load_memory_variables({})["history"] 648 | summary = llm(f"Summarize this conversation: {history}") 649 | memory.clear() 650 | memory.save_context({"input": "Summary"}, {"output": summary}) 651 | return memory 652 | ``` 653 | 654 | 70. **How do you implement memory for multi-user conversations in LangChain?** 655 | Separates contexts by user ID. 656 | ```python 657 | def create_user_memory(user_id: str): 658 | return ConversationBufferMemory(memory_key=f"history_{user_id}") 659 | ``` 660 | 661 | 71. **Write a function to synchronize memory across LangChain agents.** 662 | Shares context for collaborative NLP tasks. 663 | ```python 664 | def sync_agent_memory(agent1_memory, agent2_memory): 665 | history1 = agent1_memory.load_memory_variables({})["history"] 666 | agent2_memory.save_context(history1["input"], history1["output"]) 667 | ``` 668 | 669 | 72. **How do you optimize memory for long conversations in LangChain?** 670 | Uses summary or token-limited memory. 671 | ```python 672 | memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000) 673 | ``` 674 | 675 | ## Retrieval and Vector Stores 676 | 677 | ### Basic 678 | 73. **What is retrieval-augmented generation (RAG) in LangChain?** 679 | Combines LLMs with external knowledge for NLP. 680 | ```python 681 | from langchain.chains import RetrievalQA 682 | from langchain.vectorstores import FAISS 683 | from langchain.embeddings import OpenAIEmbeddings 684 | vectorstore = FAISS.from_texts(["Sample text"], OpenAIEmbeddings()) 685 | qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vectorstore.as_retriever()) 686 | ``` 687 | 688 | 74. **How do you create a vector store in LangChain?** 689 | Stores embeddings for NLP document retrieval. 690 | ```python 691 | from langchain.vectorstores import FAISS 692 | from langchain.embeddings import OpenAIEmbeddings 693 | vectorstore = FAISS.from_texts(["Document 1", "Document 2"], OpenAIEmbeddings()) 694 | ``` 695 | 696 | 75. **What is the role of embeddings in LangChain retrieval?** 697 | Converts text to vectors for similarity search. 698 | ```python 699 | from langchain.embeddings import OpenAIEmbeddings 700 | embeddings = OpenAIEmbeddings() 701 | vector = embeddings.embed_query("What is AI?") 702 | ``` 703 | 704 | 76. **How do you use LangChain to query a vector store?** 705 | Retrieves relevant documents for NLP tasks. 706 | ```python 707 | retriever = vectorstore.as_retriever() 708 | docs = retriever.get_relevant_documents("AI definition") 709 | ``` 710 | 711 | 77. **What is the RetrievalQA chain in LangChain?** 712 | Answers questions using retrieved documents. 713 | ```python 714 | qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) 715 | ``` 716 | 717 | 78. **How do you save and load a vector store in LangChain?** 718 | Persists embeddings for reuse. 719 | ```python 720 | vectorstore.save_local("faiss_index") 721 | vectorstore = FAISS.load_local("faiss_index", embeddings) 722 | ``` 723 | 724 | #### Intermediate 725 | 79. **Write a function to create a LangChain RAG pipeline.** 726 | Combines retrieval and generation for NLP. 727 | ```python 728 | from langchain.chains import RetrievalQA 729 | from langchain.vectorstores import FAISS 730 | from langchain.embeddings import OpenAIEmbeddings 731 | def create_rag_pipeline(documents: List[str]): 732 | embeddings = OpenAIEmbeddings() 733 | vectorstore = FAISS.from_texts(documents, embeddings) 734 | retriever = vectorstore.as_retriever() 735 | return RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) 736 | ``` 737 | 738 | 80. **How do you implement a custom retriever in LangChain?** 739 | Defines specialized document retrieval logic. 740 | ```python 741 | from langchain.schema.retriever import BaseRetriever 742 | class CustomRetriever(BaseRetriever): 743 | def _get_relevant_documents(self, query: str): 744 | return [Document(page_content=f"Custom result for {query}")] 745 | ``` 746 | 747 | 81. **Write a function to update a LangChain vector store with new documents.** 748 | Adds new texts for NLP retrieval. 749 | ```python 750 | def update_vectorstore(vectorstore, new_texts: List[str], embeddings): 751 | new_vectors = embeddings.embed_documents(new_texts) 752 | vectorstore.add_texts(new_texts, new_vectors) 753 | return vectorstore 754 | ``` 755 | 756 | 82. **How do you use LangChain to implement semantic search?** 757 | Retrieves documents based on meaning. 758 | ```python 759 | query = "What is machine learning?" 760 | docs = vectorstore.similarity_search(query, k=3) 761 | ``` 762 | 763 | 83. **Implement a function to combine multiple vector stores in LangChain.** 764 | Merges retrieval sources for NLP tasks. 765 | ```python 766 | def combine_vectorstores(stores: List[FAISS]): 767 | combined = stores[0] 768 | for store in stores[1:]: 769 | combined.merge_from(store) 770 | return combined 771 | ``` 772 | 773 | 84. **How do you optimize vector store retrieval in LangChain?** 774 | Uses efficient indexing and caching. 775 | ```python 776 | vectorstore = FAISS.from_texts([""], OpenAIEmbeddings(), index_type="HNSW") 777 | ``` 778 | 779 | #### Advanced 780 | 85. **Write a function to implement a hybrid search in LangChain.** 781 | Combines keyword and semantic search for NLP. 782 | ```python 783 | from langchain.retrievers import BM25Retriever, EnsembleRetriever 784 | def create_hybrid_retriever(documents: List[str]): 785 | bm25 = BM25Retriever.from_texts(documents) 786 | faiss = FAISS.from_texts(documents, OpenAIEmbeddings()).as_retriever() 787 | return EnsembleRetriever(retrievers=[bm25, faiss], weights=[0.5, 0.5]) 788 | ``` 789 | 790 | 86. **How do you implement a LangChain retriever with metadata filtering?** 791 | Filters documents by metadata for NLP. 792 | ```python 793 | docs = vectorstore.similarity_search_with_filter( 794 | query="AI", 795 | filter={"category": "technology"} 796 | ) 797 | ``` 798 | 799 | 87. **Write a function to implement a self-querying retriever in LangChain.** 800 | Parses queries for structured retrieval. 801 | ```python 802 | from langchain.retrievers.self_query import SelfQueryRetriever 803 | from langchain.schema import Document 804 | def create_self_query_retriever(docs: List[Document]): 805 | metadata_field_info = [{"name": "category", "type": "string"}] 806 | return SelfQueryRetriever.from_llm( 807 | llm=llm, 808 | vectorstore=FAISS.from_documents(docs, OpenAIEmbeddings()), 809 | document_contents="content", 810 | metadata_field_info=metadata_field_info 811 | ) 812 | ``` 813 | 814 | 88. **How do you handle large-scale vector stores in LangChain?** 815 | Uses distributed stores like Pinecone. 816 | ```python 817 | from langchain.vectorstores import Pinecone 818 | import pinecone 819 | pinecone.init(api_key="your-key", environment="us-west1-gcp") 820 | vectorstore = Pinecone.from_texts([""], OpenAIEmbeddings(), index_name="example") 821 | ``` 822 | 823 | 89. **Write a function to implement a contextual compression retriever.** 824 | Filters irrelevant retrieved content. 825 | ```python 826 | from langchain.retrievers import ContextualCompressionRetriever 827 | from langchain.retrievers.document_compressors import LLMChainExtractor 828 | def create_compression_retriever(): 829 | compressor = LLMChainExtractor.from_llm(llm) 830 | return ContextualCompressionRetriever( 831 | base_compressor=compressor, 832 | base_retriever=vectorstore.as_retriever() 833 | ) 834 | ``` 835 | 836 | 90. **How do you evaluate retrieval performance in LangChain?** 837 | Measures precision and recall for NLP retrieval. 838 | ```python 839 | def evaluate_retriever(retriever, queries: List[str], relevant_docs: List[List[str]]): 840 | precision = [] 841 | for query, relevant in zip(queries, relevant_docs): 842 | retrieved = [doc.page_content for doc in retriever.get_relevant_documents(query)] 843 | correct = len(set(retrieved) & set(relevant)) 844 | precision.append(correct / len(retrieved) if retrieved else 0) 845 | return {"avg_precision": sum(precision) / len(precision)} 846 | ``` 847 | 848 | ## Evaluation and Metrics 849 | 850 | ### Basic 851 | 91. **What metrics are used to evaluate LangChain applications?** 852 | BLEU, ROUGE, or custom metrics for NLP outputs. 853 | ```python 854 | from langchain.evaluation import load_evaluator 855 | evaluator = load_evaluator("string_distance") 856 | ``` 857 | 858 | 92. **How do you evaluate the quality of LangChain LLM outputs?** 859 | Uses evaluators for text similarity. 860 | ```python 861 | result = evaluator.evaluate_strings(prediction="AI is great", reference="AI is awesome") 862 | ``` 863 | 864 | 93. **What is the role of human evaluation in LangChain?** 865 | Validates NLP outputs for subjective quality. 866 | ```python 867 | def human_eval(output: str): 868 | return input(f"Rate this output (1-5): {output}") 869 | ``` 870 | 871 | 94. **How do you use LangChain to compute BLEU scores?** 872 | Measures NLP output quality against references. 873 | ```python 874 | from nltk.translate.bleu_score import sentence_bleu 875 | def compute_bleu(prediction: str, reference: str): 876 | return sentence_bleu([reference.split()], prediction.split()) 877 | ``` 878 | 879 | 95. **What is a LangChain evaluator, and how is it used?** 880 | Automates NLP output assessment. 881 | ```python 882 | from langchain.evaluation import load_evaluator 883 | evaluator = load_evaluator("embedding_distance") 884 | ``` 885 | 886 | 96. **How do you log evaluation results in LangChain?** 887 | Stores metrics for analysis. 888 | ```python 889 | import logging 890 | logging.basicConfig(filename="eval.log", level=logging.INFO) 891 | logging.info(f"Evaluation result: {result}") 892 | ``` 893 | 894 | #### Intermediate 895 | 97. **Write a function to evaluate LangChain chain outputs.** 896 | Compares predictions to ground truth. 897 | ```python 898 | from langchain.evaluation import load_evaluator 899 | def evaluate_chain(chain, inputs: List[dict], references: List[str]): 900 | evaluator = load_evaluator("string_distance") 901 | results = [] 902 | for input_dict, ref in zip(inputs, references): 903 | pred = chain.run(**input_dict) 904 | result = evaluator.evaluate_strings(prediction=pred, reference=ref) 905 | results.append(result) 906 | return results 907 | ``` 908 | 909 | 98. **How do you implement a custom evaluator in LangChain?** 910 | Defines specialized NLP metrics. 911 | ```python 912 | from langchain.evaluation import EvaluatorType, CustomEvaluator 913 | class CustomEvaluator(CustomEvaluator): 914 | def evaluate(self, prediction: str, reference: str) -> dict: 915 | return {"score": len(prediction) / len(reference)} 916 | evaluator = CustomEvaluator() 917 | ``` 918 | 919 | 99. **Write a function to compute ROUGE scores for LangChain outputs.** 920 | Measures text overlap for NLP tasks. 921 | ```python 922 | from rouge_score import rouge_scorer 923 | def compute_rouge(prediction: str, reference: str): 924 | scorer = rouge_scorer.RougeScorer(["rouge1", "rougeL"], use_stemmer=True) 925 | return scorer.score(reference, prediction) 926 | ``` 927 | 928 | 100. **How do you evaluate LangChain agent performance?** 929 | Measures tool usage and task success. 930 | ```python 931 | def evaluate_agent(agent, tasks: List[str], expected: List[str]): 932 | successes = 0 933 | for task, exp in zip(tasks, expected): 934 | result = agent.run(task) 935 | if exp in result: 936 | successes += 1 937 | return {"success_rate": successes / len(tasks)} 938 | ``` 939 | 940 | 101. **Implement a function to perform A/B testing for LangChain chains.** 941 | Compares performance of two NLP chains. 942 | ```python 943 | def ab_test_chains(chain_a, chain_b, inputs: List[dict], references: List[str]): 944 | evaluator = load_evaluator("string_distance") 945 | scores_a, scores_b = [], [] 946 | for input_dict, ref in zip(inputs, references): 947 | pred_a = chain_a.run(**input_dict) 948 | pred_b = chain_b.run(**input_dict) 949 | scores_a.append(evaluator.evaluate_strings(prediction=pred_a, reference=ref)) 950 | scores_b.append(evaluator.evaluate_strings(prediction=pred_b, reference=ref)) 951 | return {"chain_a_avg": sum(s["score"] for s in scores_a) / len(scores_a), 952 | "chain_b_avg": sum(s["score"] for s in scores_b) / len(scores_b)} 953 | ``` 954 | 955 | 102. **How do you use LangChain to evaluate factual accuracy?** 956 | Checks outputs against trusted sources. 957 | ```python 958 | from langchain.chains import RetrievalQA 959 | qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vectorstore.as_retriever()) 960 | result = qa.run("Is the Earth flat?") 961 | ``` 962 | 963 | #### Advanced 964 | 103. **Write a function to evaluate LangChain RAG performance.** 965 | Measures retrieval and generation quality. 966 | ```python 967 | def evaluate_rag(qa_chain, queries: List[str], references: List[str]): 968 | scores = [] 969 | for query, ref in zip(queries, references): 970 | pred = qa_chain.run(query) 971 | score = compute_rouge(pred, ref)["rouge1"].fmeasure 972 | scores.append(score) 973 | return {"avg_rouge1": sum(scores) / len(scores)} 974 | ``` 975 | 976 | 104. **How do you implement a multi-metric evaluation in LangChain?** 977 | Combines BLEU, ROUGE, and custom metrics. 978 | ```python 979 | def multi_metric_eval(prediction: str, reference: str): 980 | bleu = compute_bleu(prediction, reference) 981 | rouge = compute_rouge(prediction, reference)["rouge1"].fmeasure 982 | return {"bleu": bleu, "rouge1": rouge} 983 | ``` 984 | 985 | 105. **Write a function to evaluate conversational coherence.** 986 | Assesses dialogue flow in NLP tasks. 987 | ```python 988 | def evaluate_coherence(conversation: ConversationChain, inputs: List[str]): 989 | coherence = 0 990 | for i in range(len(inputs)-1): 991 | response = conversation.run(inputs[i]) 992 | next_input = inputs[i+1] 993 | if response.lower() in next_input.lower(): 994 | coherence += 1 995 | return {"coherence_score": coherence / (len(inputs)-1)} 996 | ``` 997 | 998 | 106. **How do you evaluate LangChain output diversity?** 999 | Measures variety in NLP responses. 1000 | ```python 1001 | from nltk.tokenize import word_tokenize 1002 | def evaluate_diversity(outputs: List[str]): 1003 | tokens = [set(word_tokenize(out)) for out in outputs] 1004 | unique = len(set.union(*tokens)) / sum(len(t) for t in tokens) 1005 | return {"diversity": unique} 1006 | ``` 1007 | 1008 | 107. **Implement a function to perform adversarial evaluation in LangChain.** 1009 | Tests robustness to tricky inputs. 1010 | ```python 1011 | def adversarial_eval(chain, adversarial_inputs: List[str]): 1012 | successes = 0 1013 | for input_text in adversarial_inputs: 1014 | try: 1015 | chain.run(input_text) 1016 | successes += 1 1017 | except: 1018 | pass 1019 | return {"robustness": successes / len(adversarial_inputs)} 1020 | ``` 1021 | 1022 | 108. **How do you automate evaluation pipelines in LangChain?** 1023 | Runs batch evaluations for NLP tasks. 1024 | ```python 1025 | def batch_evaluation(chain, inputs: List[dict], references: List[str]): 1026 | results = [] 1027 | for input_dict, ref in zip(inputs, references): 1028 | pred = chain.run(**input_dict) 1029 | results.append(multi_metric_eval(pred, ref)) 1030 | return results 1031 | ``` 1032 | 1033 | ## Deployment and Inference 1034 | 1035 | ### Basic 1036 | 109. **How do you deploy a LangChain application for production?** 1037 | Uses frameworks like FastAPI for NLP APIs. 1038 | ```python 1039 | from fastapi import FastAPI 1040 | app = FastAPI() 1041 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 1042 | @app.post("/predict") 1043 | async def predict(word: str): 1044 | return {"response": chain.run(word=word)} 1045 | ``` 1046 | 1047 | 110. **What is inference in LangChain, and how is it performed?** 1048 | Generates NLP outputs for new inputs. 1049 | ```python 1050 | response = chain.run(word="algorithm") 1051 | ``` 1052 | 1053 | 111. **How do you save a LangChain chain for deployment?** 1054 | Exports chain configuration. 1055 | ```python 1056 | chain.save("chain.json") 1057 | ``` 1058 | 1059 | 112. **How do you load a LangChain chain for inference?** 1060 | Restores a chain for NLP tasks. 1061 | ```python 1062 | from langchain.chains import load_chain 1063 | chain = load_chain("chain.json") 1064 | ``` 1065 | 1066 | 113. **What is the role of environment variables in LangChain deployment?** 1067 | Secures API keys for LLM providers. 1068 | ```python 1069 | import os 1070 | os.environ["OPENAI_API_KEY"] = "your-api-key" 1071 | ``` 1072 | 1073 | 114. **How do you handle batch inference in LangChain?** 1074 | Processes multiple NLP inputs efficiently. 1075 | ```python 1076 | responses = chain.batch([{"word": "AI"}, {"word": "ML"}]) 1077 | ``` 1078 | 1079 | #### Intermediate 1080 | 115. **Write a function to deploy a LangChain chain with Flask.** 1081 | Creates a web API for NLP inference. 1082 | ```python 1083 | from flask import Flask, request, jsonify 1084 | app = Flask(__name__) 1085 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 1086 | @app.route("/predict", methods=["POST"]) 1087 | def predict(): 1088 | data = request.json 1089 | return jsonify({"response": chain.run(word=data["word"])}) 1090 | ``` 1091 | 1092 | 116. **How do you optimize LangChain inference for low latency?** 1093 | Caches prompts and minimizes LLM calls. 1094 | ```python 1095 | chain = LLMChain(llm=llm, prompt=prompt, cache=True) 1096 | ``` 1097 | 1098 | 117. **Implement a function for real-time LangChain inference.** 1099 | Processes streaming NLP inputs. 1100 | ```python 1101 | def real_time_inference(chain, input_stream: List[str]): 1102 | return [chain.run(word=input_text) for input_text in input_stream] 1103 | ``` 1104 | 1105 | 118. **How do you secure LangChain inference endpoints?** 1106 | Uses authentication for API access. 1107 | ```python 1108 | from fastapi import FastAPI, HTTPException 1109 | app = FastAPI() 1110 | @app.post("/predict") 1111 | async def predict(word: str, token: str): 1112 | if token != "secret-token": 1113 | raise HTTPException(status_code=401, detail="Unauthorized") 1114 | return {"response": chain.run(word=word)} 1115 | ``` 1116 | 1117 | 119. **Write a function to monitor LangChain inference performance.** 1118 | Tracks latency and errors in production. 1119 | ```python 1120 | import time 1121 | def monitor_inference(chain, inputs: List[dict]): 1122 | start = time.time() 1123 | try: 1124 | results = chain.batch(inputs) 1125 | return {"latency": time.time() - start, "results": results} 1126 | except Exception as e: 1127 | return {"error": str(e)} 1128 | ``` 1129 | 1130 | 120. **How do you handle version control for LangChain chains in deployment?** 1131 | Manages chain versions for updates. 1132 | ```python 1133 | def save_versioned_chain(chain, version: str): 1134 | chain.save(f"chains/chain_v{version}.json") 1135 | ``` 1136 | 1137 | #### Advanced 1138 | 121. **Write a function to implement A/B testing for LangChain deployments.** 1139 | Compares performance of two NLP chains. 1140 | ```python 1141 | def ab_test_deployments(chain_a, chain_b, inputs: List[dict]): 1142 | results_a = chain_a.batch(inputs) 1143 | results_b = chain_b.batch(inputs) 1144 | return {"chain_a": results_a, "chain_b": results_b} 1145 | ``` 1146 | 1147 | 122. **How do you implement distributed inference with LangChain?** 1148 | Scales NLP inference across nodes. 1149 | ```python 1150 | from concurrent.futures import ThreadPoolExecutor 1151 | def distributed_inference(chain, inputs: List[dict]): 1152 | with ThreadPoolExecutor() as executor: 1153 | results = list(executor.map(lambda x: chain.run(**x), inputs)) 1154 | return results 1155 | ``` 1156 | 1157 | 123. **Write a function to handle failover in LangChain inference.** 1158 | Switches to backup LLM providers. 1159 | ```python 1160 | def failover_inference(primary_chain, backup_chain, inputs: dict): 1161 | try: 1162 | return primary_chain.run(**inputs) 1163 | except: 1164 | return backup_chain.run(**inputs) 1165 | ``` 1166 | 1167 | 124. **How do you implement continuous learning in LangChain deployments?** 1168 | Updates chains with new data. 1169 | ```python 1170 | def update_chain(chain, new_data: List[dict]): 1171 | for data in new_data: 1172 | chain.run(**data) # Simulate learning 1173 | chain.save("updated_chain.json") 1174 | ``` 1175 | 1176 | 125. **Write a function to optimize LangChain inference costs.** 1177 | Batches requests and selects cost-effective models. 1178 | ```python 1179 | def cost_optimized_inference(chain, inputs: List[dict], max_batch_size=10): 1180 | results = [] 1181 | for i in range(0, len(inputs), max_batch_size): 1182 | batch = inputs[i:i+max_batch_size] 1183 | results.extend(chain.batch(batch)) 1184 | return results 1185 | ``` 1186 | 1187 | 126. **How do you implement load balancing for LangChain inference?** 1188 | Distributes requests across chains. 1189 | ```python 1190 | from random import choice 1191 | def load_balanced_inference(chains: List[LLMChain], inputs: dict): 1192 | chain = choice(chains) 1193 | return chain.run(**inputs) 1194 | ``` 1195 | 1196 | ## Debugging and Error Handling 1197 | 1198 | ### Basic 1199 | 127. **How do you debug a LangChain chain that fails?** 1200 | Enables verbose logging for NLP issues. 1201 | ```python 1202 | chain = LLMChain(llm=llm, prompt=prompt, verbose=True) 1203 | ``` 1204 | 1205 | 128. **What is a try-except block in LangChain applications?** 1206 | Handles errors in NLP workflows. 1207 | ```python 1208 | try: 1209 | response = chain.run(word="algorithm") 1210 | except Exception as e: 1211 | print(f"Error: {e}") 1212 | ``` 1213 | 1214 | 129. **How do you validate inputs for LangChain chains?** 1215 | Ensures correct input formats for NLP. 1216 | ```python 1217 | def validate_input(data: dict, required_keys: List[str]): 1218 | if not all(key in data for key in required_keys): 1219 | raise ValueError("Missing required keys") 1220 | return data 1221 | ``` 1222 | 1223 | 130. **What is the role of verbose mode in LangChain?** 1224 | Logs execution details for debugging. 1225 | ```python 1226 | chain = LLMChain(llm=llm, prompt=prompt, verbose=True) 1227 | ``` 1228 | 1229 | 131. **How do you handle API errors in LangChain?** 1230 | Implements retries for LLM calls. 1231 | ```python 1232 | llm = OpenAI(max_retries=3) 1233 | ``` 1234 | 1235 | 132. **How do you log errors in LangChain applications?** 1236 | Records issues for analysis. 1237 | ```python 1238 | import logging 1239 | logging.basicConfig(filename="app.log", level=logging.ERROR) 1240 | try: 1241 | chain.run(word="test") 1242 | except Exception as e: 1243 | logging.error(f"Chain error: {e}") 1244 | ``` 1245 | 1246 | #### Intermediate 1247 | 133. **Write a function to retry LangChain chain execution on failure.** 1248 | Handles transient NLP errors. 1249 | ```python 1250 | def retry_chain(chain, inputs: dict, max_attempts=3): 1251 | for attempt in range(max_attempts): 1252 | try: 1253 | return chain.run(**inputs) 1254 | except Exception as e: 1255 | if attempt == max_attempts - 1: 1256 | raise 1257 | print(f"Attempt {attempt+1} failed: {e}") 1258 | ``` 1259 | 1260 | 134. **How do you debug LangChain agent tool selection?** 1261 | Logs tool usage and decisions. 1262 | ```python 1263 | agent = initialize_agent(tools, llm, verbose=True) 1264 | ``` 1265 | 1266 | 135. **Implement a function to validate LangChain output formats.** 1267 | Ensures structured NLP responses. 1268 | ```python 1269 | def validate_output_format(output: str, expected_type: str): 1270 | if expected_type == "json": 1271 | import json 1272 | try: 1273 | json.loads(output) 1274 | except: 1275 | raise ValueError("Invalid JSON output") 1276 | return output 1277 | ``` 1278 | 1279 | 136. **How do you profile LangChain chain performance?** 1280 | Measures execution time for NLP tasks. 1281 | ```python 1282 | import time 1283 | def profile_chain(chain, inputs: dict): 1284 | start = time.time() 1285 | result = chain.run(**inputs) 1286 | return {"result": result, "time": time.time() - start} 1287 | ``` 1288 | 1289 | 137. **Write a function to handle memory errors in LangChain.** 1290 | Manages large conversation histories. 1291 | ```python 1292 | def handle_memory_error(memory, max_size=1000): 1293 | try: 1294 | memory.save_context({"input": "test"}, {"output": "response"}) 1295 | except MemoryError: 1296 | memory.clear() 1297 | memory.save_context({"input": "summary"}, {"output": "Conversation reset"}) 1298 | ``` 1299 | 1300 | 138. **How do you debug LangChain retrieval issues?** 1301 | Inspects retrieved documents for relevance. 1302 | ```python 1303 | docs = retriever.get_relevant_documents("test query") 1304 | print([doc.page_content for doc in docs]) 1305 | ``` 1306 | 1307 | #### Advanced 1308 | 139. **Write a function to implement a custom error handler for LangChain.** 1309 | Handles specific NLP errors gracefully. 1310 | ```python 1311 | from langchain.callbacks.base import BaseCallbackHandler 1312 | class ErrorHandler(BaseCallbackHandler): 1313 | def on_chain_error(self, error: Exception, **kwargs): 1314 | print(f"Chain failed with error: {error}") 1315 | return {"error": str(error)} 1316 | chain = LLMChain(llm=llm, prompt=prompt, callbacks=[ErrorHandler()]) 1317 | ``` 1318 | 1319 | 140. **How do you implement circuit breakers in LangChain applications?** 1320 | Prevents cascading failures in NLP APIs. 1321 | ```python 1322 | from pybreaker import CircuitBreaker 1323 | breaker = CircuitBreaker(fail_max=3, reset_timeout=60) 1324 | @breaker 1325 | def run_chain(chain, inputs): 1326 | return chain.run(**inputs) 1327 | ``` 1328 | 1329 | 141. **Write a function to detect and handle LLM hallucination in LangChain.** 1330 | Validates NLP outputs against facts. 1331 | ```python 1332 | def detect_hallucination(chain, input_text: str, trusted_source: str): 1333 | response = chain.run(input_text) 1334 | if trusted_source.lower() not in response.lower(): 1335 | return {"hallucination_detected": True, "response": response} 1336 | return {"hallucination_detected": False, "response": response} 1337 | ``` 1338 | 1339 | 142. **How do you implement logging for distributed LangChain applications?** 1340 | Centralizes logs for NLP systems. 1341 | ```python 1342 | import logging.handlers 1343 | def setup_distributed_logging(): 1344 | handler = logging.handlers.SocketHandler("log-server", 9090) 1345 | logging.getLogger().addHandler(handler) 1346 | logging.info("LangChain app started") 1347 | ``` 1348 | 1349 | 143. **Write a function to handle version compatibility in LangChain.** 1350 | Checks LangChain and dependency versions. 1351 | ```python 1352 | import langchain 1353 | def check_compatibility(): 1354 | print(f"LangChain Version: {langchain.__version__}") 1355 | if langchain.__version__ < "0.0.200": 1356 | raise ValueError("Unsupported LangChain version") 1357 | ``` 1358 | 1359 | 144. **How do you debug LangChain memory issues in long conversations?** 1360 | Monitors memory size and content. 1361 | ```python 1362 | def debug_memory(memory): 1363 | history = memory.load_memory_variables({})["history"] 1364 | print(f"Memory size: {len(str(history))} characters") 1365 | return history 1366 | ``` 1367 | 1368 | ## Visualization and Interpretation 1369 | 1370 | ### Basic 1371 | 145. **How do you visualize LangChain chain execution?** 1372 | Logs steps for NLP workflows. 1373 | ```python 1374 | chain = LLMChain(llm=llm, prompt=prompt, verbose=True) 1375 | ``` 1376 | 1377 | 146. **What is the role of logging in LangChain visualization?** 1378 | Tracks chain and agent actions. 1379 | ```python 1380 | import logging 1381 | logging.basicConfig(level=logging.INFO) 1382 | logging.info("Chain executed") 1383 | ``` 1384 | 1385 | 147. **How do you visualize LangChain agent tool usage?** 1386 | Logs tool calls for NLP tasks. 1387 | ```python 1388 | agent = initialize_agent(tools, llm, verbose=True) 1389 | ``` 1390 | 1391 | 148. **How do you use LangChain to log conversation history?** 1392 | Displays dialogue for analysis. 1393 | ```python 1394 | history = memory.load_memory_variables({})["history"] 1395 | print(history) 1396 | ``` 1397 | 1398 | 149. **What is the role of callbacks in LangChain visualization?** 1399 | Tracks execution for debugging. 1400 | ```python 1401 | chain = LLMChain(llm=llm, prompt=prompt, callbacks=[StdOutCallbackHandler()]) 1402 | ``` 1403 | 1404 | 150. **How do you visualize LangChain evaluation metrics?** 1405 | Plots NLP performance metrics. 1406 | ```python 1407 | import matplotlib.pyplot as plt 1408 | def plot_metrics(metrics: List[dict]): 1409 | scores = [m["score"] for m in metrics] 1410 | plt.plot(scores) 1411 | plt.savefig("metrics.png") 1412 | ``` 1413 | 1414 | #### Intermediate 1415 | 151. **Write a function to visualize LangChain chain dependencies.** 1416 | Maps chain inputs and outputs. 1417 | ```python 1418 | def visualize_chain(chain): 1419 | print(f"Input variables: {chain.input_keys}") 1420 | print(f"Output variables: {chain.output_keys}") 1421 | ``` 1422 | 1423 | 152. **How do you implement a dashboard for LangChain metrics?** 1424 | Displays NLP performance in real-time. 1425 | ```python 1426 | from fastapi import FastAPI 1427 | app = FastAPI() 1428 | metrics = [] 1429 | @app.get("/metrics") 1430 | async def get_metrics(): 1431 | return {"metrics": metrics} 1432 | ``` 1433 | 1434 | 153. **Write a function to visualize LangChain memory growth.** 1435 | Tracks conversation history size. 1436 | ```python 1437 | import matplotlib.pyplot as plt 1438 | def plot_memory_growth(memory, inputs: List[str]): 1439 | sizes = [] 1440 | for input_text in inputs: 1441 | memory.save_context({"input": input_text}, {"output": "response"}) 1442 | sizes.append(len(str(memory.load_memory_variables({})["history"]))) 1443 | plt.plot(sizes) 1444 | plt.savefig("memory_growth.png") 1445 | ``` 1446 | 1447 | 154. **How do you visualize LangChain retrieval results?** 1448 | Displays retrieved documents for NLP. 1449 | ```python 1450 | def visualize_retrieval(retriever, query: str): 1451 | docs = retriever.get_relevant_documents(query) 1452 | for i, doc in enumerate(docs): 1453 | print(f"Document {i+1}: {doc.page_content[:100]}...") 1454 | ``` 1455 | 1456 | 155. **Implement a function to plot LangChain evaluation trends.** 1457 | Shows metric changes over time. 1458 | ```python 1459 | import matplotlib.pyplot as plt 1460 | def plot_evaluation_trends(results: List[dict]): 1461 | bleu_scores = [r["bleu"] for r in results] 1462 | plt.plot(bleu_scores, label="BLEU") 1463 | plt.legend() 1464 | plt.savefig("eval_trends.png") 1465 | ``` 1466 | 1467 | 156. **How do you visualize LangChain agent reasoning steps?** 1468 | Logs intermediate decisions for NLP. 1469 | ```python 1470 | agent = initialize_agent(tools, llm, return_intermediate_steps=True) 1471 | result = agent.run("Solve 2+2") 1472 | print(result["intermediate_steps"]) 1473 | ``` 1474 | 1475 | #### Advanced 1476 | 157. **Write a function to visualize LangChain vector store embeddings.** 1477 | Plots document embeddings for NLP. 1478 | ```python 1479 | from sklearn.manifold import TSNE 1480 | import matplotlib.pyplot as plt 1481 | def visualize_embeddings(vectorstore): 1482 | embeddings = vectorstore.embedding_function.embed_documents( 1483 | [doc.page_content for doc in vectorstore.docstore._dict.values()] 1484 | ) 1485 | tsne = TSNE(n_components=2) 1486 | reduced = tsne.fit_transform(embeddings) 1487 | plt.scatter(reduced[:, 0], reduced[:, 1]) 1488 | plt.savefig("embeddings.png") 1489 | ``` 1490 | 1491 | 158. **How do you implement a real-time visualization for LangChain inference?** 1492 | Streams NLP outputs to a dashboard. 1493 | ```python 1494 | from fastapi import FastAPI, WebSocket 1495 | app = FastAPI() 1496 | @app.websocket("/ws") 1497 | async def websocket_endpoint(websocket: WebSocket): 1498 | await websocket.accept() 1499 | while True: 1500 | data = await websocket.receive_text() 1501 | response = chain.run(word=data) 1502 | await websocket.send_text(response) 1503 | ``` 1504 | 1505 | 159. **Write a function to visualize LangChain chain latency.** 1506 | Plots execution times for NLP tasks. 1507 | ```python 1508 | import matplotlib.pyplot as plt 1509 | def plot_chain_latency(chain, inputs: List[dict]): 1510 | times = [] 1511 | for input_dict in inputs: 1512 | start = time.time() 1513 | chain.run(**input_dict) 1514 | times.append(time.time() - start) 1515 | plt.plot(times) 1516 | plt.savefig("latency.png") 1517 | ``` 1518 | 1519 | 160. **How do you visualize LangChain error rates in production?** 1520 | Tracks failures for NLP applications. 1521 | ```python 1522 | import matplotlib.pyplot as plt 1523 | def plot_error_rates(errors: List[dict]): 1524 | error_counts = [1 if e["error"] else 0 for e in errors] 1525 | plt.plot(error_counts) 1526 | plt.savefig("error_rates.png") 1527 | ``` 1528 | 1529 | 161. **Write a function to generate a LangChain workflow diagram.** 1530 | Visualizes chain and agent interactions. 1531 | ```python 1532 | from graphviz import Digraph 1533 | def create_workflow_diagram(): 1534 | dot = Digraph() 1535 | dot.node("A", "Input") 1536 | dot.node("B", "LLMChain") 1537 | dot.node("C", "Output") 1538 | dot.edges(["AB", "BC"]) 1539 | dot.render("workflow.gv", view=False) 1540 | ``` 1541 | 1542 | 162. **How do you implement interactive visualizations for LangChain outputs?** 1543 | Uses Plotly for dynamic NLP displays. 1544 | ```python 1545 | import plotly.express as px 1546 | def interactive_metric_plot(metrics: List[dict]): 1547 | scores = [m["score"] for m in metrics] 1548 | fig = px.line(x=range(len(scores)), y=scores, title="Evaluation Metrics") 1549 | fig.write_html("metrics.html") 1550 | ``` 1551 | 1552 | ## Best Practices and Optimization 1553 | 1554 | ### Basic 1555 | 163. **What are best practices for structuring LangChain code?** 1556 | Modularizes chains, agents, and memory. 1557 | ```python 1558 | def build_chain(): 1559 | return LLMChain(llm=llm, prompt=prompt) 1560 | def load_data(): 1561 | return [{"word": "AI"}] 1562 | ``` 1563 | 1564 | 164. **How do you ensure reproducibility in LangChain applications?** 1565 | Sets seeds and versions for consistency. 1566 | ```python 1567 | import random 1568 | random.seed(42) 1569 | ``` 1570 | 1571 | 165. **What is caching in LangChain, and how is it used?** 1572 | Stores LLM responses for efficiency. 1573 | ```python 1574 | from langchain.cache import InMemoryCache 1575 | langchain.llm_cache = InMemoryCache() 1576 | ``` 1577 | 1578 | 166. **How do you handle large-scale data in LangChain applications?** 1579 | Uses batch processing and vector stores. 1580 | ```python 1581 | responses = chain.batch(inputs) 1582 | ``` 1583 | 1584 | 167. **What is the role of environment configuration in LangChain?** 1585 | Secures and organizes settings. 1586 | ```python 1587 | import os 1588 | os.environ["LANGCHAIN_API_KEY"] = "your-key" 1589 | ``` 1590 | 1591 | 168. **How do you document LangChain applications?** 1592 | Uses docstrings and READMEs for clarity. 1593 | ```python 1594 | def create_chain(): 1595 | """Creates an LLMChain for text summarization.""" 1596 | return LLMChain(llm=llm, prompt=prompt) 1597 | ``` 1598 | 1599 | #### Intermediate 1600 | 169. **Write a function to optimize LangChain memory usage.** 1601 | Limits conversation history for NLP tasks. 1602 | ```python 1603 | def optimize_memory(memory, max_size=1000): 1604 | history = memory.load_memory_variables({})["history"] 1605 | if len(str(history)) > max_size: 1606 | memory.clear() 1607 | memory.save_context({"input": "Summary"}, {"output": "Conversation reset"}) 1608 | return memory 1609 | ``` 1610 | 1611 | 170. **How do you implement unit tests for LangChain chains?** 1612 | Validates NLP chain functionality. 1613 | ```python 1614 | import unittest 1615 | class TestChain(unittest.TestCase): 1616 | def test_chain_output(self): 1617 | chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Define {word}")) 1618 | result = chain.run(word="AI") 1619 | self.assertIn("intelligence", result.lower()) 1620 | if __name__ == "__main__": 1621 | unittest.main() 1622 | ``` --------------------------------------------------------------------------------