mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-22 06:39:52 +00:00
langchain_community: add image support to DuckDuckGoSearchAPIWrapper (#29816)
- [ ] **PR title**: langchain_community: add image support to DuckDuckGoSearchAPIWrapper - **Description:** This PR enhances the DuckDuckGoSearchAPIWrapper within the langchain_community package by introducing support for image searches. The enhancement includes: - Adding a new method _ddgs_images to handle image search queries. - Updating the run and results methods to process and return image search results appropriately. - Modifying the source parameter to accept "images" as a valid option, alongside "text" and "news". - **Dependencies:** No additional dependencies are required for this change.
This commit is contained in:
parent
0d9f0b4215
commit
a98c5f1c4b
@ -34,7 +34,7 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
|
|||||||
"""
|
"""
|
||||||
source: str = "text"
|
source: str = "text"
|
||||||
"""
|
"""
|
||||||
Options: text, news
|
Options: text, news, images
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
@ -91,12 +91,31 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
|
|||||||
return [r for r in ddgs_gen]
|
return [r for r in ddgs_gen]
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def _ddgs_images(
|
||||||
|
self, query: str, max_results: Optional[int] = None
|
||||||
|
) -> List[Dict[str, str]]:
|
||||||
|
"""Run query through DuckDuckGo image search and return results."""
|
||||||
|
from duckduckgo_search import DDGS
|
||||||
|
|
||||||
|
with DDGS() as ddgs:
|
||||||
|
ddgs_gen = ddgs.images(
|
||||||
|
query,
|
||||||
|
region=self.region, # type: ignore[arg-type]
|
||||||
|
safesearch=self.safesearch,
|
||||||
|
max_results=max_results or self.max_results,
|
||||||
|
)
|
||||||
|
if ddgs_gen:
|
||||||
|
return [r for r in ddgs_gen]
|
||||||
|
return []
|
||||||
|
|
||||||
def run(self, query: str) -> str:
|
def run(self, query: str) -> str:
|
||||||
"""Run query through DuckDuckGo and return concatenated results."""
|
"""Run query through DuckDuckGo and return concatenated results."""
|
||||||
if self.source == "text":
|
if self.source == "text":
|
||||||
results = self._ddgs_text(query)
|
results = self._ddgs_text(query)
|
||||||
elif self.source == "news":
|
elif self.source == "news":
|
||||||
results = self._ddgs_news(query)
|
results = self._ddgs_news(query)
|
||||||
|
elif self.source == "images":
|
||||||
|
results = self._ddgs_images(query)
|
||||||
else:
|
else:
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
@ -137,6 +156,19 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
|
|||||||
}
|
}
|
||||||
for r in self._ddgs_news(query, max_results=max_results)
|
for r in self._ddgs_news(query, max_results=max_results)
|
||||||
]
|
]
|
||||||
|
elif source == "images":
|
||||||
|
results = [
|
||||||
|
{
|
||||||
|
"title": r["title"],
|
||||||
|
"thumbnail": r["thumbnail"],
|
||||||
|
"image": r["image"],
|
||||||
|
"url": r["url"],
|
||||||
|
"height": r["height"],
|
||||||
|
"width": r["width"],
|
||||||
|
"source": r["source"],
|
||||||
|
}
|
||||||
|
for r in self._ddgs_images(query, max_results=max_results)
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user