From e7bb876935e8db5717694287392fa09c48e0c96f Mon Sep 17 00:00:00 2001 From: hagaitski Date: Mon, 12 Aug 2024 17:08:10 +0900 Subject: [PATCH] =?UTF-8?q?proura=E8=84=B1=E5=8D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libs/proura/index.test.ts | 45 ---------------------- src/libs/proura/index.ts | 59 ----------------------------- src/server/services/answer/index.ts | 53 +++++++++++++------------- 3 files changed, 26 insertions(+), 131 deletions(-) delete mode 100644 src/libs/proura/index.test.ts delete mode 100644 src/libs/proura/index.ts diff --git a/src/libs/proura/index.test.ts b/src/libs/proura/index.test.ts deleted file mode 100644 index 15adcca1..00000000 --- a/src/libs/proura/index.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { setTimeout } from "node:timers/promises"; -import { describe, expect, test, vi } from "vitest"; -import { prepareProura } from "."; - -describe("proura", () => { - test("proura", async () => { - const fn = vi.fn(); - const procs = prepareProura() - .add("a", async () => { - fn("a"); - await setTimeout(100); - return 1; - }) - .add("b", async (dependsOn) => { - const a: number = await dependsOn("a"); - fn("b"); - return a + 1; - }) - .add("c", async (dependsOn) => { - const b: number = await dependsOn("b"); - fn("c"); - return b + 1; - }) - .add("d", async () => { - fn("d"); - }); - await expect(procs.exec()).resolves.toEqual({ a: 1, b: 2, c: 3 }); - expect(fn.mock.calls).toMatchInlineSnapshot(` - [ - [ - "a", - ], - [ - "d", - ], - [ - "b", - ], - [ - "c", - ], - ] - `); - }); -}); diff --git a/src/libs/proura/index.ts b/src/libs/proura/index.ts deleted file mode 100644 index 3cbc3ed9..00000000 --- a/src/libs/proura/index.ts +++ /dev/null @@ -1,59 +0,0 @@ -export const prepareProura = (): Proura<{}> => { - return prepareInner({}); -}; - -type FunctionMap

= { - [K in keyof P]: (dependsOn: DependsOn

) => Promise; -}; - -const prepareInner =

(map: FunctionMap

): Proura

=> { - return { - add: ( - name: K, - fn: (dependsOn: DependsOn

) => Promise | T, - ) => { - return prepareInner

>( - //@ts-expect-error - { - ...map, - [name]: fn, - }, - ); - }, - exec: async () => { - const promises = {} as { - [K in keyof P]?: Promise; - }; - const results = {} as { - [K in keyof P]?: P[K]; - }; - const dependsOn = (key: K): Promise => { - const promise = promises[key]; - if (!promise) { - throw new Error(`dependsOn: ${key.toString()} is not defined`); - } - return promise; - }; - - await Promise.all( - Object.entries(map).map(async ([key, fn]) => { - const promise = (fn as (dependsOn: any) => Promise)(dependsOn); - promises[key as keyof P] = promise; - const result = await promise; - results[key as keyof P] = result; - }), - ); - return results as P; - }, - }; -}; - -type DependsOn

= (key: K) => Promise; - -type Proura

= { - add: ( - name: K, - fn: (dependsOn: DependsOn

) => Promise | T, - ) => Proura

>; - exec: () => Promise

; -}; diff --git a/src/server/services/answer/index.ts b/src/server/services/answer/index.ts index 6891c152..5aaeba5c 100644 --- a/src/server/services/answer/index.ts +++ b/src/server/services/answer/index.ts @@ -4,20 +4,16 @@ import { calculateEuclideanDistance, } from "../../../libs/math"; import { openai } from "../../../libs/openai"; -import { prepareProura } from "../../../libs/proura"; import { Story } from "../../../server/model/story"; import { createPrompt } from "./createPrompt"; export const checkAnswer = async (answer: string, story: Story) => { - const proura = prepareProura(); - return proura - .add("distance", async () => { - const { - data: [textA, textB], - } = await openai.embeddings.create({ - model: "text-embedding-ada-002", - input: [story.simpleTruth, answer], - }); + const distance = openai.embeddings + .create({ + model: "text-embedding-ada-002", + input: [story.simpleTruth, answer], + }) + .then(({ data: [textA, textB] }) => { if (!textA || !textB) { return FALLBACK_DISTANCE; } @@ -26,21 +22,20 @@ export const checkAnswer = async (answer: string, story: Story) => { textB.embedding, ); return Math.round(distanceVal * 100) / 100; - }) - .add("isCorrect", async () => { - const systemPrompt = await createPrompt(story.simpleTruth); - const { content } = await createMessage({ - model: "claude-3-opus-20240229", - system: systemPrompt, - messages: [ - { - role: "user", - content: answer, - }, - ], - temperature: 0.0, - max_tokens: 8, - }); + }); + const isCorrect = createPrompt(story.simpleTruth).then((systemPrompt) => + createMessage({ + model: "claude-3-opus-20240229", + system: systemPrompt, + messages: [ + { + role: "user", + content: answer, + }, + ], + temperature: 0.0, + max_tokens: 8, + }).then(({ content }) => { const text = content[0]?.type === "text" ? content[0].text : null; const result = (["Correct", "Incorrect"] as const).find((word) => text?.includes(word), @@ -49,6 +44,10 @@ export const checkAnswer = async (answer: string, story: Story) => { throw new Error(`Unexpected response from Claude: ${content}`); } return result === "Correct"; - }) - .exec(); + }), + ); + return { + isCorrect: await isCorrect, + distance: await distance, + }; };