Skip to content

Commit 08e0813

Browse files
committed
update for ruff checks
1 parent 23f6978 commit 08e0813

23 files changed

Lines changed: 454 additions & 305 deletions

‎examples/lm_in_repl.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"""
77

88
import os
9+
10+
from dotenv import load_dotenv
11+
912
from rlm.clients.portkey import PortkeyClient
1013
from rlm.core.lm_handler import LMHandler
1114
from rlm.environments.local_repl import LocalREPL
1215

13-
from dotenv import load_dotenv
14-
1516
load_dotenv()
1617

1718
setup_code = """

‎examples/modal_repl_example.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
Run with: python -m examples.modal_repl_example
55
"""
66

7-
from rlm.environments.modal_repl import ModalREPL
87
from rlm.clients.base_lm import BaseLM
98
from rlm.core.lm_handler import LMHandler
10-
from rlm.core.types import UsageSummary, ModelUsageSummary
9+
from rlm.core.types import ModelUsageSummary, UsageSummary
10+
from rlm.environments.modal_repl import ModalREPL
1111

1212

1313
class MockLM(BaseLM):

‎examples/rlm_example.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from rlm.logger import RLMLogger
2-
from rlm import RLM
3-
41
import os
2+
53
from dotenv import load_dotenv
64

5+
from rlm import RLM
6+
from rlm.logger import RLMLogger
7+
78
load_dotenv()
89

910
logger = RLMLogger(log_dir="./logs")
@@ -19,7 +20,5 @@
1920
logger=logger,
2021
)
2122

22-
result = rlm.completion(
23-
"Using your code, solve 2^(2^(2^(2))). Show your work in Python."
24-
)
23+
result = rlm.completion("Using your code, solve 2^(2^(2^(2))). Show your work in Python.")
2524
print(result)

‎rlm/clients/__init__.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from rlm.clients.base_lm import BaseLM
2-
from rlm.core.types import ClientBackend
1+
from typing import Any
32

43
from dotenv import load_dotenv
5-
from typing import Dict, Any
4+
5+
from rlm.clients.base_lm import BaseLM
6+
from rlm.core.types import ClientBackend
67

78
load_dotenv()
89

910

1011
def get_client(
1112
backend: ClientBackend,
12-
backend_kwargs: Dict[str, Any],
13+
backend_kwargs: dict[str, Any],
1314
) -> BaseLM:
1415
"""
1516
Routes a specific backend and the args (as a dict) to the appropriate client if supported.

‎rlm/clients/anthropic.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from rlm.clients.base_lm import BaseLM
2-
from rlm.core.types import UsageSummary, ModelUsageSummary
3-
from typing import Dict, Any, Optional, List
41
from collections import defaultdict
2+
from typing import Any
3+
54
import anthropic
65

6+
from rlm.clients.base_lm import BaseLM
7+
from rlm.core.types import ModelUsageSummary, UsageSummary
8+
79

