Skip to content

Commit

Permalink
chore: upgrades and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed May 10, 2024
1 parent c44e9df commit 92f332b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
45 changes: 22 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@
"lint": "eslint ."
},
"devDependencies": {
"@antfu/eslint-config": "^2.11.6",
"@iconify/json": "^2.2.196",
"@shikijs/rehype": "^1.2.1",
"@antfu/eslint-config": "^2.17.0",
"@iconify/json": "^2.2.208",
"@shikijs/rehype": "^1.5.1",
"@sveltejs/adapter-auto": "^3.2.0",
"@sveltejs/kit": "^2.5.5",
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"@types/eslint": "^8.56.6",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"@unocss/eslint-config": "^0.58.8",
"@unocss/extractor-svelte": "^0.58.8",
"@unocss/reset": "^0.58.8",
"ai": "^3.0.16",
"@sveltejs/kit": "^2.5.7",
"@sveltejs/vite-plugin-svelte": "^3.1.0",
"@types/eslint": "^8.56.10",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@unocss/eslint-config": "^0.58.9",
"@unocss/extractor-svelte": "^0.58.9",
"@unocss/reset": "^0.58.9",
"eslint": "^8.57.0",
"eslint-plugin-format": "^0.1.0",
"eslint-plugin-svelte": "^2.35.1",
"openai": "^4.31.0",
"pyodide": "^0.25.0",
"eslint-plugin-format": "^0.1.1",
"eslint-plugin-svelte": "^2.39.0",
"openai": "^4.44.0",
"pyodide": "0.26.0-alpha.4",
"rehype-stringify": "^10.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0",
"shiki": "^1.2.1",
"svelte": "^4.2.12",
"svelte-check": "^3.6.8",
"svelte-sonner": "^0.3.19",
"shiki": "^1.5.1",
"svelte": "^4.2.16",
"svelte-check": "^3.7.1",
"svelte-sonner": "^0.3.24",
"tslib": "^2.6.2",
"typescript": "^5.4.3",
"typescript": "^5.4.5",
"unified": "^11.0.4",
"unocss": "^0.58.8",
"vite": "^5.2.7"
"unocss": "^0.59.4",
"vite": "^5.2.11"
}
}
14 changes: 9 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dynamic = [ "version" ]
requires-python = ">=3.10"
authors = [ { name = "Muspi Merol", email = "[email protected]" } ]
dependencies = [
"promplate~=0.3.3.4",
"promplate~=0.3.4.4",
"python-box~=7.1.1",
"promptools[validation]~=0.1.3.2",
]
Expand All @@ -22,13 +22,14 @@ distribution = true

[tool.pdm.dev-dependencies]
dev = [
"black>=24.3.0",
"black>=24.4.2",
"isort>=5.13.2",
"ruff>=0.3.3",
"ruff>=0.4.4",
"watchfiles>=0.21.0",
"promplate-pyodide~=0.0.3",
"promplate-pyodide~=0.0.3.2",
"micropip~=0.6.0",
"pyodide-py~=0.25.0",
"pyodide-py~=0.25.1",
"webtypy~=0.1.7",
]

