Skip to content

Commit

Permalink
Revert "Rework prompt and use schema."
Browse files Browse the repository at this point in the history
This reverts commit ba14ac3.
  • Loading branch information
evgenius1424 committed Oct 28, 2024
1 parent ba14ac3 commit 9a78f78
Showing 1 changed file with 19 additions and 44 deletions.
63 changes: 19 additions & 44 deletions apps/learnbefore-bff/src/get-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,21 @@ import { parse } from "best-effort-json-parser"
import OpenAI from "openai"
import { Word, wordSchema } from "../types"

const wordsSchema = {
name: "word_list_response",
strict: true,
schema: {
type: "object",
properties: {
words: {
type: "array",
items: {
type: "object",
properties: {
word: {
type: "string",
},
meaning: {
type: "string",
},
translation: {
type: "string",
},
languageCode: {
type: "string",
},
},
required: ["word", "meaning", "languageCode", "translation"],
additionalProperties: false,
},
},
},
additionalProperties: false,
required: ["words"],
},
}

export async function* getWords(
openAI: OpenAI,
text: string,
): AsyncGenerator<Word> {
let data = ""
for await (const part of await openAI.chat.completions.create({
model: "gpt-4o",
model: "gpt-3.5-turbo",
stream: true,
max_tokens: 4096,
response_format: {
type: "json_schema",
json_schema: wordsSchema,
},
response_format: { type: "json_object" },
messages: [
{ role: "system", content: getSystemPrompt() },
{ role: "system", content: systemPrompt },
{
role: "user",
content: text,
content: getUserPrompt(text),
},
],
})) {
Expand Down Expand Up @@ -82,14 +45,26 @@ export async function* getWords(
}
}

function getSystemPrompt(translationLanguage = "Russian") {
return `Extract up to 40 more complex words from text and convert them to dictionary form: (infinitive, singular). Translation language is ${translationLanguage}. Example of list of words in JSON:
const systemPrompt =
"Use only RFC8259 compliant compact JSON and help to extract big list of words from the text that the language learner is unlikely to know or that are crucial to the understanding of the text. Words should be converted to dictionary form. Duplicates, names of characters, persons or toponyms are not allowed." +
"Words that do not exist in the text are not allowed. Returns an empty response if the text contains no words."

function getUserPrompt(text: string, translationLanguage = "Russian") {
return `
You must extract 40 words from the text below which language learner likely do not know or need to know in order to understand the text.
Please ensure the extracted words are diverse and relevant to the context of the text.
Translation language is ${translationLanguage}.
Example of list of words in JSON:
{
words: [
"word": "Hello", // The word itself.
"meaning": "A greeting or expression of goodwill.", // The definition or meaning of the word.
"translation": "Здравствуйте", // Translation of the word.
"languageCode": "en", // ISO 639 Language code indicating the language of the word (e.g., "en" for English).
]
}`
}
Text: ${text}`.trim()
}

0 comments on commit 9a78f78

Please sign in to comment.