Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class AiService {
temperature: 0.7,
};

const response = await this.chatService.chat.completions.create(params);
const response = await this.chatService.chat.completions.create(params, { maxRetries: 0 });

@dmlvr dmlvr Sep 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add maxRetries to the params object here and all next demos, where we have params object

const data = { choices: response.choices };

return data.choices[0].message?.content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export class AppComponent {
try {
await this.appService.regenerate();
} finally {
this.toggleDisabledState(false);
setTimeout(() => {
this.toggleDisabledState(false);
});
}
Comment on lines 95 to 99
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,19 @@ export class AppService {
this.messages.push({ role: 'assistant', content: aiResponse });
this.renderAssistantMessage(aiResponse);
}, 200);
} catch {
} catch (err: unknown) {
this.typingUsersSubject.next([]);
this.messages.pop();
this.alertLimitReached();
this.alertError(this.getErrorMessage(err));
}
}

getErrorMessage(err: unknown): string {
if (err instanceof Error) return err.message;
if (typeof err === 'string') return err;
return 'Unknown error';
}

updateLastMessage(text = this.REGENERATION_TEXT) {
const items = this.dataSource.items();
const lastMessage = items.at(-1);
Expand All @@ -135,9 +141,9 @@ export class AppService {
this.dataSource.store().push([{ type: 'insert', data: message }]);
}

alertLimitReached() {
alertError(message: string) {
this.setAlerts([{
message: 'Request limit reached, try again in a minute.',
message,
}]);

setTimeout(() => {
Expand All @@ -152,13 +158,14 @@ export class AppService {

async regenerate() {
try {
this.setAlerts([]);
const aiResponse = await this.getAIResponse(this.messages.slice(0, -1));

this.updateLastMessage(aiResponse);
this.messages.at(-1).content = aiResponse;
} catch {
} catch (err: unknown) {
this.updateLastMessage(this.messages.at(-1).content);
this.alertLimitReached();
this.alertError(this.getErrorMessage(err));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getAIResponse(messages: AIMessage[], delay?: number): Prom
temperature: 0.7,
};

const response = await chatService.chat.completions.create(params);
const response = await chatService.chat.completions.create(params, { maxRetries: 0 });
const data = { choices: response.choices };

if (delay) {
Expand Down
23 changes: 15 additions & 8 deletions apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const dataItemToMessage = (item: ChatTypes.Message): AIMessage => ({

const getMessageHistory = (): AIMessage[] => [...dataSource.items()].map(dataItemToMessage);

const getErrorMessage = (err: unknown): string => {
if (err instanceof Error) return err.message;
if (typeof err === 'string') return err;
return 'Unknown error';
};

export const useApi = () => {
const [alerts, setAlerts] = useState<ChatTypes.Alert[]>([]);

Expand All @@ -55,9 +61,9 @@ export const useApi = () => {
}]);
}, []);

const alertLimitReached = useCallback((): void => {
const alertError = useCallback((message: string): void => {
setAlerts([{
message: 'Request limit reached, try again in a minute.',
message,
}]);

setTimeout(() => {
Expand All @@ -77,12 +83,13 @@ export const useApi = () => {
author: assistant,
text: aiResponse,
});
} catch {
alertLimitReached();
} catch (err: unknown) {
alertError(getErrorMessage(err));
}
}, [alertLimitReached, insertMessage]);
}, [alertError, insertMessage]);

const regenerateLastAIResponse = useCallback(async (): Promise<void> => {
setAlerts([]);
const messageHistory = getMessageHistory();
updateLastMessageContent(REGENERATION_TEXT);

Expand All @@ -92,11 +99,11 @@ export const useApi = () => {
if (typeof aiResponse === 'string') {
updateLastMessageContent(aiResponse);
}
} catch {
} catch (err: unknown) {
updateLastMessageContent(messageHistory.at(-1)?.content as string);
alertLimitReached();
alertError(getErrorMessage(err));
}
}, [alertLimitReached, updateLastMessageContent]);
}, [alertError, updateLastMessageContent]);

return {
alerts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getAIResponse(messages, delay) {
max_completion_tokens: 1000,
temperature: 0.7,
};
const response = await chatService.chat.completions.create(params);
const response = await chatService.chat.completions.create(params, { maxRetries: 0 });
const data = { choices: response.choices };
if (delay) {
await wait(delay);
Expand Down
22 changes: 14 additions & 8 deletions apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const dataItemToMessage = (item) => ({
content: item.text,
});
const getMessageHistory = () => [...dataSource.items()].map(dataItemToMessage);
const getErrorMessage = (err) => {
if (err instanceof Error) return err.message;
if (typeof err === 'string') return err;
return 'Unknown error';
};
export const useApi = () => {
const [alerts, setAlerts] = useState([]);
const insertMessage = useCallback((data) => {
Expand All @@ -44,10 +49,10 @@ export const useApi = () => {
},
]);
}, []);
const alertLimitReached = useCallback(() => {
const alertError = useCallback((message) => {
setAlerts([
{
message: 'Request limit reached, try again in a minute.',
message,
},
]);
setTimeout(() => {
Expand All @@ -65,25 +70,26 @@ export const useApi = () => {
author: assistant,
text: aiResponse,
});
} catch {
alertLimitReached();
} catch (err) {
alertError(getErrorMessage(err));
}
},
[alertLimitReached, insertMessage],
[alertError, insertMessage],
);
const regenerateLastAIResponse = useCallback(async () => {
setAlerts([]);
const messageHistory = getMessageHistory();
updateLastMessageContent(REGENERATION_TEXT);
try {
const aiResponse = await getAIResponse(messageHistory.slice(0, -1));
if (typeof aiResponse === 'string') {
updateLastMessageContent(aiResponse);
}
} catch {
} catch (err) {
updateLastMessageContent(messageHistory.at(-1)?.content);
alertLimitReached();
alertError(getErrorMessage(err));
}
}, [alertLimitReached, updateLastMessageContent]);
}, [alertError, updateLastMessageContent]);
return {
alerts,
insertMessage,
Expand Down
20 changes: 13 additions & 7 deletions apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function renderAssistantMessage(text: string): void {
dataSource.store().push([{ type: 'insert', data: message }]);
}

function getErrorMessage(err: unknown): string {
if (err instanceof Error) return err.message;
if (typeof err === 'string') return err;
return 'Unknown error';
}

async function processMessageSending(
message: DxChatTypes.TextMessage,
event: Events.EventObject | undefined,
Expand All @@ -125,18 +131,18 @@ async function processMessageSending(
messages.push({ role: 'assistant', content: aiResponse });
renderAssistantMessage(aiResponse);
}, 200);
} catch {
} catch (err: unknown) {
typingUsers.value = [];
messages.pop();
alertLimitReached();
alertError(getErrorMessage(err));
} finally {
toggleDisabledState(false, event);
}
}

function alertLimitReached(): void {
function alertError(message: string): void {
alerts.value = [{
message: 'Request limit reached, try again in a minute.',
message,
}];

setTimeout(() => {
Expand All @@ -156,12 +162,11 @@ async function regenerate(): Promise<void> {
if (lastMessage?.content) {
lastMessage.content = aiResponse;
}
} catch {
} catch (err: unknown) {
if (lastMessage?.content) {
updateLastMessage(lastMessage.content);
}

alertLimitReached();
alertError(getErrorMessage(err));
} finally {
toggleDisabledState(false);
}
Expand All @@ -185,6 +190,7 @@ function onCopyButtonClick(message: { text: string }): void {
}

function onRegenerateButtonClick(): void {
alerts.value = [];
updateLastMessage();
regenerate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getAIResponse(messages: AIMessage[]): Promise<AIResponse>
temperature: 0.7,
};

const response = await chatService.chat.completions.create(params as any);
const response = await chatService.chat.completions.create(params as any, { maxRetries: 0 });
const data = { choices: response.choices };

return data.choices[0].message?.content || '';
Expand Down
25 changes: 14 additions & 11 deletions apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ $(() => {
temperature: 0.7,
};

const response = await chatService.chat.completions.create(params);
const response = await chatService.chat.completions.create(params, { maxRetries: 0 });
const data = { choices: response.choices };

return data.choices[0].message?.content;
}

function alertLimitReached() {
function getErrorMessage(err) {
if (err instanceof Error) return err.message;
if (typeof err === 'string') return err;
return 'Unknown error';
}

function alertError(message) {
instance.option({
alerts: [{
message: 'Request limit reached, try again in a minute.',
message,
}],
});

Expand Down Expand Up @@ -70,10 +76,10 @@ $(() => {

renderAssistantMessage(aiResponse);
}, 200);
} catch {
} catch (err) {
instance.option({ typingUsers: [] });
messages.pop();
alertLimitReached();
alertError(getErrorMessage(err));
} finally {
toggleDisabledState(false, event);
}
Expand All @@ -87,9 +93,9 @@ $(() => {

updateLastMessage(aiResponse);
messages.at(-1).content = aiResponse;
} catch {
} catch (err) {
updateLastMessage(messages.at(-1).content);
alertLimitReached();
alertError(getErrorMessage(err));
} finally {
toggleDisabledState(false);
}
Expand Down Expand Up @@ -143,10 +149,7 @@ $(() => {
}

function onRegenerateButtonClick() {
if (instance.option('alerts').length) {
return;
}

instance.option('alerts', []);
updateLastMessage();
regenerate();
}
Expand Down
Loading