HomeArtificial IntelligenceA Complete Coding Tutorial for Superior SerpAPI Integration with Google Gemini-1.5-Flash for...

A Complete Coding Tutorial for Superior SerpAPI Integration with Google Gemini-1.5-Flash for Superior Analytics


On this tutorial, we reveal methods to mix the ability of SerpAPI’s Google search capabilities with Google’s Gemini-1.5-Flash mannequin to create a sophisticated, end-to-end analysis and evaluation workflow inside a Google Colab pocket book. By defining an AdvancedSerpAPI Python class, customers achieve entry to enhanced search strategies that cowl normal internet outcomes, information articles, and pictures, whereas additionally leveraging Gemini to carry out in-depth analyses of these outcomes. The code offers specialised utilities for focusing on Marktechpost tutorials, aggregating content material throughout classes like LangChain, ChatGPT, and MLOps, after which synthesizing actionable insights utilizing a rigorously constructed immediate.

!pip set up google-search-results langchain-community langchain-core google-generativeai -q


import os
import json
from serpapi import GoogleSearch
import google.generativeai as genai
from datetime import datetime

We set up the required Python packages for SerpAPI searches, LangChain utilities, and Google’s Gemini SDK. The following imports usher in normal modules (os, json, datetime) for atmosphere configuration, JSON dealing with, and timestamps, in addition to SerpAPI’s GoogleSearch class for making API calls and genai for interacting with the Gemini mannequin.

SERPAPI_API_KEY = "Use Your API Key Right here"  
GEMINI_API_KEY = "Use Your API Key Right here"  


os.environ["SERPAPI_API_KEY"] = SERPAPI_API_KEY
genai.configure(api_key=GEMINI_API_KEY)

We assign placeholder strings to your SerpAPI and Gemini API keys, then set the SerpAPI key as an atmosphere variable (so SerpAPI calls authenticate robotically) and configure the Gemini shopper with its API key so you may invoke the Gemini mannequin.

