Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

add you retriever module

mrbeanf707aa67e8b7
diff --git a/.gitignore b/.gitignore
index 614d92022..e8edd7246 100644
--- a/.gitignore
+++ b/.gitignore
@@ -38,4 +38,5 @@ build/
checklist.md
finetuning_ckpts/
cache/
-* copy*
\ No newline at end of file
+* copy*
+.idea
diff --git a/dspy/retrieve/you_rm.py b/dspy/retrieve/you_rm.py
new file mode 100644
index 000000000..0deba4169
--- /dev/null
+++ b/dspy/retrieve/you_rm.py
@@ -0,0 +1,42 @@
+import dspy
+import os
+import requests
+
+from typing import Union, List
+
+
+class YouRM(dspy.Retrieve):
+ def __init__(self, ydc_api_key=None, k=3):
+ super().__init__(k=k)
+ if not ydc_api_key and not os.environ.get("YDC_API_KEY"):
+ raise RuntimeError("You must supply ydc_api_key or set environment variable YDC_API_KEY")
+ elif ydc_api_key:
+ self.ydc_api_key = ydc_api_key
+ else:
+ self.ydc_api_key = os.environ["YDC_API_KEY"]
+
+ def forward(self, query_or_queries: Union[str, List[str]]) -> dspy.Prediction:
+ """Search with You.com for self.k top passages for query or queries
+
+ Args:
+ query_or_queries (Union[str, List[str]]): The query or queries to search for.
+
+ Returns:
+ dspy.Prediction: An object containing the retrieved passages.
+ """
+ queries = (
+ [query_or_queries]
+ if isinstance(query_or_queries, str)
+ else query_or_queries
+ )
+ docs = []
+ for query in queries:
+ headers = {"X-API-Key": os.environ["YDC_API_KEY"]}
+ results = requests.get(
+ f"https://api.ydc-index.io/search?query={query}",
+ headers=headers,
+ ).json()
+ for hit in results["hits"][:self.k]:
+ for snippet in hit["snippets"]:
+ docs.append(snippet)
+ return dspy.Prediction(passages=docs)
diff --git a/requirements.txt b/requirements.txt
index 4d3b5f6eb..cd431edc3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,3 +8,4 @@ regex
ujson
tqdm
datasets
+requests