Perplexica/ui/lib/actions.ts

32 lines
877 B
TypeScript
Raw Normal View History

2024-05-18 13:10:39 +05:30
import { Message } from '@/components/ChatWindow';
2024-09-25 16:06:28 -05:00
export const getSuggestions = async (chatHistory: Message[]) => {
try {
const chatModel = localStorage.getItem('chatModel');
const chatModelProvider = localStorage.getItem('chatModelProvider');
2024-05-18 13:10:39 +05:30
2024-09-25 16:06:28 -05:00
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/suggestions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_history: chatHistory,
chat_model: chatModel,
chat_model_provider: chatModelProvider,
}),
});
2024-05-18 13:10:39 +05:30
2024-09-25 16:06:28 -05:00
if (!res.ok) {
throw new Error(`Error: ${res.status} ${res.statusText}`);
}
2024-05-18 13:10:39 +05:30
2024-09-25 16:06:28 -05:00
const data = (await res.json()) as { suggestions: string[] };
return data.suggestions;
} catch (error) {
console.error('Error fetching suggestions:', error);
return [];
}
2024-05-18 13:10:39 +05:30
};