class AdvancedSerpAPI:
    def __init__(self, serpapi_key, gemini_key):
        self.serpapi_key = serpapi_key
        self.gemini_model = genai.GenerativeModel('gemini-1.5-flash')
       
    def search_google(self, question, num_results=5, location="United States"):
        """Enhanced Google search with a number of parameters"""
        params = {
            "engine": "google",
            "q": question,
            "api_key": self.serpapi_key,
            "num": num_results,
            "location": location,
            "hl": "en",
            "gl": "us"
        }
       
        search = GoogleSearch(params)
        outcomes = search.get_dict()
        return self.extract_search_results(outcomes)
   
    def search_news(self, question, days_back=7):
        """Seek for current information articles"""
        params = {
            "engine": "google_news",
            "q": question,
            "api_key": self.serpapi_key,
            "gl": "us",
            "hl": "en"
        }
       
        search = GoogleSearch(params)
        outcomes = search.get_dict()
        return self.extract_news_results(outcomes)
   
    def search_images(self, question, num_images=10):
        """Seek for pictures with metadata"""
        params = {
            "engine": "google_images",
            "q": question,
            "api_key": self.serpapi_key,
            "num": num_images
        }
       
        search = GoogleSearch(params)
        outcomes = search.get_dict()
        return self.extract_image_results(outcomes)
   
    def extract_search_results(self, outcomes):
        """Extract and clear search outcomes"""
        cleaned_results = []
        if 'organic_results' in outcomes:
            for lead to outcomes['organic_results']:
                cleaned_results.append({
                    'title': outcome.get('title', ''),
                    'hyperlink': outcome.get('hyperlink', ''),
                    'snippet': outcome.get('snippet', ''),
                    'place': outcome.get('place', 0)
                })
        return cleaned_results
   
    def extract_news_results(self, outcomes):
        """Extract information articles with timestamps"""
        news_results = []
        if 'news_results' in outcomes:
            for article in outcomes['news_results']:
                news_results.append({
                    'title': article.get('title', ''),
                    'hyperlink': article.get('hyperlink', ''),
                    'snippet': article.get('snippet', ''),
                    'date': article.get('date', ''),
                    'supply': article.get('supply', '')
                })
        return news_results
   
    def extract_image_results(self, outcomes):
        """Extract picture outcomes with metadata"""
        image_results = []
        if 'images_results' in outcomes:
            for img in outcomes['images_results']:
                image_results.append({
                    'title': img.get('title', ''),
                    'unique': img.get('unique', ''),
                    'thumbnail': img.get('thumbnail', ''),
                    'supply': img.get('supply', '')
                })
        return image_results
   
    def analyze_with_gemini(self, search_results, analysis_prompt):
        """Use Gemini Flash to investigate search outcomes"""
        results_text = json.dumps(search_results, indent=2)
       
        full_prompt = f"""
        {analysis_prompt}
       
        Search Outcomes Information:
        {results_text}
       
        Please present a complete evaluation primarily based on the search outcomes.
        """
       
        strive:
            response = self.gemini_model.generate_content(full_prompt)
            return response.textual content
        besides Exception as e:
            return f"Gemini evaluation failed: {str(e)}"
   
    def search_marktechpost_tutorials(self, subject="", num_results=10):
        """Search particularly for trending tutorials from Marktechpost"""
        queries = [
            f"site:marktechpost.com {topic} tutorial guide how-to 2024 2025",
            f"site:marktechpost.com trending {topic} tutorial",
            f"site:marktechpost.com top {topic} books frameworks"
        ]
       
        all_results = []
        for question in queries:
            params = {
                "engine": "google",
                "q": question,
                "api_key": self.serpapi_key,
                "num": num_results // len(queries),
                "hl": "en",
                "gl": "us"
            }
           
            search = GoogleSearch(params)
            outcomes = search.get_dict()
            extracted = self.extract_search_results(outcomes)
            all_results.prolong(extracted)
       
        unique_results = []
        seen_links = set()
        for lead to all_results:
            if outcome['link'] not in seen_links:
                unique_results.append(outcome)
                seen_links.add(outcome['link'])
       
        return unique_results[:num_results]
   
    def get_trending_marktechpost_content(self, classes=None):
        """Get trending content material from Marktechpost throughout completely different classes"""
        if classes is None:
            classes = ["AI", "LLM", "Machine Learning", "Python", "Tutorial", "Framework"]
       
        trending_content = {}
       
        for class in classes:
            print(f"🔍 Trying to find trending {class} content material...")
            outcomes = self.search_marktechpost_tutorials(class, num_results=5)
            trending_contentAgentic AI = outcomes
            print(f"✅ Discovered {len(outcomes)} {class} tutorials")
       
        return trending_content


    def smart_research(self, subject, research_depth="medium", focus_marktechpost=True):
        """Clever analysis combining a number of search varieties with Marktechpost focus"""
        print(f"🔍 Beginning sensible analysis on: {subject}")
       
        if focus_marktechpost:
            marktechpost_results = self.search_marktechpost_tutorials(subject, num_results=8)
            print(f"✅ Discovered {len(marktechpost_results)} Marktechpost tutorials")
           
            web_results = self.search_google(f"{subject} tutorial information", num_results=3)
            print(f"✅ Discovered {len(web_results)} further internet outcomes")
           
            all_web_results = marktechpost_results + web_results
        else:
            all_web_results = self.search_google(f"{subject} overview details", num_results=5)
            print(f"✅ Discovered {len(all_web_results)} internet outcomes")
       
        news_results = self.search_news(subject)
        print(f"✅ Discovered {len(news_results)} information articles")
       
        analysis_prompt = f"""
        Analyze the search outcomes about '{subject}' with give attention to Marktechpost content material and supply:
        1. Key tutorials and guides obtainable
        2. Trending matters and frameworks
        3. Studying sources and books talked about
        4. Current developments and updates
        5. Sensible implementation guides
        6. Advisable studying path
       
        Deal with actionable insights and studying sources.
        """
       
        all_results = {
            "marktechpost_results": marktechpost_results if focus_marktechpost else [],
            "web_results": all_web_results,
            "news_results": news_results,
            "search_topic": subject,
            "timestamp": datetime.now().isoformat()
        }
       
        gemini_analysis = self.analyze_with_gemini(all_results, analysis_prompt)
       
        return {
            "subject": subject,
            "marktechpost_tutorials": marktechpost_results if focus_marktechpost else [],
            "web_results": all_web_results,
            "news_results": news_results,
            "ai_analysis": gemini_analysis,
            "total_sources": len(all_web_results) + len(news_results)
        }