810
class AnthropicClient(BaseLM):
911
"""
@@ -13,7 +15,7 @@ class AnthropicClient(BaseLM):
1315
def __init__(
1416
self,
1517
api_key: str,
16-
model_name: Optional[str] = None,
18+
model_name: str | None = None,
1719
max_tokens: int = 32768,
1820
**kwargs,
1921
):
@@ -24,14 +26,12 @@ def __init__(
2426
self.max_tokens = max_tokens
2527

2628
# Per-model usage tracking
27-
self.model_call_counts: Dict[str, int] = defaultdict(int)
28-
self.model_input_tokens: Dict[str, int] = defaultdict(int)
29-
self.model_output_tokens: Dict[str, int] = defaultdict(int)
30-
self.model_total_tokens: Dict[str, int] = defaultdict(int)
29+
self.model_call_counts: dict[str, int] = defaultdict(int)
30+
self.model_input_tokens: dict[str, int] = defaultdict(int)
31+
self.model_output_tokens: dict[str, int] = defaultdict(int)
32+
self.model_total_tokens: dict[str, int] = defaultdict(int)
3133

32-
def completion(
33-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
34-
) -> str:
34+
def completion(self, prompt: str | list[dict[str, Any]], model: str | None = None) -> str:
3535
messages, system = self._prepare_messages(prompt)
3636

3737
model = model or self.model_name
@@ -47,7 +47,7 @@ def completion(
4747
return response.content[0].text
4848

4949
async def acompletion(
50-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
50+
self, prompt: str | list[dict[str, Any]], model: str | None = None
5151
) -> str:
5252
messages, system = self._prepare_messages(prompt)
5353

@@ -64,16 +64,14 @@ async def acompletion(
6464
return response.content[0].text
6565

6666
def _prepare_messages(
67-
self, prompt: str | List[Dict[str, Any]]
68-
) -> tuple[List[Dict[str, Any]], Optional[str]]:
67+
self, prompt: str | list[dict[str, Any]]
68+
) -> tuple[list[dict[str, Any]], str | None]:
6969
"""Prepare messages and extract system prompt for Anthropic API."""
7070
system = None
7171

7272
if isinstance(prompt, str):
7373
messages = [{"role": "user", "content": prompt}]
74-
elif isinstance(prompt, list) and all(
75-
isinstance(item, dict) for item in prompt
76-
):
74+
elif isinstance(prompt, list) and all(isinstance(item, dict) for item in prompt):
7775
# Extract system message if present (Anthropic handles system separately)
7876
messages = []
7977
for msg in prompt:
@@ -90,9 +88,7 @@ def _track_cost(self, response: anthropic.types.Message, model: str):
9088
self.model_call_counts[model] += 1
9189
self.model_input_tokens[model] += response.usage.input_tokens
9290
self.model_output_tokens[model] += response.usage.output_tokens
93-
self.model_total_tokens[model] += (
94-
response.usage.input_tokens + response.usage.output_tokens
95-
)
91+
self.model_total_tokens[model] += response.usage.input_tokens + response.usage.output_tokens
9692

9793
# Track last call for handler to read
9894
self.last_prompt_tokens = response.usage.input_tokens

‎rlm/clients/base_lm.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Dict, Any
2+
from typing import Any
33

44
from rlm.core.types import UsageSummary
55

@@ -15,11 +15,11 @@ def __init__(self, model_name: str, **kwargs):
1515
self.kwargs = kwargs
1616

1717
@abstractmethod
18-
def completion(self, prompt: str | Dict[str, Any]) -> str:
18+
def completion(self, prompt: str | dict[str, Any]) -> str:
1919
raise NotImplementedError
2020

2121
@abstractmethod
22-
async def acompletion(self, prompt: str | Dict[str, Any]) -> str:
22+
async def acompletion(self, prompt: str | dict[str, Any]) -> str:
2323
raise NotImplementedError
2424

2525
@abstractmethod

‎rlm/clients/litellm.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from rlm.clients.base_lm import BaseLM
2-
from rlm.core.types import UsageSummary, ModelUsageSummary
3-
from typing import Dict, Any, Optional, List
41
from collections import defaultdict
2+
from typing import Any
3+
54
import litellm
65

6+
from rlm.clients.base_lm import BaseLM
7+
from rlm.core.types import ModelUsageSummary, UsageSummary
8+
79

810
class LiteLLMClient(BaseLM):
911
"""
@@ -13,9 +15,9 @@ class LiteLLMClient(BaseLM):
1315

