Skip to content

Commit

Permalink
flow
Browse files Browse the repository at this point in the history
  • Loading branch information
skull8888888 committed Dec 1, 2024
1 parent dc7f617 commit 29a9463
Show file tree
Hide file tree
Showing 11 changed files with 738 additions and 1,114 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Publish Python Package

on:
push:
branches: ["main"]

permissions:
contents: read

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install the project
run: uv sync --all-extras --dev
- name: Build package
run: uv build
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Flow is extremely lightweight, clearly written and has not external dependencies
### Basic Usage
```python
from concurrent.futures import ThreadPoolExecutor
from flow import Flow, TaskOutput
from lmnr_flow import Flow, TaskOutput

flow = Flow(thread_pool_executor=ThreadPoolExecutor(max_workers=4))

Expand Down
22 changes: 15 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "laminar-flow"
version = "0.1.0"
name = "lmnr-flow"
version = "0.1.0b1"
description = "Lightweight task engine for building AI agents"
readme = "README.md"
requires-python = ">=3.12"
requires-python = ">=3.10"

dependencies = [
"ipykernel>=6.29.5",
"lmnr[all]>=0.4.40",
"openai>=1.55.0",
"pydantic>=2.10.1",
"lmnr>=0.4.43",
"pytest>=8.3.3",
]

[project.urls]
"Homepage" = "https://github.com/lmnr-ai/flow"
"Bug Tracker" = "https://github.com/lmnr-ai/flow/issues"

[project.license]
file = "LICENSE"
2 changes: 0 additions & 2 deletions src/flow/__init__.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/lmnr_flow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .context import Context
from .flow import Flow, TaskOutput
from .state import State
2 changes: 1 addition & 1 deletion src/flow/context.py → src/lmnr_flow/context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from queue import Queue
from typing import Any, Dict, Optional

from flow.state import State
from .state import State


class Context:
Expand Down
26 changes: 15 additions & 11 deletions src/flow/flow.py → src/lmnr_flow/flow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from dataclasses import dataclass
from threading import Lock
from queue import Queue
import logging
import traceback
from typing import Dict, List, Optional, Any, Union, Callable
from concurrent.futures import ThreadPoolExecutor
import logging
from dataclasses import dataclass
from queue import Queue
from threading import Lock
from typing import Any, Callable, Dict, List, Optional, Union

from lmnr import Laminar, observe

from flow.context import Context
from flow.state import State
from .context import Context
from .state import State

__ERROR__ = "__ERROR__"
__OUTPUT__ = "__OUTPUT__"
Expand Down Expand Up @@ -53,9 +54,6 @@ def execute_task(
):
self.logger.info(f"Starting execution of task '{task.id}'")

with self.active_tasks_lock:
self.active_tasks.add(task.id)

try:
with Laminar.start_as_current_span(task.id, input=self.context.to_dict()):
result: TaskOutput = task.action(self.context)
Expand Down Expand Up @@ -137,7 +135,9 @@ def run(
break
continue

print(f"Running task: {task_id}")
with self.active_tasks_lock:
self.active_tasks.add(task_id)

task = self.tasks[task_id]
future = self._executor.submit(self.execute_task, task, task_queue)
futures.add(future)
Expand Down Expand Up @@ -174,6 +174,10 @@ def run_engine():
continue

task = self.tasks[task_id]

with self.active_tasks_lock:
self.active_tasks.add(task_id)

future = self._executor.submit(
self.execute_task, task, task_queue, stream_queue
)
Expand Down
File renamed without changes.
51 changes: 48 additions & 3 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
import pytest
from concurrent.futures import ThreadPoolExecutor
from flow.flow import Flow, TaskOutput
import time
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import patch

import pytest
from lmnr.openllmetry_sdk.tracing.tracing import TracerWrapper
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

from src.lmnr_flow.flow import Flow, TaskOutput


@pytest.fixture(autouse=True)
def setup_tracer():
# Create and set the tracer provider
tracer_provider = TracerProvider()
# Optional: Add console exporter to see the traces in the console
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

# Set the tracer provider for the wrapper
wrapper = TracerWrapper()
wrapper.tracer_provider = tracer_provider

yield

# Cleanup after tests
wrapper.tracer_provider = None


@pytest.fixture(autouse=True)
def mock_tracer():
with patch('lmnr.openllmetry_sdk.tracing.tracing.TracerWrapper.get_tracer'):
yield


@pytest.fixture(autouse=True)
def mock_start_as_current_span():
class MockContextManager:
def __init__(self, func, input=None):
self.func = func

def __enter__(self):
return self.func

def __exit__(self, exc_type, exc_val, exc_tb):
pass

with patch('lmnr.Laminar.start_as_current_span', side_effect=MockContextManager):
yield


@pytest.fixture
Expand Down
Loading

0 comments on commit 29a9463

Please # to comment.