OpenAI Function Calling with Thesus-AI
Thesus-AI provides a robust framework for implementing OpenAI function calling, offering powerful tools for web searches, code interpretation, and custom function execution.
Table of Contents
Introduction
Use Cases
Available Tools
Implementation Guide
Creating Function Calls
Handling Oracle Responses
Processing in Consumer Contract
Advanced Configuration
Introduction
Thesus-AI simplifies the integration of OpenAI function calling into your applications. With built-in support for various tools and extensibility options, you can create sophisticated AI-powered features with minimal setup.
Use Cases
Thesus-AI supports various function calling scenarios, including:
Dynamic Web Search Integration
Real-time query processing
Search result incorporation into responses
Python Code Execution
Direct code interpretation within smart contracts
Result processing and integration
Custom Function Extension
Add your own nodes with specialized functions
Expand default capabilities
Available Tools
Core Tools
Web Search - "websearch"
Enables real-time internet search capabilities
Returns formatted search results
Code Interpreter (Python) - "code_interpreter"
Executes Python code directly
Returns computation results
Handles various Python libraries
You can run your own node and add custom functions by following the run-a-node guide in the documentation.
Implementation Guide
Creating Function Calls
The first step involves initiating a function call to the Oracle contract using createFunctionCall:
createFunctionCall({
functionCallbackId,
functionType,
functionInput,
}: {
functionCallbackId: number;
functionType: string;
functionInput: string;
}): numberEnsure you maintain proper tracking of functionCallbackId to handle responses correctly.
Handling Oracle Responses
The Oracle node processes requests and responds through addFunctionResponse:
@call({})
addFunctionResponse({
functionId,
functionCallbackId,
response,
error,
}: {
functionId: number;
functionCallbackId: number;
response: string;
error: string;
}): NearPromise {
// Process response data
const promise = NearPromise.new(callbackAddress)
.functionCall(
"onOracleFunctionResponse",
JSON.stringify({
runId: functionCallbackId,
response: response,
errorMessage: error,
}),
BigInt(0),
HUNDRED_TGAS
)
}Processing in Consumer Contract
Implement onOracleFunctionResponse in your consumer contract to handle Oracle responses:
onOracleFunctionResponse({
runId,
response,
errorMessage,
}: {
runId: number;
response: string;
errorMessage: string;
}): NearPromise {
const promise = NearPromise.new(this.oracleAddress)
.functionCall(
"createOpenAiLlmCall",
JSON.stringify({
promptCallbackID: runId,
config: this.config,
}),
BigInt(0),
THIRTY_TGAS
)
.then(
NearPromise.new(near.currentAccountId()).functionCall(
"openAiLlmCallback",
JSON.stringify({
runId: runId,
}),
BigInt(0),
THIRTY_TGAS
)
);
return promise.asReturn();
}Advanced Configuration
Tool Configuration
To configure OpenAI function calling:
Add tool configurations in
configSet
tool_choice = "auto"for automatic tool selectionUse
tool_choice = nullfor manual tool selection
HINT : Remember to allocate sufficient gas for cross-contract calls, especially when dealing with complex function chains.
Best Practices
Always handle error cases in response callbacks
Maintain proper state management for function calls
Consider gas costs when chaining multiple functions
Implement proper logging for debugging purposes
Next Steps
Review the
run-a-nodeguide to set up your own nodeExplore custom function implementation
Join our community for support and discussions
You're now ready to implement OpenAI function calling with Thesus-AI! For additional support or questions, please refer to our community forums or documentation.
Last updated