Skip to content

Commit 2fbb423

Browse files
authored
Merge pull request #34 from database-playground/pan93412/dbp-128-降低-ai-成本
DBP-128: reduce AI cost
2 parents a8a951b + 6833e1c commit 2fbb423

File tree

5 files changed

+844
-961
lines changed

5 files changed

+844
-961
lines changed

app/api/chat/route.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { getAuthorizedUserInfo } from "@/lib/auth.rsc";
44
import { createPostHogClient } from "@/lib/posthog.rsc";
55
import { anthropic, type AnthropicProviderOptions } from "@ai-sdk/anthropic";
66
import { withTracing } from "@posthog/ai";
7-
import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
7+
import { convertToModelMessages, pruneMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
88
import { NextResponse } from "next/server";
99
import { z } from "zod";
1010

11-
export const maxDuration = 30;
12-
1311
const QUESTION_INFO = graphql(`
1412
query QuestionInfo($id: ID!) {
1513
question(id: $id) {
@@ -101,18 +99,25 @@ export async function POST(req: Request) {
10199
);
102100
}
103101

104-
const model = anthropic("claude-sonnet-4-5");
102+
const model = anthropic("claude-haiku-4-5");
105103
const posthogClient = await createPostHogClient();
106104

107105
const tracedModel = withTracing(model, posthogClient, {
108106
posthogDistinctId: userInfo.sub,
109107
});
110108

109+
const prunedMessages = pruneMessages({
110+
messages: convertToModelMessages(messages),
111+
reasoning: "none",
112+
toolCalls: "before-last-message",
113+
emptyMessages: "remove",
114+
});
115+
111116
const result = streamText({
112117
model: tracedModel,
113118
providerOptions: {
114119
anthropic: {
115-
thinking: { type: "enabled", budgetTokens: 12000 },
120+
thinking: { type: "enabled", budgetTokens: 2000 },
116121
} satisfies AnthropicProviderOptions,
117122
},
118123
messages: [
@@ -136,7 +141,7 @@ export async function POST(req: Request) {
136141
} satisfies AnthropicProviderOptions,
137142
},
138143
},
139-
...convertToModelMessages(messages),
144+
...prunedMessages,
140145
],
141146
stopWhen: stepCountIs(10),
142147
tools: {

gql/graphql.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ export type CreateGroupInput = {
4949
scopeSetIDs?: InputMaybe<Array<Scalars['ID']['input']>>;
5050
};
5151

52+
/**
53+
* CreatePointInput is used for create Point object.
54+
* Input was generated by ent.
55+
*/
56+
export type CreatePointInput = {
57+
description?: InputMaybe<Scalars['String']['input']>;
58+
grantedAt?: InputMaybe<Scalars['Time']['input']>;
59+
points?: InputMaybe<Scalars['Int']['input']>;
60+
userID: Scalars['ID']['input'];
61+
};
62+
5263
/**
5364
* CreateQuestionInput is used for create Question object.
5465
* Input was generated by ent.
@@ -388,6 +399,8 @@ export type Mutation = {
388399
createDatabase: Database;
389400
/** Create a new group. */
390401
createGroup?: Maybe<Group>;
402+
/** Create a new point record for a user. */
403+
createPoint?: Maybe<Point>;
391404
/** Create a question. */
392405
createQuestion: Question;
393406
/** Create a new scope set. */
@@ -440,6 +453,11 @@ export type MutationCreateGroupArgs = {
440453
};
441454

442455

456+
export type MutationCreatePointArgs = {
457+
input: CreatePointInput;
458+
};
459+
460+
443461
export type MutationCreateQuestionArgs = {
444462
input: CreateQuestionInput;
445463
};
@@ -897,13 +915,13 @@ export enum QuestionOrderField {
897915

898916
export type QuestionStatistics = {
899917
__typename?: 'QuestionStatistics';
900-
/** 嘗試人數 */
918+
/** Number of users who attempted */
901919
attemptedUsers: Scalars['Int']['output'];
902-
/** 答案正確的提交數 */
920+
/** Number of correct submissions */
903921
correctSubmissionCount: Scalars['Int']['output'];
904-
/** 通過的學生數 */
922+
/** Number of users who passed */
905923
passedUsers: Scalars['Int']['output'];
906-
/** 所有提交數 */
924+
/** Total number of submissions */
907925
submissionCount: Scalars['Int']['output'];
908926
};
909927

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
"format:check": "dprint check"
1515
},
1616
"dependencies": {
17-
"@ai-sdk/anthropic": "^2.0.37",
18-
"@ai-sdk/react": "^2.0.80",
19-
"@apollo/client": "4.0.7",
17+
"@ai-sdk/anthropic": "^2.0.39",
18+
"@ai-sdk/react": "^2.0.82",
19+
"@apollo/client": "4.0.8",
2020
"@apollo/client-integration-nextjs": "^0.14.1",
2121
"@bprogress/next": "^3.2.12",
2222
"@codemirror/lang-sql": "^6.10.0",
2323
"@graphql-codegen/client-preset": "^5.1.1",
2424
"@hookform/resolvers": "^5.2.2",
25-
"@posthog/ai": "^6.4.4",
25+
"@posthog/ai": "^6.5.0",
2626
"@radix-ui/react-alert-dialog": "^1.1.15",
2727
"@radix-ui/react-avatar": "^1.1.10",
2828
"@radix-ui/react-checkbox": "^1.3.3",
@@ -42,20 +42,20 @@
4242
"@tailwindcss/typography": "^0.5.19",
4343
"@tanstack/react-table": "^8.21.3",
4444
"@uiw/react-codemirror": "^4.25.2",
45-
"ai": "^5.0.80",
45+
"ai": "^5.0.82",
4646
"babel-plugin-react-compiler": "19.1.0-rc.3",
4747
"class-variance-authority": "^0.7.1",
4848
"clsx": "^2.1.1",
4949
"foxact": "^0.2.49",
5050
"graphql": "^16.11.0",
5151
"lucide-react": "^0.548.0",
52-
"next": "16.0.1-canary.2",
52+
"next": "16.0.2-canary.0",
5353
"next-themes": "^0.4.6",
54-
"posthog-js": "1.280.1",
55-
"posthog-node": "^5.10.3",
56-
"react": "19.3.0-canary-408b38ef-20251023",
54+
"posthog-js": "1.282.0",
55+
"posthog-node": "^5.10.4",
56+
"react": "19.3.0-canary-4f931700-20251029",
5757
"react-codemirror-merge": "^4.25.2",
58-
"react-dom": "19.3.0-canary-408b38ef-20251023",
58+
"react-dom": "19.3.0-canary-4f931700-20251029",
5959
"react-hook-form": "^7.65.0",
6060
"react-remark": "^2.1.0",
6161
"remark": "^15.0.1",
@@ -75,13 +75,13 @@
7575
"@graphql-typed-document-node/core": "^3.2.0",
7676
"@parcel/watcher": "^2.5.1",
7777
"@tailwindcss/postcss": "^4.1.16",
78-
"@types/node": "^24.9.1",
78+
"@types/node": "^24.9.2",
7979
"@types/react": "^19.2.2",
8080
"@types/react-dom": "^19.2.2",
8181
"@typescript-eslint/parser": "^8.46.2",
8282
"dprint": "^0.50.2",
8383
"eslint": "^9.38.0",
84-
"eslint-config-next": "16.0.1-canary.2",
84+
"eslint-config-next": "16.0.2-canary.0",
8585
"eslint-plugin-better-tailwindcss": "^3.7.10",
8686
"tailwindcss": "^4.1.16",
8787
"tw-animate-css": "^1.4.0",

0 commit comments

Comments
 (0)