Skip to content

Commit 31cb560

Browse files
Merge pull request ubiquity-os-marketplace#87 from gentlementlegen/fix/truncated-response
fix: increased max_tokens to avoid truncating responses
2 parents 62bc83e + a39fa23 commit 31cb560

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
"decimal.js": "10.4.3",
3939
"dotenv": "16.4.5",
4040
"ethers": "^6.13.0",
41+
"js-tiktoken": "1.0.14",
4142
"jsdom": "24.0.0",
4243
"lodash": "4.17.21",
4344
"markdown-it": "14.1.0",
44-
"openai": "4.29.1",
45+
"openai": "4.56.0",
4546
"tsx": "4.7.1",
4647
"typebox-validators": "0.3.5",
4748
"yaml": "2.4.1"

src/parser/content-evaluator-module.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import { Value } from "@sinclair/typebox/value";
12
import Decimal from "decimal.js";
3+
import { encodingForModel, Tiktoken } from "js-tiktoken";
24
import OpenAI from "openai";
5+
import { commentEnum, CommentType } from "../configuration/comment-types";
36
import configuration from "../configuration/config-reader";
47
import { OPENAI_API_KEY } from "../configuration/constants";
58
import {
69
ContentEvaluatorConfiguration,
710
contentEvaluatorConfigurationType,
811
} from "../configuration/content-evaluator-config";
9-
import { IssueActivity } from "../issue-activity";
10-
import { GithubCommentScore, Module, Result } from "./processor";
11-
import { Value } from "@sinclair/typebox/value";
12-
import { commentEnum, CommentType } from "../configuration/comment-types";
1312
import logger from "../helpers/logger";
13+
import { IssueActivity } from "../issue-activity";
1414
import openAiRelevanceResponseSchema, { RelevancesByOpenAi } from "../types/openai-type";
15+
import { GithubCommentScore, Module, Result } from "./processor";
1516

