diff --git a/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions .zip b/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions .zip new file mode 100644 index 000000000..28326fdd0 Binary files /dev/null and b/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions .zip differ diff --git a/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions.zip b/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions.zip deleted file mode 100644 index 55cb38946..000000000 Binary files a/samples/react-chatgpt-app/OpenAIFunctionProject/OpenAI-azure-functions.zip and /dev/null differ diff --git a/samples/react-chatgpt-app/src/hooks/useChatGpt.ts b/samples/react-chatgpt-app/src/hooks/useChatGpt.ts index aaebb0e8a..fd4c11f77 100644 --- a/samples/react-chatgpt-app/src/hooks/useChatGpt.ts +++ b/samples/react-chatgpt-app/src/hooks/useChatGpt.ts @@ -8,7 +8,18 @@ import { IHttpClientOptions, } from '@microsoft/sp-http'; -export const useChatGpt = (context: BaseComponentContext, appId: string, AzureFunctionUrl: string) => { +enum ERole { + user = "user", + assistant = "assistant", + system = "system", +} +interface IMessages { + role:ERole; + content: string; +} + +export const useChatGpt = (context: BaseComponentContext, appId:string, AzureFunctionUrl:string) => { + const messages = React.useRef([]); const client = React.useMemo(() => { if (context) { return async () => { @@ -22,27 +33,30 @@ export const useChatGpt = (context: BaseComponentContext, appId: string, AzureFu const getCompletion = React.useCallback( async (query: string): Promise => { try { + messages.current.push({ role: ERole.user, content: query }); if (!client) return; const options: IHttpClientOptions = { headers: { "Content-Type": "application/json;odata=verbose", Accept: "application/json;odata=verbose" }, mode: "cors", - body: JSON.stringify({ prompt: query }), + body: JSON.stringify({ messages: messages.current }), method: "POST", }; + const response = await (await client()).post(AzureFunctionUrl, AadHttpClient.configurations.v1, options); const answer = await response.json(); if (response.status === 200) { - return answer.choices[0].message.content; - } else { - console.log("[getCompletion] error:", answer); - throw new Error("Error on executing the request, please try again later."); - } - } catch (error) { - if (!DEBUG) { - console.log("[getCompletion] error:", error); - } - throw error; - } + messages.current.push(answer.choices[0].message) + return answer.choices[0].message.content; + } else { + console.log("[getCompletion] error:", answer); + throw new Error("Error on executing the request, please try again later."); + } + } catch (error) { + if (!DEBUG) { + console.log("[getCompletion] error:", error); + } + throw error; + } }, [client] );