Skip to content

v4 ai improvements #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 2, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use a tool to get the userId
  • Loading branch information
ericallam committed Apr 2, 2025
commit 6d90579e0eaa611a766cbc782a69d44adaecac74
26 changes: 19 additions & 7 deletions references/d3-chat/src/trigger/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,27 @@ const getUserTodos = tool({
},
});

const getUserId = tool({
description: "Use this tool to get the user_id for the current user",
parameters: z.object({}),
execute: async () => {
const userId = metadata.get("user_id");

if (!userId) {
throw new Error("No user_id found");
}

return userId;
},
});

export type TOOLS = {
queryApproval: typeof queryApproval;
executeSql: typeof executeSql;
generateId: typeof generateId;
getUserTodos: typeof getUserTodos;
crawler: typeof crawler;
getUserId: typeof getUserId;
};

export type STREAMS = {
Expand All @@ -144,6 +159,8 @@ export const todoChat = schemaTask({
userId: z.string(),
}),
run: async ({ model, input, userId }) => {
metadata.set("user_id", userId);

const system = `
You are a SQL (postgres) expert who can turn natural language descriptions for a todo app
into a SQL query which can then be executed against a SQL database. Here is the schema:
Expand All @@ -165,8 +182,6 @@ export const todoChat = schemaTask({

Only Create, Read, Update, and Delete operations are allowed.

The input will be a user_id and a prompt.

The output will be a SQL query.

If the query produced is a mutation, you will need to get approval first from an admin using the queryApproval tool.
Expand All @@ -188,11 +203,7 @@ export const todoChat = schemaTask({
If the user specifies a URL, you can use the crawler tool to crawl the URL and return the markdown, helping inform the SQL query.
`;

const prompt = `
User ${userId} has the following prompt: ${input}

Generate a SQL query to execute.
`;
const prompt = input;

const result = streamText({
model: openai(model),
Expand All @@ -205,6 +216,7 @@ export const todoChat = schemaTask({
generateId,
getUserTodos,
crawler,
getUserId,
},
experimental_telemetry: {
isEnabled: true,
Expand Down