mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
115 lines
4.2 KiB
Python
115 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Dict, List, Optional
|
|
|
|
import aiohttp
|
|
import requests
|
|
from langchain_core.callbacks import (
|
|
AsyncCallbackManagerForRetrieverRun,
|
|
CallbackManagerForRetrieverRun,
|
|
)
|
|
from langchain_core.documents import Document
|
|
from langchain_core.pydantic_v1 import Extra, root_validator
|
|
from langchain_core.retrievers import BaseRetriever
|
|
from langchain_core.utils import get_from_dict_or_env, get_from_env
|
|
|
|
DEFAULT_URL_SUFFIX = "search.windows.net"
|
|
"""Default URL Suffix for endpoint connection - commercial cloud"""
|
|
|
|
|
|
class AzureCognitiveSearchRetriever(BaseRetriever):
|
|
"""`Azure Cognitive Search` service retriever."""
|
|
|
|
service_name: str = ""
|
|
"""Name of Azure Cognitive Search service"""
|
|
index_name: str = ""
|
|
"""Name of Index inside Azure Cognitive Search service"""
|
|
api_key: str = ""
|
|
"""API Key. Both Admin and Query keys work, but for reading data it's
|
|
recommended to use a Query key."""
|
|
api_version: str = "2020-06-30"
|
|
"""API version"""
|
|
aiosession: Optional[aiohttp.ClientSession] = None
|
|
"""ClientSession, in case we want to reuse connection for better performance."""
|
|
content_key: str = "content"
|
|
"""Key in a retrieved result to set as the Document page_content."""
|
|
top_k: Optional[int] = None
|
|
"""Number of results to retrieve. Set to None to retrieve all results."""
|
|
|
|
class Config:
|
|
extra = Extra.forbid
|
|
arbitrary_types_allowed = True
|
|
|
|
@root_validator(pre=True)
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that service name, index name and api key exists in environment."""
|
|
values["service_name"] = get_from_dict_or_env(
|
|
values, "service_name", "AZURE_COGNITIVE_SEARCH_SERVICE_NAME"
|
|
)
|
|
values["index_name"] = get_from_dict_or_env(
|
|
values, "index_name", "AZURE_COGNITIVE_SEARCH_INDEX_NAME"
|
|
)
|
|
values["api_key"] = get_from_dict_or_env(
|
|
values, "api_key", "AZURE_COGNITIVE_SEARCH_API_KEY"
|
|
)
|
|
return values
|
|
|
|
def _build_search_url(self, query: str) -> str:
|
|
url_suffix = get_from_env(
|
|
"", "AZURE_COGNITIVE_SEARCH_URL_SUFFIX", DEFAULT_URL_SUFFIX
|
|
)
|
|
base_url = f"https://{self.service_name}.{url_suffix}/"
|
|
endpoint_path = f"indexes/{self.index_name}/docs?api-version={self.api_version}"
|
|
top_param = f"&$top={self.top_k}" if self.top_k else ""
|
|
return base_url + endpoint_path + f"&search={query}" + top_param
|
|
|
|
@property
|
|
def _headers(self) -> Dict[str, str]:
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"api-key": self.api_key,
|
|
}
|
|
|
|
def _search(self, query: str) -> List[dict]:
|
|
search_url = self._build_search_url(query)
|
|
response = requests.get(search_url, headers=self._headers)
|
|
if response.status_code != 200:
|
|
raise Exception(f"Error in search request: {response}")
|
|
|
|
return json.loads(response.text)["value"]
|
|
|
|
async def _asearch(self, query: str) -> List[dict]:
|
|
search_url = self._build_search_url(query)
|
|
if not self.aiosession:
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(search_url, headers=self._headers) as response:
|
|
response_json = await response.json()
|
|
else:
|
|
async with self.aiosession.get(
|
|
search_url, headers=self._headers
|
|
) as response:
|
|
response_json = await response.json()
|
|
|
|
return response_json["value"]
|
|
|
|
def _get_relevant_documents(
|
|
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
|
|
) -> List[Document]:
|
|
search_results = self._search(query)
|
|
|
|
return [
|
|
Document(page_content=result.pop(self.content_key), metadata=result)
|
|
for result in search_results
|
|
]
|
|
|
|
async def _aget_relevant_documents(
|
|
self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun
|
|
) -> List[Document]:
|
|
search_results = await self._asearch(query)
|
|
|
|
return [
|
|
Document(page_content=result.pop(self.content_key), metadata=result)
|
|
for result in search_results
|
|
]
|