diff options
author | Sophie Zhang <sophie_zhang@brown.edu> | 2023-02-22 17:35:26 -0500 |
---|---|---|
committer | Sophie Zhang <sophie_zhang@brown.edu> | 2023-02-22 17:35:26 -0500 |
commit | 4475adee0f13d9ec407aff6a47094c7ce808af0c (patch) | |
tree | 7a8bed802939042751c639db3038041e36770507 /src/client/apis/gpt/Summarization.ts | |
parent | c1e713b611f12b2070854e19e4838d6a44126c0b (diff) |
added GPT summarization functionality
Diffstat (limited to 'src/client/apis/gpt/Summarization.ts')
-rw-r--r-- | src/client/apis/gpt/Summarization.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/client/apis/gpt/Summarization.ts b/src/client/apis/gpt/Summarization.ts new file mode 100644 index 000000000..3706c7a5b --- /dev/null +++ b/src/client/apis/gpt/Summarization.ts @@ -0,0 +1,35 @@ +import { Configuration, OpenAIApi } from 'openai'; + +const gptSummarize = async (text: string) => { + try { + const configuration = new Configuration({ + apiKey: process.env.OPENAI_KEY, + }); + const openai = new OpenAIApi(configuration); + const response = await openai.createCompletion({ + model: 'text-davinci-003', + max_tokens: 256, + temperature: 0.7, + prompt: `Summarize this text in one sentence: ${text}`, + }); + return response.data.choices[0].text; + } catch (err) { + console.log(err); + return ''; + } +}; + +// Summarizing with the MeaningCloud API +const fetchSummary = async (text: string, numSentences?: number) => { + const key = '0b41c071f838e573847f477e8f69e9d9'; + const queryURL = ''; + const sentences = numSentences ? numSentences : 3; + const URL = `https://api.meaningcloud.com/summarization-1.0?key=${key}&txt=${text}&sentences=${sentences}`; + + const res = await fetch(URL); + const data = await res.json(); + console.log(data.summary); + return data.summary; +}; + +export { fetchSummary, gptSummarize }; |