Skip to content

Commit 98f53b6

Browse files
authored
Linting code base (#14)
* remove unused imports and variables * put all rules in pyproject * run lint and format ruff github action on PRs and pushes to main
1 parent 554a8d0 commit 98f53b6

31 files changed

Lines changed: 162 additions & 153 deletions

.github/workflows/ruff.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ jobs:
1111
- uses: astral-sh/ruff-action@v3
1212
with:
1313
version: "0.12.10"
14-
args: "format --check --diff"
14+
args: "--version"
15+
- run: ruff check
16+
- run: ruff format --check

.pre-commit-config.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ repos:
44
rev: v0.12.10
55
hooks:
66
# Run the linter.
7-
# - id: ruff-check
8-
# args: [ --fix ]
7+
- id: ruff-check
8+
args: [ --fix ]
99
# Run the formatter.
1010
- id: ruff-format
11+
12+
# "src/**/__init__.py" = ["F401"]

CONTRIBUTING.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Contributing
22

3-
## Formatting
3+
## Linting and Formatting
44

5-
You will need to format your code using [`ruff`][2]. The easiest way to
6-
automate formatting of code prior to each commit is via the [`pre-commit`][1]
7-
package, which simplifies management of git hooks.
5+
You will need to lint and format your code using [`ruff`][2]. The easiest way
6+
to automate linting/formatting of code prior to each commit is via the
7+
[`pre-commit`][1] package, which simplifies management of git hooks.
88

99
If using pip, `pre-commit` will be installed when you install `ursa` in
1010
editable mode via
@@ -16,8 +16,8 @@ pip install -e .[dev]
1616
If using `uv`, `pre-commit` will be installed in your default (dev)
1717
environment.
1818

19-
To install the ruff formatting git hook to your local `.git/hooks` folder, run
20-
in the following in the current directory:
19+
To install the ruff git hook to your local `.git/hooks` folder, run
20+
the following in the current directory:
2121

2222
```bash
2323
# If using pip with venv, first activate your environment, then
@@ -27,19 +27,33 @@ pre-commit install
2727
uv run pre-commit install
2828
```
2929

30-
Prior to subsequent `git commit` calls, `ruff` will first format code.
30+
Prior to subsequent `git commit` calls, `ruff` will first lint/format code.
3131

32-
Instead of running git hooks, you can format code manually via
32+
Instead of running git hooks, you can lint/format code manually via running the
33+
following in the current directory:
3334

3435
```bash
35-
# In current directory:
36+
# Lint
37+
ruff check --fix
38+
39+
# Format
3640
ruff format
3741
```
3842

43+
To continually lint while developing, you can
44+
run the following in your terminal
45+
46+
```bash
47+
ruff check --watch
48+
```
49+
50+
For editor (e.g., vim, VSCode) integration, see [here][4].
51+
3952
(You can install via ruff following instructions [here][3].)
4053

41-
If code is not formatted, PRs will be blocked.
54+
If code is not linted/formatted, PRs will be blocked.
4255

4356
[1]: https://pre-commit.com
4457
[2]: https://github.com/astral-sh/ruff
4558
[3]: https://docs.astral.sh/ruff/installation
59+
[4]: https://docs.astral.sh/ruff/editors/setup

examples/combine_agents_examples/agent_graph.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import os, sys
2-
import coolname
1+
import os
2+
from typing import Annotated, Literal
33

4+
import coolname
45
from langchain_core.messages import (
6+
AIMessage,
57
HumanMessage,
68
SystemMessage,
7-
AIMessage,
8-
ToolMessage,
99
)
10+
from langchain_core.tools import tool
1011
from langchain_litellm import ChatLiteLLM
1112
from langchain_openai import OpenAIEmbeddings
12-
13-
from ursa.agents import ArxivAgent, RecallAgent, BaseAgent, BaseChatModel
14-
from ursa.agents import ExecutionAgent, ExecutionState
15-
from ursa.prompt_library.execution_prompts import summarize_prompt
16-
from ursa.util.memory_logger import AgentMemory
17-
1813
from langgraph.graph import END, START, StateGraph
1914
from langgraph.graph.message import add_messages
20-
from langgraph.prebuilt import ToolNode, InjectedState
21-
from langchain_core.tools import tool
22-
23-
from typing import Annotated, Literal
15+
from langgraph.prebuilt import InjectedState, ToolNode
2416
from typing_extensions import TypedDict
2517

18+
from ursa.agents import (
19+
ArxivAgent,
20+
BaseAgent,
21+
BaseChatModel,
22+
ExecutionAgent,
23+
ExecutionState,
24+
RecallAgent,
25+
)
26+
from ursa.prompt_library.execution_prompts import summarize_prompt
27+
from ursa.util.memory_logger import AgentMemory
28+
2629
# --- ANSI color codes ---
2730
GREEN = "\033[92m"
2831
BLUE = "\033[94m"
@@ -51,14 +54,6 @@
5154
"""
5255

5356

54-
class State(TypedDict):
55-
messages: Annotated[list, add_messages]
56-
current_progress: str
57-
code_files: list[str]
58-
workspace: str
59-
arxiv_results: list[str]
60-
61-
6257
model = ChatLiteLLM(
6358
model="openai/o3",
6459
max_tokens=50000,
@@ -164,7 +159,7 @@ def runner(self, state: State) -> State:
164159
)
165160
os.makedirs(new_state["workspace"], exist_ok=True)
166161

167-
if type(new_state["messages"][0]) == SystemMessage:
162+
if isinstance(new_state["messages"][0], SystemMessage):
168163
new_state["messages"][0] = SystemMessage(content=self.runner_prompt)
169164
else:
170165
new_state["messages"] = [
@@ -187,7 +182,7 @@ def summarize(self, state: ExecutionState) -> ExecutionState:
187182
memories = []
188183
# Handle looping through the messages
189184
for x in state["messages"]:
190-
if not type(x) == AIMessage:
185+
if not isinstance(x, AIMessage):
191186
memories.append(x.content)
192187
elif not x.tool_calls:
193188
memories.append(x.content)

examples/hitl_examples/hitl_basic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import os, sqlite3
2-
1+
import os
2+
import sqlite3
33
from pathlib import Path
4+
45
from langchain_core.messages import HumanMessage
56
from langchain_litellm import ChatLiteLLM
6-
from langgraph.checkpoint.sqlite import SqliteSaver
77
from langchain_openai import OpenAIEmbeddings
8+
from langgraph.checkpoint.sqlite import SqliteSaver
89

910
from ursa.agents import (
1011
ArxivAgent,
1112
ExecutionAgent,
1213
PlanningAgent,
13-
WebSearchAgent,
1414
RecallAgent,
15+
WebSearchAgent,
1516
)
1617
from ursa.util.memory_logger import AgentMemory
1718

examples/single_agent_examples/arxiv_agent/high_entropy_alloy_papers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import sys
21
import time
32

4-
sys.path.append("../../.")
3+
from langchain_community.callbacks.openai_info import OpenAICallbackHandler
4+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
55

66
from ursa.agents import ArxivAgent
7-
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
8-
from langchain_community.callbacks.openai_info import OpenAICallbackHandler
97

108

119
def main():
@@ -33,7 +31,7 @@ def main():
3331

3432
t0 = time.time()
3533

36-
result = agent.run(
34+
agent.run(
3735
arxiv_search_query="High Entropy Alloys",
3836
context="Find High entropy alloys suitable for application under extreme conditions. For candidates that you identify, provide the starting structure, crystal structure, lattice parameters, and space group.",
3937
)

examples/single_agent_examples/arxiv_agent/neutron_star_radius.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import sys
1+
from pathlib import Path
22

3-
from ursa.agents import ArxivAgent
43
from langchain_litellm import ChatLiteLLM
54

5+
from ursa.agents import ArxivAgent
6+
67

78
def main():
89
llm = ChatLiteLLM(model="openai/o3", max_completion_tokens=20000)
910

11+
Path("workspace").mkdir(exist_ok=True)
1012
agent = ArxivAgent(
1113
llm=llm,
1214
summarize=True,
1315
process_images=True,
1416
max_results=3,
15-
database_path="arxiv_papers_neutron_star",
16-
summaries_path="arxiv_summaries_neutron_star",
17-
vectorstore_path="arxiv_vectorstores_neutron_star",
17+
database_path="workspace/arxiv_papers_neutron_star",
18+
summaries_path="workspace/arxiv_summaries_neutron_star",
19+
vectorstore_path="workspace/arxiv_vectorstores_neutron_star",
1820
download_papers=True,
1921
)
2022

examples/single_agent_examples/execution_agent/bayesian_optimization.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import sys
2-
31
from langchain_core.messages import HumanMessage
42
from langchain_litellm import ChatLiteLLM
53
from langchain_openai import OpenAIEmbeddings

examples/single_agent_examples/execution_agent/integer_sum.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
benchmarks them against each other and verifies the results mach.
66
"""
77

8-
from pathlib import Path
98
import sqlite3
9+
from pathlib import Path
1010

1111
from langchain_core.messages import HumanMessage
1212
from langchain_litellm import ChatLiteLLM
1313
from langgraph.checkpoint.sqlite import SqliteSaver
14-
15-
from ursa.agents import ExecutionAgent
16-
1714
from rich import get_console
1815
from rich.panel import Panel
1916

17+
from ursa.agents import ExecutionAgent
18+
2019
console = get_console() # always returns the same instance
2120

2221
# Define the workspace

examples/single_agent_examples/materials_project_agent/mpa_example.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import sys
2-
3-
from langchain_core.messages import HumanMessage
4-
from langchain_litellm import ChatLiteLLM
5-
6-
from ursa.agents import ExecutionAgent, MaterialsProjectAgent
7-
8-
import json, os
1+
from ursa.agents import MaterialsProjectAgent
92

103
# make sure your MP_API_KEY is set in env or pass it here
114
agent = MaterialsProjectAgent(max_results=5)

0 commit comments

Comments
 (0)