-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add edit button with popover * Basic ai editing working * Fix Overflow * Handle AI Messages * Move AI Button to better position * Text-to-speech working * Remove unused * Show paywall * Add darkmode * Add translations * Available on mobile; on hosted
- Loading branch information
1 parent
caed853
commit 4d82716
Showing
41 changed files
with
1,362 additions
and
442 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 |
---|---|---|
|
@@ -12,4 +12,6 @@ TODO.md | |
keys | ||
ERROR.png | ||
flowchart-fun.feature-reacher.json | ||
.parcel-cache | ||
.parcel-cache | ||
|
||
speech*.mp4 |
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,70 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { z, ZodObject } from "zod"; | ||
import { openai } from "./_openai"; | ||
import zodToJsonSchema from "zod-to-json-schema"; | ||
import OpenAI from "openai"; | ||
|
||
type Schemas<T extends Record<string, ZodObject<any>>> = T; | ||
|
||
export async function llmMany<T extends Record<string, ZodObject<any>>>( | ||
content: string, | ||
schemas: Schemas<T> | ||
) { | ||
try { | ||
// if the user passes a key "message" in schemas, throw an error | ||
if (schemas.message) throw new Error("Cannot use key 'message' in schemas"); | ||
|
||
const completion = await openai.chat.completions.create({ | ||
messages: [ | ||
{ | ||
role: "user", | ||
content, | ||
}, | ||
], | ||
tools: Object.entries(schemas).map(([key, schema]) => ({ | ||
type: "function", | ||
function: { | ||
name: key, | ||
parameters: zodToJsonSchema(schema), | ||
}, | ||
})), | ||
model: "gpt-3.5-turbo-1106", | ||
// model: "gpt-4-1106-preview", | ||
}); | ||
|
||
const choice = completion.choices[0]; | ||
|
||
if (!choice) throw new Error("No choices returned"); | ||
|
||
// Must return the full thing, message and multiple tool calls | ||
return simplifyChoice(choice) as SimplifiedChoice<T>; | ||
} catch (error) { | ||
console.error(error); | ||
const message = (error as Error)?.message || "Error with prompt"; | ||
throw new Error(message); | ||
} | ||
} | ||
|
||
type SimplifiedChoice<T extends Record<string, ZodObject<any>>> = { | ||
message: string; | ||
toolCalls: Array< | ||
{ | ||
[K in keyof T]: { | ||
name: K; | ||
args: z.infer<T[K]>; | ||
}; | ||
}[keyof T] | ||
>; | ||
}; | ||
|
||
function simplifyChoice(choice: OpenAI.Chat.Completions.ChatCompletion.Choice) { | ||
return { | ||
message: choice.message.content || "", | ||
toolCalls: | ||
choice.message.tool_calls?.map((toolCall) => ({ | ||
name: toolCall.function.name, | ||
// Wish this were type-safe! | ||
args: JSON.parse(toolCall.function.arguments ?? "{}"), | ||
})) || [], | ||
}; | ||
} |
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,42 @@ | ||
import { VercelApiHandler } from "@vercel/node"; | ||
import { llmMany } from "../_lib/_llm"; | ||
import { z } from "zod"; | ||
|
||
const nodeSchema = z.object({ | ||
// id: z.string(), | ||
// classes: z.string(), | ||
label: z.string(), | ||
}); | ||
|
||
const edgeSchema = z.object({ | ||
from: z.string(), | ||
to: z.string(), | ||
label: z.string().optional().default(""), | ||
}); | ||
|
||
const graphSchema = z.object({ | ||
nodes: z.array(nodeSchema), | ||
edges: z.array(edgeSchema), | ||
}); | ||
|
||
const handler: VercelApiHandler = async (req, res) => { | ||
const { graph, prompt } = req.body; | ||
if (!graph || !prompt) { | ||
throw new Error("Missing graph or prompt"); | ||
} | ||
|
||
const result = await llmMany( | ||
`You are a one-shot AI flowchart assistant. Help the user with a flowchart or diagram. Here is the current state of the flowchart: | ||
${JSON.stringify(graph, null, 2)} | ||
Here is the user's message: | ||
${prompt}`, | ||
{ | ||
updateGraph: graphSchema, | ||
} | ||
); | ||
|
||
res.json(result); | ||
}; | ||
|
||
export default handler; |
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,27 @@ | ||
import { VercelApiHandler } from "@vercel/node"; | ||
import { openai } from "../_lib/_openai"; | ||
import { toFile } from "openai"; | ||
|
||
const handler: VercelApiHandler = async (req, res) => { | ||
try { | ||
const { audioUrl } = req.body; | ||
|
||
if (!audioUrl) { | ||
res.status(400).json({ ok: false, error: "No audioUrl provided" }); | ||
return; | ||
} | ||
|
||
const base64Data = audioUrl.split(";base64,").pop(); | ||
const binaryData = Buffer.from(base64Data, "base64"); | ||
const transcription = await openai.audio.transcriptions.create({ | ||
file: await toFile(binaryData, "audio.mp4"), | ||
model: "whisper-1", | ||
}); | ||
res.send(transcription.text); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ ok: false, error: "Something went wrong" }); | ||
} | ||
}; | ||
|
||
export default handler; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.