hashbrown

useStructuredChat

This React hook creates a chat instance used to interact with the LLM. The result object contains functions and state enabling you to send and receive messages and monitor the state of the chat.

The useStructuredChat hook provides functionality for structured chats. Structured chats are used when you want to use the LLM to generate structured data according to a defined schema. This is particularly useful for: - Generating typed data structures - Creating form responses - Building UI components - Extracting information into a specific format

API

export declare function useStructuredChat<
  Schema extends  s.HashbrownType ,
  Tools extends  Chat.AnyTool ,
  Output extends
     s.Infer <Schema> =  s.Infer <Schema>,
>(
  options:  UseStructuredChatOptions <
    Schema,
    Tools,
    Output
  >,
):  UseStructuredChatResult <Output, Tools>;
useStructuredChat
UseStructuredChatResult<Output, Tools>
@paramoptions:
UseStructuredChatOptions<Schema, Tools, Output>
@typeSchema
s.HashbrownType
@typeTools
Chat.AnyTool
@typeOutput
s.Infer<Schema>
@returns
UseStructuredChatResult<Output, Tools>

Examples

In this example, the LLM will respond with a JSON object containing the translations of the input message into English, Spanish, and French.

const { messages, sendMessage } = useStructuredChat({
  model: 'gpt-4o',
  system: 'You are a helpful translator that provides accurate translations.',
  schema: s.object('Translations', {
    english: s.string('English translation'),
    spanish: s.string('Spanish translation'),
    french: s.string('French translation')
  }),
});