-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jmilldotdev/chatgpt
Chatgpt
- Loading branch information
Showing
10 changed files
with
352 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { request, RequestParam } from "obsidian"; | ||
import { pythonifyKeys } from "src/util"; | ||
|
||
export enum ChatGPTModelType { | ||
Default = "gpt-3.5-turbo", | ||
} | ||
|
||
export type ChatRole = "user" | "system" | "assistant"; | ||
|
||
export interface ChatMessage { | ||
role: ChatRole; | ||
content: string; | ||
} | ||
|
||
export interface ChatGPTSettings { | ||
modelType: ChatGPTModelType; | ||
systemMessage: string; | ||
maxTokens: number; | ||
temperature: number; | ||
topP: number; | ||
presencePenalty: number; | ||
frequencyPenalty: number; | ||
stop: string[]; | ||
} | ||
|
||
export const defaultChatGPTSettings: ChatGPTSettings = { | ||
modelType: ChatGPTModelType.Default, | ||
systemMessage: | ||
"You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.", | ||
maxTokens: 200, | ||
temperature: 1.0, | ||
topP: 1.0, | ||
presencePenalty: 0, | ||
frequencyPenalty: 0, | ||
stop: [], | ||
}; | ||
|
||
export const getChatGPTCompletion = async ( | ||
apiKey: string, | ||
messages: ChatMessage[], | ||
settings: ChatGPTSettings, | ||
suffix?: string | ||
): Promise<string> => { | ||
const apiUrl = `https://api.openai.com/v1/chat/completions`; | ||
const headers = { | ||
Authorization: `Bearer ${apiKey}`, | ||
"Content-Type": "application/json", | ||
}; | ||
const { modelType, systemMessage, ...params } = settings; | ||
let body = { | ||
messages, | ||
model: modelType, | ||
...pythonifyKeys(params), | ||
stop: settings.stop.length > 0 ? settings.stop : undefined, | ||
suffix: suffix ? suffix : undefined, | ||
}; | ||
const requestParam: RequestParam = { | ||
url: apiUrl, | ||
method: "POST", | ||
contentType: "application/json", | ||
body: JSON.stringify(body), | ||
headers, | ||
}; | ||
const res: any = await request(requestParam) | ||
.then((response) => { | ||
return JSON.parse(response); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
return res?.choices?.[0]?.message?.content ?? null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.