Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#104 from gentlementlegen/f…
Browse files Browse the repository at this point in the history
…ix/token-count

fix: token count
  • Loading branch information
gentlementlegen authored Aug 28, 2024
2 parents 6befef0 + 91f08b2 commit 9853408
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 121 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ with:
incentives:
requirePriceLabel: true
contentEvaluator:
openAi:
model: "gpt-4o"
endpoint: "https://api.openai.com/v1"
multipliers:
- role: [ISSUE_SPECIFICATION]
relevance: 1
Expand Down
15 changes: 15 additions & 0 deletions src/configuration/content-evaluator-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { Static, Type } from "@sinclair/typebox";
import { commentType } from "./formatting-evaluator-config";

const openAiType = Type.Object(
{
/**
* AI model to use for comment evaluation.
*/
model: Type.String({ default: "gpt-4o" }),
/**
* Specific endpoint to send the comments to.
*/
endpoint: Type.String({ default: "" }),
},
{ default: {} }
);

export const contentEvaluatorConfigurationType = Type.Object({
openAi: openAiType,
/**
* Multipliers applied to different types of comments
*/
Expand Down
1 change: 1 addition & 0 deletions src/configuration/formatting-evaluator-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const htmlType = Type.Record(Type.String(), Type.Number(), {
ul: 1,
td: 1,
hr: 0,
pre: 0,
},
});

Expand Down
4 changes: 2 additions & 2 deletions src/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Octokit } from "@octokit/rest";
import { retry } from "@octokit/plugin-retry";
import program from "./parser/command-line";
import configuration from "./configuration/config-reader";
import { paginateGraphQL, paginateGraphQLInterface } from "@octokit/plugin-paginate-graphql";
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql";

const customOctokit = Octokit.plugin(retry, paginateGraphQL);

let octokitInstance: (Octokit & paginateGraphQLInterface) | null = null;
let octokitInstance: InstanceType<typeof customOctokit> | null = null;

function getOctokitInstance() {
if (!octokitInstance) {
Expand Down
11 changes: 8 additions & 3 deletions src/parser/content-evaluator-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import { GithubCommentScore, Module, Result } from "./processor";
* Evaluates and rates comments.
*/
export class ContentEvaluatorModule implements Module {
readonly _openAi = new OpenAI({ apiKey: OPENAI_API_KEY });
readonly _configuration: ContentEvaluatorConfiguration | null = configuration.incentives.contentEvaluator;
readonly _openAi = new OpenAI({
apiKey: OPENAI_API_KEY,
...(this._configuration?.openAi.endpoint && { baseURL: this._configuration.openAi.endpoint }),
});
private readonly _fixedRelevances: { [k: string]: number } = {};

_getEnumValue(key: CommentType) {
Expand Down Expand Up @@ -86,7 +89,9 @@ export class ContentEvaluatorModule implements Module {
}
}

const relevancesByAI = await this._evaluateComments(specificationBody, commentsToEvaluate);
const relevancesByAI = commentsToEvaluate.length
? await this._evaluateComments(specificationBody, commentsToEvaluate)
: {};

if (Object.keys(relevancesByAI).length !== commentsToEvaluate.length) {
console.error("Relevance / Comment length mismatch! \nWill use 1 as relevance for missing comments.");
Expand Down Expand Up @@ -137,7 +142,7 @@ export class ContentEvaluatorModule implements Module {
const maxTokens = this._calculateMaxTokens(dummyResponse);

const response: OpenAI.Chat.ChatCompletion = await this._openAi.chat.completions.create({
model: "gpt-4o-2024-08-06",
model: this._configuration?.openAi.model || "gpt-4o-2024-08-06",
response_format: { type: "json_object" },
messages: [
{
Expand Down
4 changes: 4 additions & 0 deletions src/parser/github-comment-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export class GithubCommentModule implements Module {

async postComment(body: string, updateLastComment = true) {
const { eventPayload } = program;
if (!this._configuration?.post) {
logger.debug("Won't post a comment since posting is disabled.", { body });
return;
}
if (updateLastComment && this._lastCommentId !== null) {
await getOctokitInstance().issues.updateComment({
body,
Expand Down
Loading

0 comments on commit 9853408

Please sign in to comment.