Skip to content

Commit

Permalink
Edit api: replace fetch to requestUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeonseoNam committed Mar 24, 2023
1 parent 310a038 commit c30359f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { requestUrl } from "obsidian";
export class ChatGPT {
private static baseUrl = 'https://api.openai.com/v1/chat/completions';

Expand All @@ -12,10 +13,10 @@ export class ChatGPT {
frequency_penalty = 0,
presence_penalty = 0.5): Promise<string> {

const headers = new Headers({
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
});
};

const body = JSON.stringify({
model: 'gpt-3.5-turbo',
Expand All @@ -33,18 +34,18 @@ export class ChatGPT {
presence_penalty: presence_penalty
});

const response = await fetch(`${this.baseUrl}`, {
const response = await requestUrl({
url: this.baseUrl,
method: 'POST',
headers: headers,
body: body,
});

if (!response.ok) {
throw new Error(`API call error: ${response.statusText}`);
if (response.status >= 400) {
throw new Error(`API call error: ${response.status}`);
}

const data = await response.json();
// return data.choices[0].text.trim();
const data = JSON.parse(response.text);
return data.choices[0].message.content;
}
}

0 comments on commit c30359f

Please sign in to comment.