Skip to content

Commit

Permalink
feat: change request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
duxiaoqing committed Mar 8, 2024
1 parent 84bfb23 commit b2df781
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ const ChatOperation = (props: ChatOperationProps) => {
const model = currSession.name === SERIAL_SESSION ? botStore.currentBot : chatStore.lastBotMessage(currSession.name).model;
const provider = models[model]?.provider || '';
const isStream = streamModels.includes(model);
chatStore.retry(currSession.name, provider, model, isStream);
const webui = models[model]?.webui || { payload: { options: {} } }
chatStore.retry(currSession.name, provider, model, isStream, webui);
};

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ const PromptInput = () => {
if (configStore.mode === SERIAL_MODE) {
const provider = models[botStore.currentBot]?.provider;
const isStream = streamModels.includes(botStore.currentBot);
chatStore.onUserInput(currPrompt, provider, botStore.currentBot, SERIAL_SESSION, isStream);
const webui = models[botStore.currentBot]?.webui || { payload: { options: {} } }
chatStore.onUserInput(currPrompt, provider, botStore.currentBot, SERIAL_SESSION, isStream, webui);
} else {
botStore.chosenBotNames.forEach((botName) => {
const provider = models[botName]?.provider;
const isStream = streamModels.includes(botName);
chatStore.onUserInput(currPrompt, provider, botName, botName, isStream);
const webui = models[botName]?.webui || { payload: { options: {} } }
chatStore.onUserInput(currPrompt, provider, botName, botName, isStream, webui);
});
}
}
Expand Down
30 changes: 26 additions & 4 deletions openaoe/frontend/src/services/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ export const getHeaders = () => {
export const getUrl = (provider) => {
return API.get(provider)?.url || API.get(DEFAULT_PROVIDER).url;
};
/** Build your own api payload here
/** Build your own api payload here deprived
* @param provider service provider, each provider has its own payload format and may have more than one model
* @param model model name
* @param prompt
* @param messages
*/
export const getPayload = (provider: string, model: string, prompt: string, messages: { text: string; sender_type: string; }[]) => {
export const getPayload2 = (provider: string, model: string, prompt: string, messages: { text: string; sender_type: string; }[]) => {
const payload = { ...API.get(provider) || API.get(DEFAULT_PROVIDER) };
delete payload.url;
payload.model = model;
if (['openai', 'internlm', 'gpt-4', 'mistral'].includes(provider)) {
payload.prompt = prompt;
// payload.prompt = prompt;
payload.messages = messages;
}
if (provider === 'minimax') {
payload.prompt = prompt;
// payload.prompt = prompt;
payload.messages = messages.map((item) => {
return {
text: item.text,
Expand Down Expand Up @@ -82,5 +82,27 @@ export const getPayload = (provider: string, model: string, prompt: string, mess
delete payload.model;
payload.payload.message.text = formatMessage;
}
console.log('payload', payload);
return payload;
};

/** Build your own api payload here
* @param provider service provider, each provider has its own payload format and may have more than one model
* @param model model name
* @param prompt
* @param messages
*/
export const getPayload = (provider: string, model: string, prompt: string, messages: { text: string; sender_type: string; }[], payload: any) => {
console.log(provider, model, messages);
delete payload.url;
const requestParam = {
model,
messages: messages.map(item => ({
role: item.sender_type,
content: item.text
})),
options: payload?.options || {}
};

return requestParam;
};
18 changes: 9 additions & 9 deletions openaoe/frontend/src/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from 'zustand';
import { fetchEventSource } from '@fortaine/fetch-event-source';
import { persist } from 'zustand/middleware';
import { scrollToBottom } from '@utils/utils.ts';
import { getHeaders, getPayload, getUrl } from '@services/fetch.ts';
import { getHeaders, getPayload } from '@services/fetch.ts';
import { fetchBotAnswer } from '@services/home.ts';
import { DEFAULT_BOT, SERIAL_SESSION, STREAM_BOT } from '@constants/models.ts';

Expand Down Expand Up @@ -58,7 +58,7 @@ interface ChatStore {
updateSession: (sessionIndex: number, override: Partial<ChatSession>) => void;
onNewMessage: (message: ChatMessage, sessionIndex?: number) => void;
deleteMessage: (sessionName: string, messageIndex: number) => void;
onUserInput: (content: string, provider: string, model: string, sessionId: string, isStreamApi: boolean) => Promise<void>;
onUserInput: (content: string, provider: string, model: string, sessionId: string, isStreamApi: boolean, webui: any) => Promise<void>;
updateMessage: (
sessionIndex: number,
messageIndex: number,
Expand All @@ -68,7 +68,7 @@ interface ChatStore {
lastBotMessage: (sessionName: string) => ChatMessage;
lastUserMessage: (sessionName: string) => ChatMessage;
closeController: (sessionName: string) => void;
retry: (sessionName: string, provider: string, model: string, isStreamApi: boolean) => void;
retry: (sessionName: string, provider: string, model: string, isStreamApi: boolean, webui: any) => void;

clearAllData: () => void;
}
Expand Down Expand Up @@ -125,17 +125,17 @@ export const useChatStore = create<ChatStore>()(
set(() => ({ sessions: get().sessions }));
}
},
retry(bot: '', provider: '', model: '', isStreamApi = true) {
retry(bot: '', provider: '', model: '', isStreamApi = true, webui = {}) {
const text = get().lastUserMessage(bot).text;
if ((get().lastMessage(bot).id === get().lastBotMessage(bot).id) && get().getSession(bot).clearContextIndex !== get().getSession(bot).messages.length) {
// If the last message is a reply from the bot and the context is not cleared yet, replace the last two messages,
// Otherwise resend the last conversation
get().deleteMessage(bot, get().getSession(bot).messages.length - 1);
get().deleteMessage(bot, get().getSession(bot).messages.length - 1);
}
get().onUserInput(text, provider, model, bot, isStreamApi);
get().onUserInput(text, provider, model, bot, isStreamApi, webui);
},
async onUserInput(text, provider, model, sessionName, isStreamApi = true) {
async onUserInput(text, provider, model, sessionName, isStreamApi = true, webui: any) {
const sessionIndex = get().sessions.findIndex((session) => session.name === sessionName);
const currSession = get().sessions[sessionIndex];
if (!currSession) {
Expand Down Expand Up @@ -179,15 +179,15 @@ export const useChatStore = create<ChatStore>()(
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
...getPayload(provider, model, text, messages),
...getPayload(provider, model, text, messages, webui.payload),
}),
};

const url = getUrl(provider);
const url = webui.path;
let isStream = true;
if (!isStreamApi) {
try {
const rsp = await fetchBotAnswer(url, getPayload(provider, model, text, messages), abortController.signal);
const rsp = await fetchBotAnswer(url, getPayload(provider, model, text, messages, webui.payload), abortController.signal);
if (rsp.msgCode === '10000' && rsp.data) {
if (provider === 'google') {
// Google: The data format returned by the chat-stream interface is different and requires special processing
Expand Down

0 comments on commit b2df781

Please sign in to comment.