Skip to content

Commit

Permalink
feat: auto shrink content to 3600 token
Browse files Browse the repository at this point in the history
  • Loading branch information
chouch0u committed Apr 11, 2023
1 parent b81c221 commit f581a72
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@plasmohq/messaging": "0.1.6",
"@plasmohq/storage": "1.2.3",
"copy-to-clipboard": "3.3.3",
"gpt-3-encoder": "1.1.4",
"plasmo": "0.67.3",
"rc-dialog": "9.1.0",
"rc-tooltip": "6.0.1",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/components/with-ai-context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {createContext, useReducer} from 'react'
import {sendToContentScript} from '@plasmohq/messaging'
import {encode, decode} from 'gpt-3-encoder'

import {askOpenAI, parseStream} from '@/io'
import {gettext, toast} from '@/utils'
Expand Down Expand Up @@ -46,10 +47,16 @@ export function withAIContext(Component) {

aiDispatch({type: AI_REDUCER_ACTION_TYPE.REQUEST})

// due to ChatGPT3.5 max token limit, srhink all content to 3600 token.
let prompt = onlyCommand ? command : `${command}:\n\n${selectedText}\n\n`

const encoded = encode(prompt)
if (encoded.length > 3600) prompt = decode(encoded.slice(0, 3600))

return askOpenAI({
authKey: authKey || config.authKey,
model: config.model,
prompt: onlyCommand ? command : `${command}:\n\n${selectedText}\n\n`,
prompt,
})
.then(streamReader => {
return parseStream(streamReader, result => {
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/prompt-board/ask-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function AskPage() {

// FIXME: article maybe too long for ask, need token calculator
if (command) {
askAI({command: `${command}:\n\n${article}\n\n`, onlyCommand: true})
askAI({command, text: article})
}
}
}
Expand Down

0 comments on commit f581a72

Please sign in to comment.