support enable/disable temperature and top_p

This commit is contained in:
2023-11-03 14:01:16 +08:00
parent 40081f81f7
commit 6bf6fb9455
4 changed files with 61 additions and 13 deletions

View File

@@ -35,7 +35,9 @@ export interface ChatStore {
responseModelName: string;
cost: number;
temperature: number;
temperature_enabled: boolean;
top_p: number;
top_p_enabled: boolean;
presence_penalty: number;
frequency_penalty: number;
develop_mode: boolean;
@@ -70,7 +72,9 @@ const newChatStore = (
responseModelName: "",
cost: 0,
temperature: getDefaultParams("temp", temperature),
temperature_enabled: true,
top_p: 1,
top_p_enabled: false,
presence_penalty: 0,
frequency_penalty: 0,
develop_mode: getDefaultParams("dev", dev),

View File

@@ -158,7 +158,9 @@ export default function ChatBOX(props: {
client.sysMessageContent = chatStore.systemMessageContent;
client.tokens_margin = chatStore.tokenMargin;
client.temperature = chatStore.temperature;
client.enable_temperature = chatStore.temperature_enabled;
client.top_p = chatStore.top_p;
client.enable_top_p = chatStore.top_p_enabled
client.frequency_penalty = chatStore.frequency_penalty;
client.presence_penalty = chatStore.presence_penalty;
client.messages = chatStore.history

View File

@@ -47,7 +47,9 @@ class Chat {
apiEndpoint: string;
model: string;
temperature: number;
enable_temperature: boolean;
top_p: number;
enable_top_p: boolean;
presence_penalty: number;
frequency_penalty: number;
@@ -60,7 +62,9 @@ class Chat {
apiEndPoint = "https://api.openai.com/v1/chat/completions",
model = "gpt-3.5-turbo",
temperature = 0.7,
enable_temperature = true,
top_p = 1,
enable_top_p = false,
presence_penalty = 0,
frequency_penalty = 0,
} = {}
@@ -77,7 +81,9 @@ class Chat {
this.apiEndpoint = apiEndPoint;
this.model = model;
this.temperature = temperature;
this.enable_temperature = enable_temperature;
this.top_p = top_p;
this.enable_top_p = enable_top_p;
this.presence_penalty = presence_penalty;
this.frequency_penalty = frequency_penalty;
}
@@ -111,21 +117,28 @@ class Chat {
messages.push({ role: "system", content: this.sysMessageContent });
}
messages.push(...this.messages);
const body: any = {
model: this.model,
messages,
stream,
presence_penalty: this.presence_penalty,
frequency_penalty: this.frequency_penalty,
};
if (this.enable_temperature) {
body["temperature"] = this.temperature;
}
if (this.enable_top_p) {
body["top_p"] = this.top_p;
}
return fetch(this.apiEndpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${this.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: this.model,
messages,
stream,
temperature: this.temperature,
top_p: this.top_p,
presence_penalty: this.presence_penalty,
frequency_penalty: this.frequency_penalty,
}),
body: JSON.stringify(body),
});
}

View File

@@ -4,6 +4,7 @@ import { ChatStore, TemplateAPI, clearTotalCost, getTotalCost } from "./app";
import models from "./models";
import { TemplateChatStore } from "./chatbox";
import { tr, Tr, langCodeContext, LANG_OPTIONS } from "./translate";
import p from "preact-markdown";
const Help = (props: { children: any; help: string }) => {
return (
@@ -100,14 +101,42 @@ const Input = (props: {
const Slicer = (props: {
chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void;
field: "temperature";
field: "temperature" | "top_p";
help: string;
}) => {
const enable_filed_name: "temperature_enabled" | "top_p_enabled" =
`${props.field}_enabled` as any;
const enabled = props.chatStore[enable_filed_name];
if (enabled === null || enabled === undefined) {
if (props.field === "temperature") {
props.chatStore[enable_filed_name] = true;
}
if (props.field === "top_p") {
props.chatStore[enable_filed_name] = false;
}
}
const setEnabled = (state: boolean) => {
props.chatStore[enable_filed_name] = state;
props.setChatStore({ ...props.chatStore });
};
return (
<Help help={props.help}>
<label className="m-2 p-2">{props.field}</label>
<span>
<label className="m-2 p-2">{props.field}</label>
<input
type="checkbox"
checked={props.chatStore[enable_filed_name]}
onClick={() => {
setEnabled(!enabled);
}}
/>
</span>
<span>
<input
disabled={!enabled}
className="m-2 p-2 border rounded focus w-28"
type="range"
min="0"
@@ -121,6 +150,7 @@ const Slicer = (props: {
}}
/>
<input
disabled={!enabled}
className="m-2 p-2 border rounded focus w-28"
type="number"
value={props.chatStore[props.field]}
@@ -143,7 +173,6 @@ const Number = (props: {
| "maxTokens"
| "tokenMargin"
| "postBeginIndex"
| "top_p"
| "presence_penalty"
| "frequency_penalty";
readOnly: boolean;
@@ -357,7 +386,7 @@ export default (props: {
{...props}
/>
<Slicer field="temperature" help="温度" {...props} />
<Number field="top_p" help="top_p" readOnly={false} {...props} />
<Slicer field="top_p" help="top_p" {...props} />
<Number
field="presence_penalty"
help="presence_penalty"