-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Eric Lau <[email protected]>
- Loading branch information
Showing
28 changed files
with
615 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
dist | ||
*.tsbuildinfo | ||
node_modules | ||
|
||
|
||
.env | ||
.env.local |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
[build] | ||
command = "yarn --cwd packages/ui build" | ||
publish = "packages/ui/public" | ||
|
||
edge_functions = "packages/ui/api" | ||
|
||
[[edge_functions]] | ||
path = "/ai" | ||
function = "ai" |
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,77 @@ | ||
import OpenAI from 'https://esm.sh/[email protected]' | ||
import { OpenAIStream, StreamingTextResponse } from 'https://esm.sh/[email protected]' | ||
import { erc20Function, erc721Function, erc1155Function, governorFunction, customFunction } from '../src/wiz-functions.ts' | ||
import { Redis } from 'https://esm.sh/@upstash/[email protected]' | ||
|
||
export default async (req: Request) => { | ||
try { | ||
const data = await req.json() | ||
const apiKey = Deno.env.get('OPENAI_API_KEY') | ||
|
||
const redisUrl = Deno.env.get('REDIS_URL') | ||
const redisToken = Deno.env.get('REDIS_TOKEN') | ||
|
||
if (!redisUrl || !redisToken) { throw new Error('missing redis credentials') } | ||
|
||
const redis = new Redis({ | ||
url: redisUrl, | ||
token: redisToken, | ||
}) | ||
|
||
const openai = new OpenAI({ | ||
apiKey: apiKey | ||
}) | ||
|
||
const validatedMessages = data.messages.filter((message: { role: string, content: string }) => { | ||
return message.content.length < 500 | ||
}) | ||
|
||
const messages = [{ | ||
role: 'system', | ||
content: ` | ||
The current options are ${JSON.stringify(data.currentOpts)}. | ||
Please be kind and concise. Keep responses to <100 words. | ||
`.trim() | ||
}, ...validatedMessages] | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-4-1106-preview', | ||
messages, | ||
functions: [ | ||
erc20Function, erc721Function, erc1155Function, governorFunction, customFunction | ||
], | ||
temperature: 0.7, | ||
stream: true | ||
}) | ||
|
||
const stream = OpenAIStream(response, { | ||
async onCompletion(completion) { | ||
const id = data.chatId | ||
const updatedAt = Date.now() | ||
const payload = { | ||
id, | ||
updatedAt, | ||
messages: [ | ||
...messages, | ||
{ | ||
content: completion, | ||
role: 'assistant' | ||
} | ||
] | ||
} | ||
const exists = await redis.exists(`chat:${id}`) | ||
if (!exists) { | ||
// @ts-ignore redis types seem to require [key: string] | ||
payload.createdAt = updatedAt | ||
} | ||
await redis.hset(`chat:${id}`, payload) | ||
} | ||
}); | ||
return new StreamingTextResponse(stream); | ||
|
||
} catch (e) { | ||
return Response.json({ | ||
error: 'Could not retrieve results.' | ||
}) | ||
} | ||
} |
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
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,25 @@ | ||
<script lang="ts"> | ||
import Tooltip from './Tooltip.svelte'; | ||
export let link: string | undefined = undefined; | ||
export let align: 'right' | undefined = undefined; | ||
export let placement: 'top' | 'bottom' | 'left' | 'right' = 'right'; | ||
</script> | ||
|
||
<Tooltip let:trigger interactive placement={placement} theme="light-yellow border" maxWidth="15em"> | ||
<div | ||
use:trigger | ||
class="tooltip bg-blue-300 uppercase text-xs font-semibold px-2 py-1 rounded-md text-white hover:bg-blue-400" | ||
class:ml-auto={align === 'right'} | ||
> | ||
ALPHA | ||
</div> | ||
|
||
<div slot="content"> | ||
<slot></slot> | ||
{#if link} | ||
<br> | ||
<a target="_blank" rel="noopener noreferrer" href={link}>Read more.</a> | ||
{/if} | ||
</div> | ||
</Tooltip> |
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
Oops, something went wrong.