AI Agentను External APIsతో ఎలా Connect చేయాలి? Pythonతో పూర్తి గైడ్ Connect AI Agent to External APIs

AI Agent అనేది యూజర్ ఇచ్చిన లక్ష్యాన్ని అర్థం చేసుకొని, అవసరమైన Tools ఉపయోగించి పనులు చేయగల AI వ్యవస్థ.

ఉదాహరణకు, మీరు ఒక AI Agentను ఇలా అడిగారని అనుకుందాం:

“Hyderabadలో ప్రస్తుతం వాతావరణం ఎలా ఉంది?”

AI Agentకు Weather సమాచారం నేరుగా తెలియకపోవచ్చు. కాబట్టి అది External Weather APIని ఉపయోగించి తాజా సమాచారాన్ని తీసుకొని మీకు సమాధానం ఇవ్వగలదు.

2. External API అంటే ఏమిటి?

API అంటే Application Programming Interface. ఇది రెండు Software Applications మధ్య సమాచారాన్ని మార్పిడి చేసుకోవడానికి ఉపయోగపడుతుంది.

ఉదాహరణకు:

  • Weather API – వాతావరణ సమాచారం.
  • Payment API – Online Payments.
  • Google Maps API – Location సమాచారం.
  • Currency API – Currency Exchange Rates.

AI Agent ఈ APIsతో కనెక్ట్ అయినప్పుడు సాధారణ సమాధానాలు ఇవ్వడమే కాకుండా బయటి Services నుంచి సమాచారాన్ని పొందగలదు.

3. AI Agent External APIతో ఎలా పనిచేస్తుంది?

దీని పని విధానం ఐదు దశల్లో జరుగుతుంది.

  1. User ఒక ప్రశ్న అడుగుతాడు.
  2. AI Model ప్రశ్నను అర్థం చేసుకుంటుంది.
  3. అవసరమైతే API Call చేయాలని నిర్ణయిస్తుంది.
  4. Python Code External API నుంచి సమాచారాన్ని తీసుకుంటుంది.
  5. AI ఆ సమాచారాన్ని ఉపయోగించి తుది సమాధానం ఇస్తుంది.

ఈ విధానాన్ని Function Calling లేదా Tool Calling అంటారు.

4. అవసరమైన Tools

Toolఉపయోగం
PythonBackend Programming
Google Gemini APIAI Model
Google AI StudioAPI Key
RequestsExternal API Calls
Open-MeteoWeather Information

5. Step-by-Step: AI Agent తయారు చేయడం

Step 1: Gemini API Key పొందండి

  1. Google AI Studio ఓపెన్ చేయండి.
  2. Google Accountతో Sign in అవ్వండి.
  3. API Keys విభాగంలోకి వెళ్లండి.
  4. Create API Key ఎంచుకోండి.
  5. Keyను సురక్షితంగా భద్రపరచండి.

Step 2: Python Packages Install చేయండి

Terminalలో ఈ Command అమలు చేయండి.

pip install -U google-genai requests

Step 3: API Key Configure చేయండి

Windows PowerShellలో:

$env:GEMINI_API_KEY="YOUR_API_KEY"

Step 4: External APIతో కనెక్ట్ చేయండి

ఈ ఉదాహరణలో Open-Meteo Weather API ఉపయోగిస్తున్నాం. ఇది అవసరమైన Weather సమాచారం అందిస్తుంది.

agent.py అనే ఫైల్ తయారు చేసి ఈ Code రాయండి.

import os
import json
import requests
from google import genai

client = genai.Client(
    api_key=os.environ["GEMINI_API_KEY"]
)

# External Weather API function
def get_weather(location):
    geo = requests.get(
        "https://geocoding-api.open-meteo.com/v1/search",
        params={"name": location, "count": 1},
        timeout=15
    )
    geo.raise_for_status()

    places = geo.json().get("results", [])
    if not places:
        return {"error": "Location not found"}

    place = places[0]

    weather = requests.get(
        "https://api.open-meteo.com/v1/forecast",
        params={
            "latitude": place["latitude"],
            "longitude": place["longitude"],
            "current": "temperature_2m,wind_speed_10m"
        },
        timeout=15
    )
    weather.raise_for_status()

    return {
        "location": place["name"],
        "weather": weather.json().get("current", {})
    }

# Tell Gemini about the available function
weather_tool = {
    "type": "function",
    "name": "get_weather",
    "description": "Get current weather for a city",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {"type": "string"}
        },
        "required": ["location"]
    }
}

available_functions = {
    "get_weather": get_weather
}

question = input("Ask about weather: ")
previous_id = None

while True:
    result = client.interactions.create(
        model="gemini-3.8-flash",
        input=question,
        tools=[weather_tool],
        previous_interaction_id=previous_id
    )

    function_results = []

    for step in result.steps:
        if step.type == "function_call":
            function = available_functions.get(step.name)

            if function is None:
                continue

            data = function(**step.arguments)

            function_results.append({
                "type": "function_result",
                "name": step.name,
                "call_id": step.id,
                "result": [{
                    "type": "text",
                    "text": json.dumps(data)
                }]
            })

    if not function_results:
        break

    question = function_results
    previous_id = result.id

print(result.output_text)

Step 5: AI Agentను Run చేయండి

Terminalలో:

python agent.py

ఇప్పుడు ఈ ప్రశ్న అడగండి:

What is the current weather in Hyderabad?

AI Agent Weather API నుంచి సమాచారాన్ని తీసుకొని సమాధానం తయారు చేయడానికి ప్రయత్నిస్తుంది.

6. AI Agent External APIs ఉపయోగాలు

  • Customer Support కోసం Order Details పొందడం.
  • Weather Information అందించడం.
  • Payment Status పరిశీలించడం.
  • Currency Exchange Rates పొందడం.
  • Google Maps ద్వారా Location Information పొందడం.
  • Business Applicationsలో Data Retrieval చేయడం.

7. సాధారణ తప్పులు మరియు పరిమితులు

  • తప్పు API URL ఉపయోగించడం.
  • API Keyను Publicగా ఉంచడం.
  • API Rate Limitsను పట్టించుకోకపోవడం.
  • Network Errorsను Handle చేయకపోవడం.
  • AI సూచించిన ప్రతి Functionను ధృవీకరణ లేకుండా Execute చేయడం.
  • API నుంచి వచ్చిన సమాచారాన్ని సరైన విధంగా Validate చేయకపోవడం.

ముఖ్యంగా Payments, Account Changes లేదా Data Deletion వంటి ముఖ్యమైన పనులకు User Confirmation తప్పనిసరిగా ఏర్పాటు చేయాలి. అలాగే API Permissionsను పరిమితం చేసి, Request Timeouts మరియు Error Handling ఉపయోగించాలి.

ముగింపు

AI Agentను External APIsతో Connect చేయడం ద్వారా AI Applications మరింత ఉపయోగకరంగా మారతాయి. Python, Gemini API మరియు External Services ఉపయోగించి Weather Assistant, Customer Support Agent లేదా Business Automation Agent వంటి Projects నిర్మించవచ్చు.

ముందుగా ఒక చిన్న Weather Agentతో ప్రారంభించండి. ఆ తర్వాత అవసరాన్ని బట్టి ఇతర APIsను అనుసంధానం చేయండి.

గుర్తుంచుకోండి: AI Agent నిర్ణయం తీసుకోవచ్చు, కానీ External APIతో నిజమైన పని చేయడానికి మీ Python Code మరియు భద్రతా నియమాలు అవసరం.

Official Documentation

Leave a Comment