Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI serving tool #702

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions outlines/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING, Callable, Optional, Tuple, Union

import requests
from pydantic import RootModel

from outlines import generate, models

Expand All @@ -11,6 +12,30 @@
from outlines.prompts import Prompt


class NewFunction:
"""Represents an Outlines function.

Functions are a convenient way to encapsulate a prompt template, a language
model and a Pydantic model that define the output structure. Once defined,
the function can be called with arguments that will be used to render the
prompt template.

"""

model: str
prompt_template: Optional[Callable] = None
pydantic_schema: Optional[RootModel] = None

def prompt(self, fn: Callable):
pass

def schema(self, model: RootModel):
pass

def __call__(self, *args, **kwargs):
pass


@dataclass
class Function:
"""Represents an Outlines function.
Expand Down
42 changes: 42 additions & 0 deletions outlines/serve/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import typer
from typing_extensions import Annotated

from outlines.function import extract_function_from_file

app = typer.Typer(
no_args_is_help=True, help="Serve Outlines functions as APIs", add_completion=False
)


@app.command()
def serve(
path: Annotated[
str,
typer.Argument(help="Path to the script that defines the Outlines function."),
],
name: Annotated[
str,
typer.Option("--name", "-n", help="The name of the function in the script."),
] = "fn",
port: Annotated[
int,
typer.Option("--port", "-p", help="Port to serve the function locally"),
] = 8000,
):
"""Serve the Outlines function."""

with open(path) as file:
content = file.read()

_ = extract_function_from_file(content, name)

# 1. Load the file and import objects
# 2. Find the function by its name
# 3. Create an API based on the prompt function's parameters and model name
# 4. Return example of calling the API

print(f"{path}{port}{name}")


def main():
app()
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ serve = [
"uvicorn",
"fastapi",
"pydantic>=2.0",
"typer[all]",
]

[project.urls]
Expand All @@ -76,6 +77,9 @@ repository = "https://github.com/outlines-dev/outlines"
file="README.md"
content-type = "text/markdown"

[project.scripts]
outlines = "outlines.serve.main:main"

[tool.setuptools]
packages = ["outlines"]

Expand All @@ -95,6 +99,9 @@ filterwarnings = [
"ignore::UserWarning",
]

[tool.poetry.scripts]
rick-portal-gun = "rick_portal_gun.main:app"

[tool.mypy]
exclude=["examples"]

Expand All @@ -117,6 +124,7 @@ module = [
"tiktoken.*",
"torch.*",
"transformers.*",
"typer.*",
"llama_cpp",
"huggingface_hub",
"lark.*",
Expand Down
Loading