From 83a337ec0579b0405d2eb86d0c2215cb1ad73f42 Mon Sep 17 00:00:00 2001 From: William Bakst Date: Wed, 16 Oct 2024 09:52:50 -0700 Subject: [PATCH 1/2] feat: support for openrouter --- ...ojectId.llm-fns_.$llmFunctionId.fn-params.tsx | 16 +++++++++++++++- client/src/types/types.ts | 1 + lilypad/_utils.py | 16 +++++++++++++++- lilypad/server/models/fn_params.py | 1 + pyproject.toml | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/client/src/routes/projects_/$projectId.llm-fns_.$llmFunctionId.fn-params.tsx b/client/src/routes/projects_/$projectId.llm-fns_.$llmFunctionId.fn-params.tsx index d51b135..efe8afb 100644 --- a/client/src/routes/projects_/$projectId.llm-fns_.$llmFunctionId.fn-params.tsx +++ b/client/src/routes/projects_/$projectId.llm-fns_.$llmFunctionId.fn-params.tsx @@ -128,7 +128,6 @@ const EditorContainer = () => { { value: "o1-preview", label: "o1-preview" }, { value: "o1-mini", label: "o1-mini" }, { value: "gpt-4-turbo", label: "GPT-4 Turbo" }, - { value: "gpt-3.5-turbo", label: "GPT-3.5 Turbo" }, ], [Provider.ANTHROPIC]: [ { value: "claude-3-5-sonnet-20240620", label: "Claude 3.5 Sonnet" }, @@ -136,6 +135,20 @@ const EditorContainer = () => { { value: "claude-3-sonnet-20240229", label: "Claude 3 Sonnet" }, { value: "claude-3-haiku-20240307", label: "Claude 3 Haiku" }, ], + [Provider.OPENROUTER]: [ + { value: "openai/chatgpt-4o-latest", label: "GPT-4o" }, + { value: "openai/gpt-4o-mini", label: "GPT-4o-mini" }, + { value: "openai/o1-preview", label: "o1-preview" }, + { value: "openai/o1-mini", label: "o1-mini" }, + { value: "openai/gpt-4-turbo", label: "GPT-4 Turbo" }, + { + value: "anthropic/claude-3.5-sonnet", + label: "Claude 3.5 Sonnet", + }, + { value: "anthropic/claude-3-opus", label: "Claude 3 Opus" }, + { value: "anthropic/claude-3-sonnet", label: "Claude 3 Sonnet" }, + { value: "anthropic/claude-3-haiku", label: "Claude 3 Haiku" }, + ], }; const options = modelOptions[provider] || []; @@ -193,6 +206,7 @@ const EditorContainer = () => { OpenAI Anthropic + OpenRouter )} diff --git a/client/src/types/types.ts b/client/src/types/types.ts index 46ef375..8841a19 100644 --- a/client/src/types/types.ts +++ b/client/src/types/types.ts @@ -198,6 +198,7 @@ export interface ProjectTable { export enum Provider { OPENAI = "openai", ANTHROPIC = "anthropic", + OPENROUTER = "openrouter", } /** diff --git a/lilypad/_utils.py b/lilypad/_utils.py index 1a60802..13a8405 100644 --- a/lilypad/_utils.py +++ b/lilypad/_utils.py @@ -145,10 +145,24 @@ def traced_synced_llm_function_constructor( if not trace_decorator: trace_decorator = lambda x: x # noqa: E731 + provider = fn_params.provider.value + + # Custom Clients + client = None + if fn_params.provider.value == "openrouter": + from openai import OpenAI + + provider = "openai" + client = OpenAI( + base_url="https://openrouter.ai/api/v1", + api_key=os.getenv("OPENROUTER_API_KEY"), + ) + call_decorator = partial( - import_module(f"mirascope.core.{fn_params.provider.value}").call, + import_module(f"mirascope.core.{provider}").call, model=fn_params.model, json_mode=False, + client=client, ) def decorator( diff --git a/lilypad/server/models/fn_params.py b/lilypad/server/models/fn_params.py index 1407480..cc26273 100644 --- a/lilypad/server/models/fn_params.py +++ b/lilypad/server/models/fn_params.py @@ -21,6 +21,7 @@ class Provider(str, Enum): OPENAI = "openai" ANTHROPIC = "anthropic" + OPENROUTER = "openrouter" class FnParamsBase(BaseSQLModel): diff --git a/pyproject.toml b/pyproject.toml index b6ee733..b1f6686 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "python-lilypad" -version = "0.0.3" +version = "0.0.4" description = "An open-source prompt engineering framework." readme = "README.md" license = { file = "LICENSE" } @@ -42,7 +42,7 @@ dependencies = [ [project.urls] # Homepage = "https://mirascope.com/lilypad" -# Documentation = "https://lilypad.mirascope.com/docs" +Documentation = "https://lilypad.mirascope.com/docs" Repository = "https://github.com/Mirascope/lilypad" Issues = "https://github.com/Mirascope/lilypad/issues" Changelog = "https://github.com/Mirascope/lilypad/releases" From 495bca20cc656a7f02d23707f4b5732922b58877 Mon Sep 17 00:00:00 2001 From: William Bakst Date: Wed, 16 Oct 2024 09:55:13 -0700 Subject: [PATCH 2/2] fix: ruff lint errors --- lilypad/cli/commands/run.py | 2 -- lilypad/cli/commands/start.py | 1 - lilypad/configure.py | 1 - lilypad/server/main.py | 2 -- uv.lock | 2 +- 5 files changed, 1 insertion(+), 7 deletions(-) diff --git a/lilypad/cli/commands/run.py b/lilypad/cli/commands/run.py index 1dc1330..dc54b6d 100644 --- a/lilypad/cli/commands/run.py +++ b/lilypad/cli/commands/run.py @@ -1,9 +1,7 @@ """The `run` command, which runs a prompt with lilypad opentelemetry tracing.""" -import json import os import runpy -from pathlib import Path from rich import print from typer import Argument, Option diff --git a/lilypad/cli/commands/start.py b/lilypad/cli/commands/start.py index 41e5fe0..66b8787 100644 --- a/lilypad/cli/commands/start.py +++ b/lilypad/cli/commands/start.py @@ -1,7 +1,6 @@ """The `start` command to initialize Lilypad.""" import contextlib -import json import os import signal import subprocess diff --git a/lilypad/configure.py b/lilypad/configure.py index 44d2525..007c924 100644 --- a/lilypad/configure.py +++ b/lilypad/configure.py @@ -1,7 +1,6 @@ """Initialize Lilypad OpenTelemetry instrumentation.""" import importlib.util -import os from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider diff --git a/lilypad/server/main.py b/lilypad/server/main.py index 6f2f01c..be6ee0e 100644 --- a/lilypad/server/main.py +++ b/lilypad/server/main.py @@ -1,9 +1,7 @@ """Main FastAPI application module for Lilypad.""" import json -import os from collections.abc import Sequence -from pathlib import Path from typing import Annotated, Any from fastapi import Depends, FastAPI, HTTPException, Request, status diff --git a/uv.lock b/uv.lock index af32801..c0583c5 100644 --- a/uv.lock +++ b/uv.lock @@ -1303,7 +1303,7 @@ wheels = [ [[package]] name = "python-lilypad" -version = "0.0.1" +version = "0.0.4" source = { editable = "." } dependencies = [ { name = "fastapi", extra = ["standard"] },