Skip to content

Instantly share code, notes, and snippets.

@airhorns
Last active October 22, 2024 20:42
Show Gist options
  • Save airhorns/7a4bd768ead6ee94f7126c53253c3ebe to your computer and use it in GitHub Desktop.
Save airhorns/7a4bd768ead6ee94f7126c53253c3ebe to your computer and use it in GitHub Desktop.
// 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,
});
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