Skip to content

Commit

Permalink
Merge pull request #898 from korosuke613/kiba-improve-ai
Browse files Browse the repository at this point in the history
fix: update gpt-4o
  • Loading branch information
korosuke613 authored Jan 21, 2025
2 parents 9b49cb4 + 3f2885d commit 9b16177
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
6 changes: 1 addition & 5 deletions tools/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ export { matter };
export { marked } from "npm:marked@6.0.0";
export type { Token, Tokens } from "npm:marked@6.0.0";

export { OpenAI } from "https://deno.land/x/openai@v4.56.0/mod.ts";
export type {
ChatCompletionCreateParamsNonStreaming,
CompletionUsage,
} from "https://deno.land/x/openai@v4.56.0/resources/mod.ts";
export { OpenAI } from "jsr:@openai/openai";

export { join } from "https://deno.land/std@0.224.0/path/join.ts";
export { LogRecord } from "https://deno.land/std@0.224.0/log/mod.ts";
Expand Down
44 changes: 24 additions & 20 deletions tools/libs/AiReviewer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { DiagnosticResult } from "./ReviewDogJsonLine.ts";
import {
ChatCompletionCreateParamsNonStreaming,
CompletionUsage,
log,
LogRecord,
OpenAI,
} from "../deps.ts";
import { log, LogRecord, OpenAI } from "../deps.ts";

export type ReviewResult = {
review: {
Expand All @@ -32,8 +26,8 @@ export type OpenAIPricingData = {
export class AiReviewer {
private readonly openai: OpenAI;
private static readonly SYSTEM_PROMPT = `
あなたは日本語文章を校正するアシスタントです
与えられたマークダウン形式の文章で、誤字・脱字、および、文法誤りのある行を抜き出し、修正した行を出力してください。
あなたは日本語技術記事を校正するアシスタントです
与えられたマークダウン形式の文章で、誤字・脱字、文法誤り、論理的・技術的な誤りのある行を抜き出し、修正した行を出力してください。
その際、確実に修正すべき誤りのみを出力してください。
また、以下のルールに従って修正を行ってください。
- 句読点の追加や削除はしない
Expand All @@ -53,19 +47,28 @@ export class AiReviewer {
private static readonly pricing: Record<string, OpenAIPricingData> = {
// ref: https://openai.com/api/pricing/
"gpt-4o": {
input: 5 / (1 * 1000000),
output: 15 / (1 * 1000000),
input: 2.5 / (1 * 1000000),
output: 10 / (1 * 1000000),
},
"gpt-4o-2024-08-06": {
input: 5 / (1 * 1000000),
output: 15 / (1 * 1000000),
"o1-mini": {
input: 3 / (1 * 1000000),
output: 12 / (1 * 1000000),
},
"o1-preview": {
input: 15 / (1 * 1000000),
output: 60 / (1 * 1000000),
},
"o1": {
input: 15 / (1 * 1000000),
output: 60 / (1 * 1000000),
},
};
static readonly defaultOptions: AiReviewerOptions = {
max_tokens: 1024,
// GPT-4o が Structured Outputs にまだ対応してないため、最新版である gpt-4o-2024-08-06 を使用
// TODO: GPT-4o が Structured Outputs に対応したら、GPT-4o を使用する
model: "gpt-4o-2024-08-06",
// o1-mini か o1-preview が使いたいが、system role が使えない、structured outputs が使えないなどまだ制限があるため、gpt-4o を使用
// ref: https://community.openai.com/t/o1-models-do-not-support-system-role-in-chat-completion/953880/12
// ref: https://platform.openai.com/docs/guides/structured-outputs#supported-models
model: "gpt-4o",
logging: true,
reviewNum: 5,
};
Expand Down Expand Up @@ -158,7 +161,7 @@ export class AiReviewer {
};

createReviewInput = (markdown: string) => {
const input: ChatCompletionCreateParamsNonStreaming = {
const input: OpenAI.ChatCompletionCreateParamsNonStreaming = {
model: this.options.model,
messages: [
{
Expand All @@ -174,7 +177,7 @@ export class AiReviewer {
},
],
temperature: 1,
max_tokens: this.options.max_tokens,
max_completion_tokens: this.options.max_tokens,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
Expand Down Expand Up @@ -215,7 +218,7 @@ export class AiReviewer {
return response.replaceAll("```json", "").replaceAll("```", "");
};

getPricing = (usage?: CompletionUsage) => {
getPricing = (usage?: OpenAI.CompletionUsage) => {
if (usage === undefined) {
if (this.options.logging) log.warn("No usage data from OpenAI.");
return undefined;
Expand All @@ -227,6 +230,7 @@ export class AiReviewer {
AiReviewer.pricing[this.options.model].output;

return {
model: this.options.model,
tokens: usage,
pricing: {
input: `${input.toFixed(3)} USD`,
Expand Down

0 comments on commit 9b16177

Please sign in to comment.