Skip to content

Commit

Permalink
fix: colorgen demo
Browse files Browse the repository at this point in the history
  • Loading branch information
gsuuon committed Oct 31, 2024
1 parent 56ae2ea commit 1c96bee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
10 changes: 3 additions & 7 deletions example/vite-demo/colorgen/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ const view: View = {
hex: ({ context, update, primary, a }) => {
const assistant = context('You are a helpful coloring assistant.')

const [hexColor] = createSignal(
assistant`#${a(`shade of ${primary} as a hex rgb value`, {
const hexColor = assistant`#${a(`shade of ${primary} as a hex rgb value`, {
stops: ['\n', ' '],
maxTokens: 6,
validate: {
retries: 10,
check: x => x.length === 6
check: (x) => x.match(/[0-9a-fA-F]{6}/) !== null,
},
id: 'hex'
})} `
)

return (
<ShowInfer
Expand All @@ -72,13 +70,11 @@ const view: View = {
primary: ({ model, context, prompt, update }) => {
const assistant = context('You are a helpful coloring assistant.')

const [pickPrimaryColor] = createSignal(
assistant`Color: ${prompt('pick a primary color between red, green or blue', {
const pickPrimaryColor = assistant`Color: ${prompt('pick a primary color between red, green or blue', {
sampler: model.bias.accept(sample.oneOf(['red\n', 'green\n', 'blue\n'])),
stops: ['\n'],
id: 'color'
})}`
)

return (
<ShowInfer
Expand Down
52 changes: 23 additions & 29 deletions example/vite-demo/colorgen/components/ShowInfer.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import '../../style.css'
import { Template, StreamPartial } from 'ad-llama'
import { Accessor, For, Show, createEffect, createSignal } from 'solid-js'
import { For, Show, createSignal } from 'solid-js'

export default function ShowInfer({ template, onComplete, showTemplate }: {
showTemplate?: boolean
template: Accessor<Template>
template: Template
onComplete: (refs: any) => any
}) {
const [canCancel, setCanCancel] = createSignal(true)
const [prompt, setPrompt] = createSignal('')
const [templateText, setTemplateText] = createSignal('')
const [partials, setPartials] = createSignal<StreamPartial[]>([])

createEffect(() => {
setPartials([])
template.collect_refs(partial => {
switch (partial.type) {
case 'ungen':
setPartials([...partials().slice(0, -partial.tokenCount)])
break
case 'lit':
case 'gen':
setPartials([...partials(), partial])

template().collect_refs(partial => {
switch (partial.type) {
case 'ungen':
setPartials([...partials().slice(0, -partial.tokenCount)])
break
case 'lit':
case 'gen':
setPartials([...partials(), partial])
if (partial.type === 'gen') {
setPrompt(partial.prompt)
}

if (partial.type === 'gen') {
setPrompt(partial.prompt)
}

break
case 'template':
setTemplateText(partial.content)
break
}
}).then(results => {
setCanCancel(false)
onComplete(results)
})

console.log('showinfer', template())
})
break
case 'template':
setTemplateText(partial.content)
break
}
}).then(results => {
setCanCancel(false)
onComplete(results)
})

return (
<div>
Expand All @@ -56,7 +50,7 @@ export default function ShowInfer({ template, onComplete, showTemplate }: {
</code></pre>
<Show when={canCancel()} >
<div id='controls'>
<button onClick={() => template().model.cancel()}>cancel</button>
<button onClick={() => template.model.cancel()}>cancel</button>
</div>
</Show>
</div>
Expand Down

0 comments on commit 1c96bee

Please sign in to comment.