Skip to content

Commit

Permalink
文字数制限
Browse files Browse the repository at this point in the history
  • Loading branch information
eatski committed Aug 13, 2024
1 parent 584140f commit 97fbb12
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libs/openai/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ToUnionWithField } from "@/common/util/type";
import { OpenAI } from "openai";
import {
ChatCompletionCreateParamsNonStreaming,
ChatCompletionMessageParam,
EmbeddingCreateParams,
} from "openai/resources";
const openai = new OpenAI({
Expand All @@ -13,6 +15,16 @@ const openai = new OpenAI({
export const createOpenAICompletion = async (
config: ChatCompletionCreateParamsNonStreaming,
) => {
// いずれかのmessageの中身が1000文字を超えた場合、エラーを返す。
const tooLong = config.messages
.filter(
(m: ToUnionWithField<ChatCompletionMessageParam, "content">) =>
m.content !== null && m.content !== undefined,
)
.find((m) => m.content.length > 500);
if (tooLong) {
throw new Error(`message content is too long(${tooLong.content.length})`);
}
return openai.chat.completions.create(config);
};

Expand Down

0 comments on commit 97fbb12

Please sign in to comment.