The way we've structured our Monad
class gives you a flexible setup where you can choose to interact directly with the OpenAI API through the openai
client object, or you can intercept and pre-handle requests by defining custom methods within the Monad
class itself.
-
Direct Access:
- When using
monad.openai
, you are directly accessing the OpenAI client without any intermediary logic. This means you cannot manipulate or pre-process the request before it is sent or handle the response before it is returned to the caller. - This is straightforward and ensures that you're using the OpenAI client as intended without modifications, but it also means you miss out on opportunities to add custom logic such as logging, error handling, transformations, or validations specific to your application's needs.
- When using
-
Managed Access:
- By defining custom methods in the
Monad
class, you can add a layer of abstraction where you manage or manipulate the data before sending it to OpenAI or after receiving a response from OpenAI. - This allows you to integrate business logic directly related to how your application needs to interact with OpenAI, providing a centralized point to add functionalities like input validation, error handling, logging, or even caching responses if necessary.
- By defining custom methods in the
Here’s how you can add a managed method to pre-handle requests or post-process responses:
import OpenAI from "openai";
class Monad {
constructor(apiKey) {
this.openai = new OpenAI({
apiKey: apiKey
});
console.log("Hello monads!");
}
// Custom method to handle requests with additional logic
async query(promptText, model = 'text-davinci-003', max_tokens = 150) {
console.log("Preparing to query OpenAI with:", promptText); // Pre-handle logging
try {
const completion = await this.openai.createCompletion({
model: model,
prompt: promptText,
max_tokens: max_tokens
});
console.log("Received response from OpenAI"); // Post-process logging
return completion.choices[0].text;
} catch (error) {
console.error("Error during OpenAI query:", error);
throw error; // Enhanced error handling
}
}
}
export default Monad;
You can now use this managed method, which provides additional logging and error handling, in your application:
import Monad from './monads.openai';
const monad = new Monad(process.env.OPENAI_API_KEY);
async function runQuery() {
try {
const response = await monad.query("How does the stock market work?");
console.log("Query response:", response);
} catch (error) {
console.error("Query failed:", error);
}
}
runQuery();
Or you can straight use the openai through the monad.openai:
async function listAssistants() { const myAssistants = await monad.openai.beta.assistants.list({ order: "desc", limit: "20", });
console.log(myAssistants.data); }
listAssistants();
By creating a layer of abstraction within your Monad
class, you gain the flexibility to add custom functionality and control over the requests and responses involving OpenAI's API. This setup not only allows for direct API interactions when needed but also provides the capability to inject application-specific logic as required, enhancing the integration of OpenAI services within your application's architecture.