Skip to content

Commit

Permalink
rename (#545)
Browse files Browse the repository at this point in the history
Add new function syntax with jinja
Add more capabilities to prompt fiddle

---------

Co-authored-by: Sam Lijin <[email protected]>
Co-authored-by: Aaron Villalpando <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent 47404e3 commit 4582bdd
Show file tree
Hide file tree
Showing 440 changed files with 23,388 additions and 7,604 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/client_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ jobs:
poetry install --no-root
working-directory: clients/python

- name: Build FFI
run: |
./tools/build clients/python
working-directory: ./

- name: Lint Python Client
run: |
poetry run mypy .
Expand Down
71 changes: 71 additions & 0 deletions .github/workflows/release_python_ffi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Release clients/python-ffi

on:
push:
branches:
- sam/maturin2

permissions:
contents: read
id-token: write

jobs:
build:
strategy:
matrix:
_:
- runs_on: ubuntu-latest
target: x86_64
name: x86_64-unknown-linux-gnu

- runs_on: ubuntu-latest
target: aarch64
name: aarch64-unknown-linux-gnu

- runs_on: macos-latest
target: x86_64
name: x86_64-apple-darwin

- runs_on: macos-latest
target: aarch64
name: aarch64-apple-darwin

- runs_on: windows-latest
target: x64
name: x86_64-pc-windows-msvc

runs-on: ${{ matrix._.runs_on }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
architecture: ${{ matrix._.runs_on == 'windows-latest' && 'x64' || null }}

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix._.target }}
command: build
args: --release --out dist --manifest-path clients/python-ffi/Cargo.toml
sccache: "true"
manylinux: ${{ matrix._.runs_on == 'ubuntu-latest' && 'auto' || null }}

- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix._.name }}
path: dist

publish:
runs-on: ubuntu-latest
needs: [build]
steps:
- uses: actions/download-artifact@v4

