Local branch had diverged from origin/master (sibling commits on the same base). Rewrote local history linearly on top of origin/master, folding in all local content: exa/hot-platform discovery hunters, leaks ledger, nightly sweep orchestrator, updated .gitignore and skill docs, plus the latest hunt output and vault state. Remote-only files (vault keys, channel scripts) were restored rather than dropped, so the resulting tree is a full union of both sides.
1 line
30 KiB
JSON
1 line
30 KiB
JSON
{"repo": "bharadwajrachakonda/langchain-rag", "files": {"LangChain_Outputs.ipynb": {"17c4fa4aef247f589b655cf64cc6c67f4d249f18": {"t": 1786068563.4155295, "n": 28250, "h": "b2d0c747ab47e6ab", "c": "{\n \"nbformat\": 4,\n \"nbformat_minor\": 0,\n \"metadata\": {\n \"colab\": {\n \"provenance\": [],\n \"collapsed_sections\": [\n \"eVHCuzsnmSLL\",\n \"8HA6AuqbMwEj\",\n \"JtGM90RWM1On\",\n \"OFeW4LojTnc6\",\n \"FRenTXvuzCnR\",\n \"82xf3PwQ9iBy\"\n ],\n \"authorship_tag\": \"ABX9TyMWJ9QQfmUhdL3QelfPi8R/\"\n },\n \"kernelspec\": {\n \"name\": \"python3\",\n \"display_name\": \"Python 3\"\n },\n \"language_info\": {\n \"name\": \"python\"\n }\n },\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"pip install langchain langchain-core langchain-openai openai langchain-anthropic langchain-google-genai google-generativeai langchain-huggingface transformers huggingface-hub python-dotenv numpy scikit-learn\"\n ],\n \"metadata\": {\n \"collapsed\": true,\n \"id\": \"FYRE5l065ygP\"\n },\n \"execution_count\": null,\n \"outputs\": []\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"#**Output as json**\\n\",\n \"> if the model supports you can use ***with_structured_output*** function else you need to use **output parsers**\"\n ],\n \"metadata\": {\n \"id\": \"sndXJ7Gglsh4\"\n }\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"##with_structured_output\\n\",\n \"\\n\",\n \"> *typed dictionary, pydantic, json_schema*\"\n ],\n \"metadata\": {\n \"id\": \"eVHCuzsnmSLL\"\n }\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###typed dictionary\"\n ],\n \"metadata\": {\n \"id\": \"8HA6AuqbMwEj\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"#typed dictionary\\n\",\n \"'''it dosent validate during runtime but tells what type key should be while writing code'''\\n\",\n \"\\n\",\n \"class Person:\\n\",\n \" name:str\\n\",\n \" id:int\\n\",\n \"\\n\",\n \"d: Person = {\\\"name\\\": \\\"hello\\\", \\\"id\\\": 10}\"\n ],\n \"metadata\": {\n \"id\": \"9lEbLCeTmgre\"\n },\n \"execution_count\": null,\n \"outputs\": []\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from google.colab import userdata\\n\",\n \"from typing import TypedDict\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"class Person(TypedDict):\\n\",\n \" name:str\\n\",\n \" topic: str\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token=huggingface_api_key, repo_id=\\\"deepseek-ai/DeepSeek-V4-Flash\\\", task=\\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"new_model = model.with_structured_output(Person) # mode = json to get output as json, mode = function calling if you want to call a function\\n\",\n \"\\n\",\n \"result = new_model.invoke(\\\"Bharat is from class 10 with poor marks in his CBSE marks due to mismanagement of the exam by the board that conducts it.\\\")\\n\",\n \"print(result)\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"qmD2efKU5h6R\",\n \"outputId\": \"1eff4790-159a-4f3a-c1b0-29125b2c3c83\"\n },\n \"execution_count\": null,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'name': 'Bharat', 'topic': 'CBSE exam mismanagement and poor marks'}\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"# if model dosent understand context of output to be gen\\n\",\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from google.colab import userdata\\n\",\n \"from typing import TypedDict, Annotated, Optional, Literal\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"class Person(TypedDict):\\n\",\n \" name: Annotated[str, \\\"name of the main character\\\"]\\n\",\n \" topic: Annotated[str, \\\"One word that describes the topic the person is involved in\\\"]\\n\",\n \" wrong: Annotated[Optional[str], \\\"what went wrong in his situation in atmost 3 words\\\"]\\n\",\n \" outcome: Annotated[Literal[\\\"worst\\\", \\\"ok\\\", \\\"great\\\"], \\\"what was the outcome to that person\\\"]\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token=huggingface_api_key, repo_id=\\\"deepseek-ai/DeepSeek-V4-Flash\\\", task=\\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"new_model = model.with_structured_output(Person)\\n\",\n \"\\n\",\n \"result = new_model.invoke(\\\"Bharat is from class 10 with poor marks in his CBSE marks due to mismanagement of the exam by the board that conducts it due to which he sucided.\\\")\\n\",\n \"print(result)\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"dYxP9u1xKDSB\",\n \"outputId\": \"64157db9-6988-42f8-ed80-1128169c16e1\"\n },\n \"execution_count\": null,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'name': 'Bharat', 'topic': 'Exams', 'wrong': 'Exam mismanagement', 'outcome': 'worst'}\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###Pydantic\"\n ],\n \"metadata\": {\n \"id\": \"JtGM90RWM1On\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"#pydantic not only imposes types, but also validates it\\n\",\n \"!pip install pydantic email_validator\"\n ],\n \"metadata\": {\n \"id\": \"wFsRB8gqM7CB\"\n },\n \"execution_count\": null,\n \"outputs\": []\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from pydantic import BaseModel, EmailStr, Field\\n\",\n \"\\n\",\n \"class Person(BaseModel):\\n\",\n \" name:str #for things like optional, literal, annotated you can just use typing module things\\n\",\n \" name_:str = \\\"default_value\\\"\\n\",\n \" email: EmailStr = \\\"abc@gmail.com\\\"\\n\",\n \" marks: float = Field(gt=0, lt=10, default=5, description=\\\"marks\\\")\\n\",\n \"\\n\",\n \"student = {\\\"name\\\": \\\"bharat\\\", \\\"marks\\\": 5}\\n\",\n \"\\n\",\n \"print(Person(**student))\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"tEaQw6h4P9k8\",\n \"outputId\": \"ab427283-1e89-406f-d943-614daf8e28d9\"\n },\n \"execution_count\": null,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"name='bharat' name_='default_value' email='abc@gmail.com' marks=5.0\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###JSON\"\n ],\n \"metadata\": {\n \"id\": \"OFeW4LojTnc6\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"#use json if you are working with multiple languages\\n\",\n \"'''format of json document'''\\n\",\n \"\\n\",\n \"{\\n\",\n \" \\\"title\\\": \\\"person\\\",\\n\",\n \" \\\"description\\\": \\\"schema of person\\\",\\n\",\n \" \\\"type\\\": \\\"object\\\",\\n\",\n \" \\\"properties\\\": {\\n\",\n \" \\\"name\\\": \\\"string\\\",\\n\",\n \" \\\"age\\\": \\\"integer\\\"\\n\",\n \" },\\n\",\n \" \\\"required\\\": [\\\"name\\\"]\\n\",\n \"}\"\n ],\n \"metadata\": {\n \"id\": \"Xl4vJEbHQZHX\"\n },\n \"execution_count\": null,\n \"outputs\": []\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"#An example json schema\\n\",\n \"json_schema = {\\n\",\n \" \\\"title\\\": \\\"Review\\\",\\n\",\n \" \\\"type\\\": \\\"object\\\",\\n\",\n \" \\\"properties\\\": {\\n\",\n \" \\\"key_themes\\\": {\\n\",\n \" \\\"type\\\": \\\"array\\\",\\n\",\n \" \\\"items\\\": {\\n\",\n \" \\\"type\\\": \\\"string\\\"\\n\",\n \" },\\n\",\n \" \\\"description\\\": \\\"Write down all the key themes discussed in the review in a list\\\"\\n\",\n \" },\\n\",\n \" \\\"summary\\\": {\\n\",\n \" \\\"type\\\": \\\"string\\\",\\n\",\n \" \\\"description\\\": \\\"A brief summary of the review\\\"\\n\",\n \" },\\n\",\n \" \\\"sentiment\\\": {\\n\",\n \" \\\"type\\\": \\\"string\\\",\\n\",\n \" \\\"enum\\\": [\\\"pos\\\", \\\"neg\\\"],\\n\",\n \" \\\"description\\\": \\\"Return sentiment of the review either negative, positive or neutral\\\"\\n\",\n \" },\\n\",\n \" \\\"pros\\\": {\\n\",\n \" \\\"type\\\": [\\\"array\\\", \\\"null\\\"],\\n\",\n \" \\\"items\\\": {\\n\",\n \" \\\"type\\\": \\\"string\\\"\\n\",\n \" },\\n\",\n \" \\\"description\\\": \\\"Write down all the pros inside a list\\\"\\n\",\n \" },\\n\",\n \" \\\"cons\\\": {\\n\",\n \" \\\"type\\\": [\\\"array\\\", \\\"null\\\"],\\n\",\n \" \\\"items\\\": {\\n\",\n \" \\\"type\\\": \\\"string\\\"\\n\",\n \" },\\n\",\n \" \\\"description\\\": \\\"Write down all the cons inside a list\\\"\\n\",\n \" },\\n\",\n \" \\\"name\\\": {\\n\",\n \" \\\"type\\\": [\\\"string\\\", \\\"null\\\"],\\n\",\n \" \\\"description\\\": \\\"Write the name of the reviewer\\\"\\n\",\n \" }\\n\",\n \" },\\n\",\n \" \\\"required\\\": [\\\"key_themes\\\", \\\"summary\\\", \\\"sentiment\\\"]\\n\",\n \"}\"\n ],\n \"metadata\": {\n \"id\": \"xJ0k79p6U8dy\"\n },\n \"execution_count\": null,\n \"outputs\": []\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"Everything else is same for all 3 from with standard output\"\n ],\n \"metadata\": {\n \"id\": \"cnPr31H7VPla\"\n }\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"##Output parsers\\n\",\n \"\\n\",\n \"> ***StrOutputParser*** - For string output, ***JSONOutputParser*** - Json output - But not structured, ***StructuredOutputParser*** - Structured Json - But No validation, ***PydanticOutputParser*** - Structured Json and Validated\"\n ],\n \"metadata\": {\n \"id\": \"SxAReX-qwN-h\"\n }\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###StrOutputParser\"\n ],\n \"metadata\": {\n \"id\": \"FRenTXvuzCnR\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from langchain_core.prompts import PromptTemplate, MessagesPlaceholder\\n\",\n \"from langchain_core.output_parsers import StrOutputParser\\n\",\n \"from google.colab import userdata\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"template1 = PromptTemplate(template=\\\"write a detailed report on {topic}\\\", input_variables=[\\\"topic\\\"])\\n\",\n \"\\n\",\n \"template2 = PromptTemplate(template=\\\"summarise this text {text}\\\", input_variables=[\\\"text\\\"])\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token = huggingface_api_key, repo_id=\\\"meta-llama/Llama-3.1-8B-Instruct\\\", task = \\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"\\n\",\n \"parser = StrOutputParser()\\n\",\n \"chain = template1 | model | parser | template2 | model | parser\\n\",\n \"result = chain.invoke({\\n\",\n \" \\\"topic\\\": \\\"AI\\\"\\n\",\n \"})\\n\",\n \"\\n\",\n \"print(result)\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"collapsed\": true,\n \"id\": \"Vhwrht5hwSmo\",\n \"outputId\": \"ee13668c-b193-4fcc-b662-b5721d1f3601\"\n },\n \"execution_count\": null,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"### **Summary of the Comprehensive Report on Artificial Intelligence (AI)**\\n\",\n \"\\n\",\n \"#### **1. Introduction**\\n\",\n \"AI is a transformative technology that enables machines to simulate human intelligence, impacting industries, economies, and daily life. This report covers AI's fundamentals, applications, ethical concerns, challenges, and future trends.\\n\",\n \"\\n\",\n \"#### **2. Definition & Types of AI**\\n\",\n \"- **AI** refers to computer systems performing tasks like learning, reasoning, perception, decision-making, and natural language processing (NLP).\\n\",\n \"- **Types of AI**:\\n\",\n \" - **Narrow (Weak) AI**: Task-specific (e.g., Siri, chatbots).\\n\",\n \" - **General (Strong) AI**: Human-like reasoning (not yet achieved).\\n\",\n \" - **Superintelligent AI**: Hypothetical AI surpassing human intelligence.\\n\",\n \"\\n\",\n \"#### **3. Key AI Technologies**\\n\",\n \"- **Machine Learning (ML)**: Algorithms that learn from data.\\n\",\n \"- **Natural Language Processing (NLP)**: Enables AI to understand and generate human language.\\n\",\n \"- **Computer Vision**: AI interpreting visual data (e.g., facial recognition).\\n\",\n \"- **Robotics**: AI-powered machines for physical tasks.\\n\",\n \"- **Expert Systems**: AI for specialized decision-making.\\n\",\n \"\\n\",\n \"#### **4. Applications of AI**\\n\",\n \"- **Healthcare**: Disease diagnosis, drug discovery, personalized medicine.\\n\",\n \"- **Finance**: Fraud detection, algorithmic trading, AI customer service.\\n\",\n \"\\n\",\n \"*(Note: The summary is incomplete due to the source text being cut off.)*\\n\",\n \"\\n\",\n \"This report highlights AI's broad impact, underlying technologies, and key applications while emphasizing its potential and challenges.\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###JsonOutputParser\"\n ],\n \"metadata\": {\n \"id\": \"82xf3PwQ9iBy\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from langchain_core.prompts import PromptTemplate\\n\",\n \"from langchain_core.output_parsers import JsonOutputParser\\n\",\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from google.colab import userdata\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"parser = JsonOutputParser()\\n\",\n \"template = PromptTemplate(template=\\\"write a summary on blackhole {format_instruction}\\\", partial_variables={\\n\",\n \" \\\"format_instruction\\\": parser.get_format_instructions()\\n\",\n \"})\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token = huggingface_api_key, repo_id=\\\"meta-llama/Llama-3.1-8B-Instruct\\\", task = \\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"\\n\",\n \"'''instead of this'''\\n\",\n \"# prompt = template.format()\\n\",\n \"# result = model.invoke(prompt)\\n\",\n \"# print(parser.parse(result.content))\\n\",\n \"\\n\",\n \"'''you can use this'''\\n\",\n \"chain = template | model | parser\\n\",\n \"result = chain.invoke({})\\n\",\n \"print(result)\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"xwUnJvCQ9sMk\",\n \"outputId\": \"a1f594b4-517b-4c3c-ffc7-b30edca5da5a\"\n },\n \"execution_count\": null,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'summary': 'A black hole is a region in spacetime where gravity is so strong that nothing, not even light, can escape. They form when massive stars collapse under their own gravity at the end of their life cycle. Black holes have an event horizon, a boundary beyond which escape is impossible, and a singularity at their center where matter is compressed to infinite density. They play a crucial role in astrophysics, influencing galaxy formation, and their existence has been confirmed through observations of gravitational waves and the Event Horizon Telescope.', 'key_points': ['Formed from the collapse of massive stars', 'Infinite gravitational pull beyond the event horizon', 'Singularity at the center', 'Can be detected through gravitational waves and radiation', 'Supermassive black holes exist at the centers of galaxies']}\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###StructuredOutputParser\"\n ],\n \"metadata\": {\n \"id\": \"wVGJ3L8Xs07m\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"!pip install langchain_classic\"\n ],\n \"metadata\": {\n \"collapsed\": true,\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"pBigX6YpwQm6\",\n \"outputId\": \"9ca08f1d-c08a-4c00-fbef-c0f4f350e785\"\n },\n \"execution_count\": 3,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"Collecting langchain_classic\\n\",\n \" Downloading langchain_classic-1.0.8-py3-none-any.whl.metadata (5.1 kB)\\n\",\n \"Requirement already satisfied: langchain-core<2.0.0,>=1.4.4 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (1.4.6)\\n\",\n \"Collecting langchain-text-splitters<2.0.0,>=1.1.2 (from langchain_classic)\\n\",\n \" Downloading langchain_text_splitters-1.1.2-py3-none-any.whl.metadata (3.3 kB)\\n\",\n \"Requirement already satisfied: langsmith<1.0.0,>=0.1.17 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (0.8.9)\\n\",\n \"Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (2.12.3)\\n\",\n \"Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (6.0.3)\\n\",\n \"Requirement already satisfied: requests<3.0.0,>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (2.32.4)\\n\",\n \"Requirement already satisfied: sqlalchemy<3.0.0,>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from langchain_classic) (2.0.50)\\n\",\n \"Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (1.33)\\n\",\n \"Requirement already satisfied: langchain-protocol>=0.0.14 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (0.0.16)\\n\",\n \"Requirement already satisfied: packaging>=23.2.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (26.2)\\n\",\n \"Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (9.1.4)\\n\",\n \"Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (4.15.0)\\n\",\n \"Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.4.4->langchain_classic) (0.16.0)\\n\",\n \"Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (0.28.1)\\n\",\n \"Requirement already satisfied: orjson>=3.9.14 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (3.11.9)\\n\",\n \"Requirement already satisfied: requests-toolbelt>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (1.0.0)\\n\",\n \"Requirement already satisfied: websockets>=15.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (15.0.1)\\n\",\n \"Requirement already satisfied: xxhash>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (3.7.0)\\n\",\n \"Requirement already satisfied: zstandard>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.17->langchain_classic) (0.25.0)\\n\",\n \"Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain_classic) (0.7.0)\\n\",\n \"Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain_classic) (2.41.4)\\n\",\n \"Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain_classic) (0.4.2)\\n\",\n \"Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.0.0->langchain_classic) (3.4.7)\\n\",\n \"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.0.0->langchain_classic) (3.18)\\n\",\n \"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.0.0->langchain_classic) (2.5.0)\\n\",\n \"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.0.0->langchain_classic) (2026.5.20)\\n\",\n \"Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.12/dist-packages (from sqlalchemy<3.0.0,>=1.4.0->langchain_classic) (3.5.1)\\n\",\n \"Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.17->langchain_classic) (4.13.0)\\n\",\n \"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.17->langchain_classic) (1.0.9)\\n\",\n \"Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.17->langchain_classic) (0.16.0)\\n\",\n \"Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.12/dist-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.4.4->langchain_classic) (3.1.1)\\n\",\n \"Downloading langchain_classic-1.0.8-py3-none-any.whl (1.0 MB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m1.0/1.0 MB\\u001b[0m \\u001b[31m14.0 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading langchain_text_splitters-1.1.2-py3-none-any.whl (35 kB)\\n\",\n \"Installing collected packages: langchain-text-splitters, langchain_classic\\n\",\n \"Successfully installed langchain-text-splitters-1.1.2 langchain_classic-1.0.8\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from langchain_classic.output_parsers import StructuredOutputParser, ResponseSchema\\n\",\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from langchain_core.prompts import PromptTemplate\\n\",\n \"from google.colab import userdata\\n\",\n \"\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token = huggingface_api_key, repo_id=\\\"meta-llama/Llama-3.1-8B-Instruct\\\", task = \\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"\\n\",\n \"schema = [\\n\",\n \" ResponseSchema(name = 'fact_1', description='fact 1 of topic'),\\n\",\n \" ResponseSchema(name = 'fact_2', description='fact 2 of topic'),\\n\",\n \" ResponseSchema(name = 'fact_3', description='fact 3 of topic')\\n\",\n \"]\\n\",\n \"\\n\",\n \"parser = StructuredOutputParser.from_response_schemas(schema)\\n\",\n \"format_instructions = parser.get_format_instructions()\\n\",\n \"\\n\",\n \"template = PromptTemplate(template=\\\"give 3 facts on {topic} \\\\n {format_instructions}\\\", input_variables=[\\\"topic\\\"], partial_variables={\\n\",\n \" \\\"format_instructions\\\": format_instructions\\n\",\n \"})\\n\",\n \"\\n\",\n \"'''you can use this'''\\n\",\n \"# chain = template | model | parser\\n\",\n \"# print(chain.invoke(\\\"Hi\\\"))\\n\",\n \"\\n\",\n \"'''or this'''\\n\",\n \"prompt = template.invoke({\\n\",\n \" \\\"topic\\\": \\\"Hi - Hello\\\"\\n\",\n \" })\\n\",\n \"result = model.invoke(prompt)\\n\",\n \"print(parser.parse(result.content))\"\n ],\n \"metadata\": {\n \"id\": \"6F0fu6WJvbhr\",\n \"outputId\": \"92a0eff4-2a16-44e1-dc5e-5ddf17ef2e99\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"collapsed\": true\n },\n \"execution_count\": 22,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'fact_1': \\\"The word 'Hello' is one of the most widely recognized greetings in the world, used by over 80% of the global population.\\\", 'fact_2': \\\"The first recorded use of the word 'Hello' as a greeting dates back to 1839 in the United States, where it was first used as a telephone greeting.\\\", 'fact_3': \\\"In some cultures, saying 'Hello' is considered impolite or even rude, such as in Japan, where it's considered more polite to use a more formal greeting like 'Konnichiwa'.\\\"}\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"source\": [\n \"###PydanticOutputParser\"\n ],\n \"metadata\": {\n \"id\": \"7EnkfnRC0DMf\"\n }\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"from langchain_core.output_parsers import PydanticOutputParser\\n\",\n \"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\\n\",\n \"from langchain_core.prompts import PromptTemplate\\n\",\n \"from pydantic import BaseModel, Field\\n\",\n \"from google.colab import userdata\\n\",\n \"\\n\",\n \"huggingface_api_key = userdata.get('HUGGINGFACEHUB_ACCESS_TOKEN')\\n\",\n \"\\n\",\n \"llm = HuggingFaceEndpoint(huggingfacehub_api_token = huggingface_api_key, repo_id=\\\"meta-llama/Llama-3.1-8B-Instruct\\\", task = \\\"text-generation\\\")\\n\",\n \"model = ChatHuggingFace(llm=llm)\\n\",\n \"\\n\",\n \"\\n\",\n \"class Person(BaseModel):\\n\",\n \" name:str = Field(description=\\\"name of the person\\\")\\n\",\n \" age:int = Field(gt=18, description=\\\"age of the person\\\")\\n\",\n \" city:str = Field(description=\\\"city from which the person belongs to\\\")\\n\",\n \"\\n\",\n \"parser = PydanticOutputParser(pydantic_object=Person)\\n\",\n \"\\n\",\n \"# it gave errors without mentioning to return json, small models hellusinate and it is giving me python code to generate json, so I mentioned it to only return json\\n\",\n \"template = PromptTemplate(template=\\\"Generate the name, age and city of a ficitonal {place} person Return ONLY a JSON object. \\\\n {format_instructions}\\\", input_variables=[\\\"place\\\"], partial_variables={\\n\",\n \" \\\"format_instructions\\\": parser.get_format_instructions()\\n\",\n \"})\\n\",\n \"'''you can also use this'''\\n\",\n \"# chain = template | model | parser\\n\",\n \"# print(chain.invoke({\\n\",\n \"# \\\"place\\\": \\\"indian\\\"\\n\",\n \"# }))\\n\",\n \"\\n\",\n \"prompt = template.invoke({\\n\",\n \" \\\"place\\\": \\\"indian\\\"\\n\",\n \"})\\n\",\n \"\\n\",\n \"result = model.invoke(prompt)\\n\",\n \"print(parser.parse(result.content))\"\n ],\n \"metadata\": {\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n },\n \"id\": \"JjALWFnM5DP6\",\n \"outputId\": \"d2a910f8-84f6-4f09-f344-a0cd50eb6874\"\n },\n \"execution_count\": 37,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"name='Rohan Sharma' age=32 city='Mumbai'\\n\"\n ]\n }\n ]\n }\n ]\n}"}}}} |