a MongoDB implementation of @k-apps-io/llm-dsl ChatStorage
To install the package, run:
npm install @k-apps-io/llm-dsl-mongo
By default, the collection name is chat
. However, this can be customized using the MONGO_CHAT_COLLECTION
environment variable or by specifying the collection name directly when creating the model. See the example below for more details.
import mongoose from "mongoose";
import { DSL } from "@k-apps-io/llm-dsl";
import { ChatGPT, Options } from "@k-apps-io/llm-dsl-chatgpt";
import { useMongoStorage, Schema } from "@k-apps-io/llm-dsl-mongo";
// Define a Mongoose model for the chat storage
const Chat = mongoose.model("Chat", Schema, "myCollectionName");
// Initialize the MongoDB storage mechanism
const MongoStorage = useMongoStorage({ model: Chat });
// Set up the DSL with ChatGPT and MongoDB storage
const chat = new DSL<Options, any>({
llm: new ChatGPT({}), // Configure ChatGPT with your desired options
storage: MongoStorage,
options: {
model: "gpt-4o-mini", // Specify the model to use
},
});
// Use the DSL to send a prompt and process the response
chat
.prompt({ message: "hello" })
.stream((chunk) => {
if (chunk.type === "message") process.stdout.write(chunk.content);
})
.then(() => console.log("Done"))
.catch((error) => {
console.error("Error:", error);
throw error;
});