halo - v1.0.0
    Preparing search index...

    Class AiAgent

    AiAgent class for interacting with an AI agent via OpenAI API. It initializes the agent with a URL and API key, and allows starting a chat session. This class provides methods to start a chat session, generate text responses, and handle various tasks.

    Configuration for the AI agent.

    const agent = new AiAgent({
    agentUrl: "https://api.example.com/ai",
    apiKey: "your-api-key",
    model: "gpt-4o",
    fallbackModel: "gpt-3.5-turbo",
    systemPrompt: {
    text: "You are a helpful assistant. Answer the questions as best you can.",
    },
    tools: {
    getCurrentTime: TaskHandler.getCurrentTime
    }
    });
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Queries the AI agent with a prompt and optional session messages. It returns a ChatBuilder object that can be used to generate text or stream responses.

      Parameters

      • params: { media?: null | InlineData; prompt: string; session?: null | CoreMessage[] }
        • Optionalmedia?: null | InlineData

          Optional media data to include in the chat. This can be an inline data object containing file data and mime type.

        • prompt: string

          The user's message to start the chat. This should be a non-empty string representing the user's input.

        • Optionalsession?: null | CoreMessage[]

          An optional array of previous messages in the chat session. This allows the AI agent to continue the conversation with the existing context.

      Returns Promise<ChatBuilder>

      A ChatBuilder object with methods to generate text or stream responses.

      const { user, session, saveHistory } = await chatSession.useJSONFileSession({
      folderName: "sessions",
      sessionFileNameSuffix: username,
      user: {
      username: username,
      phone: phone,
      },
      })
      const chatBuilder = await agent.query({
      session: session,
      prompt: "Hi there!",
      media: null
      });
      const textResponse = await chatBuilder.generateText();
      const streamResponse = await chatBuilder.generateStream();
      const objectResponse = await chatBuilder.generateObject(
      z.object({
      answer: z.string()
      })
      );
      console.log(textResponse.text);
      console.log(streamResponse.textStream);
      console.log(objectResponse.object);

      Will throw an error if the prompt is not a string or if the session is not an array of CoreMessage.

    • Starts a chat session with the AI agent. It initializes the conversation and returns the AI's response.

      Parameters

      • params: {
            media?: null | InlineData;
            prompt: string;
            session?: null | CoreMessage[];
            streamMethod?: "text" | "stream";
        }
        • Optionalmedia?: null | InlineData

          Optional media data to include in the chat. This can be an inline data object containing file data and mime type. If not provided, the chat will not include any media.

        • prompt: string

          The user's message to start the chat. This should be a non-empty string representing the user's input. If the prompt is invalid or empty, an error will be thrown.

        • Optionalsession?: null | CoreMessage[]

          An optional array of previous messages in the chat session. This allows the AI agent to continue the conversation with the existing context. If not provided, a new chat session will be started.

        • OptionalstreamMethod?: "text" | "stream"

          The method to use for streaming responses. Can be "stream" for streaming responses or "text" for text responses. Default is "text".

      Returns Promise<StartChatResult>

      A promise that resolves to the AI's response, which can be either a text response or a stream of text responses.

      Will throw an error if the prompt is not a string or if the session is invalid.