-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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 #847 from fuergaosi233/chatgpt-api
Merge chatgpt-api
- Loading branch information
Showing
15 changed files
with
121 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
OPENAI_API_KEY="" | ||
ENDPOINT="https://API.EXAMPLE.COM/v1" | ||
OPENAI_API_KEY="sk-XXXXXXXXXXXXXXXXX" | ||
MODEL="gpt-3.5-turbo" | ||
CHAT_PRIVATE_TRIGGER_KEYWORD= | ||
TEMPERATURE= | ||
TEMPERATURE=0.6 | ||
BLOCK_WORDS="VPN" | ||
CHATGPT_BLOCK_WORDS="VPN" | ||
WECHATY_PUPPET=wechaty-puppet-wechat |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1 +1,33 @@ | ||
import {ChatCompletionRequestMessage} from "openai"; | ||
|
||
import GPT3TokenizerImport from 'gpt3-tokenizer'; | ||
import {config} from "./config.js"; | ||
|
||
export const regexpEncode = (str: string) => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
|
||
const GPT3Tokenizer: typeof GPT3TokenizerImport = | ||
typeof GPT3TokenizerImport === 'function' | ||
? GPT3TokenizerImport | ||
: (GPT3TokenizerImport as any).default; | ||
// https://github.com/chathub-dev/chathub/blob/main/src/app/bots/chatgpt-api/usage.ts | ||
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' }) | ||
function calTokens(chatMessage:ChatCompletionRequestMessage[]):number { | ||
let count = 0 | ||
for (const msg of chatMessage) { | ||
count += countTokens(msg.content) | ||
count += countTokens(msg.role) | ||
} | ||
return count + 2 | ||
} | ||
|
||
function countTokens(str: string):number { | ||
const encoded = tokenizer.encode(str) | ||
return encoded.bpe.length | ||
} | ||
export function isTokenOverLimit(chatMessage:ChatCompletionRequestMessage[]): boolean { | ||
let limit = 4096; | ||
if (config.model==="gpt-3.5-turbo" || config.model==="gpt-3.5-turbo-0301") { | ||
limit = 4096; | ||
} | ||
return calTokens(chatMessage) > limit; | ||
} |