GPTScript Execution
Motivation
The GPTScript execution mechanism is designed to execute JavaScript code within the context of a specific GPT model. This enables the model to leverage the power of JavaScript for:
- Dynamically interacting with the world: Fetching data from APIs, manipulating data structures, and generating dynamic content.
- Customizing the response format: Shaping the model’s output into a specific format for different applications.
- Integrating with external libraries: Leveraging pre-built JavaScript libraries for enhanced functionality.
Overview
The GPTScript execution process follows these steps:
- Code Injection: The user provides JavaScript code within a GPTScript context. This code is injected into a secure sandboxed environment.
- Execution: The injected JavaScript code is executed within the sandboxed environment, with access to the model’s context and the provided GPTScript functions.
- Result Retrieval: The results of the JavaScript execution are captured and returned to the user, either as a string or a structured data object.
GPTScript Functions
The following functions are available to the user within the GPTScript environment:
gpt.generateText(prompt, options)
: Generates text using the GPT model.gpt.getMemory(key)
: Retrieves a value from the model’s memory.gpt.setMemory(key, value)
: Stores a value in the model’s memory.
Code Examples
Example 1: Generating a formatted response:
gpt.generateText("Write a summary of the text below: {text}", {
maxTokens: 100,
}).then(response => {
console.log(`Here's a summary: ${response}`);
});
Example 2: Fetching data from an API:
fetch("https://api.example.com/data")
.then(response => response.json())
.then(data => {
console.log(`Data from API: ${data}`);
});
Example 3: Using external libraries:
const moment = require("moment");
console.log(`Current date: ${moment().format("YYYY-MM-DD")}`);
Important Notes:
- The GPTScript execution environment is sandboxed for security reasons. Access to external resources is restricted to prevent malicious code execution.
- The use of external libraries is limited to pre-approved libraries available within the sandboxed environment.
- For more detailed information on available functions and limitations, please refer to the GPTScript documentation: GPTScript Documentation
Source Code
The GPTScript execution codebase is located within the apps-client
repository:
apps/gpt-script/
: This directory contains the GPTScript engine and related components.
Future Directions
The GPTScript execution mechanism is under constant development, and new features and enhancements are planned for the future, such as:
- Enhanced security measures: Further tightening security protocols within the sandboxed environment.
- Extended library support: Expanding the library ecosystem available within GPTScript.
- Customizable execution environments: Providing users with greater control over the sandboxed environment’s configurations.