mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-29 07:19:59 +00:00
This PR upgrades langchain-community to pydantic 2.
* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2
Breaking Changes:
- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492
Other changes:
- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
import json
|
|
from typing import List
|
|
|
|
import requests
|
|
from langchain_core.documents import Document
|
|
from pydantic import BaseModel
|
|
from yarl import URL
|
|
|
|
|
|
class JinaSearchAPIWrapper(BaseModel):
|
|
"""Wrapper around the Jina search engine."""
|
|
|
|
base_url: str = "https://s.jina.ai/"
|
|
"""The base URL for the Jina search engine."""
|
|
|
|
def run(self, query: str) -> str:
|
|
"""Query the Jina search engine and return the results as a JSON string.
|
|
|
|
Args:
|
|
query: The query to search for.
|
|
|
|
Returns: The results as a JSON string.
|
|
|
|
"""
|
|
web_search_results = self._search_request(query=query)
|
|
final_results = [
|
|
{
|
|
"title": item.get("title"),
|
|
"link": item.get("url"),
|
|
"snippet": item.get("description"),
|
|
"content": item.get("content"),
|
|
}
|
|
for item in web_search_results
|
|
]
|
|
return json.dumps(final_results)
|
|
|
|
def download_documents(self, query: str) -> List[Document]:
|
|
"""Query the Jina search engine and return the results as a list of Documents.
|
|
|
|
Args:
|
|
query: The query to search for.
|
|
|
|
Returns: The results as a list of Documents.
|
|
|
|
"""
|
|
results = self._search_request(query)
|
|
return [
|
|
Document(
|
|
page_content=item.get("content"), # type: ignore[arg-type]
|
|
metadata={
|
|
"title": item.get("title"),
|
|
"link": item.get("url"),
|
|
"description": item.get("description"),
|
|
},
|
|
)
|
|
for item in results
|
|
]
|
|
|
|
def _search_request(self, query: str) -> List[dict]:
|
|
headers = {
|
|
"Accept": "application/json",
|
|
}
|
|
url = str(URL(self.base_url + query))
|
|
response = requests.get(url, headers=headers)
|
|
if not response.ok:
|
|
raise Exception(f"HTTP error {response.status_code}")
|
|
|
|
return response.json().get("data", [])
|