mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-19 05:13:46 +00:00
(Community): Added API Key for Jina Search API Wrapper (#29622)
- **Description:** Simple change for adding the API Key for Jina Search API Wrapper - **Issue:** #29596
This commit is contained in:
parent
f1c66a3040
commit
96ad09fa2d
@ -64,7 +64,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os"
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.environ.get(\"JINA_API_KEY\"):\n",
|
||||
" os.environ[\"JINA_API_KEY\"] = getpass.getpass(\"Jina API key:\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -30,7 +30,7 @@ class JinaSearch(BaseTool): # type: ignore[override]
|
||||
"each in clean, LLM-friendly text. This way, you can always keep your LLM "
|
||||
"up-to-date, improve its factuality, and reduce hallucinations."
|
||||
)
|
||||
search_wrapper: JinaSearchAPIWrapper = Field(default_factory=JinaSearchAPIWrapper)
|
||||
search_wrapper: JinaSearchAPIWrapper = Field(default_factory=JinaSearchAPIWrapper) # type: ignore[arg-type]
|
||||
|
||||
def _run(
|
||||
self,
|
||||
|
@ -1,18 +1,34 @@
|
||||
import json
|
||||
from typing import List
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import requests
|
||||
from langchain_core.documents import Document
|
||||
from pydantic import BaseModel
|
||||
from langchain_core.utils import get_from_dict_or_env
|
||||
from pydantic import BaseModel, ConfigDict, SecretStr, model_validator
|
||||
from yarl import URL
|
||||
|
||||
|
||||
class JinaSearchAPIWrapper(BaseModel):
|
||||
"""Wrapper around the Jina search engine."""
|
||||
|
||||
api_key: SecretStr
|
||||
|
||||
base_url: str = "https://s.jina.ai/"
|
||||
"""The base URL for the Jina search engine."""
|
||||
|
||||
model_config = ConfigDict(
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that api key and endpoint exists in environment."""
|
||||
api_key = get_from_dict_or_env(values, "api_key", "JINA_API_KEY")
|
||||
values["api_key"] = api_key
|
||||
|
||||
return values
|
||||
|
||||
def run(self, query: str) -> str:
|
||||
"""Query the Jina search engine and return the results as a JSON string.
|
||||
|
||||
@ -59,6 +75,7 @@ class JinaSearchAPIWrapper(BaseModel):
|
||||
def _search_request(self, query: str) -> List[dict]:
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Bearer {self.api_key.get_secret_value()}",
|
||||
}
|
||||
url = str(URL(self.base_url + query))
|
||||
response = requests.get(url, headers=headers)
|
||||
|
@ -0,0 +1,27 @@
|
||||
import os
|
||||
import unittest
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from langchain_community.tools.jina_search.tool import JinaSearch
|
||||
from langchain_community.utilities.jina_search import JinaSearchAPIWrapper
|
||||
|
||||
os.environ["JINA_API_KEY"] = "test_key"
|
||||
|
||||
|
||||
class TestJinaSearchTool(unittest.TestCase):
|
||||
@patch(
|
||||
"langchain_community.tools.jina_search.tool.JinaSearch.invoke",
|
||||
return_value="mocked_result",
|
||||
)
|
||||
def test_invoke(self, mock_run: Any) -> None:
|
||||
query = "Test query text"
|
||||
wrapper = JinaSearchAPIWrapper(api_key="test_key") # type: ignore[arg-type]
|
||||
jina_search_tool = JinaSearch(api_wrapper=wrapper) # type: ignore[call-arg]
|
||||
results = jina_search_tool.invoke(query)
|
||||
expected_result = "mocked_result"
|
||||
assert results == expected_result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
Reference in New Issue
Block a user