- run: mkdir dist && mv wheels-*/* dist

# authz is managed via OIDC configured at https://pypi.org/manage/project/baml-core-ffi/settings/publishing/
# it is pinned to this filename, so renaming this file is not safe!!: release_python_ffi.yaml
- name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This file is generated by the BAML compiler.
# Do not edit this file directly.
# Instead, edit the BAML files and recompile.

# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off

from ..types.classes.cls_reviewanalysis import ReviewAnalysis
from ..types.enums.enm_reviewhelpfulness import ReviewHelpfulness
from ..types.enums.enm_reviewsentiment import ReviewSentiment
from ..types.partial.classes.cls_reviewanalysis import PartialReviewAnalysis
from baml_core.stream import AsyncStream
from baml_lib._impl.functions import BaseBAMLFunction
from typing import AsyncIterator, Callable, Protocol, runtime_checkable


IAnalyzeProductReviewOutput = ReviewAnalysis

@runtime_checkable
class IAnalyzeProductReview(Protocol):
"""
This is the interface for a function.
Args:
product_review: str
Returns:
ReviewAnalysis
"""

async def __call__(self, *, product_review: str) -> ReviewAnalysis:
...



@runtime_checkable
class IAnalyzeProductReviewStream(Protocol):
"""
This is the interface for a stream function.
Args:
product_review: str
Returns:
AsyncStream[ReviewAnalysis, PartialReviewAnalysis]
"""

def __call__(self, *, product_review: str
) -> AsyncStream[ReviewAnalysis, PartialReviewAnalysis]:
...
class IBAMLAnalyzeProductReview(BaseBAMLFunction[ReviewAnalysis, PartialReviewAnalysis]):
def __init__(self) -> None:
super().__init__(
"AnalyzeProductReview",
IAnalyzeProductReview,
["default_config"],
)

async def __call__(self, *args, **kwargs) -> ReviewAnalysis:
return await self.get_impl("").run(*args, **kwargs)

def stream(self, *args, **kwargs) -> AsyncStream[ReviewAnalysis, PartialReviewAnalysis]:
res = self.get_impl("").stream(*args, **kwargs)
return res

BAMLAnalyzeProductReview = IBAMLAnalyzeProductReview()

__all__ = [ "BAMLAnalyzeProductReview" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# This file is generated by the BAML compiler.
# Do not edit this file directly.
# Instead, edit the BAML files and recompile.

# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off

from ..types.classes.cls_reviewanalysis import ReviewAnalysis
from ..types.enums.enm_reviewhelpfulness import ReviewHelpfulness
from ..types.enums.enm_reviewsentiment import ReviewSentiment
from ..types.partial.classes.cls_reviewanalysis import PartialReviewAnalysis
from baml_core.stream import AsyncStream
from typing import Callable, Protocol, runtime_checkable


import typing

import pytest
from contextlib import contextmanager
from unittest import mock

ImplName = typing.Literal["default_config"]

T = typing.TypeVar("T", bound=typing.Callable[..., typing.Any])
CLS = typing.TypeVar("CLS", bound=type)


IAnalyzeProductReviewOutput = ReviewAnalysis

@runtime_checkable
class IAnalyzeProductReview(Protocol):
"""
This is the interface for a function.
Args:
product_review: str
Returns:
ReviewAnalysis
"""

async def __call__(self, *, product_review: str) -> ReviewAnalysis:
...



@runtime_checkable
class IAnalyzeProductReviewStream(Protocol):
"""
This is the interface for a stream function.
Args:
product_review: str
Returns:
AsyncStream[ReviewAnalysis, PartialReviewAnalysis]
"""

def __call__(self, *, product_review: str
) -> AsyncStream[ReviewAnalysis, PartialReviewAnalysis]:
...
class BAMLAnalyzeProductReviewImpl:
async def run(self, *, product_review: str) -> ReviewAnalysis:
...

def stream(self, *, product_review: str
) -> AsyncStream[ReviewAnalysis, PartialReviewAnalysis]:
...

class IBAMLAnalyzeProductReview:
def register_impl(
self, name: ImplName
) -> typing.Callable[[IAnalyzeProductReview, IAnalyzeProductReviewStream], None]:
...

async def __call__(self, *, product_review: str) -> ReviewAnalysis:
...

def stream(self, *, product_review: str
) -> AsyncStream[ReviewAnalysis, PartialReviewAnalysis]:
...

def get_impl(self, name: ImplName) -> BAMLAnalyzeProductReviewImpl:
...

@contextmanager
def mock(self) -> typing.Generator[mock.AsyncMock, None, None]:
"""
Utility for mocking the AnalyzeProductReviewInterface.
Usage:
```python
# All implementations are mocked.
async def test_logic() -> None:
with baml.AnalyzeProductReview.mock() as mocked:
mocked.return_value = ...
result = await AnalyzeProductReviewImpl(...)
assert mocked.called
```
"""
...

@typing.overload
def test(self, test_function: T) -> T:
"""
Provides a pytest.mark.parametrize decorator to facilitate testing different implementations of
the AnalyzeProductReviewInterface.
Args:
test_function : T
The test function to be decorated.
Usage:
```python
# All implementations will be tested.
@baml.AnalyzeProductReview.test
async def test_logic(AnalyzeProductReviewImpl: IAnalyzeProductReview) -> None:
result = await AnalyzeProductReviewImpl(...)
```
"""
...

@typing.overload
def test(self, *, exclude_impl: typing.Iterable[ImplName] = [], stream: bool = False) -> pytest.MarkDecorator:
"""
Provides a pytest.mark.parametrize decorator to facilitate testing different implementations of
the AnalyzeProductReviewInterface.
Args:
exclude_impl : Iterable[ImplName]
The names of the implementations to exclude from testing.
stream: bool
If set, will return a streamable version of the test function.
Usage:
```python
# All implementations except the given impl will be tested.
@baml.AnalyzeProductReview.test(exclude_impl=["implname"])
async def test_logic(AnalyzeProductReviewImpl: IAnalyzeProductReview) -> None:
result = await AnalyzeProductReviewImpl(...)
```
```python
# Streamable version of the test function.
@baml.AnalyzeProductReview.test(stream=True)
async def test_logic(AnalyzeProductReviewImpl: IAnalyzeProductReviewStream) -> None:
async for result in AnalyzeProductReviewImpl(...):
...
```
"""
...

@typing.overload
def test(self, test_class: typing.Type[CLS]) -> typing.Type[CLS]:
"""
Provides a pytest.mark.parametrize decorator to facilitate testing different implementations of
the AnalyzeProductReviewInterface.
Args:
test_class : Type[CLS]
The test class to be decorated.
Usage:
```python
# All implementations will be tested in every test method.
@baml.AnalyzeProductReview.test
class TestClass:
def test_a(self, AnalyzeProductReviewImpl: IAnalyzeProductReview) -> None:
...
def test_b(self, AnalyzeProductReviewImpl: IAnalyzeProductReview) -> None:
...
```
"""
...

BAMLAnalyzeProductReview: IBAMLAnalyzeProductReview
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class IClassifyTool(Protocol):
This is the interface for a function.
Args:
query: str
context: str
query: str
Returns:
ClassifyResponse
"""

async def __call__(self, *, query: str, context: str) -> ClassifyResponse:
async def __call__(self, *, context: str, query: str) -> ClassifyResponse:
...


Expand All @@ -41,14 +41,14 @@ class IClassifyToolStream(Protocol):
This is the interface for a stream function.
Args:
query: str
context: str
query: str
Returns:
AsyncStream[ClassifyResponse, PartialClassifyResponse]
"""

def __call__(self, *, query: str, context: str
def __call__(self, *, context: str, query: str
) -> AsyncStream[ClassifyResponse, PartialClassifyResponse]:
...
class IBAMLClassifyTool(BaseBAMLFunction[ClassifyResponse, PartialClassifyResponse]):
Expand Down
Loading

0 comments on commit 4582bdd

Please sign in to comment.