Last active
October 22, 2024 20:42
-
-
Save airhorns/7a4bd768ead6ee94f7126c53253c3ebe to your computer and use it in GitHub Desktop.
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
// what would be really annoying to do | |
const result = await gpt4oMini.withConfig({callbacks: [BraintrustCallbackHandler]}).withStructuredOutput( //... | |
// what would be much better | |
export const gpt4oMini = new ChatOpenAI({ | |
model: "gpt-4o-mini", | |
callbacks: [BraintrustCallbackHandler], | |
...retryConfig, | |
}); |
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
import { AIMessage, HumanMessage } from "@langchain/core/messages"; | |
import { ChatOpenAI } from "@langchain/openai"; | |
import { traceable } from "langsmith/traceable"; | |
import { z } from "zod"; | |
import { AssistantToolCallFailure } from "../errors"; | |
import { immediateRetry } from "../utils"; | |
export const gpt4oMini = new ChatOpenAI({ | |
model: "gpt-4o-mini", | |
...retryConfig, | |
}); | |
const NameSchema = z.object({ | |
name: z | |
.string() | |
.describe( | |
"A short name to identify a web application that's being created according to these instructions" | |
), | |
slug: z | |
.string() | |
.describe( | |
"A short, URL-safe slug to identify a web application that's being created according to these instructions" | |
), | |
}); | |
export const nameForApp = traceable( | |
async (prompt: string) => { | |
const messages = [ | |
new HumanMessage( | |
`Generate a short, playful name and URL slug to identify a web application that is being created according to the instructions below. Don't name it "silly-<something>", just be whimsical, naming the app things like "manager-3000" or "appy-mc-app-face" or "tool-o-matic" or similar, but more specific to the instructions. Don't just use these jokes. The slug must be under 49 characters long, and the name should be short as well. | |
# Instructions | |
${prompt} | |
Current time: ${new Date().toISOString()}` | |
), | |
]; | |
return await immediateRetry(async (attempt) => { | |
const result = await gpt4oMini | |
.withStructuredOutput(NameSchema) | |
.withConfig({ cache: false, temperature: 0.8 } as any) | |
.invoke(messages, { runName: "nameForApp" }); | |
if (result.slug.length > 49) { | |
messages.push( | |
new AIMessage( | |
`Here's the generated name and slug:\n\n\`\`\`json\n${JSON.stringify( | |
result, | |
null, | |
2 | |
)}\n\`\`\`` | |
), | |
new HumanMessage( | |
`The slug is too long. Generate another name and slug that is shorter.` | |
) | |
); | |
throw new AssistantToolCallFailure( | |
`Slug is too long after ${attempt} attempt(s)`, | |
{ | |
result, | |
} | |
); | |
} | |
return result; | |
}); | |
}, | |
{ name: "nameForApp" } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment