LangChain / RAG / वेक्टर डेटाबेस · पाठ

बाहरी API को उपकरणों के रूप में एकीकृत करना

अपने RAG सिस्टम को बाहरी API और सेवाओं से जोड़ें, ताकि एजेंट वास्तविक समय का डेटा प्राप्त कर सकें या कार्य कर सकें।

पाठ 3, कुल 4 में से11 चरण

बाहरी API को उपकरणों के रूप में एकीकृत करना, CoddyKit पर LangChain / RAG / वेक्टर डेटाबेस का एक निःशुल्क पाठ है। यह 4 में से 3वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह LangChain / RAG / वेक्टर डेटाबेस सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। LangChain / RAG / वेक्टर डेटाबेस पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

एजेंट और बाहरी APIs

एजेंट केवल पाठ तैयार करने तक सीमित नहीं हैं! वे "उपकरण" कहलाने वाले विशेष फ़ंक्शन का उपयोग करके वास्तविक दुनिया के साथ बातचीत कर सकते हैं। बाहरी APIs इसके लिए बिल्कुल उपयुक्त हैं, क्योंकि इनके माध्यम से एजेंट वास्तविक समय का डेटा प्राप्त कर सकते हैं या कार्रवाइयाँ कर सकते हैं।

वास्तविक दुनिया से बातचीत के उपकरण

LangChain में उपकरण मूल रूप से एक पाइथन फ़ंक्शन होता है जिसे एजेंट बुला सकता है। ये फ़ंक्शन लगभग किसी भी चीज़ को समाहित कर सकते हैं:

  • वेब पर खोज करना
  • डेटाबेस से जानकारी प्राप्त करना
  • ईमेल भेजना
  • किसी बाहरी API के साथ बातचीत करना

उपकरण एजेंटों की आंतरिक जानकारी से आगे उनकी क्षमताओं का विस्तार करके उन्हें असाधारण शक्तियाँ देते हैं।

API उपकरण तैयार करना

किसी बाहरी API को एकीकृत करने के लिए पहले एक मानक पाइथन फ़ंक्शन परिभाषित करें, जो API कॉल करता हो। फिर LangChain की Tool क्लास का उपयोग करके इस फ़ंक्शन को समाहित करें।

Tool को इन चीज़ों की आवश्यकता होती है:

  • एक नाम: एजेंट इसे किस नाम से संदर्भित करता है।
  • एक विवरण: उपकरण क्या करता है और उसे किस इनपुट की अपेक्षा है।
  • वास्तविक फ़ंक्शन (func), जो कार्रवाई करता है।

सरल उपकरण फ़ंक्शन का उदाहरण

आइए एक ऐसे बुनियादी Python फ़ंक्शन से शुरुआत करते हैं जो API कॉल का अनुकरण करता है। यह फ़ंक्शन दिए गए शहर के लिए "वर्तमान समय प्राप्त" करेगा।

ध्यान दें कि फ़ंक्शन एकल स्ट्रिंग आर्ग्युमेंट लेता है, जिसे एजेंट अपने तर्क के आधार पर उपलब्ध कराएगा।

from langchain.tools import Tool

def get_current_time(city: str) -> str:
    """Gets the current time for a specified city."""
    # This would typically make an actual API call
    if city.lower() == "london":
        return "10:30 AM GMT"
    elif city.lower() == "new york":
        return "05:30 AM EST"
    else:
        return "Time not available for that city."

# This function will be wrapped into a Tool later.

सरल टूल का उपयोग करता एजेंट

यहाँ एक पूरा उदाहरण है, जिसमें LangChain एजेंट हमारे get_current_time टूल का उपयोग करता है। एजेंट उपयोगकर्ता की क्वेरी और टूल के विवरण के आधार पर तय करता है कि टूल को कब कॉल करना है।

ध्यान दें: इसे चलाने के लिए आपको अपना OPENAI_API_KEY पर्यावरण चर सेट करना होगा।

import os
from langchain.tools import Tool
from langchain_openai import OpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain import hub

# Mock function for getting time (simulates API)
def get_current_time(city: str) -> str:
    """Gets the current time for a specified city."""
    if city.lower() == "london":
        return "10:30 AM GMT"
    elif city.lower() == "new york":
        return "05:30 AM EST"
    else:
        return "Time not available for that city."

# Create the LangChain Tool
time_tool = Tool(
    name="get_time",
    func=get_current_time,
    description="Useful for getting the current time for a given city. Input should be a city name (e.g., 'London')."
)

# List of tools available to the agent
tools = [time_tool]

# Initialize the LLM (ensure OPENAI_API_KEY is set)
llm = OpenAI(temperature=0)

