Skip to content

Commit

Permalink
Replace relative imports by absolute imports (#1945)
Browse files Browse the repository at this point in the history
* init

* Remove relative imports

* fix broken tests
  • Loading branch information
chenmoneygithub authored Dec 17, 2024
1 parent e0907eb commit 7c2f604
Show file tree
Hide file tree
Showing 21 changed files with 188 additions and 82 deletions.
17 changes: 9 additions & 8 deletions dspy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from .predict import *
from .primitives import *
from .retrieve import *
from .signatures import *
from dspy.predict import *
from dspy.primitives import *
from dspy.retrieve import *
from dspy.signatures import *
from dspy.teleprompt import *

import dspy.retrievers

# Functional must be imported after primitives, predict and signatures
from .functional import * # isort: skip
from dspy.evaluate import Evaluate # isort: skip
from dspy.clients import * # isort: skip
from dspy.adapters import * # isort: skip
from dspy.functional import * # isort: skip
from dspy.evaluate import Evaluate # isort: skip
from dspy.clients import * # isort: skip
from dspy.adapters import * # isort: skip
from dspy.utils.logging_utils import configure_dspy_loggers, disable_logging, enable_logging
from dspy.utils.asyncify import asyncify
from dspy.utils.saving import load
Expand Down
3 changes: 2 additions & 1 deletion dspy/adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dspy.adapters.base import Adapter
from dspy.adapters.chat_adapter import ChatAdapter
from dspy.adapters.json_adapter import JSONAdapter
from dspy.adapters.json_adapter import JSONAdapter
from dspy.adapters.image_utils import Image
24 changes: 19 additions & 5 deletions dspy/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .lm import LM
from .provider import Provider, TrainingJob
from .base_lm import BaseLM, inspect_history
from .embedding import Embedder
from dspy.clients.lm import LM
from dspy.clients.provider import Provider, TrainingJob
from dspy.clients.base_lm import BaseLM, inspect_history
from dspy.clients.embedding import Embedder
import litellm
import os
from pathlib import Path
Expand All @@ -15,7 +15,7 @@
litellm.cache = Cache(disk_cache_dir=DISK_CACHE_DIR, type="disk")

if litellm.cache.cache.disk_cache.size_limit != DISK_CACHE_LIMIT:
litellm.cache.cache.disk_cache.reset('size_limit', DISK_CACHE_LIMIT)
litellm.cache.cache.disk_cache.reset("size_limit", DISK_CACHE_LIMIT)

litellm.telemetry = False

Expand All @@ -26,8 +26,22 @@
# Accessed at run time by litellm; i.e., fine to keep after import
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"


def enable_litellm_logging():
litellm.suppress_debug_info = False


def disable_litellm_logging():
litellm.suppress_debug_info = True


__all__ = [
"LM",
"Provider",
"TrainingJob",
"BaseLM",
"inspect_history",
"Embedder",
"enable_litellm_logging",
"disable_litellm_logging",
]
18 changes: 13 additions & 5 deletions dspy/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from .colors import Colors
from .dataloader import DataLoader
from .dataset import Dataset
from .hotpotqa import HotPotQA
from .math import MATH
from dspy.datasets.colors import Colors
from dspy.datasets.dataloader import DataLoader
from dspy.datasets.dataset import Dataset
from dspy.datasets.hotpotqa import HotPotQA
from dspy.datasets.math import MATH

__all__ = [
"Colors",
"DataLoader",
"Dataset",
"HotPotQA",
"MATH",
]
14 changes: 11 additions & 3 deletions dspy/evaluate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from dspy.dsp.utils import EM, normalize_text

from .auto_evaluation import *
from .evaluate import Evaluate
from .metrics import *
from dspy.evaluate import auto_evaluation
from dspy.evaluate.evaluate import Evaluate
from dspy.evaluate import metrics

__all__ = [
"auto_evaluation",
"Evaluate",
"metrics",
"EM",
"normalize_text",
]
6 changes: 3 additions & 3 deletions dspy/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .module_graph import *
from dspy.experimental.module_graph import *

from .synthesizer import *
from .synthetic_data import *
from dspy.experimental.synthesizer import *
from dspy.experimental.synthetic_data import *
2 changes: 1 addition & 1 deletion dspy/experimental/synthesizer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .synthesizer import *
from dspy.experimental.synthesizer import *
32 changes: 22 additions & 10 deletions dspy/predict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from .aggregation import majority
from .chain_of_thought import ChainOfThought
from .chain_of_thought_with_hint import ChainOfThoughtWithHint
from .knn import KNN
from .multi_chain_comparison import MultiChainComparison
from .predict import Predict
from .program_of_thought import ProgramOfThought
from .react import ReAct, Tool
from .parallel import Parallel
# from .retry import Retry
from dspy.predict.aggregation import majority
from dspy.predict.chain_of_thought import ChainOfThought
from dspy.predict.chain_of_thought_with_hint import ChainOfThoughtWithHint
from dspy.predict.knn import KNN
from dspy.predict.multi_chain_comparison import MultiChainComparison
from dspy.predict.predict import Predict
from dspy.predict.program_of_thought import ProgramOfThought
from dspy.predict.react import ReAct, Tool
from dspy.predict.parallel import Parallel

__all__ = [
"majority",
"ChainOfThought",
"ChainOfThoughtWithHint",
"KNN",
"MultiChainComparison",
"Predict",
"ProgramOfThought",
"ReAct",
"Tool",
"Parallel",
]
6 changes: 3 additions & 3 deletions dspy/predict/avatar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .avatar import *
from .models import *
from .signatures import *
from dspy.predict.avatar.avatar import *
from dspy.predict.avatar.models import *
from dspy.predict.avatar.signatures import *
25 changes: 20 additions & 5 deletions dspy/primitives/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
from .assertions import *
from .example import *
from .prediction import *
from .program import *
from .python_interpreter import *
from dspy.primitives import assertions
from dspy.primitives.example import Example
from dspy.primitives.module import BaseModule
from dspy.primitives.prediction import Prediction, Completions
from dspy.primitives.program import Program, Module
from dspy.primitives.python_interpreter import PythonInterpreter, TextPrompt, CodePrompt


__all__ = [
"assertions",
"Example",
"BaseModule",
"Prediction",
"Completions",
"Program",
"Module",
"PythonInterpreter",
"TextPrompt",
"CodePrompt",
]
4 changes: 1 addition & 3 deletions dspy/primitives/program.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from dspy.utils.callback import with_callbacks
import magicattr

from dspy.predict.parallel import Parallel
from dspy.primitives.module import BaseModule
# import dspy
# from dspy.primitives.assertions import *
from dspy.utils.callback import with_callbacks


class ProgramMeta(type):
Expand Down
9 changes: 1 addition & 8 deletions dspy/primitives/python_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
import re
import typing
from collections.abc import Mapping
from typing import (
Any,
Dict,
List,
Optional,
Set,
Tuple,
)
from typing import Any, Dict, List, Optional, Set, Tuple


class InterpreterError(ValueError):
Expand Down
6 changes: 5 additions & 1 deletion dspy/propose/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .grounded_proposer import GroundedProposer
from dspy.propose.grounded_proposer import GroundedProposer

__all__ = [
"GroundedProposer",
]
2 changes: 1 addition & 1 deletion dspy/propose/grounded_proposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dspy.propose.utils import create_example_string, create_predictor_level_history_string, strip_prefix, get_dspy_source_code
from dspy.teleprompt.utils import get_signature, get_prompt_model

from .propose_base import Proposer
from dspy.propose.propose_base import Proposer

# Hardcoded variables (TODO: update)
MAX_INSTRUCT_IN_HISTORY = 5 # 10
Expand Down
6 changes: 5 additions & 1 deletion dspy/retrieve/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .retrieve import Retrieve
from dspy.retrieve.retrieve import Retrieve

__all__ = [
"Retrieve",
]
4 changes: 3 additions & 1 deletion dspy/retrievers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .embeddings import Embeddings
from dspy.retrievers.embeddings import Embeddings

__all__ = ["Embeddings"]
25 changes: 23 additions & 2 deletions dspy/signatures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
from .field import *
from .signature import *
from dspy.signatures.field import InputField, OutputField, OldField, OldInputField, OldOutputField
from dspy.signatures.signature import (
SignatureMeta,
Signature,
update_signatures,
ensure_signature,
make_signature,
infer_prefix,
)

__all__ = [
"InputField",
"OutputField",
"OldField",
"OldInputField",
"OldOutputField",
"SignatureMeta",
"Signature",
"infer_prefix",
"update_signatures",
"ensure_signature",
"make_signature",
]
40 changes: 28 additions & 12 deletions dspy/teleprompt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
from .avatar_optimizer import *
from .bettertogether import BetterTogether
from .bootstrap import *
from .bootstrap_finetune import BootstrapFinetune
from .copro_optimizer import COPRO
from .ensemble import *
from .knn_fewshot import *
from dspy.teleprompt.avatar_optimizer import AvatarOptimizer
from dspy.teleprompt.bettertogether import BetterTogether
from dspy.teleprompt.bootstrap import BootstrapFewShot
from dspy.teleprompt.bootstrap_finetune import BootstrapFinetune
from dspy.teleprompt.copro_optimizer import COPRO
from dspy.teleprompt.ensemble import Ensemble
from dspy.teleprompt.knn_fewshot import KNNFewShot

# from .mipro_optimizer import MIPRO
from .mipro_optimizer_v2 import MIPROv2
from .random_search import *
from dspy.teleprompt.mipro_optimizer_v2 import MIPROv2
from dspy.teleprompt.random_search import BootstrapFewShotWithRandomSearch

# from .signature_opt import SignatureOptimizer
# from .signature_opt_bayesian import BayesianSignatureOptimizer
from .teleprompt import *
from .teleprompt_optuna import *
from .vanilla import *
from dspy.teleprompt.teleprompt import Teleprompter
from dspy.teleprompt.teleprompt_optuna import BootstrapFewShotWithOptuna
from dspy.teleprompt.vanilla import LabeledFewShot

__all__ = [
"AvatarOptimizer",
"BetterTogether",
"BootstrapFewShot",
"BootstrapFinetune",
"COPRO",
"Ensemble",
"KNNFewShot",
"MIPROv2",
"BootstrapFewShotWithRandomSearch",
"BootstrapFewShotWithOptuna",
"LabeledFewShot",
]
2 changes: 1 addition & 1 deletion dspy/teleprompt/bootstrap_finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import Any, Callable, Dict, List, Optional, Union

import dspy
from dspy import LM # TODO: Remove after the old LM class is removed
from dspy.adapters.base import Adapter
from dspy.clients.lm import LM # TODO: Remove after the old LM class is removed
from dspy.clients.utils_finetune import infer_data_format
from dspy.evaluate.evaluate import Evaluate
from dspy.predict.predict import Predict
Expand Down
23 changes: 16 additions & 7 deletions dspy/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from dspy.utils.callback import BaseCallback, with_callbacks
from dspy.utils.dummies import *
from dspy.utils.caching import *
from dspy.utils.logging_utils import *
from dspy.utils.dummies import DummyLM, DummyVectorizer, dummy_rm

import os
import ujson
import requests


def download(url):
filename = os.path.basename(url)
remote_size = int(requests.head(url, allow_redirects=True).headers.get('Content-Length', 0))
remote_size = int(requests.head(url, allow_redirects=True).headers.get("Content-Length", 0))
local_size = os.path.getsize(filename) if os.path.exists(filename) else 0

if local_size != remote_size:
print(f"Downloading '{filename}'...")
with requests.get(url, stream=True) as r, open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192): f.write(chunk)
with requests.get(url, stream=True) as r, open(filename, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)


__all__ = [
"download",
"BaseCallback",
"with_callbacks",
"DummyLM",
"DummyVectorizer",
"dummy_rm",
]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ target-version = "py39"
select = [
"F", # Pyflakes
"E", # Pycodestyle

"TID252", # Absolute imports
]
ignore = [
"E501", # Line too long
Expand Down

0 comments on commit 7c2f604

Please sign in to comment.