Skip to content

Commit

Permalink
Merge pull request #3 from Mirascope/sdk-commands
Browse files Browse the repository at this point in the history
added pull command
  • Loading branch information
Brendan Kao authored Sep 7, 2024
2 parents 8ea8092 + b506d06 commit c9f07ce
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 79 deletions.
21 changes: 21 additions & 0 deletions examples/sqlite_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Pulling a prompt from the server."""

from openai import OpenAI

import lilypad

client = OpenAI()


def recommend_book(genre: str) -> str | None:
"""Recommends a `genre` book using OpenAI"""
prompt = lilypad.prompt(recommend_book)(genre)
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=lilypad.openai.messages(prompt),
)
message = completion.choices[0].message
return message.content


print(recommend_book("fantasy"))
34 changes: 20 additions & 14 deletions lilypad/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Annotated

from fastapi import Depends, FastAPI, Form, HTTPException, Request
from fastapi import Depends, FastAPI, Form, Header, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from sqlmodel import Session, select
Expand Down Expand Up @@ -76,33 +76,39 @@ async def show_project(
)


@app.get("/projects/{project_name}/versions", response_class=HTMLResponse)
@app.get(
"/projects/{project_name}/versions",
response_model=PromptVersionTable,
)
async def get_prompt_versions(
request: Request,
session: Annotated[Session, Depends(get_session)],
project_name: str,
) -> HTMLResponse:
hx_request: str | None = Header(None),
) -> HTMLResponse | PromptVersionTable:
"""Render the version_list.html template."""
project = session.exec(
select(ProjectTable).where(ProjectTable.name == project_name)
).first()

if not project:
raise HTTPException(status_code=404)

return templates.TemplateResponse(
"partials/version_list.html",
{
"request": request,
"project": project,
"prompt_versions": project.prompt_versions,
},
)
if hx_request:
return templates.TemplateResponse(
"partials/version_list.html",
{
"request": request,
"project": project,
"prompt_versions": project.prompt_versions,
},
)
else:
return sorted(project.prompt_versions, key=lambda x: x.id or 0, reverse=True)[
:1
][0]


@app.post("/projects/{project_name}/versions")
async def create_version(
request: Request,
session: Annotated[Session, Depends(get_session)],
project_name: str,
prompt_template: str = Form(...),
Expand Down
6 changes: 5 additions & 1 deletion lilypad/app/templates/partials/version_list.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% for prompt_version in prompt_versions %}
<li>
<a href="{{prompt_version.id}}" hx-push-url="true">{{ prompt_version.id}}</a>
<a
href="/projects/{{project.name}}/versions/{{prompt_version.id}}"
hx-push-url="true"
>{{ prompt_version.id}}</a
>
</li>
{% endfor %}
12 changes: 12 additions & 0 deletions lilypad/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Lilypad commands."""

import requests

from lilypad.app.models import PromptVersionTable


def pull(project_name: str) -> str:
"""Pull the latest prompt version."""
url = f"http://localhost:8000/projects/{project_name}/versions"
response = requests.get(url)
return PromptVersionTable(**response.json()).prompt_template
9 changes: 2 additions & 7 deletions lilypad/prompts.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
"""Utility for retrieving the prompt for a function."""

import hashlib
import inspect
from collections.abc import Callable
from functools import wraps
from typing import Any, ParamSpec, TypeVar

from mirascope.core import base
from pydantic import create_model

from .dummy_database import get_dummy_database
from .commands import pull

_P = ParamSpec("_P")
_R = TypeVar("_R")


def prompt(fn: Callable[_P, _R]) -> Callable[_P, base.BasePrompt]:
"""Returns a method for constructing a `BasePrompt` using `fn`'s args."""
unique_id = hashlib.sha256(
f"{fn.__name__}:{inspect.signature(fn)}".encode()
).hexdigest()
prompt_template = get_dummy_database()[unique_id]["prompt_template"]
prompt_template = pull(fn.__name__)
template_vars = base._utils.get_template_variables(
prompt_template, include_format_spec=False
)
Expand Down
114 changes: 57 additions & 57 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9f07ce

Please sign in to comment.