1617
/**
1718
* Evaluates and rates comments.
@@ -112,30 +113,47 @@ export class ContentEvaluatorModule implements Module {
112113
return commentsWithScore;
113114
}
114115

116+
/**
117+
* Will try to predict the maximum of tokens expected, to a maximum of totalTokenLimit.
118+
*/
119+
_calculateMaxTokens(prompt: string, totalTokenLimit: number = 16384) {
120+
const tokenizer: Tiktoken = encodingForModel("gpt-4o-2024-08-06");
121+
const inputTokens = tokenizer.encode(prompt).length;
122+
return Math.min(inputTokens, totalTokenLimit);
123+
}
124+
125+
_generateDummyResponse(comments: { id: number; comment: string }[]) {
126+
return comments.reduce<Record<string, number>>((acc, curr) => {
127+
return { ...acc, [curr.id]: 0.5 };
128+
}, {});
129+
}
130+
115131
async _evaluateComments(
116132
specification: string,
117133
comments: { id: number; comment: string }[]
118134
): Promise<RelevancesByOpenAi> {
119135
const prompt = this._generatePrompt(specification, comments);
136+
const dummyResponse = JSON.stringify(this._generateDummyResponse(comments), null, 2);
137+
const maxTokens = this._calculateMaxTokens(dummyResponse);
120138

121139
const response: OpenAI.Chat.ChatCompletion = await this._openAi.chat.completions.create({
122-
model: "gpt-4o",
140+
model: "gpt-4o-2024-08-06",
123141
response_format: { type: "json_object" },
124142
messages: [
125143
{
126144
role: "system",
127145
content: prompt,
128146
},
129147
],
130-
temperature: 1,
131-
max_tokens: 128,
148+
max_tokens: maxTokens,
132149
top_p: 1,
150+
temperature: 1,
133151
frequency_penalty: 0,
134152
presence_penalty: 0,
135153
});
136154

137155
const rawResponse = String(response.choices[0].message.content);
138-
logger.info(`OpenAI raw response: ${rawResponse}`);
156+
logger.info(`OpenAI raw response (using max_tokens: ${maxTokens}): ${rawResponse}`);
139157

140158
const jsonResponse = JSON.parse(rawResponse);
141159

yarn.lock

+16-19
Original file line numberDiff line numberDiff line change
@@ -3700,10 +3700,10 @@ balanced-match@^1.0.0:
37003700
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
37013701
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
37023702

3703-
base-64@^0.1.0:
3704-
version "0.1.0"
3705-
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
3706-
integrity sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==
3703+
base64-js@^1.5.1:
3704+
version "1.5.1"
3705+
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
3706+
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
37073707

37083708
37093709
version "1.1.4"
@@ -4456,14 +4456,6 @@ diff@^4.0.1:
44564456
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
44574457
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
44584458

4459-
digest-fetch@^1.3.0:
4460-
version "1.3.0"
4461-
resolved "https://registry.yarnpkg.com/digest-fetch/-/digest-fetch-1.3.0.tgz#898e69264d00012a23cf26e8a3e40320143fc661"
4462-
integrity sha512-CGJuv6iKNM7QyZlM2T3sPAdZWd/p9zQiRNS9G+9COUCwzWFTs0Xp8NF5iePx7wtvhDykReiRRrSeNb4oMmB8lA==
4463-
dependencies:
4464-
base-64 "^0.1.0"
4465-
md5 "^2.3.0"
4466-
44674459
dir-glob@^3.0.1:
44684460
version "3.0.1"
44694461
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
@@ -6284,6 +6276,13 @@ [email protected]:
62846276
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
62856277
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==
62866278

6279+
6280+
version "1.0.14"
6281+
resolved "https://registry.yarnpkg.com/js-tiktoken/-/js-tiktoken-1.0.14.tgz#756f353262d559da16b58b5bcecfd93330076da2"
6282+
integrity sha512-Pk3l3WOgM9joguZY2k52+jH82RtABRgB5RdGFZNUGbOKGMVlNmafcPA3b0ITcCZPu1L9UclP1tne6aw7ZI4Myg==
6283+
dependencies:
6284+
base64-js "^1.5.1"
6285+
62876286
js-tokens@^4.0.0:
62886287
version "4.0.0"
62896288
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
@@ -7159,20 +7158,18 @@ onetime@^6.0.0:
71597158
dependencies:
71607159
mimic-fn "^4.0.0"
71617160

7162-
openai@4.29.1:
7163-
version "4.29.1"
7164-
resolved "https://registry.yarnpkg.com/openai/-/openai-4.29.1.tgz#89d981f8ced9d1d0db2e09ca1b16b0d7775dcf36"
7165-
integrity sha512-vvKRIgB4/7w48PGVbeR8OceH/PT6fRo4sTIjRC7+y7WoK7by1R0cXs2SZRx4KsEh0ZB8J0eqdVIdRgs8XzeoEg==
7161+
openai@4.56.0:
7162+
version "4.56.0"
7163+
resolved "https://registry.yarnpkg.com/openai/-/openai-4.56.0.tgz#07d3982544cabd5781127288a8dfcceb7319a4cf"
7164+
integrity sha512-zcag97+3bG890MNNa0DQD9dGmmTWL8unJdNkulZzWRXrl+QeD+YkBI4H58rJcwErxqGK6a0jVPZ4ReJjhDGcmw==
71667165
dependencies:
71677166
"@types/node" "^18.11.18"
71687167
"@types/node-fetch" "^2.6.4"
71697168
abort-controller "^3.0.0"
71707169
agentkeepalive "^4.2.1"
7171-
digest-fetch "^1.3.0"
71727170
form-data-encoder "1.7.2"
71737171
formdata-node "^4.3.2"
71747172
node-fetch "^2.6.7"
7175-
web-streams-polyfill "^3.2.1"
71767173

71777174
optionator@^0.9.3:
71787175
version "0.9.4"
@@ -8677,7 +8674,7 @@ [email protected]:
86778674
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38"
86788675
integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==
86798676

8680-
web-streams-polyfill@^3.0.3, web-streams-polyfill@^3.2.1:
8677+
web-streams-polyfill@^3.0.3:
86818678
version "3.3.3"
86828679
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b"
86838680
integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==

0 commit comments

Comments
 (0)