Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 575bed8

Browse files
committed
set limits on tokens sent in extra messages
1 parent 7c8a97b commit 575bed8

File tree

3 files changed

+95
-60
lines changed

3 files changed

+95
-60
lines changed

dist/index.js

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

src/bot.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export type Ids = {
1818
export class Bot {
1919
private bot: ChatGPTUnofficialProxyAPI | null = null // free
2020
private turbo: ChatGPTAPI | null = null // not free
21-
private MAX_PATCH_COUNT: number = 4000
2221

2322
private options: Options
2423

@@ -67,12 +66,6 @@ export class Bot {
6766
if (!message) {
6867
return ['', {}]
6968
}
70-
if (message.length > this.MAX_PATCH_COUNT) {
71-
core.warning(
72-
`Message is too long, truncate to ${this.MAX_PATCH_COUNT} tokens`
73-
)
74-
message = message.substring(0, this.MAX_PATCH_COUNT)
75-
}
7669
if (this.options.debug) {
7770
core.info(`sending to chatgpt: ${message}`)
7871
}

src/review.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3+
import {get_encoding} from '@dqbd/tiktoken'
34
import {Octokit} from '@octokit/action'
45
import {Bot} from './bot.js'
56
import {Commenter} from './commenter.js'
67
import {Inputs, Options, Prompts} from './options.js'
78

9+
// TODO: make this configurable
10+
const tokenizer = get_encoding('cl100k_base')
11+
812
const token = core.getInput('token')
913
? core.getInput('token')
1014
: process.env.GITHUB_TOKEN
1115
const octokit = new Octokit({auth: `token ${token}`})
1216
const context = github.context
1317
const repo = context.repo
1418

19+
const MAX_TOKENS_FOR_EXTRA_CONTENT = 2500
20+
1521
export const codeReview = async (
1622
bot: Bot,
1723
options: Options,
@@ -127,40 +133,54 @@ export const codeReview = async (
127133
// reset chat session for each file while reviewing
128134
next_review_ids = review_begin_ids
129135

130-
if (file_content.length > 0 && file_content.length < 3000) {
131-
// review file
132-
const [resp, review_file_ids] = await bot.chat(
133-
prompts.render_review_file(inputs),
134-
next_review_ids
135-
)
136-
if (!resp) {
137-
core.info('review: nothing obtained from chatgpt')
136+
if (file_content.length > 0) {
137+
const file_content_tokens = await get_token_count(file_content)
138+
if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
139+
// review file
140+
const [resp, review_file_ids] = await bot.chat(
141+
prompts.render_review_file(inputs),
142+
next_review_ids
143+
)
144+
if (!resp) {
145+
core.info('review: nothing obtained from chatgpt')
146+
} else {
147+
next_review_ids = review_file_ids
148+
}
138149
} else {
139-
next_review_ids = review_file_ids
150+
core.info(
151+
`skip sending content of file: ${inputs.filename} due to token count: ${file_content_tokens}`
152+
)
140153
}
141154
}
142155

143-
if (file_diff.length > 0 && file_diff.length < 3000) {
144-
// review diff
145-
const [resp, review_diff_ids] = await bot.chat(
146-
prompts.render_review_file_diff(inputs),
147-
next_review_ids
148-
)
149-
if (!resp) {
150-
core.info('review: nothing obtained from chatgpt')
151-
} else {
152-
next_review_ids = review_diff_ids
153-
}
156+
if (file_diff.length > 0) {
157+
const file_diff_tokens = await get_token_count(file_diff)
158+
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
159+
// review diff
160+
const [resp, review_diff_ids] = await bot.chat(
161+
prompts.render_review_file_diff(inputs),
162+
next_review_ids
163+
)
164+
if (!resp) {
165+
core.info('review: nothing obtained from chatgpt')
166+
} else {
167+
next_review_ids = review_diff_ids
168+
}
154169

155-
// summarize diff
156-
const [summarize_resp, summarize_diff_ids] = await bot.chat(
157-
prompts.render_summarize_file_diff(inputs),
158-
next_summarize_ids
159-
)
160-
if (!summarize_resp) {
161-
core.info('summarize: nothing obtained from chatgpt')
170+
// summarize diff
171+
const [summarize_resp, summarize_diff_ids] = await bot.chat(
172+
prompts.render_summarize_file_diff(inputs),
173+
next_summarize_ids
174+
)
175+
if (!summarize_resp) {
176+
core.info('summarize: nothing obtained from chatgpt')
177+
} else {
178+
next_summarize_ids = summarize_diff_ids
179+
}
162180
} else {
163-
next_summarize_ids = summarize_diff_ids
181+
core.info(
182+
`skip sending diff of file: ${inputs.filename} due to token count: ${file_diff_tokens}`
183+
)
164184
}
165185
}
166186

@@ -308,3 +328,8 @@ const patch_comment_line = (patch: string): number => {
308328
return -1
309329
}
310330
}
331+
332+
const get_token_count = async (text: string): Promise<number> => {
333+
text = text.replace(/<\|endoftext\|>/g, '')
334+
return tokenizer.encode(text).length
335+
}

0 commit comments

Comments
 (0)