This class, AdvancedSerpAPI, encapsulates SerpAPI-based search strategies (internet, information, and pictures) and helper features to scrub the ensuing JSON knowledge. It additionally integrates a Gemini-1.5-Flash mannequin, through analyze_with_gemini, to generate an AI-driven abstract of any collected search knowledge. Further utilities embrace specialised Marktechpost tutorial lookups, a “get trending” routine throughout classes, and a mixed “sensible analysis” workflow that stitches collectively tutorials, internet outcomes, information, and Gemini evaluation.

def demo_marktechpost_tutorials():
    """Demo particularly centered on Marktechpost tutorials"""
   
    searcher = AdvancedSerpAPI(SERPAPI_API_KEY, GEMINI_API_KEY)
   
    print("🚀 Marktechpost Trending Tutorials Finder")
    print("=" * 50)
   
    print("n📚 Demo 1: Trending Marktechpost Tutorials by Class")
    trending_content = searcher.get_trending_marktechpost_content([
        "LangChain", "ChatGPT", "Python", "AI", "MLOps"
    ])
   
    for class, tutorials in trending_content.objects():
        print(f"n🔥 Trending {class} Tutorials:")
        for i, tutorial in enumerate(tutorials[:3], 1):
            print(f"  {i}. {tutorial['title']}")
            print(f"     📎 {tutorial['link']}")
            if tutorial['snippet']:
                print(f"     📝 {tutorial['snippet'][:100]}...")
   
    print("n🎯 Demo 2: Deep Dive - LangChain Tutorials")
    langchain_research = searcher.smart_research("LangChain", focus_marktechpost=True)
   
    print(f"n📊 Analysis Abstract:")
    print(f"Matter: {langchain_research['topic']}")
    print(f"Marktechpost Tutorials Discovered: {len(langchain_research['marktechpost_tutorials'])}")
    print(f"Complete Sources: {langchain_research['total_sources']}")
   
    print(f"n🤖 AI Evaluation Preview:")
    print(langchain_research['ai_analysis'][:600] + "..." if len(langchain_research['ai_analysis']) > 600 else langchain_research['ai_analysis'])
   
    print("n📰 Demo 3: Newest AI Developments from Marktechpost")
    ai_trends = searcher.search_marktechpost_tutorials("AI traits 2024 2025", num_results=5)
   
    print("Current AI pattern articles:")
    for i, article in enumerate(ai_trends, 1):
        print(f"{i}. {article['title']}")
        print(f"   🔗 {article['link']}")


def demo_advanced_serpapi():
    """Complete demo of SerpAPI capabilities"""
   
    searcher = AdvancedSerpAPI(SERPAPI_API_KEY, GEMINI_API_KEY)
   
    print("🚀 Superior SerpAPI Tutorial with Gemini Flash")
    print("=" * 50)
   
    print("n📊 Demo 1: Good Analysis on AI Know-how")
    research_results = searcher.smart_research("synthetic intelligence 2024 traits")
   
    print(f"n🔍 Analysis Abstract:")
    print(f"Matter: {research_results['topic']}")
    print(f"Complete Sources: {research_results['total_sources']}")
   
    print(f"n🤖 AI Evaluation Preview:")
    print(research_results['ai_analysis'][:500] + "..." if len(research_results['ai_analysis']) > 500 else research_results['ai_analysis'])
   
    print("n📰 Demo 2: Current Information Search")
    tech_news = searcher.search_news("know-how breakthrough", days_back=7)
   
    print(f"Discovered {len(tech_news)} current tech information articles:")
    for i, article in enumerate(tech_news[:3], 1):
        print(f"{i}. {article['title'][:80]}...")
        print(f"   Supply: {article['source']} | Date: {article['date']}")
   
    print("n🖼️  Demo 3: Picture Search")
    space_images = searcher.search_images("area exploration 2024", num_images=5)
   
    print(f"Discovered {len(space_images)} space-related pictures:")
    for i, img in enumerate(space_images[:3], 1):
        print(f"{i}. {img['title'][:60]}...")
        print(f"   Supply: {img['source']}")

