Skip to content

Commit

Permalink
Refactor OpenAIAPI response format and update vocabulary service to h…
Browse files Browse the repository at this point in the history
…andle JSON output
  • Loading branch information
chanwit-y committed Nov 24, 2024
1 parent a0f5601 commit ac1cd51
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"zod-validation-error": "npm:zod-validation-error@^3.4.0"
},
"tasks": {
"start": "deno run --allow-env --allow-net --allow-write main.ts",
"dev": "deno run --allow-env --allow-net --allow-write main.ts",
"watch": "deno run --allow-net --watch main.ts"
},
"compilerOptions": {
Expand Down
6 changes: 3 additions & 3 deletions libs/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class OpenAIAPI {
role: "user",
content: [
{
text: `ตอบกลับเป็น json\n{\n "word": "${word}",\n "thai": "...", //แปลคำว่า ${word} เป็นภาษาไทยของคำว่า \n "english": "...", //คำอ่านและการออกเสียง obse${word}ssion เป็นภาษาไทย\n "type": "...", //ประเภทของคำของคำว่า ${word}\n "examples": [...], //ตัวอย่างการใช้คำว่า ${word} ในประเภทคำต่างๆ\n}`,
type: "text",
text: `คำว่า ${word} \n- แปลเป็นภาษาไทย\n- คำอ่านในภาษาอังกฤษ\n- ประเภทของคำ \n- ยกตัวอย่างประโยชเป็นภาษาอังกฤษ`,
},
],
},
Expand All @@ -32,7 +32,7 @@ export class OpenAIAPI {
frequency_penalty: 0,
presence_penalty: 0,
response_format: {
type: "text",
type: "json_object",
},
});
}
Expand All @@ -44,7 +44,7 @@ export class OpenAIAPI {
input: text,
});

return res
return res;
// console.log(mp3);
}
}
15 changes: 4 additions & 11 deletions libs/mod/vocabulary/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,12 @@ export class VocabularyService implements IVocabularyService {
console.log(`call openai: ${word}`);
const res = await this._openai.translate(word);
const content = res.choices[0].message.content ?? "";
const { thai, english, example, type, remark } = transform(content);
// console.log("==============")
// console.log(content)
// // console.log(data);
// console.log("==============")
console.log(content);

vocabulary = await this._repo.insert({
word,
...JSON.parse(content),
content,
remark,
thai,
english,
type,
example,
remark: "-",
});
}
return vocabulary;
Expand Down

0 comments on commit ac1cd51

Please sign in to comment.