# Pull the ReAct prompt template from LangChain Hub
prompt = hub.pull("hwchase17/react")

# Create the agent
agent = create_react_agent(llm, tools, prompt)

# Create the AgentExecutor to run the agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Invoke the agent with a query
response = agent_executor.invoke({"input": "What time is it in London?"})
print(response["output"])

`requests` के साथ वास्तविक API से जुड़ना

वास्तविक दुनिया में API के साथ काम करने के लिए Python की requests लाइब्रेरी सबसे उपयोगी विकल्प है। यह बाहरी सेवाओं को HTTP अनुरोध भेजना आसान बनाती है।

आमतौर पर, अपने टूल फ़ंक्शन के भीतर आप एक अनुरोध भेजेंगे, HTTP प्रतिक्रिया प्राप्त करेंगे और फिर प्रासंगिक डेटा निकालने के लिए उसकी सामग्री (अक्सर JSON) का विश्लेषण करेंगे।

import requests

def fetch_data_from_api(query: str) -> str:
    """Fetches data from a hypothetical external API."""
    try:
        # Example: calling a public API (e.g., a mock weather API)
        # Replace with your actual API endpoint and parameters
        api_url = f"https://api.example.com/data?q={query}" # Placeholder URL
        response = requests.get(api_url)
        response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
        data = response.json()
        return str(data) # Return relevant part of the data
    except requests.exceptions.RequestException as e:
        return f"Error fetching data: {e}"

# This function would then be wrapped in a LangChain Tool.

मौसम का टूल बनाना

आइए एक अधिक व्यावहारिक उदाहरण बनाते हैं: ऐसा टूल जो एक नकली मौसम API का उपयोग करके किसी शहर का वर्तमान मौसम प्राप्त करता है। वास्तविक परिस्थिति में आप OpenWeatherMap जैसी सेवा से जुड़ेंगे।

विस्तृत विवरण पर ध्यान दें, जो LLM को इनपुट के प्रारूप के बारे में मार्गदर्शन देता है।

import requests
from langchain.tools import Tool

# Mock weather API function
def get_weather_for_city(city: str) -> str:
    """
    Fetches the current weather for a specified city.
    Uses a mock API for demonstration.
    """
    # In a real app, replace with an actual weather API call
    mock_weather_data = {
        "london": {"temperature": "15C", "conditions": "Cloudy"},
        "new york": {"temperature": "22C", "conditions": "Sunny"},
        "paris": {"temperature": "18C", "conditions": "Partly Cloudy"}
    }
    weather = mock_weather_data.get(city.lower())
    if weather:
        return f"The current weather in {city} is {weather['temperature']} and {weather['conditions']}."
    else:
        return f"Weather data not available for {city}."

# Create the LangChain Tool for weather
weather_tool = Tool(
    name="get_weather",
    func=get_weather_for_city,
    description="Useful for getting the current weather conditions (temperature, conditions) for a specific city. Input should be a city name (e.g., 'London')."
)

# This tool can now be added to an agent's tool list.

मौसम API को कॉल करता एजेंट

अब देखते हैं कि कोई एजेंट हमारे get_weather टूल का उपयोग कैसे करता है। एजेंट की तर्क प्रक्रिया (यदि verbose=True हो) दिखाएगी कि वह टूल का उपयोग करने का निर्णय कैसे लेता है, उसे कॉल करता है और फिर क्वेरी का उत्तर देने के लिए परिणाम का उपयोग करता है।

import os
from langchain.tools import Tool
from langchain_openai import OpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain import hub

# Mock weather API function (from previous scene)
def get_weather_for_city(city: str) -> str:
    """
    Fetches the current weather for a specified city.
    Uses a mock API for demonstration.
    """
    mock_weather_data = {
        "london": {"temperature": "15C", "conditions": "Cloudy"},
        "new york": {"temperature": "22C", "conditions": "Sunny"},
        "paris": {"temperature": "18C", "conditions": "Partly Cloudy"}
    }
    weather = mock_weather_data.get(city.lower())
    if weather:
        return f"The current weather in {city} is {weather['temperature']} and {weather['conditions']}."
    else:
        return f"Weather data not available for {city}."

# Create the LangChain Tool for weather
weather_tool = Tool(
    name="get_weather",
    func=get_weather_for_city,
    description="Useful for getting the current weather conditions (temperature, conditions) for a specific city. Input should be a city name (e.g., 'London')."
)

# List of tools available to the agent
tools = [weather_tool]

# Initialize the LLM (ensure OPENAI_API_KEY is set)
llm = OpenAI(temperature=0)

# Pull the ReAct prompt template from LangChain Hub
prompt = hub.pull("hwchase17/react")

