Skip to content

Commit

Permalink
fix: update OpenAI SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
korosuke613 committed Jan 15, 2025
1 parent dbf499f commit 3f2885d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 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
32 changes: 21 additions & 11 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 Down Expand Up @@ -33,7 +27,7 @@ export class AiReviewer {
private readonly openai: OpenAI;
private static readonly SYSTEM_PROMPT = `
あなたは日本語技術記事を校正するアシスタントです。
与えられたマークダウン形式の文章で、誤字・脱字、文法誤り、論理的に誤っている行を抜き出し、修正した行を出力してください。
与えられたマークダウン形式の文章で、誤字・脱字、文法誤り、論理的・技術的な誤りのある行を抜き出し、修正した行を出力してください。
その際、確実に修正すべき誤りのみを出力してください。
また、以下のルールに従って修正を行ってください。
- 句読点の追加や削除はしない
Expand All @@ -56,9 +50,24 @@ export class AiReviewer {
input: 2.5 / (1 * 1000000),
output: 10 / (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,
// 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 @@ -152,7 +161,7 @@ export class AiReviewer {
};

createReviewInput = (markdown: string) => {
const input: ChatCompletionCreateParamsNonStreaming = {
const input: OpenAI.ChatCompletionCreateParamsNonStreaming = {
model: this.options.model,
messages: [
{
Expand All @@ -168,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 @@ -209,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 @@ -221,6 +230,7 @@ export class AiReviewer {
AiReviewer.pricing[this.options.model].output;

return {
model: this.options.model,

Check warning on line 233 in tools/libs/AiReviewer.ts

View check run for this annotation

Codecov / codecov/patch

tools/libs/AiReviewer.ts#L233

Added line #L233 was not covered by tests
tokens: usage,
pricing: {
input: `${input.toFixed(3)} USD`,
Expand Down

0 comments on commit 3f2885d

Please sign in to comment.