|
| 1 | +import { useState } from 'react'; |
| 2 | +import { AIWidget } from './AIWidget'; |
| 3 | +import AIWidgetIcon from '../../../svgs/Icons/Media/AIWidget'; |
| 4 | +import { IChatMessage } from '../../../../../Views/Base/BaseViewModel'; |
| 5 | +import { stringifyError } from '../../../../../../utils/parse'; |
| 6 | +import ErrorMessageIcon from './ErrorMessageIcon'; |
| 7 | + |
| 8 | +interface MessageWidgetProps { |
| 9 | + // https://api.openai.com/v1/chat/completions' |
| 10 | + // api: 'v1/chat/completions', |
| 11 | + // servername: 'https://myproxy.com', |
| 12 | + // https://func270519800.azurewebsites.net/api/TranslateTextToEng |
| 13 | + servername: string; |
| 14 | + api: string; |
| 15 | + port: string; |
| 16 | + sessionToken: string; |
| 17 | +} |
| 18 | +export default function UseDefaultAIAssistAnswerWidgetWithProxy({ |
| 19 | + servername, |
| 20 | + api, |
| 21 | + port, |
| 22 | + sessionToken, |
| 23 | +}: MessageWidgetProps): AIWidget { |
| 24 | + const [errorMessage, setErrorMessage] = useState<string>(''); |
| 25 | + |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function |
| 27 | + const fileToWidget = (file: File, context: IChatMessage[]): void => {}; |
| 28 | + |
| 29 | + const renderWidget = (): JSX.Element => { |
| 30 | + if (errorMessage && errorMessage.length > 0) { |
| 31 | + const errorsDescriptions: |
| 32 | + | { title: string; action: () => void }[] |
| 33 | + | undefined = []; |
| 34 | + |
| 35 | + return ( |
| 36 | + <ErrorMessageIcon |
| 37 | + errorMessageText={errorMessage} |
| 38 | + errorsDescriptions={errorsDescriptions} |
| 39 | + /> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return <AIWidgetIcon applyZoom color="green" />; |
| 44 | + }; |
| 45 | + |
| 46 | + // async function getData( |
| 47 | + // textToSend: string, |
| 48 | + // dialogMessages: ChatCompletionRequestMessage[], |
| 49 | + // ): Promise<string> { |
| 50 | + // // |
| 51 | + // const apiEndpoint = 'https://api.openai.com/v1/chat/completions'; |
| 52 | + // const { apiKey } = QBConfig.configAIApi.AIAnswerAssistWidgetConfig; // Замените на ваш реальный ключ API |
| 53 | + // const model = 'gpt-3.5-turbo'; |
| 54 | + // const requestOptions = { |
| 55 | + // method: 'POST', |
| 56 | + // headers: { |
| 57 | + // 'Content-Type': 'application/json', |
| 58 | + // Authorization: `Bearer ${apiKey}`, |
| 59 | + // }, |
| 60 | + // body: JSON.stringify({ |
| 61 | + // messages: [...dialogMessages, { role: 'user', content: textToSend }], |
| 62 | + // model, |
| 63 | + // temperature: 0.5, |
| 64 | + // }), |
| 65 | + // }; |
| 66 | + // |
| 67 | + async function getData( |
| 68 | + textToSend: string, |
| 69 | + dialogMessages: IChatMessage[], |
| 70 | + ): Promise<string> { |
| 71 | + let outputMessage = ''; |
| 72 | + const apiEndpoint = `${servername}${port}${api}`; |
| 73 | + const apiKey = sessionToken; // Замените на ваш реальный ключ API |
| 74 | + const model = 'gpt-3.5-turbo'; |
| 75 | + const prompt = `Respond as a knowledgeable customer support specialist with access to ChatGPT features, and provide a simple and informative response to the inquiry :"${textToSend}"`; |
| 76 | + |
| 77 | + const requestOptions = { |
| 78 | + method: 'POST', |
| 79 | + headers: { |
| 80 | + 'Content-Type': 'application/json', |
| 81 | + Authorization: `Bearer ${apiKey}`, |
| 82 | + }, |
| 83 | + body: JSON.stringify({ |
| 84 | + messages: [...dialogMessages, { role: 'user', content: prompt }], |
| 85 | + model, |
| 86 | + temperature: 0.5, |
| 87 | + }), |
| 88 | + }; |
| 89 | + |
| 90 | + // |
| 91 | + try { |
| 92 | + const response = await fetch(apiEndpoint, requestOptions); |
| 93 | + const data = await response.json(); |
| 94 | + |
| 95 | + outputMessage = data.choices[0].message?.content || ''; |
| 96 | + } catch (err) { |
| 97 | + outputMessage = stringifyError(err); |
| 98 | + setErrorMessage(outputMessage); |
| 99 | + } |
| 100 | + |
| 101 | + return outputMessage; |
| 102 | + } |
| 103 | + |
| 104 | + const [textFromWidgetToContent, setTextFromWidgetToContent] = useState(''); |
| 105 | + // const textToWidget = (value: string, context: IChatMessage[]): void => { |
| 106 | + // if (value && value.length > 0) { |
| 107 | + // // eslint-disable-next-line promise/catch-or-return |
| 108 | + // getOpenAIApiData(value, context as ChatCompletionRequestMessage[]).then( |
| 109 | + // // eslint-disable-next-line promise/always-return |
| 110 | + // (data) => { |
| 111 | + // setTextFromWidgetToContent(data); |
| 112 | + // }, |
| 113 | + // ); |
| 114 | + // } |
| 115 | + // }; |
| 116 | + |
| 117 | + const textToWidget = (value: string, context: IChatMessage[]): void => { |
| 118 | + if (value && value.length > 0) { |
| 119 | + // eslint-disable-next-line promise/catch-or-return |
| 120 | + getData(value, context).then( |
| 121 | + // eslint-disable-next-line promise/always-return |
| 122 | + (data) => { |
| 123 | + setTextFromWidgetToContent(data); |
| 124 | + }, |
| 125 | + ); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + return { |
| 130 | + fileToContent: undefined, |
| 131 | + textToContent: textFromWidgetToContent, |
| 132 | + fileToWidget, |
| 133 | + renderWidget, |
| 134 | + textToWidget, |
| 135 | + }; |
| 136 | +} |
0 commit comments