File size: 3,510 Bytes
23afe01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from serpapi.google_search import GoogleSearch
from dotenv import load_dotenv
import os
from openai import OpenAI
import asyncio
import requests

from agents import enable_verbose_stdout_logging
enable_verbose_stdout_logging()

from agents import (
    Agent,
    function_tool,
    Runner,
    WebSearchTool,
)

load_dotenv()
SERP_API_KEY = os.getenv("SERP_API_KEY")
openai_client = OpenAI()

def describe_all_images(images_results):
    descriptions = []
    for image in images_results:
        description = describe_thumbnail(image["thumbnail"])
        descriptions.append({
            "title": image["title"],
            "link": image["link"],
            "description": description
        })
        break
    return descriptions

def describe_thumbnail(image_url):
    response = openai_client.responses.create(
        model="gpt-4.1",
        input=[{
            "role": "user",
            "content": [
                {"type": "input_text", "text": "what's in this image? To what era/year does it belong?"},
                {
                    "type": "input_image",
                    "image_url": image_url,
                },
            ],
        }],
    )
    return response.output_text + "\n"

def search_google(item_to_find: str):
    params = {
    "engine": "google_images_light",
    "q": item_to_find,
    "api_key": SERP_API_KEY
    }

    search = GoogleSearch(params)
    raw_results = search.get_dict()["images_results"]
    images_results = [
        {
            "thumbnail": result["thumbnail"],
            "link": result["link"],
            "title": result["title"]
        }
        for result in raw_results
    ]
    return raw_results 

@function_tool  
def seach_google_for_images(item_to_find: str) -> list:
    """
    Search Google Images for a given item and return a list of image results, with links and descriptions.

    Args:
        item_to_find (str): The item or query to search for in Google Images.

    Returns:
        list: A list of dictionaries, each containing the image title, link, and a description generated by the image analysis.
    """
    raw_results = search_google(item_to_find)
    results_w_description = describe_all_images(raw_results)
    return results_w_description

@function_tool
def wikipedia_lookup(query):
    """
    Look up a query on Wikipedia and return the summary extract of the most relevant page.

    Args:
        query (str): The search term to look up on Wikipedia.

    Returns:
        str: The summary extract of the most relevant Wikipedia page, or a message if no page is found.
    """
    # Step 1: Search
    search_url = "https://en.wikipedia.org/w/rest.php/v1/search/title"
    params = {"q": query, "limit": 1}
    search_resp = requests.get(search_url, params=params)
    search_resp.raise_for_status()
    search_data = search_resp.json()
    
    if not search_data.get("pages"):
        return f"No Wikipedia page found for '{query}'"
    
    page_key = search_data["pages"][0]["key"]
    
    # Step 2: Fetch summary
    summary_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_key}"
    summary_resp = requests.get(summary_url)
    summary_resp.raise_for_status()
    return summary_resp.json().get("extract")



# if __name__ == "__main__":
#     item_to_find = "mediterranean houses in the 16h century"
#     results = search_google(item_to_find)
#     desc = describe_all_images(results)
#     print(desc[0])
#     # print(wikipedia_lookup("United States Constitution"))