From 2379e94cbfa274dbe847df795ef041301958a4cc Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 2 Mar 2024 03:01:44 -0800 Subject: [PATCH 1/7] feat: text completion playground --- backend/apps/ollama/main.py | 11 +- src/lib/apis/ollama/index.ts | 26 ++ .../chat/MessageInput/Models.svelte | 20 +- src/lib/components/layout/Sidebar.svelte | 26 ++ src/routes/(app)/playground/+page.svelte | 263 ++++++++++++++++++ 5 files changed, 336 insertions(+), 10 deletions(-) create mode 100644 src/routes/(app)/playground/+page.svelte diff --git a/backend/apps/ollama/main.py b/backend/apps/ollama/main.py index bc797f08..55579740 100644 --- a/backend/apps/ollama/main.py +++ b/backend/apps/ollama/main.py @@ -91,7 +91,13 @@ async def proxy(path: str, request: Request, user=Depends(get_current_user)): def stream_content(): try: - if path in ["chat"]: + if path == "generate": + data = json.loads(body.decode("utf-8")) + + if not ("stream" in data and data["stream"] == False): + yield json.dumps({"id": request_id, "done": False}) + "\n" + + elif path == "chat": yield json.dumps({"id": request_id, "done": False}) + "\n" for chunk in r.iter_content(chunk_size=8192): @@ -103,7 +109,8 @@ async def proxy(path: str, request: Request, user=Depends(get_current_user)): finally: if hasattr(r, "close"): r.close() - REQUEST_POOL.remove(request_id) + if request_id in REQUEST_POOL: + REQUEST_POOL.remove(request_id) r = requests.request( method=request.method, diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 5fc8a5fe..4597dd80 100644 --- a/src/lib/apis/ollama/index.ts +++ b/src/lib/apis/ollama/index.ts @@ -217,6 +217,32 @@ export const generatePrompt = async (token: string = '', model: string, conversa return res; }; +export const generateTextCompletion = async (token: string = '', model: string, text: string) => { + let error = null; + + const res = await fetch(`${OLLAMA_API_BASE_URL}/generate`, { + method: 'POST', + headers: { + 'Content-Type': 'text/event-stream', + Authorization: `Bearer ${token}` + }, + body: JSON.stringify({ + model: model, + prompt: text, + stream: true + }) + }).catch((err) => { + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const generateChatCompletion = async (token: string = '', body: object) => { let controller = new AbortController(); let error = null; diff --git a/src/lib/components/chat/MessageInput/Models.svelte b/src/lib/components/chat/MessageInput/Models.svelte index cedd1d5b..2c364810 100644 --- a/src/lib/components/chat/MessageInput/Models.svelte +++ b/src/lib/components/chat/MessageInput/Models.svelte @@ -79,14 +79,18 @@ throw data; } - if (data.done == false) { - if (prompt == '' && data.response == '\n') { - continue; - } else { - prompt += data.response; - console.log(data.response); - chatInputElement.scrollTop = chatInputElement.scrollHeight; - await tick(); + if ('id' in data) { + console.log(data); + } else { + if (data.done == false) { + if (prompt == '' && data.response == '\n') { + continue; + } else { + prompt += data.response; + console.log(data.response); + chatInputElement.scrollTop = chatInputElement.scrollHeight; + await tick(); + } } } } diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 6201e077..1061a1ab 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -594,6 +594,32 @@
Admin Panel
+ + {/if} + + + +
+ + + +
+ + +
+
+