commit changes on ChatGPT-APP
This commit is contained in:
parent
0ea049f9a3
commit
dd66bc3881
Binary file not shown.
Binary file not shown.
|
@ -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<IMessages[]>([]);
|
||||
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<string> => {
|
||||
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]
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue