Build with AI oracle LLM calls

Oracle Functions in User Contract

Overview

These functions manage the interaction between the user contract and the Oracle, facilitating the sending and receiving of requests and responses. Each function performs a specific role in the process of handling prompts and function calls, ensuring that the responses from the Oracle are delivered back to the user contract efficiently.

Functions

1. createOpenAiLlmCall({ promptCallbackID, config }: { promptCallbackID: number; config: openAIRequest; }): number

This function initiates a request to the Oracle for processing an OpenAI prompt. It prepares and stores the necessary data to facilitate the callback from the Oracle.

Parameters

  • promptCallbackID: A unique identifier for tracking the callback associated with this prompt.

  • config: An object containing the configuration for the OpenAI request.

Behavior

  • Increments the prompt count and stores the callback address, prompt callback ID, and configuration.

  • Logs the details of the created call for Thesus Oracle node to capture and process

Returns

  • A number representing the promptId, which uniquely identifies the prompt request.


2. addOpenAiResponse({ promptId, promptCallbackID, response, error }: { promptId: number; promptCallbackID: number; response: openAIResponse; error: string; }): NearPromise

This function handles the response received from the Oracle for an OpenAI prompt request.

Parameters

  • promptId: The unique identifier of the prompt.

  • promptCallbackID: The callback ID associated with the prompt.

  • response: The response object returned by OpenAI.

  • error: An error message, if any, returned during processing.

Behavior

  • Marks the prompt as processed and retrieves the callback address.

  • Asserts that the callback address is valid.

  • Sends a cross-contract call to the user contract's onOracleOpenAiLlmResponse function, passing the response and error message.

Returns

  • A NearPromise representing the asynchronous execution of the callback.


3. createFunctionCall({ functionCallbackId, functionType, functionInput }: { functionCallbackId: number; functionType: string; functionInput: string; }): number

This function initiates a function call to the Oracle, preparing the necessary details for the callback.

Parameters

  • functionCallbackId: A unique identifier for tracking the callback associated with this function call.

  • functionType: A string indicating the type of function being called.

  • functionInput: A string representing the input data for the function.

Behavior

  • Increments the function count and stores the callback address, function callback ID, type, and input.

  • Logs the details of the created function call for Thesus Oracle node to capture and process

Returns

  • A number representing the functionId, which uniquely identifies the function call request.


4. addFunctionResponse({ functionId, functionCallbackId, response, error }: { functionId: number; functionCallbackId: number; response: string; error: string; }): NearPromise

This function processes the response received from the Oracle for a function call request.

Parameters

  • functionId: The unique identifier of the function call.

  • functionCallbackId: The callback ID associated with the function call.

  • response: The response data returned from the function execution.

  • error: An error message, if any, returned during processing.

Behavior

  • Marks the function call as processed and retrieves the callback address.

  • Asserts that the callback address is valid.

  • Sends a cross-contract call to the user contract's onOracleFunctionResponse function, passing the response and error message.

Returns

  • A NearPromise representing the asynchronous execution of the callback.


User Contract Functions

startChat(message: string): NearPromise {}

The startChat function initiates a new chat session. This function performs several preprocessing tasks, including:

  • Data Storage: It stores the input message and structures the necessary data for further processing.

  • Cross-Contract Call: It creates a cross-contract call to the Oracle using the createOpenAiLlmCall method, which handles the integration with OpenAI's language model.

Parameters

  • message: A string representing the user's input message to start the chat.

Returns

  • A NearPromise indicating the status of the transaction.


onOracleOpenAiLlmResponse({ runId, response, errorMessage }: { runId: number; response: openAIResponse; errorMessage: string; }): void

The onOracleOpenAiLlmResponse function is responsible for processing the response received from the Oracle after invoking the OpenAI language model. This function updates the chat session based on the response.

Parameters

  • runId: A number representing the unique identifier for the chat session.

  • response: An object containing the content returned by OpenAI.

  • errorMessage: A string that holds any error message returned during the response.

Behavior

  1. Authorization Check: Calls this.onlyOracle() to ensure that only the Oracle can invoke this function.

  2. Retrieve Chat Run: Fetches the chat run using runId. An assertion checks that the chat run exists.

  3. Message Handling:

    • If the last message in the run is not from the user, it logs a warning indicating that there is no user message to respond to.

    • If an errorMessage is provided, it creates a new message indicating the error and appends it to the chat run.

    • If there is no error, it creates a new message with the content from the response and appends it to the chat run.

  4. Update State: The updated chat run is stored back into the contract's state.

Returns

  • This function does not return a value; it modifies the internal state of the contract.


This documentation provides a concise overview of the key functions in your user contract, detailing their purpose, parameters, behavior, and return types.

Here is the detailed video of how the contracts works video

Last updated