demo_marktechpost_tutorials() initializes the AdvancedSerpAPI class and prints trending tutorials from Marktechpost for an inventory of classes (LangChain, ChatGPT, Python, AI, MLOps). It then performs a “deep dive” sensible analysis on “LangChain,” exhibiting counts of tutorials and a preview of Gemini’s AI evaluation. Lastly, it retrieves and lists the highest 5 current “AI traits 2024–2025” articles from Marktechpost. 

Additionally, demo_advanced_serpapi() creates an AdvancedSerpAPI occasion however focuses on a broader workflow: it runs sensible analysis on “synthetic intelligence 2024 traits” and prints the subject abstract and AI evaluation snippet. It then performs a information seek for “know-how breakthrough,” lists the primary three articles with sources and dates, and concludes by fetching and displaying a handful of “area exploration 2024” picture outcomes.

if __name__ == "__main__":
    if SERPAPI_API_KEY == "your_serpapi_key_here" or GEMINI_API_KEY == "your_gemini_key_here":
        print("⚠️  Please set your API keys earlier than working the demo!")
        print("1. Get SerpAPI key from: https://serpapi.com")
        print("2. Get Gemini API key from: https://makersuite.google.com")
    else:
        print("🎯 Working Marktechpost-focused demo...")
        demo_marktechpost_tutorials()
       
        print("n" + "="*50)
        print("🌟 Working normal demo...")
        demo_advanced_serpapi()


def compare_search_engines(question, engines=['google', 'bing', 'duckduckgo']):
    """Examine outcomes throughout completely different engines like google"""
    outcomes = {}
   
    for engine in engines:
        params = {
            "engine": engine,
            "q": question,
            "api_key": SERPAPI_API_KEY
        }
       
        strive:
            search = GoogleSearch(params)
            outcomes[engine] = search.get_dict()
        besides Exception as e:
            outcomes[engine] = {"error": str(e)}
   
    return outcomes


def trending_searches(location="United States"):
    """Get trending searches"""
    params = {
        "engine": "google_trends_trending_now",
        "api_key": SERPAPI_API_KEY,
        "geo": location
    }
   
    search = GoogleSearch(params)
    return search.get_dict()


print("✅ Superior SerpAPI Tutorial with Marktechpost Focus loaded efficiently!")
print("🔑 Bear in mind to set your API keys earlier than working demos")
print("📚 New Capabilities: search_marktechpost_tutorials, get_trending_marktechpost_content")
print("🎯 Marktechpost-specific options: LangChain, ChatGPT, Python, AI, MLOps tutorials")


print("n🚀 Fast Begin Examples:")
print("searcher = AdvancedSerpAPI(SERPAPI_API_KEY, GEMINI_API_KEY)")
print("langchain_tutorials = searcher.search_marktechpost_tutorials('LangChain')")
print("trending_ai = searcher.get_trending_marktechpost_content(['AI', 'Python'])")
print("analysis = searcher.smart_research('ChatGPT', focus_marktechpost=True)")

Lastly, the part features a Python “primary” guard that first verifies your SerpAPI and Gemini keys, prompting you to acquire them in the event that they’re nonetheless placeholders, and in any other case runs the Marktechpost‐centered and normal demos in sequence. It additionally defines two utility features: compare_search_engines, which queries a number of engines like google (Google, Bing, DuckDuckGo) through SerpAPI and returns their uncooked JSON outcomes or errors, and trending_searches, which fetches right now’s trending matters utilizing the Google Developments endpoint. After these definitions, the script prints a short standing message confirming that the tutorial loaded efficiently, reminds you to set your API keys, and highlights newly added strategies for fetching Marktechpost tutorials and trending content material.

In conclusion, by following this tutorial, customers could have a reusable, modular Python class that streamlines internet analysis and evaluation, from performing keyword-driven searches to robotically summarizing findings utilizing Gemini-powered AI. The mix of SerpAPI’s dependable search endpoints and Gemini’s pure language understanding permits a seamless “research-to-insights” workflow, perfect for content material creators, builders, and technical groups who want to remain up-to-date with the most recent tutorials and business traits.


Try the Pocket book right here. All credit score for this analysis goes to the researchers of this undertaking. Additionally, be happy to comply with us on Twitter and don’t overlook to hitch our 95k+ ML SubReddit and Subscribe to our E-newsletter.


Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its recognition amongst audiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments