mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-04 06:37:58 +00:00
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from langchain_core.callbacks import CallbackManagerForRetrieverRun
|
|
from langchain_core.documents import Document
|
|
from langchain_core.retrievers import BaseRetriever
|
|
from pydantic import model_validator
|
|
|
|
|
|
class MetalRetriever(BaseRetriever):
|
|
"""`Metal API` retriever."""
|
|
|
|
client: Any
|
|
"""The Metal client to use."""
|
|
params: Optional[dict] = None
|
|
"""The parameters to pass to the Metal client."""
|
|
|
|
@model_validator(mode="before")
|
|
@classmethod
|
|
def validate_client(cls, values: dict) -> Any:
|
|
"""Validate that the client is of the correct type."""
|
|
from metal_sdk.metal import Metal
|
|
|
|
if "client" in values:
|
|
client = values["client"]
|
|
if not isinstance(client, Metal):
|
|
raise ValueError(
|
|
"Got unexpected client, should be of type metal_sdk.metal.Metal. "
|
|
f"Instead, got {type(client)}"
|
|
)
|
|
|
|
values["params"] = values.get("params", {})
|
|
|
|
return values
|
|
|
|
def _get_relevant_documents(
|
|
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
|
|
) -> List[Document]:
|
|
results = self.client.search({"text": query}, **self.params)
|
|
final_results = []
|
|
for r in results["data"]:
|
|
metadata = {k: v for k, v in r.items() if k != "text"}
|
|
final_results.append(Document(page_content=r["text"], metadata=metadata))
|
|
return final_results
|