Skip to content

Commit 4baf5c3

Browse files
committed
Backup to anthropic
1 parent 1a02bcb commit 4baf5c3

File tree

3 files changed

+61
-48
lines changed

3 files changed

+61
-48
lines changed

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/d3-chat/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"db:migrate:down": "tsx -r dotenv/config src/lib/migrate.ts down"
1919
},
2020
"dependencies": {
21+
"@ai-sdk/anthropic": "^1.2.4",
2122
"@ai-sdk/openai": "1.3.3",
2223
"@e2b/code-interpreter": "^1.1.0",
2324
"@radix-ui/react-avatar": "^1.1.3",

references/d3-chat/src/trigger/chat.ts

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { openai } from "@ai-sdk/openai";
2-
import { logger, metadata, schemaTask, wait } from "@trigger.dev/sdk/v3";
2+
import { anthropic } from "@ai-sdk/anthropic";
3+
import { python } from "@trigger.dev/python";
4+
import { ai } from "@trigger.dev/sdk/ai";
5+
import { metadata, schemaTask, wait } from "@trigger.dev/sdk/v3";
36
import { sql } from "@vercel/postgres";
4-
import { streamText, TextStreamPart } from "ai";
7+
import { streamText, TextStreamPart, tool } from "ai";
58
import { nanoid } from "nanoid";
69
import { z } from "zod";
7-
import { QueryApproval } from "./schemas";
8-
import { tool } from "ai";
9-
import { ai } from "@trigger.dev/sdk/ai";
10-
import { python } from "@trigger.dev/python";
1110
import { sendSQLApprovalMessage } from "../lib/slack";
1211
import { chartTool } from "./sandbox";
12+
import { QueryApproval } from "./schemas";
1313

1414
const crawlerTask = schemaTask({
1515
id: "crawler",
@@ -35,23 +35,11 @@ const queryApprovalTask = schemaTask({
3535
query: z.string().describe("The SQL query to execute"),
3636
}),
3737
run: async ({ userId, input, query }) => {
38-
const toolOptions = ai.currentToolOptions();
39-
40-
if (toolOptions) {
41-
logger.info("tool options", {
42-
toolOptions,
43-
});
44-
}
45-
4638
const token = await wait.createToken({
4739
tags: [`user:${userId}`, "approval"],
4840
timeout: "5m", // timeout in 5 minutes
4941
});
5042

51-
logger.info("waiting for approval", {
52-
query,
53-
});
54-
5543
await sendSQLApprovalMessage({
5644
query,
5745
userId,
@@ -62,24 +50,13 @@ const queryApprovalTask = schemaTask({
6250

6351
const result = await wait.forToken<QueryApproval>(token);
6452

53+
// result.ok === false if the token timed out
6554
if (!result.ok) {
6655
return {
6756
approved: false,
6857
};
6958
} else {
70-
if (result.output.approved) {
71-
logger.info("query approved", {
72-
query,
73-
});
74-
} else {
75-
logger.warn("query denied", {
76-
query,
77-
});
78-
}
79-
80-
return {
81-
approved: result.output.approved,
82-
};
59+
return result.output;
8360
}
8461
},
8562
});
@@ -135,33 +112,18 @@ const getUserId = tool({
135112
},
136113
});
137114

138-
export type TOOLS = {
139-
queryApproval: typeof queryApproval;
140-
executeSql: typeof executeSql;
141-
generateId: typeof generateId;
142-
getUserTodos: typeof getUserTodos;
143-
crawler: typeof crawler;
144-
getUserId: typeof getUserId;
145-
chart: typeof chartTool;
146-
};
147-
148-
export type STREAMS = {
149-
fullStream: TextStreamPart<TOOLS>;
150-
};
151-
152115
export const todoChat = schemaTask({
153116
id: "todo-chat",
154117
description: "Chat with the todo app",
155118
schema: z.object({
156-
model: z.string().default("gpt-4o"),
157119
input: z
158120
.string()
159121
.describe(
160122
"The input to chat with the todo app. Will be a request to read or update the todo list."
161123
),
162124
userId: z.string(),
163125
}),
164-
run: async ({ model, input, userId }) => {
126+
run: async ({ input, userId }) => {
165127
metadata.set("user_id", userId);
166128

167129
const system = `
@@ -209,7 +171,7 @@ export const todoChat = schemaTask({
209171
const prompt = input;
210172

211173
const result = streamText({
212-
model: openai(model),
174+
model: getModel(),
213175
system,
214176
prompt,
215177
maxSteps: 10,
@@ -240,3 +202,27 @@ export const todoChat = schemaTask({
240202
return textParts.join("");
241203
},
242204
});
205+
206+
export type TOOLS = {
207+
queryApproval: typeof queryApproval;
208+
executeSql: typeof executeSql;
209+
generateId: typeof generateId;
210+
getUserTodos: typeof getUserTodos;
211+
crawler: typeof crawler;
212+
getUserId: typeof getUserId;
213+
chart: typeof chartTool;
214+
};
215+
216+
export type STREAMS = {
217+
fullStream: TextStreamPart<TOOLS>;
218+
};
219+
220+
const CHAT_PROVIDER: "openai" | "anthropic" = "openai";
221+
222+
function getModel() {
223+
if (CHAT_PROVIDER === "openai") {
224+
return openai("gpt-4o");
225+
} else {
226+
return anthropic("claude-3-5-sonnet-latest");
227+
}
228+
}

0 commit comments

Comments
 (0)