{"id":25047357,"url":"https://github.com/tc7a/langchain_python","last_synced_at":"2026-04-22T23:35:02.740Z","repository":{"id":235122642,"uuid":"790043250","full_name":"tc7A/langchain_python","owner":"tc7A","description":"Here, I document my journey of learning and exploring LangChain with Python.  I aim to share my insights, discoveries, and code snippets as I delve deeper into understanding LangChain's capabilities and integrating it with Python.","archived":false,"fork":false,"pushed_at":"2024-04-27T03:29:12.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T01:48:12.009Z","etag":null,"topics":["ai-chatbot","chatgpt","chatgpt-api","generative-ai","langchain","langchain-python"],"latest_commit_sha":null,"homepage":"","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/tc7A.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":"2024-04-22T06:53:44.000Z","updated_at":"2024-12-20T14:44:22.000Z","dependencies_parsed_at":"2024-04-27T04:25:43.627Z","dependency_job_id":null,"html_url":"https://github.com/tc7A/langchain_python","commit_stats":null,"previous_names":["masum184e/llm_python","tc7a/langchain_python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc7A%2Flangchain_python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc7A%2Flangchain_python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc7A%2Flangchain_python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc7A%2Flangchain_python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tc7A","download_url":"https://codeload.github.com/tc7A/langchain_python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246403895,"owners_count":20771526,"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-chatbot","chatgpt","chatgpt-api","generative-ai","langchain","langchain-python"],"created_at":"2025-02-06T07:10:53.969Z","updated_at":"2026-04-22T23:35:02.695Z","avatar_url":"https://github.com/tc7A.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Language Model\n\nLanguage Model is a computer program that analyze a given sequence of words and provide a basis for their word prediction. Language model is used in AI, NLP, NLU, NLG system, particularly ones that perform text generation, machine translation and question answering.\n\n__LLM - Large Language Model__ are are designed to understand and generate human language at scale. **GPT**, **BERT**.\n\n__MLM - Masked Language Model__ are a specific type of language model that predicts masked or hidden or blank words in a sentence.\n\n__CLM - Casual Language Model__ generate text sequentially, one token at a time, based only on the tokens that came before it in the input sequence. It basically predict next word based on previous word\n\nHere's how a typical language model works:\n\n1. *Input:* The process starts with the user providing input in the form of text. This input can be a question, a prompt for generating text, or any other form of communication.\n\n2. *Tokenization:* The input text is split into smaller units called tokens. These tokens could be words, subwords, or even characters, depending on the model architecture and tokenization strategy used.\n\n3. *Embedding:* Each token is then converted into a numerical representation called word embeddings or token embeddings. These embeddings capture the semantic meaning of the tokens and their relationships with other tokens.\n\n4. *Processing:* The embeddings of the tokens are fed into the model's neural network architecture. This network consists of multiple layers of processing units (neurons) that transform the input embeddings through various mathematical operations.\n\n5. *Contextual Understanding:* As the input propagate through the network, the model learns to understand the contextual relationships between the tokens. It allow the model to focus on relevant parts of the input.\n\n6. *Prediction:* Based on its understanding of the input text and the context provided, the model generates a response. \n\n7. *Output:* The model outputs the predicted tokens, which can be used to generate text or to perform other tasks such as text classification, translation, or summarization.\n\n# Large Language Model\nLarge language model is a machine learning model designed to understand, generate, and manipulate human language on a vast scale. These models are typically built using deep learning techniques, especially variants of the transformer architecture, and are trained on massive datasets of text from the internet and other sources.\n\n# Generative AI\nGenerative AI refers to deep-learning models that can generate high-quality text, images, and other content based on the data they were trained on.\n\n## Quick Information\n- GPT(Generative Pre-trained Transformer) is a series of llm developed by OpenAI\n- ChatGPT is a generative AI specifically fine-tuned for conversational interactions.\n- OpenAI's work best with JSON while Anthropic's models work best with XML.\n\n# Langchain\nLangChain is an open source framework for building applications based on large language models (LLMs). It provides tools and abstractions to improve the customization, accuracy, and relevancy of the information the models generate. Basically it integrate ai(LLm model) with web/mobile applications. By abstracting complexities, it simplifies the process compared to direct integration, making it more accessible and manageable. The core element of any language model application is...the model. LangChain gives you the building blocks to interface with any language model.\n\n## Installation\n```\npip install langchain\n```\n\n# Model I/O - OpenAI\nLanguage models in LangChain come in two flavors:\n\n__ChatModels:__ The ChatModel objects take a list of messages as input and output a message. Chat models are often backed by LLMs but tuned specifically for having conversations. \n\n__LLM:__ LLMs in LangChain refer to pure text completion models. The LLM objects take string as input and output string. OpenAI's GPT-3 is implemented as an LLM.\n\nThe LLM returns a string, while the ChatModel returns a message. The main difference between them is their input and output schemas.  \n\n## Installation\n```\npip install langchain-openai\n```\n\n## Initialize The Model\nWe can see the difference between an LLM and a ChatModel when we invoke it.\n\n```\nfrom langchain_openai import ChatOpenAI\nfrom langchain_openai import OpenAI\n\nllm = ChatOpenAI(model_name=\"gpt-3.5-turbo-0125\",api_key=\"...\")\nchat_model = ChatOpenAI(model=\"gpt-3.5-turbo-0125\",api_key=\"...\")\n\ntext = \"What would be a good company name for a company that makes colorful socks?\"\nprint(\"LLM Response: \"+llm.invoke(text))\n\nmessages = [HumanMessage(content=text)]\nprint(\"Chat Model: \"+chat_model.invoke(messages))\n```\n\n__Reference:__ [OpenAI Model List](https://platform.openai.com/docs/models), [OpenAI](https://api.python.langchain.com/en/latest/llms/langchain_openai.llms.base.OpenAI.html), [ChatOpenAI](https://api.python.langchain.com/en/latest/llms/langchain_openai.llms.base.OpenAI.html), [HumanMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.human.HumanMessage.html)\n\n## Prompt Templates\nMost LLM applications do not pass user input directly into an LLM. Usually they will add the user input to a larger piece of text, that provides additional context on the specific task at hand so that llm can understand user input more efficiently.\n\nTypically, language models expect the prompt to either be a string or else a list of chat messages. Use `PromptTemplate` to create a template for a string prompt and `ChatPromptTemplate` to create a list of messages\n\nIf the user only had to provide the description of a specific topic but not the instruction that model needs, it would be great!! PromptTemplates help with exactly this! It bundle up all the logic \u0026 instruction going from user input into a fully fromatted prompt that llm model required.\n\n```\nfrom langchain_openai import ChatOpenAI\nfrom langchain_openai import OpenAI\nfrom langchain_core.prompts import PromptTemplate\n\nllm = ChatOpenAI(model_name=\"gpt-3.5-turbo-0125\",api_key=\"...\")\nchat_model = ChatOpenAI(model=\"gpt-3.5-turbo-0125\",api_key=\"...\")\n\nprompt = PromptTemplate.from_template(\"What is a good name for a company that makes {product}?\")\nprompt.format(product=\"colorful socks\")\n\n# PROMPT FROM PROMPT TEMPLATE\nprint(\"Prompt: \"+prompt)\n\n# LLM RESPONSE\nprint(\"LLM Response: \"+llm.invoke(text))\n\n# CHAT MODEL RESPONSE\nmessages = [HumanMessage(content=text)]\nprint(\"Chat Model: \"+chat_model.invoke(messages))\n```\n\n__Reference:__ [PromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.prompt.PromptTemplate.html)\n\n## ChatPromptTemplate\nEach chat message is associated with content, and an additional parameter called `role`. For example, in the OpenAI Chat Completions API, a chat message can be associated with an AI assistant, a human or a system role.\n\n```\nfrom langchain_openai import ChatOpenAI\nfrom langchain_openai import OpenAI\nfrom langchain_core.prompts import ChatPromptTemplate\n\nllm = ChatOpenAI(model_name=\"gpt-3.5-turbo-0125\",api_key=\"...\")\nchat_model = ChatOpenAI(model=\"gpt-3.5-turbo-0125\",api_key=\"...\")\n\nchat_template = ChatPromptTemplate.from_messages(\n    [\n        (\"system\", \"You are a helpful AI bot. Your name is {name}.\"),\n        (\"human\", \"Hello, how are you doing?\"),\n        (\"ai\", \"I'm doing well, thanks!\"),\n        (\"human\", \"{user_input}\"),\n    ]\n)\n\nprompt = chat_template.format_messages(name=\"Bob\", user_input=\"What is your name?\")\n\n# PROMPT FROM PROMPT TEMPLATE\nprint(\"Prompt: \"+prompt)\n\n# LLM RESPONSE\nprint(\"LLM Response: \"+llm.invoke(text))\n\n# CHAT MODEL RESPONSE\nmessages = [HumanMessage(content=text)]\nprint(\"Chat Model: \"+chat_model.invoke(messages))\n```\n\n__Reference:__ [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)\n\n## Message Prompts\nLangChain provides different types of MessagePromptTemplate. The most commonly used are `AIMessagePromptTemplate`, `SystemMessagePromptTemplate` and `HumanMessagePromptTemplate`, which create an AI message, system message and human message respectively.\n\nAll messages have a role and a content property. The role describes WHO is saying the message. The content property describes the content of the message. This can be a few different things:\n\n__Reference:__ [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)\n\n## Output parsers\nOutputParsers convert the raw output of a language model into a format that can be used downstream.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftc7a%2Flangchain_python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftc7a%2Flangchain_python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftc7a%2Flangchain_python/lists"}