mirror of https://github.com/hwchase17/langchain
community[minor]: add mojeek search util (#20922)
**Description:** This pull request introduces a new feature to community tools, enhancing its search capabilities by integrating the Mojeek search engine **Dependencies:** None --------- Co-authored-by: Igor Brai <igor@mojeek.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com>pull/20988/head^2
parent
4822beb298
commit
b3e74f2b98
@ -0,0 +1,77 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Mojeek Search\n",
|
||||
"\n",
|
||||
"The following notebook will explain how to get results using Mojeek Search. Please visit [Mojeek Website](https://www.mojeek.com/services/search/web-search-api/) to obtain an API key."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.tools import MojeekSearch"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"api_key = \"KEY\" # obtained from Mojeek Website"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"search = MojeekSearch.config(api_key=api_key, search_kwargs={\"t\": 10})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"In `search_kwargs` you can add any search parameter that you can find on [Mojeek Documentation](https://www.mojeek.com/support/api/search/request_parameters.html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"search.run(\"mojeek\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForToolRun,
|
||||
CallbackManagerForToolRun,
|
||||
)
|
||||
from langchain_core.tools import BaseTool
|
||||
|
||||
from langchain_community.utilities.mojeek_search import MojeekSearchAPIWrapper
|
||||
|
||||
|
||||
class MojeekSearch(BaseTool):
|
||||
name: str = "mojeek_search"
|
||||
description: str = (
|
||||
"A wrapper around Mojeek Search. "
|
||||
"Useful for when you need to web search results. "
|
||||
"Input should be a search query."
|
||||
)
|
||||
api_wrapper: MojeekSearchAPIWrapper
|
||||
|
||||
@classmethod
|
||||
def config(
|
||||
cls, api_key: str, search_kwargs: Optional[dict] = None, **kwargs: Any
|
||||
) -> MojeekSearch:
|
||||
wrapper = MojeekSearchAPIWrapper(
|
||||
api_key=api_key, search_kwargs=search_kwargs or {}
|
||||
)
|
||||
return cls(api_wrapper=wrapper, **kwargs)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
return self.api_wrapper.run(query)
|
||||
|
||||
async def _arun(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
"""Use the tool asynchronously."""
|
||||
raise NotImplementedError("MojeekSearch does not support async")
|
@ -0,0 +1,44 @@
|
||||
import json
|
||||
from typing import List
|
||||
|
||||
import requests
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
|
||||
|
||||
class MojeekSearchAPIWrapper(BaseModel):
|
||||
api_key: str
|
||||
search_kwargs: dict = Field(default_factory=dict)
|
||||
api_url = "https://api.mojeek.com/search"
|
||||
|
||||
def run(self, query: str) -> str:
|
||||
search_results = self._search(query)
|
||||
|
||||
results = []
|
||||
|
||||
for result in search_results:
|
||||
title = result.get("title", "")
|
||||
url = result.get("url", "")
|
||||
desc = result.get("desc", "")
|
||||
results.append({"title": title, "url": url, "desc": desc})
|
||||
|
||||
return json.dumps(results)
|
||||
|
||||
def _search(self, query: str) -> List[dict]:
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
}
|
||||
|
||||
req = requests.PreparedRequest()
|
||||
request = {
|
||||
**self.search_kwargs,
|
||||
**{"q": query, "fmt": "json", "api_key": self.api_key},
|
||||
}
|
||||
req.prepare_url(self.api_url, request)
|
||||
if req.url is None:
|
||||
raise ValueError("prepared url is None, this should not happen")
|
||||
|
||||
response = requests.get(req.url, headers=headers)
|
||||
if not response.ok:
|
||||
raise Exception(f"HTTP error {response.status_code}")
|
||||
|
||||
return response.json().get("response", {}).get("results", [])
|
Loading…
Reference in New Issue