# Create the agent
agent = create_react_agent(llm, tools, prompt)

# Create the AgentExecutor to run the agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Invoke the agent with a query
response = agent_executor.invoke({"input": "What's the weather like in Paris?"})
print(response["output"])

प्रभावी टूल विवरण

LLM यह तय करने के लिए आपके टूल के description पर बहुत अधिक निर्भर करता है कि उसका उपयोग करना है या नहीं और कैसे करना है। अपने विवरण को ऐसा बनाएँ:

  • स्पष्ट और संक्षिप्त: ठीक-ठीक बताएँ कि टूल क्या करता है।
  • इनपुट के बारे में विशिष्ट: स्पष्ट रूप से समझाएँ कि वह किस प्रकार का आर्ग्युमेंट अपेक्षित करता है।
  • उद्देश्य-केंद्रित: समझाएँ कि टूल क्यों उपयोगी है या किन परिस्थितियों में इसका उपयोग किया जाता है।
  • उदाहरण दें: यदि इनपुट जटिल है, तो उसके प्रारूप का एक उदाहरण दें।

अच्छी तरह तैयार किया गया विवरण एजेंट की सफलता के लिए अत्यंत महत्वपूर्ण है।

टूल एकीकरण जाँच

आप ऐसा एजेंट बना रहे हैं जो उपयोगकर्ताओं को वास्तविक समय की जानकारी खोजने में सहायता करेगा। आपको एक ऐसा टूल बनाना है जो किसी कंपनी के दिए गए टिकर (जैसे, "AAPL") के लिए बाहरी API को कॉल करके वर्तमान शेयर मूल्य प्राप्त करे।

निम्नलिखित में से कौन-सा विकल्प LLM द्वारा Tool की परिभाषा को सही ढंग से समझने और उपयोग करने के लिए उसके सबसे महत्वपूर्ण पहलू का वर्णन करता है?

पुनरावलोकन: एजेंट टूल के रूप में API

आपने सीखा कि बाहरी API को टूल के रूप में एकीकृत करके LangChain एजेंट को अधिक सक्षम कैसे बनाया जाता है। इससे आपके एजेंट ये कार्य कर सकते हैं:

  • इंटरनेट से वास्तविक समय का डेटा प्राप्त करना।
  • वास्तविक दुनिया में कार्य करना (जैसे ईमेल भेजना या डेटाबेस अपडेट करना)।
  • अपनी प्रारंभिक प्रशिक्षण सामग्री से कहीं अधिक क्षमताएँ प्राप्त करना।

API कॉल को अच्छी तरह वर्णित Tool ऑब्जेक्ट में लपेटकर आप अपने एजेंट की बुद्धिमत्ता और उपयोगिता को काफ़ी बढ़ा देते हैं!

शुरुआत निःशुल्क

एआई शिक्षक के साथ LangChain / RAG / वेक्टर डेटाबेस सीखें — निःशुल्क

अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।

पाठ्यक्रम
12
पाठ
48

अक्सर पूछे जाने वाले प्रश्न

क्या “बाहरी API को उपकरणों के रूप में एकीकृत करना” पाठ निःशुल्क है?

हाँ—“बाहरी API को उपकरणों के रूप में एकीकृत करना” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और LangChain / RAG / वेक्टर डेटाबेस पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। LangChain / RAG / वेक्टर डेटाबेस पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

“बाहरी API को उपकरणों के रूप में एकीकृत करना” में मैं क्या सीखूँगा?

अपने RAG सिस्टम को बाहरी API और सेवाओं से जोड़ें, ताकि एजेंट वास्तविक समय का डेटा प्राप्त कर सकें या कार्य कर सकें। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ LangChain / RAG / वेक्टर डेटाबेस का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।

क्या LangChain / RAG / वेक्टर डेटाबेस शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?

पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर LangChain / RAG / वेक्टर डेटाबेस शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 3वाँ पाठ है।

“बाहरी API को उपकरणों के रूप में एकीकृत करना” पाठ पूरा करने में कितना समय लगता है?

CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।

क्या मैं इस LangChain / RAG / वेक्टर डेटाबेस पाठ में कोड लिख और चला सकता हूँ?

हाँ। हर LangChain / RAG / वेक्टर डेटाबेस पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।

इस पाठ्यक्रम के सभी पाठ

  1. LangChain एजेंट और उपकरणों की अवधारणाएँ
  2. बहु-एजेंट RAG कार्यप्रवाह बनाना
  3. बाहरी API को उपकरणों के रूप में एकीकृत करना
  4. एजेंटिक RAG में मेमोरी और स्थिति
← LangChain / RAG / वेक्टर डेटाबेस पर वापस जाएँ