allow to set model name

main
Philipp Schmid 2023-03-15 21:13:14 +01:00
parent d1e5a0cda8
commit 1d74cafe2d
6 changed files with 50 additions and 12 deletions

View File

@ -2,9 +2,9 @@ import SettingsTab from "./tab";
import SettingsOption from "./option"; import SettingsOption from "./option";
import { Button, Slider, Textarea } from "@mantine/core"; import { Button, Slider, Textarea } from "@mantine/core";
import { useCallback, useMemo } from "react"; import { useCallback, useMemo } from "react";
import { defaultSystemPrompt } from "../../openai"; import { defaultSystemPrompt, defaultModel } from "../../openai";
import { useAppDispatch, useAppSelector } from "../../store"; import { useAppDispatch, useAppSelector } from "../../store";
import { resetSystemPrompt, selectSystemPrompt, selectTemperature, setSystemPrompt, setTemperature } from "../../store/parameters"; import { resetModel, setModel, selectModel, resetSystemPrompt, selectSystemPrompt, selectTemperature, setSystemPrompt, setTemperature } from "../../store/parameters";
import { selectSettingsOption } from "../../store/settings-ui"; import { selectSettingsOption } from "../../store/settings-ui";
import { FormattedMessage, useIntl } from "react-intl"; import { FormattedMessage, useIntl } from "react-intl";
@ -13,16 +13,22 @@ export default function GenerationOptionsTab(props: any) {
const option = useAppSelector(selectSettingsOption); const option = useAppSelector(selectSettingsOption);
const initialSystemPrompt = useAppSelector(selectSystemPrompt); const initialSystemPrompt = useAppSelector(selectSystemPrompt);
const model = useAppSelector(selectModel);
const temperature = useAppSelector(selectTemperature); const temperature = useAppSelector(selectTemperature);
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const onSystemPromptChange = useCallback((event: React.ChangeEvent<HTMLTextAreaElement>) => dispatch(setSystemPrompt(event.target.value)), [dispatch]); const onSystemPromptChange = useCallback((event: React.ChangeEvent<HTMLTextAreaElement>) => dispatch(setSystemPrompt(event.target.value)), [dispatch]);
const onModelChange = useCallback((event: React.ChangeEvent<HTMLTextAreaElement>) => dispatch(setModel(event.target.value)), [dispatch]);
const onResetSystemPrompt = useCallback(() => dispatch(resetSystemPrompt()), [dispatch]); const onResetSystemPrompt = useCallback(() => dispatch(resetSystemPrompt()), [dispatch]);
const onResetModel = useCallback(() => dispatch(resetModel()), [dispatch]);
const onTemperatureChange = useCallback((value: number) => dispatch(setTemperature(value)), [dispatch]); const onTemperatureChange = useCallback((value: number) => dispatch(setTemperature(value)), [dispatch]);
const resettable = initialSystemPrompt const resettableSystemPromopt = initialSystemPrompt
&& (initialSystemPrompt?.trim() !== defaultSystemPrompt.trim()); && (initialSystemPrompt?.trim() !== defaultSystemPrompt.trim());
const resettableModel = model
&& (model?.trim() !== defaultModel.trim());
const systemPromptOption = useMemo(() => ( const systemPromptOption = useMemo(() => (
<SettingsOption heading={intl.formatMessage({ defaultMessage: "System Prompt" })} <SettingsOption heading={intl.formatMessage({ defaultMessage: "System Prompt" })}
focused={option === 'system-prompt'}> focused={option === 'system-prompt'}>
@ -36,11 +42,30 @@ export default function GenerationOptionsTab(props: any) {
<FormattedMessage defaultMessage="The System Prompt is shown to ChatGPT by the &quot;System&quot; before your first message. The <code>'{{ datetime }}'</code> tag is automatically replaced by the current date and time." <FormattedMessage defaultMessage="The System Prompt is shown to ChatGPT by the &quot;System&quot; before your first message. The <code>'{{ datetime }}'</code> tag is automatically replaced by the current date and time."
values={{ code: chunk => <code style={{ whiteSpace: 'nowrap' }}>{chunk}</code> }} /> values={{ code: chunk => <code style={{ whiteSpace: 'nowrap' }}>{chunk}</code> }} />
</p> </p>
{resettable && <Button size="xs" compact variant="light" onClick={onResetSystemPrompt}> {resettableSystemPromopt && <Button size="xs" compact variant="light" onClick={onResetSystemPrompt}>
<FormattedMessage defaultMessage="Reset to default" /> <FormattedMessage defaultMessage="Reset to default" />
</Button>} </Button>}
</SettingsOption> </SettingsOption>
), [option, initialSystemPrompt, resettable, onSystemPromptChange, onResetSystemPrompt]); ), [option, initialSystemPrompt, resettableSystemPromopt, onSystemPromptChange, onResetSystemPrompt]);
const modelOption = useMemo(() => (
<SettingsOption heading={intl.formatMessage({ defaultMessage: "Model" })}
focused={option === 'model'}>
<Textarea
value={model || defaultModel}
onChange={onModelChange}
minRows={1}
maxRows={1}
autosize />
<p style={{ marginBottom: '0.7rem' }}>
<FormattedMessage defaultMessage="The model name. Known names are: gpt-3.5, gpt-3.5-turbo and gpt-4.0"
values={{ code: chunk => <code style={{ whiteSpace: 'nowrap' }}>{chunk}</code> }} />
</p>
{resettableModel && <Button size="xs" compact variant="light" onClick={onResetModel}>
<FormattedMessage defaultMessage="Reset to default" />
</Button>}
</SettingsOption>
), [option, model, resettableModel, onModelChange, onResetModel]);
const temperatureOption = useMemo(() => ( const temperatureOption = useMemo(() => (
<SettingsOption heading={intl.formatMessage({ defaultMessage: "Temperature: {temperature, number, ::.0}", }, { temperature })} <SettingsOption heading={intl.formatMessage({ defaultMessage: "Temperature: {temperature, number, ::.0}", }, { temperature })}
@ -55,9 +80,10 @@ export default function GenerationOptionsTab(props: any) {
const elem = useMemo(() => ( const elem = useMemo(() => (
<SettingsTab name="options"> <SettingsTab name="options">
{systemPromptOption} {systemPromptOption}
{modelOption}
{temperatureOption} {temperatureOption}
</SettingsTab> </SettingsTab>
), [systemPromptOption, temperatureOption]); ), [systemPromptOption, modelOption, temperatureOption]);
return elem; return elem;
} }

View File

@ -9,6 +9,8 @@ Knowledge cutoff: 2021-09
Current date and time: {{ datetime }} Current date and time: {{ datetime }}
`.trim(); `.trim();
export const defaultModel = 'gpt-3.5-turbo';
export interface OpenAIResponseChunk { export interface OpenAIResponseChunk {
id?: string; id?: string;
done: boolean; done: boolean;
@ -53,7 +55,7 @@ export async function createChatCompletion(messages: OpenAIMessage[], parameters
const openai = new OpenAIApi(configuration); const openai = new OpenAIApi(configuration);
const response = await openai.createChatCompletion({ const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo', model: parameters.model,
temperature: parameters.temperature, temperature: parameters.temperature,
messages: messages as any, messages: messages as any,
}); });
@ -93,7 +95,7 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
payload: JSON.stringify({ payload: JSON.stringify({
"model": "gpt-3.5-turbo", "model": parameters.model,
"messages": messagesToSend, "messages": messagesToSend,
"temperature": parameters.temperature, "temperature": parameters.temperature,
"stream": true, "stream": true,

View File

@ -1,7 +1,9 @@
import { defaultModel } from "./openai";
import { Parameters } from "./types"; import { Parameters } from "./types";
export const defaultParameters: Parameters = { export const defaultParameters: Parameters = {
temperature: 0.5, temperature: 0.5,
model: defaultModel
}; };
export function loadParameters(id: string | null | undefined = null): Parameters { export function loadParameters(id: string | null | undefined = null): Parameters {

View File

@ -1,6 +1,6 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import type { RootState } from '.'; import type { RootState } from '.';
import { defaultSystemPrompt } from '../openai'; import { defaultSystemPrompt, defaultModel } from '../openai';
import { defaultParameters } from '../parameters'; import { defaultParameters } from '../parameters';
import { Parameters } from '../types'; import { Parameters } from '../types';
@ -16,15 +16,22 @@ export const parametersSlice = createSlice({
resetSystemPrompt: (state) => { resetSystemPrompt: (state) => {
state.initialSystemPrompt = defaultSystemPrompt; state.initialSystemPrompt = defaultSystemPrompt;
}, },
setModel: (state, action: PayloadAction<string>) => {
state.model = action.payload;
},
resetModel: (state) => {
state.model = defaultModel;
},
setTemperature: (state, action: PayloadAction<number>) => { setTemperature: (state, action: PayloadAction<number>) => {
state.temperature = action.payload; state.temperature = action.payload;
}, },
}, },
}) })
export const { setSystemPrompt, setTemperature, resetSystemPrompt } = parametersSlice.actions; export const { setSystemPrompt, setModel, setTemperature, resetSystemPrompt, resetModel } = parametersSlice.actions;
export const selectSystemPrompt = (state: RootState) => state.parameters.initialSystemPrompt; export const selectSystemPrompt = (state: RootState) => state.parameters.initialSystemPrompt;
export const selectModel = (state: RootState) => state.parameters.model;
export const selectTemperature = (state: RootState) => state.parameters.temperature; export const selectTemperature = (state: RootState) => state.parameters.temperature;
export default parametersSlice.reducer; export default parametersSlice.reducer;

View File

@ -1,4 +1,4 @@
import { createChatCompletion } from "./openai"; import { createChatCompletion, defaultModel } from "./openai";
import { OpenAIMessage, Chat } from "./types"; import { OpenAIMessage, Chat } from "./types";
const systemPrompt = ` const systemPrompt = `
@ -38,7 +38,7 @@ export async function createTitle(chat: Chat, apiKey: string | undefined | null,
}, },
]; ];
let title = await createChatCompletion(messages as any, { temperature: 0.5, apiKey }); let title = await createChatCompletion(messages as any, { temperature: 0.5, model: defaultModel, apiKey });
if (!title?.length) { if (!title?.length) {
if (firstUserMessage.content.trim().length > 2 && firstUserMessage.content.trim().length < 250) { if (firstUserMessage.content.trim().length > 2 && firstUserMessage.content.trim().length < 250) {

View File

@ -4,6 +4,7 @@ export interface Parameters {
temperature: number; temperature: number;
apiKey?: string; apiKey?: string;
initialSystemPrompt?: string; initialSystemPrompt?: string;
model: string;
} }
export interface Message { export interface Message {