Skip to content

Commit

Permalink
fix: config openai translator (#829)
Browse files Browse the repository at this point in the history
* alway use config from options in openai

* adjust requestPath correction rule

* config openai arguments

* completing the v1 prefix by default
  • Loading branch information
xtyuns authored Jun 9, 2024
1 parent dbe45a9 commit c1868d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
36 changes: 34 additions & 2 deletions src/services/translate/openai/Config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import { translate } from './index';
import { Language } from './index';
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';

export const defaultRequestArguments = JSON.stringify({
temperature: 0,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
})

export function Config(props) {
const { instanceKey, updateServiceList, onClose } = props;
const [openaiConfig, setOpenaiConfig] = useConfig(
Expand All @@ -34,6 +41,7 @@ export function Config(props) {
},
{ role: 'user', content: `Translate into $to:\n"""\n$text\n"""` },
],
requestArguments: defaultRequestArguments
},
{ sync: false }
);
Expand All @@ -52,6 +60,12 @@ export function Config(props) {
],
});
}
if(openaiConfig.requestArguments === undefined) {
setOpenaiConfig({
...openaiConfig,
requestArguments: defaultRequestArguments
})
}
}

const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -296,8 +310,8 @@ export function Config(props) {
openaiConfig.promptList.length === 0
? 'system'
: openaiConfig.promptList.length % 2 === 0
? 'assistant'
: 'user',
? 'assistant'
: 'user',
content: '',
},
],
Expand All @@ -308,6 +322,24 @@ export function Config(props) {
</Button>
</div>
<br />

<h3 className='my-auto'>Request Arguments</h3>
<div className='config-item'>
<Textarea
label=''
labelPlacement='outside'
variant='faded'
value={openaiConfig['requestArguments']}
placeholder={`Input API Request Arguments`}
onValueChange={(value) => {
setOpenaiConfig({
...openaiConfig,
requestArguments: value,
});
}}
/>
</div>
<br />
<Button
type='submit'
isLoading={isLoading}
Expand Down
21 changes: 8 additions & 13 deletions src/services/translate/openai/index.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { fetch, Body } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';
import { Language } from './info';
import { defaultRequestArguments } from './Config';

export async function translate(text, from, to, options = {}) {
export async function translate(text, from, to, options) {
const { config, setResult, detect } = options;

let translateConfig = await store.get('openai');
if (config !== undefined) {
translateConfig = config;
}
let { service, requestPath, model, apiKey, stream, promptList } = translateConfig;
let { service, requestPath, model, apiKey, stream, promptList, requestArguments } = config;

if (!/https?:\/\/.+/.test(requestPath)) {
requestPath = `https://${requestPath}`;
}
if (requestPath.endsWith('/')) {
requestPath = requestPath.slice(0, -1);
}
if (service === 'openai' && !requestPath.includes('/v1/chat/completions')) {

// /v1 is not required
if (service === 'openai' && !requestPath.endsWith('/chat/completions')) {
requestPath += '/v1/chat/completions';
}

Expand Down Expand Up @@ -54,12 +52,9 @@ export async function translate(text, from, to, options = {}) {
'Content-Type': 'application/json',
'api-key': apiKey,
};
let body = {
temperature: 0,
const body = {
...JSON.parse(requestArguments ?? defaultRequestArguments),
stream: stream,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
messages: promptList,
};
if (service === 'openai') {
Expand Down

0 comments on commit c1868d5

Please sign in to comment.