1416
def __init__(
1517
self,
16-
model_name: Optional[str] = None,
17-
api_key: Optional[str] = None,
18-
api_base: Optional[str] = None,
18+
model_name: str | None = None,
19+
api_key: str | None = None,
20+
api_base: str | None = None,
1921
**kwargs,
2022
):
2123
super().__init__(model_name=model_name, **kwargs)
@@ -24,19 +26,15 @@ def __init__(
2426
self.api_base = api_base
2527

2628
# Per-model usage tracking
27-
self.model_call_counts: Dict[str, int] = defaultdict(int)
28-
self.model_input_tokens: Dict[str, int] = defaultdict(int)
29-
self.model_output_tokens: Dict[str, int] = defaultdict(int)
30-
self.model_total_tokens: Dict[str, int] = defaultdict(int)
29+
self.model_call_counts: dict[str, int] = defaultdict(int)
30+
self.model_input_tokens: dict[str, int] = defaultdict(int)
31+
self.model_output_tokens: dict[str, int] = defaultdict(int)
32+
self.model_total_tokens: dict[str, int] = defaultdict(int)
3133

32-
def completion(
33-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
34-
) -> str:
34+
def completion(self, prompt: str | list[dict[str, Any]], model: str | None = None) -> str:
3535
if isinstance(prompt, str):
3636
messages = [{"role": "user", "content": prompt}]
37-
elif isinstance(prompt, list) and all(
38-
isinstance(item, dict) for item in prompt
39-
):
37+
elif isinstance(prompt, list) and all(isinstance(item, dict) for item in prompt):
4038
messages = prompt
4139
else:
4240
raise ValueError(f"Invalid prompt type: {type(prompt)}")
@@ -56,13 +54,11 @@ def completion(
5654
return response.choices[0].message.content
5755

5856
async def acompletion(
59-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
57+
self, prompt: str | list[dict[str, Any]], model: str | None = None
6058
) -> str:
6159
if isinstance(prompt, str):
6260
messages = [{"role": "user", "content": prompt}]
63-
elif isinstance(prompt, list) and all(
64-
isinstance(item, dict) for item in prompt
65-
):
61+
elif isinstance(prompt, list) and all(isinstance(item, dict) for item in prompt):
6662
messages = prompt
6763
else:
6864
raise ValueError(f"Invalid prompt type: {type(prompt)}")

‎rlm/clients/openai.py‎

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from rlm.clients.base_lm import BaseLM, UsageSummary, ModelUsageSummary
2-
from typing import Dict, Any, Optional, List
3-
from collections import defaultdict
4-
import openai
51
import os
2+
from collections import defaultdict
3+
from typing import Any
64

5+
import openai
76
from dotenv import load_dotenv
87

8+
from rlm.clients.base_lm import BaseLM, ModelUsageSummary, UsageSummary
9+
910
load_dotenv()
1011

1112
# Load API keys from environment variables
@@ -20,9 +21,9 @@ class OpenAIClient(BaseLM):
2021

2122
def __init__(
2223
self,
23-
api_key: Optional[str] = None,
24-
model_name: Optional[str] = None,
25-
base_url: Optional[str] = None,
24+
api_key: str | None = None,
25+
model_name: str | None = None,
26+
base_url: str | None = None,
2627
**kwargs,
2728
):
2829
super().__init__(model_name=model_name, **kwargs)
@@ -38,19 +39,15 @@ def __init__(
3839
self.model_name = model_name
3940

4041
# Per-model usage tracking
41-
self.model_call_counts: Dict[str, int] = defaultdict(int)
42-
self.model_input_tokens: Dict[str, int] = defaultdict(int)
43-
self.model_output_tokens: Dict[str, int] = defaultdict(int)
44-
self.model_total_tokens: Dict[str, int] = defaultdict(int)
42+
self.model_call_counts: dict[str, int] = defaultdict(int)
43+
self.model_input_tokens: dict[str, int] = defaultdict(int)
44+
self.model_output_tokens: dict[str, int] = defaultdict(int)
45+
self.model_total_tokens: dict[str, int] = defaultdict(int)
4546

46-
def completion(
47-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
48-
) -> str:
47+
def completion(self, prompt: str | list[dict[str, Any]], model: str | None = None) -> str:
4948
if isinstance(prompt, str):
5049
messages = [{"role": "user", "content": prompt}]
51-
elif isinstance(prompt, list) and all(
52-
isinstance(item, dict) for item in prompt
53-
):
50+
elif isinstance(prompt, list) and all(isinstance(item, dict) for item in prompt):
5451
messages = prompt
5552
else:
5653
raise ValueError(f"Invalid prompt type: {type(prompt)}")
@@ -64,13 +61,11 @@ def completion(
6461
return response.choices[0].message.content
6562

6663
async def acompletion(
67-
self, prompt: str | List[Dict[str, Any]], model: Optional[str] = None
64+
self, prompt: str | list[dict[str, Any]], model: str | None = None
6865
) -> str:
6966
if isinstance(prompt, str):
7067
messages = [{"role": "user", "content": prompt}]
71-
elif isinstance(prompt, list) and all(
72-
isinstance(item, dict) for item in prompt
73-
):
68+
elif isinstance(prompt, list) and all(isinstance(item, dict) for item in prompt):
7469
messages = prompt
7570
else:
7671
raise ValueError(f"Invalid prompt type: {type(prompt)}")
@@ -79,9 +74,7 @@ async def acompletion(
7974
if not model:
8075
raise ValueError("Model name is required for OpenAI client.")
8176

82-
response = await self.client.chat.completions.create(
83-
model=model, messages=messages
84-
)
77+
response = await self.client.chat.completions.create(model=model, messages=messages)
8578
self._track_cost(response, model)
8679
return response.choices[0].message.content
8780

0 commit comments

Comments
 (0)