-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored GH Action, added playwright test
- Loading branch information
Showing
15 changed files
with
578 additions
and
982 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from openai.types.chat import ChatCompletionChunk | ||
import random | ||
import time | ||
import asyncio | ||
import uuid | ||
from .logs import logger | ||
|
||
GOOD_DUMMY_RESPONSE = """--- | ||
name: Dummy | ||
emoji: 🤖 | ||
--- | ||
<div class="text-foreground"> | ||
<h1 class="text-primary">Hello, world!</h1> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
<li>Item 3</li> | ||
</ul> | ||
<p>This is a dummy response.</p> | ||
<div class="w-full bg-secondary text-secondary-foreground"> | ||
<img src="https://placehold.co/600x400" alt="Placeholder Image"> | ||
</div> | ||
<div class="prose"> | ||
<h2>Some more text</h2> | ||
<p>This is some more text.</p> | ||
<button class="bg-primary text-primary-foreground">Click me</button> | ||
</div> | ||
</div> | ||
""" | ||
|
||
|
||
def content_to_openai(content, id): | ||
data = { | ||
"id": str(id), | ||
"object": "chat.completion.chunk", | ||
"created": int(time.time()), | ||
"model": "openui-dummy", | ||
"system_fingerprint": None, | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"delta": {"content": content}, | ||
"role": "assistant", | ||
"logprobs": None, | ||
"finish_reason": None, | ||
} | ||
], | ||
} | ||
return ChatCompletionChunk.model_validate(data) | ||
|
||
|
||
def ollama_chunk_to_sse(content, id): | ||
data = content_to_openai(content, id) | ||
return f"data: {data.model_dump_json()}\n\n" | ||
|
||
|
||
async def dummy_stream_generator(input): | ||
id = uuid.uuid1() | ||
if input.get("model") == "dummy/good": | ||
response = GOOD_DUMMY_RESPONSE | ||
else: | ||
response = "This is a bad dummy response." | ||
response_length = len(response) | ||
chunk_size = 10 | ||
start = 0 | ||
while start < response_length: | ||
end = min(start + random.randint(1, chunk_size), response_length) | ||
chunk = response[start:end] | ||
start = end | ||
yield ollama_chunk_to_sse(chunk, id) | ||
await asyncio.sleep(random.uniform(0.01, 0.10)) | ||
yield "data: [DONE]\n\n" | ||
|
||
|
||
class DummyStreamGenerator: | ||
def __init__(self, *args, **kwargs): | ||
self.generator_func = dummy_stream_generator | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
async def __aiter__(self): | ||
generator = self.generator_func(*self.args, **self.kwargs) | ||
async for item in generator: | ||
yield item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect, test } from '@playwright/test' | ||
import { HOST } from './util' | ||
|
||
test('test good flow', async ({ page }) => { | ||
await page.goto(`${HOST}/ai/new?dummy=good`) | ||
|
||
await expect(page).toHaveTitle(/Create a new UI/) | ||
await page.locator('#llm-input button[type="submit"]').click() | ||
// Wait for our LLM to finish generating the UI | ||
await page.waitForFunction( | ||
s => !document.querySelector(s), | ||
'#llm-input .rendering', | ||
{ timeout: 10000 } | ||
) | ||
await page | ||
.getByRole('button', { name: 'Toggle dark/light mode', exact: true }) | ||
.click() | ||
await page.getByRole('button', { name: 'Change theme', exact: true }).click() | ||
await page.getByRole('button', { name: 'Orange' }).click() | ||
const iframe = await page.frameLocator('#version-0') | ||
await expect(iframe.locator('h1')).toHaveText('Hello, world!') | ||
await expect(iframe.locator('img')).toHaveAttribute( | ||
'src', | ||
/.*unsplash\.com.*/ | ||
) | ||
const annotator = await page.$('#version-0') | ||
await annotator?.screenshot({ path: 'annotator-screenshot.png' }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const HOST = 'http://127.0.0.1:7979' |
Oops, something went wrong.