[tool.pdm.scripts]
Expand All @@ -45,3 +46,6 @@ dev = { cmd = [

[tool.black]
line-length = 120

[tool.pyright]
reportMissingModuleSource = false
4 changes: 2 additions & 2 deletions reasonify-headless/reasonify/core/step3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Step3Schema(TypedDict):
actions: list[Action]

# from parents
tools: list[Tool]
tools: NotRequired[list[Tool]]


@step3.mid_process
Expand All @@ -49,7 +49,7 @@ async def run_tools(context):
raise Jump(step3) # fail to generate actions

for action in c["actions"]:
for tool in cast(list[Tool], c["tools"]):
for tool in cast(list[Tool], c.get("tools")):
if tool["id"] == action["tool_id"]:
action["tool"] = tool
action["result"] = await resolve(tool["run"](**action["payload"]))
2 changes: 1 addition & 1 deletion reasonify-headless/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.2.1"
__version__ = "0.0.2.2"
12 changes: 5 additions & 7 deletions src/lib/py/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ async def patch_promplate():
async def get_reasonify_chain(patch_promplate=patch_promplate):
await gather(patch_promplate(), install(getenv("PACKAGE", "reasonify-headless")))

from js import window
from promplate.prompt.utils import get_builtins

from reasonify import chain

if not TYPE_CHECKING:
from js import prompt

def input(any):
if (result := prompt(str(any))) is None:
raise EOFError("User refused to input anything.")
return result
def input(any):
if (result := window.prompt(str(any))) is None:
raise EOFError("User refused to input anything.")
return result

get_builtins()["input"] = input

Expand Down
12 changes: 7 additions & 5 deletions src/lib/py/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import type { PyProxy } from "pyodide/ffi";

import chat from "../chat";
import { promplateReady, pyodideReady, reasonifyReady } from "../stores";
import version from "./version";
import VERSION from "./version";
import { dev } from "$app/environment";
import * as env from "$env/static/public";
import { cacheSingleton } from "$lib/utils/cache";
import { version } from "pyodide";

const indexURL = "https://cdn.jsdelivr.net/pyodide/v0.25.0/full/";
const indexURL = env.PUBLIC_PYODIDE_INDEX_URL?.replace("{}", version) ?? `https://cdn.jsdelivr.net/pyodide/v${version}/full/`;

export const getPy = cacheSingleton(async () => {
const { loadPyodide } = await import("pyodide");
const PACKAGE = dev ? `/whl/reasonify_headless-${version}-py3-none-any.whl` : `reasonify-headless==${version}`;
const py = await loadPyodide({ indexURL, packages: ["micropip", "typing-extensions"], env: { PACKAGE }, args: ["-O"] });
const PACKAGE = dev ? `/whl/reasonify_headless-${VERSION}-py3-none-any.whl` : `reasonify-headless==${VERSION}`;
const py = await loadPyodide({ indexURL, packages: ["micropip", "typing-extensions"], env: { PACKAGE }, args: ["-OO"] });
pyodideReady.set(true);
return py;
});
Expand All @@ -32,7 +34,7 @@ export const getChain = cacheSingleton(async () => {
return {
async *astream(context) {
const dict = await getDict();
for await (const proxy of chain.astream(py.toPy(context), await getGenerate())) {
for await (const proxy of chain.astream(context, await getGenerate())) {
const { result } = proxy;
yield { ...dict(proxy).toJs({ dict_converter: Object.fromEntries }), result };
}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Modal from "$lib/components/Modal.svelte";
import { getChain } from "$lib/py";
import { promplateReady, pyodideReady, reasonifyReady } from "$lib/stores";
import { version } from "pyodide";
</script>

{#if browser}
Expand All @@ -15,7 +16,7 @@

<Modal show={!$reasonifyReady}>
<ol class="flex flex-col gap-2.5 whitespace-nowrap">
<li><LoadingItem loading={!$pyodideReady} text="Loading pyodide runtime" /></li>
<li><LoadingItem loading={!$pyodideReady} text="Loading pyodide v{version}" /></li>
<li class:op-30={!$pyodideReady}><LoadingItem loading={!$promplateReady} text="Patching promplate" /></li>
<li class:op-30={!$promplateReady}><LoadingItem loading={!$reasonifyReady} text="Building headless agent" /></li>
</ol>
Expand Down
13 changes: 9 additions & 4 deletions src/routes/api/chat/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ChatCompletionCreateParams } from "openai/resources/index.mjs";

import { error, text } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";
import { OpenAIStream } from "ai";
import { Readable } from "node:stream";
import OpenAI from "openai";

const openai = new OpenAI({
Expand All @@ -13,15 +13,20 @@ const openai = new OpenAI({
});

export const POST = (async ({ request }) => {
const data: ChatCompletionCreateParams = await request.json();
const data = await request.json() as ChatCompletionCreateParams;
data.model = "gpt-3.5-turbo-0125";
console.log(data);
const headers = { "content-type": data?.response_format?.type === "json_object" ? "application/json" : "text/markdown; charset=utf-8" };
try {
if (data.stream) {
const res = await openai.chat.completions.create(data);
const stream = OpenAIStream(res);
return new Response(stream, { headers });
return new Response(Readable.from((async function* () {
for await (const event of res) {
const chunk = event.choices[0]?.delta.content;
if (chunk)
yield chunk;
}
})()), { headers });
} else {
const res = await openai.chat.completions.create(data);
return text(res.choices[0].message.content ?? "", { headers });
Expand Down

0 comments on commit 92f332b

Please sign in to comment.