{"id":22515115,"url":"https://github.com/rggh/langchain-course","last_synced_at":"2025-10-16T21:31:40.300Z","repository":{"id":188629228,"uuid":"677322954","full_name":"RGGH/LangChain-Course","owner":"RGGH","description":"Full LangChain Course for Python","archived":false,"fork":false,"pushed_at":"2023-08-17T10:32:54.000Z","size":3723,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-07T03:31:35.989Z","etag":null,"topics":["ai","langchain","python"],"latest_commit_sha":null,"homepage":"https://redandgreen.co.uk/why-use-langchain/ai-ml/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RGGH.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-11T09:30:33.000Z","updated_at":"2024-09-14T17:07:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"7e1e5da6-f860-4270-83d0-253da073607c","html_url":"https://github.com/RGGH/LangChain-Course","commit_stats":null,"previous_names":["rggh/langchain-course"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2FLangChain-Course","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2FLangChain-Course/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2FLangChain-Course/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2FLangChain-Course/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RGGH","download_url":"https://codeload.github.com/RGGH/LangChain-Course/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236749064,"owners_count":19198617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","langchain","python"],"created_at":"2024-12-07T03:28:23.551Z","updated_at":"2025-10-16T21:31:34.798Z","avatar_url":"https://github.com/RGGH.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LangChain Course\n\n### Introduction ~ LangChain : what is it, how does it work, why might you use it?\n\n\u003cdiv\u003e\n    \u0026nbsp;\u003cbr\u003e\n\u003ca href=\"https://www.youtube.com/watch?v=jfJbaJHnnP0\"\u003e\n    \u003cimg src=\"https://github.com/RGGH/LangChain-Course/blob/main/readme/thumb1.jpg\" alt=\"Python360 YouTube\" width=\"200\"/\u003e\n\u003c/a\u003e\n    \u0026nbsp;\u003cbr\u003e\n     \u0026nbsp;\u003cbr\u003e\n\u003c/div\u003e\n\n“First, **it makes it easier to build applications that use LLMs**. LLMs are complex models that can be difficult to use directly. LangChain provides a simple interface that makes it easy to connect LLMs to your application. Second, LangChain allows you to connect LLMs to other sources of data.\n\n### TLDR;\n\n**Components - prompt templates**\n\n**Chains - solve a specific task**\n\n**Agents - allow LLMs to interact (with external APIs)**\n\n### Without LangChain vs with LangChain\n\nAvailable for **Python *and* JavaScript**\n\n[https://js.langchain.com/docs/get_started/introduction/](https://js.langchain.com/docs/get_started/introduction/)\n\n[https://python.langchain.com/docs/get_started/introduction.html](https://python.langchain.com/docs/get_started/introduction.html)\n\nIf you want to create a GUI then try “Streamlit” in Python, or sign up for [https://www.langflow.org/](https://www.langflow.org/) or try [https://flowiseai.com/](https://flowiseai.com/)\n\nFeatures of LangChain:\n\n- Models\n- Prompts\n- Chains\n- Memory\n- Indexes\n- Agents \u0026 Tools\n\n**Use Cases:**\n\n- [Chatbots](https://python.langchain.com/docs/use_cases/chatbots/)\n- [Answering questions using sources](https://python.langchain.com/docs/use_cases/question_answering/)\n- [Analyzing structured data](https://python.langchain.com/docs/use_cases/tabular.html)\n\n### 1 - Try **the OpenAI API *without* LangChain**\n\n[https://platform.openai.com/examples](https://platform.openai.com/examples)\n\n[https://platform.openai.com/playground](https://platform.openai.com/playground)\n\n- Go to [http://platform.openai.com](http://platform.openai.com/)\n- Sign in, and start testing prompts!\n- Save your API key in .env file or ~/.zshrc !\n\n```jsx\nexport OPENAI_API_KEY=sk-xsssdfsddf67sadfasdfOXT3BlbkFJo12Ssdafdfasfadsfsafas\n```\n\n- Add .env to gitignore\n- I use a global gitignore file\n\n```jsx\npip install openai\n```\n\n****ChatGPT API Transition Guide :****\n\n'role' can take one of three values:\n\n'system',\n\n'user'\n\n'assistant’\n\nSign up to OpenAI if do you want to use them with your projects - requires credit card*\n\n### Tips to save $\n\nUse caching + FakeLLM\n\nSet a billing limit\n\nView token usage in your code :\n\nYou can try free LLMs but they often need RAM == $$$\n\n\n```bash\nfrom langchain.callbacks import get_openai_callback\n```\n\n---\n\n### **2 - Install LangChain**\n\nInstall LangChain:\n\n```jsx\npip install langchain\n```\n\nLangChain automates LLM calls, choose whichever LLM you prefer\n\n[https://python.langchain.com/docs/integrations/llms/openai](https://python.langchain.com/docs/integrations/llms/openai)\n\n---\n\n### 3 - **Models**\n\nThere are lots of LLM providers (OpenAI, Cohere, Hugging Face, etc) - the `LLM` class is designed to provide a standard interface for all of them.\n\n[https://python.langchain.com/docs/use_cases/chatbots/#overview](https://python.langchain.com/docs/use_cases/chatbots/#overview)\n\nYou can use `LLMs` (see [here](https://python.langchain.com/docs/modules/model_io/models/llms)) for chatbots as well, but chat models have a more conversational tone and natively support a message interface.\n\n```jsx\nfrom langchain.llms import OpenAI\nfrom langchain.llms import Cohere\nfrom langchain.llms import GooseAI\n```\n\nOnce you have imported you can create an instance of it\n\n```python\nllm = OpenAI()\n```\n\nAs of August 2023 - **gpt-3.5-turbo is the default model for the OpenAI class if you don’t specify anything inside the brackets.**\n\nWhat is the difference between LLM and chat model in LangChain?\n\n- [LLMs](https://python.langchain.com/docs/modules/model_io/models/llms/): Models that take a text string as input and return a text string\n- [Chat models](https://python.langchain.com/docs/modules/model_io/models/chat/): Models that are backed by a language model but take a list of Chat Messages as input and return a Chat Message\n\nChat Models: **Unlike LLMs, chat models take chat messages as inputs and return them as outputs**.\n\n[https://platform.openai.com/docs/guides/gpt/which-model-should-i-use](https://platform.openai.com/docs/guides/gpt/which-model-should-i-use)\n\n**`gpt-3.5-turbo`** returns outputs with lower latency and costs much less per token\n\n.predict and .run methods are usually the same!\n\n### 4 - Prompts\n\nTLDR; “Prompts are the text that you send to the LLM”\n\n[https://python.langchain.com/docs/modules/model_io/prompts/](https://python.langchain.com/docs/modules/model_io/prompts/)\n\nA prompt for a language model is a **set of instructions or input provided by a user** to guide the model's response, helping it understand the context and generate relevant and coherent language-based output, such as answering questions, completing sentences, or engaging in a conversation.\n\nIt’s like a Python f-string…Use a prompt template and you can pass a dynamically formed question.\n\n```jsx\nfrom langchain import PromptTemplate\n```\n\nChatPromptTemplate\n\nPromptTemplate\n\n[https://learnprompting.org/docs/category/-basics](https://learnprompting.org/docs/category/-basics)\n\n[https://platform.openai.com/playground](https://platform.openai.com/playground)\n\nSingle shot v Few Shot\n\n[https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/few_shot_examples](https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/few_shot_examples)\n\n[https://platform.openai.com/docs/guides/gpt/chat-completions-vs-completions](https://platform.openai.com/docs/guides/gpt/chat-completions-vs-completions)\n\nPrompts, Chains and Parser Basics\n\n### 5 - Chains\n\nSimpleSequentialChain - produces only 1 output\n\nSequentialChain - output of 1st chain goes into next chain  - *chain takes a dict!\n\n```jsx\nfrom langchain.chains import ...\n```\n\n### 6 - Router Chains\n\nThe RouterChain itself (responsible for selecting the next chain to call)\n\nUse `MultiPromptChain` to create a question-answering chain that selects the prompt which is most relevant for a given question. e.g. physics_template and maths_template\n\n### 7 - Memory\n\n**By default, LLMs are stateless,** which means each incoming query is processed independently of other interactions without memory, so every query is treated as an entirely independent input without considering past interactions.\n\n**Memory - Buffer vs. Summary**\n\n```jsx\nfrom langchain.memory import ChatMessageHistory\n```\n\n```bash\n# Retrieve chat messages with ConversationBufferHistory (as a variable)\nfrom langchain.memory import ConversationBufferMemory\n```\n\nConversationBufferMemory stores everything, but uses lots of tokens and response is slower.\n\n```bash\nfrom langchain.memory import ConversationBufferMemory\n# useful for keeping a sliding window of the most recent interactions, \n# so the buffer does not get too large\n```\n\n```bash\nmemory = ConversationBufferMemory()\nmemory.chat_memory.add_user_message(\"hi!\")\nmemory.chat_memory.add_ai_message(\"whats up?\")\n```\n\nConversationSummaryMemory keeps a **summarized** form of the conversation.\n\n\u003e “Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.”\n\n```bash\nconversation_sum = ConversationChain(\n    llm=llm, \n    memory=ConversationSummaryMemory(llm=llm)\n)\n```\n\n```bash\n# extracts information on entities (using an LLM) and \n# builds up its knowledge about that entity over time (also using an LLM)\nfrom langchain.memory import ConversationEntityMemory\n```\n\n**More memory options:**\n\n```bash\nfrom langchain.chains.conversation.memory import (ConversationBufferMemory, \n                                                  ConversationSummaryMemory, \n                                                  ConversationBufferWindowMemory,\n                                                  ConversationKGMemory)\n```\n\n[https://python.langchain.com/docs/modules/memory/#using-a-chatmodel](https://python.langchain.com/docs/modules/memory/#using-a-chatmodel)\n\n[https://www.pinecone.io/learn/series/langchain/langchain-conversational-memory/](https://www.pinecone.io/learn/series/langchain/langchain-conversational-memory/)\n\n### [View the Memory Store](https://python.langchain.com/docs/modules/memory/types/entity_summary_memory#inspecting-the-memory-store)\n\n```bash\nfrom pprint import pprint\npprint(conversation.memory.entity_store.store)\n```\n\n### Indexes\n\n- Document loaders\n- Text splitters\n- Retrievers\n- Vector stores - embed text as vectors (Similarity Search)\n\n\n[https://www.pinecone.io/](https://www.pinecone.io/)\n\n[https://qdrant.tech/](https://qdrant.tech/)\n\n[https://python.langchain.com/docs/integrations/vectorstores/qdrant](https://python.langchain.com/docs/integrations/vectorstores/qdrant)\n\n[https://python.langchain.com/docs/use_cases/question_answering/](https://python.langchain.com/docs/use_cases/question_answering/)\n\n[https://redandgreen.co.uk/text-embedding-and-upsert-with-qdrant-in-python/ai-ml/](https://redandgreen.co.uk/text-embedding-and-upsert-with-qdrant-in-python/ai-ml/)\n\n```python\npip install langchain.vectorstores langchain.embeddings \nlangchain.text_splitter qdrant_client numpy sentence_transformers tqdm\n```\n\n\n```python\nimport langchain\nlangchain.debug = True\n```\n\n```jsx\npython3.10 -i q1.py\n```\n\n\n### Agents - LLMs plus Tools\n\nLanguage models can run Python code! - tool = PythonREPL()\n\nHow ChatGPT Plugins work\n\nEvaluation - Use LLMs to evaluate LLMs\n\n### Links\n\n[https://github.com/kyrolabs/awesome-langchain](https://github.com/kyrolabs/awesome-langchain)\n\n[https://github.com/openai/openai-cookbook](https://github.com/openai/openai-cookbook)\n\n[https://platform.openai.com/](https://platform.openai.com/)\n\n[https://alphasec.io/langchain-decoded-part-1-models/](https://alphasec.io/langchain-decoded-part-1-models/)\n\n[https://platform.openai.com/playground](https://platform.openai.com/playground)\n\n[https://github.com/pinecone-io/examples/tree/master/learn/generation/langchain/handbook](https://github.com/pinecone-io/examples/tree/master/learn/generation/langchain/handbook)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frggh%2Flangchain-course","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frggh%2Flangchain-course","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frggh%2Flangchain-course/lists"}