UNPKG

aquanet-bot-lib

Version:

A library for building aquaculture chatbots with LLM integration

40 lines (39 loc) 1.35 kB
import { useState, useCallback } from 'react'; import { AquanetBot } from '../core/AquanetBot'; export function useAquanetBot({ config }) { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const bot = new AquanetBot({ config, framework: 'react' }); const chat = useCallback(async (messages, systemPrompt) => { try { setLoading(true); setError(null); const response = await bot.chat(messages, systemPrompt); return typeof response === 'string' ? response : response.choices[0]?.message?.content || ''; } catch (err) { const error = err instanceof Error ? err : new Error('Unknown error occurred'); setError(error); throw error; } finally { setLoading(false); } }, [bot]); const query = useCallback(async (prompt) => { try { setLoading(true); setError(null); return await bot.query(prompt); } catch (err) { const error = err instanceof Error ? err : new Error('Unknown error occurred'); setError(error); throw error; } finally { setLoading(false); } }, [bot]); return { loading, error, chat, query }; }