Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Fix style errors of dspy/utils (#8189)

TomuHirata3bcce2c37b61
diff --git a/dspy/utils/__init__.py b/dspy/utils/__init__.py
index fd8f00f70..e7a68677a 100644
--- a/dspy/utils/__init__.py
+++ b/dspy/utils/__init__.py
@@ -1,11 +1,12 @@
-from dspy.utils.callback import BaseCallback, with_callbacks
-from dspy.utils.dummies import DummyLM, DummyVectorizer, dummy_rm
-from dspy.streaming.messages import StatusMessageProvider, StatusMessage
-from dspy.utils import exceptions
-
import os
+
import requests
+from dspy.streaming.messages import StatusMessage, StatusMessageProvider
+from dspy.utils import exceptions
+from dspy.utils.callback import BaseCallback, with_callbacks
+from dspy.utils.dummies import DummyLM, DummyVectorizer, dummy_rm
+
def download(url):
filename = os.path.basename(url)
diff --git a/dspy/utils/caching.py b/dspy/utils/caching.py
index 7bd0d50c4..de11b19ba 100644
--- a/dspy/utils/caching.py
+++ b/dspy/utils/caching.py
@@ -1,7 +1,6 @@
import os
from pathlib import Path
-
_DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".dspy_cache")
DSPY_CACHEDIR = os.environ.get("DSPY_CACHEDIR") or _DEFAULT_CACHE_DIR
diff --git a/dspy/utils/dummies.py b/dspy/utils/dummies.py
index 99a028fd0..26c06f74f 100644
--- a/dspy/utils/dummies.py
+++ b/dspy/utils/dummies.py
@@ -121,9 +121,9 @@ class DummyLM(LM):
# Logging, with removed api key & where `cost` is None on cache hit.
kwargs = {k: v for k, v in kwargs.items() if not k.startswith("api_")}
- entry = dict(prompt=prompt, messages=messages, kwargs=kwargs)
- entry = dict(**entry, outputs=outputs, usage=0)
- entry = dict(**entry, cost=0)
+ entry = {"prompt": prompt, "messages": messages, "kwargs": kwargs}
+ entry = {**entry, "outputs": outputs, "usage": 0}
+ entry = {**entry, "cost": 0}
self.history.append(entry)
self.update_global_history(entry)
@@ -141,7 +141,7 @@ def dummy_rm(passages=()) -> callable:
if not passages:
def inner(query: str, *, k: int, **kwargs):
- assert False, "No passages defined"
+ raise ValueError("No passages defined")
return inner
max_length = max(map(len, passages)) + 100
@@ -153,8 +153,8 @@ def dummy_rm(passages=()) -> callable:
query_vec = vectorizer([query])[0]
scores = passage_vecs @ query_vec
largest_idx = (-scores).argsort()[:k]
- # return dspy.Prediction(passages=[passages[i] for i in largest_idx])
- return [dotdict(dict(long_text=passages[i])) for i in largest_idx]
+
+ return [dotdict(long_text=passages[i]) for i in largest_idx]
return inner
diff --git a/dspy/utils/saving.py b/dspy/utils/saving.py
index 2e01be014..801f8f70b 100644
--- a/dspy/utils/saving.py
+++ b/dspy/utils/saving.py
@@ -35,7 +35,7 @@ def load(path):
if not path.exists():
raise FileNotFoundError(f"The path '{path}' does not exist.")
- with open(path / "metadata.json", "r") as f:
+ with open(path / "metadata.json") as f:
metadata = ujson.load(f)
dependency_versions = get_dependency_versions()
diff --git a/dspy/utils/unbatchify.py b/dspy/utils/unbatchify.py
index bafdc8cb3..264a0c56c 100644
--- a/dspy/utils/unbatchify.py
+++ b/dspy/utils/unbatchify.py
@@ -1,8 +1,9 @@
-import time
import queue
import threading
-from typing import Any, Callable, List
+import time
from concurrent.futures import Future
+from typing import Any, Callable, List
+
class Unbatchify:
def __init__(