diff --git a/.vscode/settings.json b/.vscode/settings.json index ce350ca2fe..5e26e47785 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,7 @@ "comkey", "cooldown", "cooldowns", + "datafile", "Deduplicator", "Dockerized", "docpath", diff --git a/frontend/package.json b/frontend/package.json index a5f754a3c6..fa40e7b33a 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -68,4 +68,4 @@ "tailwindcss": "^3.3.1", "vite": "^4.3.0" } -} \ No newline at end of file +} diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index dcf3c5f9e3..3737541f24 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -63,6 +63,7 @@ const ExperimentalFeatures = lazy( const LiveDocumentSyncManage = lazy( () => import("@/pages/Admin/ExperimentalFeatures/Features/LiveSync/manage") ); +const FineTuningWalkthrough = lazy(() => import("@/pages/FineTuning")); export default function App() { return ( @@ -186,6 +187,11 @@ export default function App() { path="/settings/beta-features/live-document-sync/manage" element={} /> + + } + /> diff --git a/frontend/src/components/SettingsSidebar/index.jsx b/frontend/src/components/SettingsSidebar/index.jsx index bde09e9053..9fa6fd6112 100644 --- a/frontend/src/components/SettingsSidebar/index.jsx +++ b/frontend/src/components/SettingsSidebar/index.jsx @@ -21,6 +21,7 @@ import { useTranslation } from "react-i18next"; import showToast from "@/utils/toast"; import System from "@/models/system"; import Option from "./MenuOption"; +import { FineTuningAlert } from "@/pages/FineTuning/Banner"; export default function SettingsSidebar() { const { t } = useTranslation(); @@ -132,48 +133,53 @@ export default function SettingsSidebar() { } return ( -
- - Logo - -
-
-
- {t("settings.title")} -
-
-
-
- -
- - - {t("settings.privacy")} - + <> +
+ + Logo + +
+
+
+ {t("settings.title")} +
+
+
+
+ +
+ + + {t("settings.privacy")} + +
-
-
-
+
+
+
-
+ + ); } diff --git a/frontend/src/index.css b/frontend/src/index.css index 830d8ea672..94b30bdf7a 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -766,3 +766,31 @@ does not extend the close button beyond the viewport. */ display: none; } } + +.top-banner { + animation: popTop 500ms forwards; +} + +@keyframes popTop { + 0% { + top: -3.5rem; + } + + 100% { + top: 0px; + } +} + +.rm-top-banner { + animation: rmPopTop 500ms forwards; +} + +@keyframes rmPopTop { + 0% { + top: 0px; + } + + 100% { + top: -3.5rem; + } +} diff --git a/frontend/src/models/experimental/fineTuning.js b/frontend/src/models/experimental/fineTuning.js new file mode 100644 index 0000000000..3c4d90bcc0 --- /dev/null +++ b/frontend/src/models/experimental/fineTuning.js @@ -0,0 +1,129 @@ +import { API_BASE } from "@/utils/constants"; +import { baseHeaders, safeJsonParse } from "@/utils/request"; + +const FineTuning = { + cacheKeys: { + dismissed_cta: "anythingllm_dismissed_fine_tune_notif", + eligibility: "anythingllm_can_fine_tune", + }, + + /** + * Get the information for the Fine-tuning product to display in various frontends + * @returns {Promise<{ + * productDetails: { + * name: string, + * description: string, + * icon: string, + * active: boolean, + * }, + * pricing: { + * usd: number, + * }, + * availableBaseModels: string[] + * }>} + */ + info: async function () { + return await fetch(`${API_BASE}/experimental/fine-tuning/info`, { + method: "GET", + headers: baseHeaders(), + }) + .then((res) => { + if (!res.ok) throw new Error("Could not get model info."); + return res.json(); + }) + .then((res) => res) + .catch((e) => { + console.error(e); + return null; + }); + }, + datasetStat: async function ({ slugs = [], feedback = null }) { + return await fetch(`${API_BASE}/experimental/fine-tuning/dataset`, { + method: "POST", + headers: baseHeaders(), + body: JSON.stringify({ slugs, feedback }), + }) + .then((res) => { + if (!res.ok) throw new Error("Could not get dataset info."); + return res.json(); + }) + .then((res) => res) + .catch((e) => { + console.error(e); + return { count: null }; + }); + }, + /** + * Generates Fine-Tuning order. + * @param {{email:string, baseModel:string, modelName: string, trainingData: {slugs:string[], feedback:boolean|null}}} param0 + * @returns {Promise<{checkoutUrl:string, jobId:string}|null>} + */ + createOrder: async function ({ email, baseModel, modelName, trainingData }) { + return await fetch(`${API_BASE}/experimental/fine-tuning/order`, { + method: "POST", + headers: baseHeaders(), + body: JSON.stringify({ + email, + baseModel, + modelName, + trainingData, + }), + }) + .then((res) => { + if (!res.ok) throw new Error("Could not order fine-tune."); + return res.json(); + }) + .then((res) => res) + .catch((e) => { + console.error(e); + return null; + }); + }, + + /** + * Determine if a user should see the CTA alert. In general this alert + * Can only render if the user is empty (single user) or is an admin role. + * @returns {boolean} + */ + canAlert: function (user = null) { + if (!!user && user.role !== "admin") return false; + return !window?.localStorage?.getItem(this.cacheKeys.dismissed_cta); + }, + checkEligibility: async function () { + const cache = window.localStorage.getItem(this.cacheKeys.eligibility); + if (!!cache) { + const { data, lastFetched } = safeJsonParse(cache, { + data: null, + lastFetched: 0, + }); + if (!!data && Date.now() - lastFetched < 1.8e7) + // 5 hours + return data.eligible; + } + + return await fetch(`${API_BASE}/experimental/fine-tuning/check-eligible`, { + method: "GET", + headers: baseHeaders(), + }) + .then((res) => { + if (!res.ok) throw new Error("Could not check if eligible."); + return res.json(); + }) + .then((res) => { + window.localStorage.setItem( + this.cacheKeys.eligibility, + JSON.stringify({ + data: { eligible: res.eligible }, + lastFetched: Date.now(), + }) + ); + return res.eligible; + }) + .catch((e) => { + console.error(e); + return false; + }); + }, +}; + +export default FineTuning; diff --git a/frontend/src/pages/FineTuning/Banner/index.jsx b/frontend/src/pages/FineTuning/Banner/index.jsx new file mode 100644 index 0000000000..cab896d619 --- /dev/null +++ b/frontend/src/pages/FineTuning/Banner/index.jsx @@ -0,0 +1,66 @@ +import { useEffect, useState } from "react"; +import useUser from "@/hooks/useUser"; +import FineTuning from "@/models/experimental/fineTuning"; +import { createPortal } from "react-dom"; +import { Sparkle } from "@phosphor-icons/react"; +import { Link, useLocation } from "react-router-dom"; +import paths from "@/utils/paths"; + +export function FineTuningAlert() { + const { user } = useUser(); + const location = useLocation(); + const [className, setClassName] = useState("top-banner"); + const [isEligible, setIsEligible] = useState(false); + + function dismissAlert() { + setClassName("rm-top-banner"); + window?.localStorage?.setItem(FineTuning.cacheKeys.dismissed_cta, "1"); + setTimeout(() => { + setIsEligible(false); + }, 550); + } + + useEffect(() => { + if (!FineTuning.canAlert(user)) return; + if ( + location.pathname === paths.orderFineTune() || + location.pathname === paths.settings.chats() + ) + return; + FineTuning.checkEligibility() + .then((eligible) => setIsEligible(eligible)) + .catch(() => null); + }, [user]); + + if (!isEligible) return null; + return createPortal( +
+ +
+
+ +

+ You have enough data for a fine-tune! +

+
+

click to learn more →

+
+ +
+ +
+
, + document.getElementById("root") + ); +} diff --git a/frontend/src/pages/FineTuning/Steps/Confirmation/index.jsx b/frontend/src/pages/FineTuning/Steps/Confirmation/index.jsx new file mode 100644 index 0000000000..acf4b4091e --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/Confirmation/index.jsx @@ -0,0 +1,147 @@ +import FineTuning from "@/models/experimental/fineTuning"; +import { dollarFormat } from "@/utils/numbers"; +import showToast from "@/utils/toast"; +import { CheckCircle } from "@phosphor-icons/react"; +import { useState } from "react"; +import FineTuningSteps from "../index"; + +/** + * @param {{settings: import("../index").OrderSettings}} param0 + * @returns + */ +export default function Confirmation({ settings, setSettings, setStep }) { + const [loading, setLoading] = useState(false); + async function handleCheckout() { + setLoading(true); + const data = await FineTuning.createOrder({ + email: settings.email, + baseModel: settings.baseModel, + modelName: settings.modelName, + trainingData: { + slugs: settings.trainingData.slugs, + feedback: settings.trainingData.feedback, + }, + }); + + if (!data) { + setLoading(false); + showToast("Could not generate new order.", "error", { clear: true }); + return; + } + + window.open(data.checkoutUrl, "_blank"); + setSettings((prev) => { + return { + ...prev, + jobId: data.jobId, + checkoutUrl: data.checkoutUrl, + }; + }); + setStep(FineTuningSteps.confirmation.next()); + } + + return ( +
+
+
+

Confirm & Submit

+

+ Below are your fine-tuning order details. If you have any questions + before or after ordering your fine-tune you can{" "} + + checkout the fine-tuning FAQ + {" "} + or email{" "} + + team@mintplexlabs.com + + . +

+
+
+

Contact e-mail:

+

{settings.email}

+
+
+

Base LLM:

+

{settings.baseModel}

+
+
+

Output model name:

+

"{settings.modelName}"

+
+
+
+

Training on workspaces:

+ {settings.trainingData.slugs.map((slug, i) => { + return ( +

+ "{slug}" + {i !== settings.trainingData.slugs.length - 1 ? "," : ""} +

+ ); + })} +
+ {settings.trainingData.feedback === true ? ( +

+ training on positive-feedback chats only. +

+ ) : ( +

+ training on all chats. +

+ )} +
+ +
+
+ +

Agreed to Terms and Conditions

+
+
+ +

Understand privacy & data handling

+
+
+ +

Agreed to Fulfillment terms

+
+ +
+
+

Total one-time cost:

+

+ {dollarFormat(settings.tuningInfo.pricing.usd)} + * +

+
+

+ * price does not include any coupons, incentives, or + discounts you can apply at checkout. +

+
+
+

+ Once you proceed to checkout, if you do not complete this purchase + your data will be deleted from our servers within 1 hour of + abandonment of the creation of the checkout in accordance to our + privacy and data handling policy. +

+
+ + +
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/DataUpload/index.jsx b/frontend/src/pages/FineTuning/Steps/DataUpload/index.jsx new file mode 100644 index 0000000000..d4cec7ea45 --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/DataUpload/index.jsx @@ -0,0 +1,295 @@ +import { useEffect, useState } from "react"; +import FineTuning from "@/models/experimental/fineTuning"; +import Workspace from "@/models/workspace"; +import { CheckCircle, Warning, X } from "@phosphor-icons/react"; +import FineTuningSteps from ".."; + +export default function DataUpload({ setSettings, setStep }) { + const [workspaces, setWorkspaces] = useState([]); + const [dataFilters, setDataFilters] = useState({ + workspaces: [], + feedback: null, + }); + + useEffect(() => { + Workspace.all() + .then((workspaces) => { + const workspaceOpts = workspaces.map((ws) => { + return { slug: ws.slug, name: ws.name }; + }); + setWorkspaces(workspaceOpts); + setDataFilters((prev) => { + return { ...prev, workspaces: workspaceOpts }; + }); + }) + .catch(() => null); + }, []); + + async function handleSubmit(e) { + e.preventDefault(); + setSettings((prev) => { + return { + ...prev, + trainingData: { + slugs: dataFilters.workspaces.map((ws) => ws.slug), + feedback: dataFilters.feedback, + }, + }; + }); + setStep(FineTuningSteps["data-selection"].next()); + } + + return ( +
+
+
+
+

+ Select your training dataset. +

+

+ This is the data your model will be trained and tuned on. This is + a critical step and you should always train on the exact + information you want the model to inherit. By default, AnythingLLM + will use all chats, but you can filter chats by workspace and even + limit training to chats which users have left a positive feedback + indication on (thumbs up). +

+ +
+
+ +

+ Enabling this toggle will filter your dataset to only use + "positive" responses that were marked during chatting. +

+
+ +
+ +
+
+ +

+ You training data will be limited to these workspaces. +

+
+ +
+ +
+ + +
+
+
+ ); +} + +function WorkspaceSelector({ + workspaces = [], + selectedWorkspaces = [], + setDataFilters, +}) { + const [query, setQuery] = useState(""); + const [showSuggestions, setShowSuggestions] = useState(false); + const availableWorkspaces = workspaces.filter( + (ws) => + !selectedWorkspaces.find((selectedWs) => selectedWs.slug === ws.slug) + ); + + function handleAddWorkspace(workspace) { + setDataFilters((prev) => { + return { + ...prev, + workspaces: [...prev.workspaces, workspace], + }; + }); + setQuery(""); + setShowSuggestions(false); + } + + function handleRemoveWorkspace(workspace) { + setDataFilters((prev) => { + const filtered = prev.workspaces.filter( + (ws) => ws.slug !== workspace.slug + ); + return { + ...prev, + workspaces: filtered, + }; + }); + setQuery(""); + setShowSuggestions(false); + } + + return ( +
+
+
+
+
+ {selectedWorkspaces.map((workspace) => { + return ( +
+
+ {workspace.name} +
+
+ +
+
+ ); + })} +
+ setQuery(e.target.value)} + onFocus={() => setShowSuggestions(true)} + onBlur={() => + setTimeout(() => { + setShowSuggestions(false); + }, 500) + } + placeholder="Enter a workspace name" + className="w-[200px] bg-transparent p-1 px-2 appearance-none outline-none h-full w-full text-white" + /> +
+
+
+
+ {showSuggestions && ( +
+
+ +
+
+ )} +
+
+ ); +} + +function WorkspaceSuggestions({ + availableWorkspaces = [], + addWorkspace, + query = "", +}) { + if (availableWorkspaces.length === 0) { + return ( +
+

+ no workspaces available to select. +

+
+ ); + } + + const filteredWorkspace = !!query + ? availableWorkspaces.filter((ws) => { + return ( + ws.slug.toLowerCase().includes(query.toLowerCase()) || + ws.name.toLowerCase().includes(query.toLowerCase()) + ); + }) + : availableWorkspaces; + + return ( +
+ {filteredWorkspace.map((workspace) => { + return ( + + ); + })} +
+ ); +} + +function DatasetSummary({ workspaces = [], feedback = null }) { + const [stats, setStats] = useState({ count: null, recommendedMin: 50 }); + useEffect(() => { + function getStats() { + const slugs = workspaces?.map((ws) => ws.slug); + if (!slugs || slugs.length === 0) return; + + FineTuning.datasetStat({ slugs, feedback }) + .then((stats) => setStats(stats)) + .catch((e) => null); + } + getStats(); + }, [workspaces, feedback]); + + return ( +
+

Training dataset size: {stats.count ?? "Unknown"}

+ {stats.count < stats.recommendedMin ? ( +
+ +

+ Your dataset is below the recommended minimum of{" "} + {stats.recommendedMin}! You may see no impact from a fine-tune. +

+
+ ) : ( +
+ +

+ Your dataset is large enough that you should see good results from a + fine-tune. +

+
+ )} +
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/FulfillmentPolicy/index.jsx b/frontend/src/pages/FineTuning/Steps/FulfillmentPolicy/index.jsx new file mode 100644 index 0000000000..950668c79f --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/FulfillmentPolicy/index.jsx @@ -0,0 +1,129 @@ +import FineTuningSteps from ".."; + +export default function Fulfillment({ setSettings, setStep }) { + const handleAccept = () => { + setSettings((prev) => { + return { ...prev, agreedToTerms: true }; + }); + setStep(FineTuningSteps.fulfillment.next()); + }; + + return ( +
+
+
+

+ Fulfillment Policy +

+

+ Fulfillment of a fine-tune model is straight-forward. We do not host + your model. We provide you a download link to run the model in a + standard format where ever you run local LLMs +

+
+

+ Fulfillment Terms +

+

+ Last updated: July 15, 2024 +

+ +

+ These fulfillment terms outline the agreement between Mintplex + Labs Inc. (“Company,” “we,” “us,” or “our”) and the customer + regarding the creation and delivery of fine-tuned models. +

+ +

+ Delivery of Model +

+

+ Upon completion of a fine-tuning job, we will deliver a download + link to a .gguf model file suitable for LLM text inferencing. The + customer acknowledges that this exchange is strictly transactional + and non-recurring. Once the model file is delivered, the agreement + is considered concluded and will be ineligible for a refund. +

+ +

Support

+

+ Please note that the delivery of the model does not include any + dedicated support. Customers are encouraged to refer to available + documentation and resources for guidance on using the model. +

+ +

+ Requesting Download Links +

+

+ Customers may request refreshed download links from + my.mintplexlabs.com as long as the model is retained in our cloud + storage. We will retain a model in our storage for a maximum of 3 + months or until the customer requests its removal. All download + links are valid for 24 hours. +

+ +

+ Cancellation and Refunds +

+

+ Mintplex Labs Inc. reserves the right to cancel any fine-tuning + job at our discretion. In the event of a cancellation, a refund + may be issued. Additionally, we reserve the right to deny a + payment from the Customer or issue refunds for any reason without + cause or notice to the Customer. +

+ +

No Guarantees

+

+ Mintplex Labs Inc. makes NO GUARANTEES regarding + the resulting model's output, functionality, speed, or + compatibility with your tools, infrastructure and devices. Refund + requests of this nature are not eligible for refunds. +

+

+ Models are delivered and accepted in "As-Is" condition. All + delivered model and output files are deemed final and + non-refundable for any reason after training is complete and a + model has been generated. +

+ +

Payment Terms

+

+ All payments are required prior to the commencement of the + fine-tuning process. Customers are responsible for ensuring that + valid payment information is provided. Checkout sessions not + completed within 1 hour of creation will be considered as + abandoned and will be deleted from our system. +

+ +

+ Denial of Service for Payment Reasons +

+

+ Mintplex Labs Inc. reserves the right to deny service to any + customer with an outstanding balance or invalid payment + information. If any discrepancies arise regarding payment or + usage, we may suspend services until the matter is resolved. +

+ +

Contact

+

+ For any questions related to payment or fulfillment of services, + please contact us at{" "} + team@mintplexlabs.com. +

+
+
+ + +
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/Introduction/index.jsx b/frontend/src/pages/FineTuning/Steps/Introduction/index.jsx new file mode 100644 index 0000000000..7b2a0b199d --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/Introduction/index.jsx @@ -0,0 +1,110 @@ +import { CheckCircle, XCircle } from "@phosphor-icons/react"; +import FineTuningSteps from ".."; + +export default function Introduction({ setSettings, setStep }) { + const handleAccept = () => { + setSettings((prev) => { + return { ...prev, agreedToTerms: true }; + }); + setStep(FineTuningSteps.intro.next()); + }; + + return ( +
+
+
+

+ What is a "Fine-Tuned" model? +

+
+

+ Fine-tuned models are basically "customized" + Language-Learning-Models (LLMs). These can be based on popular + open-source foundational models like LLama3 8B or even some + closed source models like GPT-3.5. +

+

+ Typically, you would use an open-source model - you probably are + using one right now with AnythingLLM! +

+

+ When you create a custom fine-tune with AnythingLLM we will train + a custom base model on your specific data already inside of this + AnythingLLM instance and give you back a GGUF file + you can then load back into tools like Ollama, LMStudio, and + anywhere else you use local LLMs. +

+
+ +
+

+ When should I get a fine-tuned model? +

+

+ Fine-tuned models are perfect for when you need any of the + following +

+
    +
  • + Setting the style, + tone, format, or other qualitative aspects without prompting +
  • +
  • + Improving reliability + at producing a desired output +
  • +
  • + Correcting failures + to follow complex prompts, citations, or lack of background + knowledge +
  • +
  • + You want to run this + model privately or offline +
  • +
+
+ +
+

+ What are fine-tunes bad for? +

+

+ Fine-tuned models powerful, but they are not the "silver bullet" + to any issues you have with RAG currently. Some notable + limitations are +

+
    +
  • + You need perfect recall of + some piece of literature or reference document +
  • +
  • + You want your model to have + perfect memory or recollection +
  • +
+
+ +
+

+ In summary, if you are getting good results with RAG currently, + creating a fine-tune can squeeze even more performance out + of a model. Fine-Tunes are are for improving response quality and + general responses, but they are not for knowledge recall - + that is what RAG is for! Together, it is a powerful combination. +

+
+
+ + +
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/OrderDetails/index.jsx b/frontend/src/pages/FineTuning/Steps/OrderDetails/index.jsx new file mode 100644 index 0000000000..7a390dbab3 --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/OrderDetails/index.jsx @@ -0,0 +1,138 @@ +import FineTuning from "@/models/experimental/fineTuning"; +import { useEffect, useState } from "react"; +import FineTuningSteps from ".."; +import { CircleNotch } from "@phosphor-icons/react/dist/ssr"; + +export default function OrderDetails({ setSettings, setStep }) { + const [info, setInfo] = useState({}); + useEffect(() => { + FineTuning.info() + .then((res) => { + setInfo(res); + setSettings((prev) => { + return { ...prev, tuningInfo: res }; + }); + }) + .catch(() => setInfo({})); + }, []); + + async function handleSubmit(e) { + e.preventDefault(); + const form = new FormData(e.target); + setSettings((prev) => { + return { + ...prev, + email: form.get("email"), + baseModel: form.get("baseModel"), + modelName: form.get("modelName"), + }; + }); + setStep(FineTuningSteps["order-details"].next()); + } + + return ( +
+
+
+
+

+ Time to create your fine tune! +

+

+ Creating a model is quite simple. Currently we have a limited base + model selection, however in the future we plan to expand support + to many more foundational models. +

+ +
+
+ +

+ This e-mail is where you will receive all order information + and updates. This e-mail must be accurate or else we + won't be able to contact you with your fine-tuned model! +

+
+ +
+ +
+
+ +

+ This is the foundational model your fine-tune will be based + on. We recommend Llama 3 8B. +

+
+ {info.hasOwnProperty("availableBaseModels") ? ( + + ) : ( +
+ +

fetching available models...

+
+ )} +
+ +
+
+ +

+ What would you like to call your model? This has no impact on + its output or training and is only used for how we communicate + with you about the model. +

+
+ +
+
+ +
+
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/OrderPlaced/index.jsx b/frontend/src/pages/FineTuning/Steps/OrderPlaced/index.jsx new file mode 100644 index 0000000000..018b8fba10 --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/OrderPlaced/index.jsx @@ -0,0 +1,69 @@ +export default function OrderPlaced({ settings }) { + return ( +
+
+
+

+ Your order is placed! +

+ +
+

+ Your fine-tune will begin once payment is complete. If the payment + window did not automatically open - your checkout link is below. +

+ + {new URL(settings.checkoutUrl).origin} + +

+ Your fine-tune does not begin until this payment is completed. +

+
+ +
+

+ Reference: {settings.jobId} +

+

+ This reference id is how we will communicate with you about your + fine-tune training status. Save this reference id. +

+
+ +
+

+ Contact: {settings.email} +

+

+ Check the email above for order confirmation, status updates, and + more. Mintplex Labs will only contact you about your order via + email. +

+
+ + + +

+ You can close this window or navigate away once you see the + confirmation email in your inbox. +

+
+
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/Privacy/index.jsx b/frontend/src/pages/FineTuning/Steps/Privacy/index.jsx new file mode 100644 index 0000000000..6e0d5e9807 --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/Privacy/index.jsx @@ -0,0 +1,233 @@ +import FineTuningSteps from ".."; + +export default function PrivacyHandling({ setSettings, setStep }) { + const handleAccept = () => { + setSettings((prev) => { + return { ...prev, agreedToPrivacy: true }; + }); + setStep(FineTuningSteps.privacy.next()); + }; + + return ( +
+
+
+

+ Data Handling Policy & Privacy +

+

+ Please accept the terms and conditions to continue with creation and + ordering of a fine-tune model. We take the handling of your data + very seriously and will only use your uploaded data for training the + model, after the model is created or the order is concluded, + completed, or canceled your information is automatically and + permanently deleted. +

+
+

Privacy Policy

+ +

+ Mintplex Labs Inc. +

+

Effective Date: July 15, 2024

+ +

+ 1. Introduction +

+

+ Welcome to Mintplex Labs Inc. ("we", "our", "us"). We are + committed to protecting your privacy and ensuring the security of + your personal information. This Privacy Policy describes how we + collect, use, and protect your information when you use our + services. +

+ +

+ 2. Information We Collect +

+

+ When you place an order with us for tuning and large language + model (LLM) fulfillment, we collect certain personal information + from you, including but not limited to: +

+
    +
  • Email address
  • +
  • Payment information
  • +
  • Uploaded training data
  • +
+ +

+ 3. Use of Information +

+

We use the information we collect for the following purposes:

+
    +
  • To process and fulfill your order
  • +
  • To communicate with you regarding your order
  • +
  • To improve our services
  • +
+ +

+ 4. Data Retention and Deletion +

+

+ Uploaded training data is only retained for the duration of the + model training. Upon training completion, failure, or order + cancellation, the user data is permanently deleted from our + storage. +

+

+ If you partially complete the order flow and do not finalize your + order, any details and information associated with your order will + be deleted 1 hour from abandonment. +

+

+ After you confirm receipt of your resulting model files, you can + request us to delete your model from our storage at any time. + Additionally, we may proactively reach out to you to confirm that + you have received your model so we can delete it from storage. Our + model file retention policy is 3 months, after which we will + contact you to confirm receipt so we can remove the model from our + storage. +

+ +

+ 5. Data Storage and Security +

+

+ Our cloud storage provider is AWS. We have implement standard + encryption and protection policies to ensure the security of your + data. The storage solution has no public access, and all requests + for download URLs are pre-validated and signed by a minimal trust + program. Download URLs for the model file and associated outputs + are valid for 24 hours at a time. After expiration you can produce + refreshed links from https://my.mintplexlabs.com using the same + e-mail you used during checkout. +

+ +

+ 6. Payment Processing +

+

+ We use Stripe as our payment processor. Your email may be shared + with Stripe for customer service and payment management purposes. +

+ +

+ 7. Data Sharing +

+

+ We do not sell or share your personal information with third + parties except as necessary to provide our services, comply with + legal obligations, or protect our rights. +

+ +

+ 8. Your Rights +

+

+ You have the right to access, correct, or delete your personal + information. If you wish to exercise these rights, please contact + us at{" "} + team@mintplexlabs.com. +

+ +

+ 9. California Privacy Rights +

+

+ Under the California Consumer Privacy Act as amended by the + California Privacy Rights Act (the “CCPA”), California residents + have additional rights beyond what is set out in this privacy + notice: +

+
    +
  • + Right to Know: You have the right to request + information about the categories and specific pieces of personal + information we have collected about you, as well as the + categories of sources from which the information is collected, + the purpose for collecting such information, and the categories + of third parties with whom we share personal information. +
  • +
  • + Right to Delete: You have the right to request + the deletion of your personal information, subject to certain + exceptions. +
  • +
  • + Right to Correct: You have the right to request + the correction of inaccurate personal information that we have + about you. +
  • +
  • + Right to Opt-Out: You have the right to opt-out + of the sale of your personal information. Note, however, that we + do not sell your personal information. +
  • +
  • + Right to Non-Discrimination: You have the right + not to receive discriminatory treatment for exercising any of + your CCPA rights. +
  • +
+

+ Submitting a Request: +
+ You may submit a request to know, delete, or correct your personal + information by contacting us at{" "} + team@mintplexlabs.com. + We will confirm your identity before processing your request and + respond within 45 days. If more time is needed, we will inform you + of the reason and extension period in writing. You may make a + request for your information twice every 12 months. If you are + making an erasure request, please include details of the + information you would like erased. +

+

+ Please note that if you request that we remove your information, + we may retain some of the information for specific reasons, such + as to resolve disputes, troubleshoot problems, and as required by + law. Some information may not be completely removed from our + databases due to technical constraints and regular backups. +

+

+ We will not discriminate against you for exercising any of your + CCPA rights. +

+ +

+ 10. Contact Us +

+

+ If you have any questions or concerns about this Privacy Policy, + please contact us at{" "} + team@mintplexlabs.com. +

+ +

+ 11. Changes to This Privacy Policy +

+

+ We may update this Privacy Policy from time to time. We will + notify you of any changes by posting the new Privacy Policy on our + website. You are advised to review this Privacy Policy + periodically for any changes. +

+

+ By using our services, you agree to the terms of this Privacy + Policy. +

+
+
+ + +
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/TermsAndConditions/index.jsx b/frontend/src/pages/FineTuning/Steps/TermsAndConditions/index.jsx new file mode 100644 index 0000000000..339c43de3d --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/TermsAndConditions/index.jsx @@ -0,0 +1,188 @@ +import FineTuningSteps from ".."; + +export default function TermsAndConditions({ setSettings, setStep }) { + const handleAccept = () => { + setSettings((prev) => { + return { ...prev, agreedToTerms: true }; + }); + setStep(FineTuningSteps.tos.next()); + }; + + return ( +
+
+
+

+ Terms and Conditions +

+

+ Please accept the terms and conditions to continue with creation and + ordering of a fine-tune model. +

+
+

+ Mintplex Labs Inc. Fine-Tuning Terms of Service +

+

+ Last Updated: July 15, 2024 +

+ +

+ This Agreement is between Mintplex Labs Inc. ("Company") and the + customer ("Customer") accessing or using the services provided by + the Company. By signing up, accessing, or using the services, + Customer indicates its acceptance of this Agreement and agrees to + be bound by the terms and conditions outlined below. +

+ +

+ 1. Services Provided +

+

+ Mintplex Labs Inc. provides model fine-tuning services for + customers. The deliverable for these services is a download link + to the output ".GGUF" file that can be used by the Customer for + Large-Language text inferencing. +

+ +

+ 2. Payment Terms +

+
    +
  • + One-Time Payment: A one-time payment is + required before the execution of the training. +
  • +
  • + Payment Due Date: Payment is due upon order + placement. +
  • +
  • + Refund Policy: Payments are refundable in the + event of training failure or if the Company fails to deliver the + complete model file to the Customer. +
  • +
+ +

+ 3. Order Form +

+
    +
  • + Service: Model fine-tuning +
  • +
  • + Payment Amount: As specified in the order form +
  • +
  • + Payment Due Date: Upon order placement +
  • +
+ +

+ 4. Customer Responsibilities +

+

+ The Customer must provide all necessary data and information + required for model fine-tuning. +

+

+ The Customer must ensure timely payment as per the terms mentioned + above. +

+

+ The Customer understands the data collected for tuning will be + stored to a private cloud storage location temporarily while + training is in progress. +

+

+ The Customer understands the data collected for tuning will be + fully deleted once the order is completed or canceled by the + Company. +

+

+ The Customer understands and has reviewed the Privacy Policy for + Fine-Tuning by the Company. +

+ +

+ 5. Refund Policy +

+

+ Refunds will be processed in the event of training failure or if + the complete model file is not delivered to the Customer. Refunds + will be issued to the original payment method within 30 days of + the refund request. +

+ +

+ 6. Governing Law +

+

+ This Agreement shall be governed by and construed in accordance + with the laws of the State of California. +

+ +

+ 7. Dispute Resolution +

+

+ Any disputes arising out of or in connection with this Agreement + shall be resolved in the state or federal courts located in + California. +

+ +

+ 8. Notices +

+

+ All notices under this Agreement shall be in writing and shall be + deemed given when delivered personally, sent by confirmed email, + or sent by certified or registered mail, return receipt requested, + and addressed to the respective parties as follows: +

+

+ For Company:{" "} + team@mintplexlabs.com +

+

For Customer: The main email address on Customer's account

+ +

+ 9. Amendments +

+

+ The Company reserves the right to amend these terms at any time by + providing notice to the Customer. The Customer's continued use of + the services after such amendments will constitute acceptance of + the amended terms. +

+ +

+ 10. Indemnity +

+

+ The Customer agrees to indemnify, defend, and hold harmless + Mintplex Labs Inc., its affiliates, and their respective officers, + directors, employees, agents, and representatives from and against + any and all claims, liabilities, damages, losses, costs, expenses, + fees (including reasonable attorneys' fees and court costs) that + arise from or relate to: (a) the Customer's use of the services; + (b) any violation of this Agreement by the Customer; (c) any + breach of any representation, warranty, or covenant made by the + Customer; or (d) the Customer's violation of any rights of another + person or entity. +

+
+
+ + +
+
+ ); +} diff --git a/frontend/src/pages/FineTuning/Steps/index.jsx b/frontend/src/pages/FineTuning/Steps/index.jsx new file mode 100644 index 0000000000..55ed589d89 --- /dev/null +++ b/frontend/src/pages/FineTuning/Steps/index.jsx @@ -0,0 +1,148 @@ +import { isMobile } from "react-device-detect"; +import { useState } from "react"; +import Sidebar from "@/components/Sidebar"; +import Introduction from "./Introduction"; +import PrivacyPolicy from "./Privacy"; +import TermsAndConditions from "./TermsAndConditions"; +import Fulfillment from "./FulfillmentPolicy"; +import OrderDetails from "./OrderDetails"; +import DataUpload from "./DataUpload"; +import Confirmation from "./Confirmation"; +import OrderPlaced from "./OrderPlaced"; + +/** + * @typedef OrderSettings + * @property {string} email + * @property {string} baseModel + * @property {string} modelName + * @property {boolean} agreedToTerms + * @property {boolean} agreedToPrivacy + * @property {string} modelName + * @property {string|null} checkoutUrl + * @property {string|null} jobId + * @property {{slugs: string[], feedback: boolean|null}} trainingData + * @property {{pricing: {usd: number}, availableBaseModels: string[]}} tuningInfo + */ + +const FineTuningSteps = { + intro: { + name: "Introduction to Fine-Tuning", + next: () => "privacy", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + privacy: { + name: "How your data is handled", + next: () => "tos", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + tos: { + name: "Terms of service", + next: () => "fulfillment", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + fulfillment: { + name: "Fulfillment terms", + next: () => "order-details", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + "order-details": { + name: "Model details & information", + next: () => "data-selection", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + "data-selection": { + name: "Data selection", + next: () => "confirmation", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + confirmation: { + name: "Review and Submit", + next: () => "done", + component: ({ settings, setSettings, setStep }) => ( + + ), + }, + done: { + name: "Order placed", + next: () => "done", + component: ({ settings }) => , + }, +}; + +export function FineTuningCreationLayout({ setStep, children }) { + const [settings, setSettings] = useState({ + email: null, + baseModel: null, + modelName: null, + agreedToTerms: false, + agreedToPrivacy: false, + data: { + workspaceSlugs: [], + feedback: false, + }, + tuningInfo: { + pricing: { + usd: 0.0, + }, + availableBaseModels: [], + }, + checkoutUrl: null, + jobId: null, + }); + + return ( +
+ +
+ {children(settings, setSettings, setStep)} +
+
+ ); +} +export default FineTuningSteps; diff --git a/frontend/src/pages/FineTuning/index.jsx b/frontend/src/pages/FineTuning/index.jsx new file mode 100644 index 0000000000..f1c293306e --- /dev/null +++ b/frontend/src/pages/FineTuning/index.jsx @@ -0,0 +1,70 @@ +import React, { useState } from "react"; +import FineTuningSteps, { FineTuningCreationLayout } from "./Steps"; +import { CheckCircle, Circle, Sparkle } from "@phosphor-icons/react"; +import { isMobile } from "react-device-detect"; + +function SideBarSelection({ currentStep }) { + const currentIndex = Object.keys(FineTuningSteps).indexOf(currentStep); + return ( +
+ {Object.entries(FineTuningSteps).map(([stepKey, props], index) => { + const isSelected = currentStep === stepKey; + const isLast = index === Object.keys(FineTuningSteps).length - 1; + const isDone = + currentIndex === Object.keys(FineTuningSteps).length - 1 || + index < currentIndex; + return ( +
+
{props.name}
+
+ {isDone ? ( + + ) : ( + + )} +
+
+ ); + })} +
+ ); +} + +export default function FineTuningFlow() { + const [step, setStep] = useState("intro"); + const StepPage = FineTuningSteps.hasOwnProperty(step) + ? FineTuningSteps[step] + : FineTuningSteps.intro; + + return ( + + {(settings, setSettings, setStep) => ( +
+
+
+ +

Custom Fine-Tuned Model

+
+ +
+ {StepPage.component({ settings, setSettings, setStep })} +
+ )} +
+ ); +} diff --git a/frontend/src/pages/GeneralSettings/Chats/index.jsx b/frontend/src/pages/GeneralSettings/Chats/index.jsx index 3631c8c3e3..4ad5788852 100644 --- a/frontend/src/pages/GeneralSettings/Chats/index.jsx +++ b/frontend/src/pages/GeneralSettings/Chats/index.jsx @@ -7,9 +7,10 @@ import useQuery from "@/hooks/useQuery"; import ChatRow from "./ChatRow"; import showToast from "@/utils/toast"; import System from "@/models/system"; -import { CaretDown, Download, Trash } from "@phosphor-icons/react"; +import { CaretDown, Download, Sparkle, Trash } from "@phosphor-icons/react"; import { saveAs } from "file-saver"; import { useTranslation } from "react-i18next"; +import paths from "@/utils/paths"; const exportOptions = { csv: { @@ -159,13 +160,22 @@ export default function WorkspaceChats() {
{chats.length > 0 && ( - + <> + + + + Order Fine-Tune Model + + )}

diff --git a/frontend/src/pages/Main/index.jsx b/frontend/src/pages/Main/index.jsx index d0b3cbf8ce..757b78c077 100644 --- a/frontend/src/pages/Main/index.jsx +++ b/frontend/src/pages/Main/index.jsx @@ -5,6 +5,7 @@ import PasswordModal, { usePasswordModal } from "@/components/Modals/Password"; import { isMobile } from "react-device-detect"; import { FullScreenLoader } from "@/components/Preloader"; import UserMenu from "@/components/UserMenu"; +import { FineTuningAlert } from "../FineTuning/Banner"; export default function Main() { const { loading, requiresAuth, mode } = usePasswordModal(); @@ -15,11 +16,14 @@ export default function Main() { } return ( - -

- {!isMobile && } - -
- + <> + +
+ {!isMobile && } + +
+
+ + ); } diff --git a/frontend/src/pages/WorkspaceChat/index.jsx b/frontend/src/pages/WorkspaceChat/index.jsx index 6d6ce4b4b2..4f249eedf9 100644 --- a/frontend/src/pages/WorkspaceChat/index.jsx +++ b/frontend/src/pages/WorkspaceChat/index.jsx @@ -6,6 +6,7 @@ import Workspace from "@/models/workspace"; import PasswordModal, { usePasswordModal } from "@/components/Modals/Password"; import { isMobile } from "react-device-detect"; import { FullScreenLoader } from "@/components/Preloader"; +import { FineTuningAlert } from "../FineTuning/Banner"; export default function WorkspaceChat() { const { loading, requiresAuth, mode } = usePasswordModal(); @@ -44,9 +45,12 @@ function ShowWorkspaceChat() { }, []); return ( -
- {!isMobile && } - -
+ <> +
+ {!isMobile && } + +
+ + ); } diff --git a/frontend/src/utils/paths.js b/frontend/src/utils/paths.js index 10522c2df7..00fce51178 100644 --- a/frontend/src/utils/paths.js +++ b/frontend/src/utils/paths.js @@ -76,6 +76,9 @@ export default { apiDocs: () => { return `${API_BASE}/docs`; }, + orderFineTune: () => { + return `/fine-tuning`; + }, settings: { system: () => { return `/settings/system-preferences`; diff --git a/server/endpoints/experimental/fineTuning.js b/server/endpoints/experimental/fineTuning.js new file mode 100644 index 0000000000..3fe1098219 --- /dev/null +++ b/server/endpoints/experimental/fineTuning.js @@ -0,0 +1,108 @@ +const { FineTuning } = require("../../models/fineTuning"); +const { Telemetry } = require("../../models/telemetry"); +const { WorkspaceChats } = require("../../models/workspaceChats"); +const { reqBody } = require("../../utils/http"); +const { + flexUserRoleValid, + ROLES, +} = require("../../utils/middleware/multiUserProtected"); +const { validatedRequest } = require("../../utils/middleware/validatedRequest"); + +function fineTuningEndpoints(app) { + if (!app) return; + + app.get( + "/experimental/fine-tuning/check-eligible", + [validatedRequest, flexUserRoleValid([ROLES.admin])], + async (_request, response) => { + try { + const chatCount = await WorkspaceChats.count(); + response + .status(200) + .json({ eligible: chatCount >= FineTuning.recommendedMinDataset }); + } catch (e) { + console.error(e); + response.status(500).end(); + } + } + ); + + app.get( + "/experimental/fine-tuning/info", + [validatedRequest, flexUserRoleValid([ROLES.admin])], + async (_request, response) => { + try { + const fineTuningInfo = await FineTuning.getInfo(); + await Telemetry.sendTelemetry("fine_tuning_interest", { + step: "information", + }); + response.status(200).json(fineTuningInfo); + } catch (e) { + console.error(e); + response.status(500).end(); + } + } + ); + + app.post( + "/experimental/fine-tuning/dataset", + [validatedRequest, flexUserRoleValid([ROLES.admin])], + async (request, response) => { + try { + const { slugs = [], feedback = null } = reqBody(request); + if (!Array.isArray(slugs) || slugs.length === 0) { + return response.status(200).json({ + count: 0, + recommendedMin: FineTuning.recommendedMinDataset, + }); + } + + const count = await FineTuning.datasetSize(slugs, feedback); + await Telemetry.sendTelemetry("fine_tuning_interest", { + step: "uploaded_dataset", + }); + response + .status(200) + .json({ count, recommendedMin: FineTuning.recommendedMinDataset }); + } catch (e) { + console.error(e); + response.status(500).end(); + } + } + ); + + app.post( + "/experimental/fine-tuning/order", + [validatedRequest, flexUserRoleValid([ROLES.admin])], + async (request, response) => { + try { + const { email, baseModel, modelName, trainingData } = reqBody(request); + if ( + !email || + !baseModel || + !modelName || + !trainingData || + !trainingData?.slugs.length + ) + throw new Error("Invalid order details"); + + const { jobId, checkoutUrl } = await FineTuning.newOrder({ + email, + baseModel, + modelName, + trainingData, + }); + await Telemetry.sendTelemetry("fine_tuning_interest", { + step: "created_order", + jobId, + }); + response.status(200).json({ jobId, checkoutUrl }); + } catch (e) { + console.error(e); + response.status(500).end(); + } + } + ); +} + +module.exports = { fineTuningEndpoints }; diff --git a/server/endpoints/experimental/index.js b/server/endpoints/experimental/index.js index e452aff31e..e7dd144c5b 100644 --- a/server/endpoints/experimental/index.js +++ b/server/endpoints/experimental/index.js @@ -1,3 +1,4 @@ +const { fineTuningEndpoints } = require("./fineTuning"); const { liveSyncEndpoints } = require("./liveSync"); // All endpoints here are not stable and can move around - have breaking changes @@ -5,6 +6,7 @@ const { liveSyncEndpoints } = require("./liveSync"); // When a feature is promoted it should be removed from here and added to the appropriate scope. function experimentalEndpoints(router) { liveSyncEndpoints(router); + fineTuningEndpoints(router); } module.exports = { experimentalEndpoints }; diff --git a/server/models/fineTuning.js b/server/models/fineTuning.js new file mode 100644 index 0000000000..629cfc015f --- /dev/null +++ b/server/models/fineTuning.js @@ -0,0 +1,222 @@ +const { default: slugify } = require("slugify"); +const { safeJsonParse } = require("../utils/http"); +const { Telemetry } = require("./telemetry"); +const { Workspace } = require("./workspace"); +const { WorkspaceChats } = require("./workspaceChats"); +const fs = require("fs"); +const path = require("path"); +const { v4: uuidv4 } = require("uuid"); +const tmpStorage = + process.env.NODE_ENV === "development" + ? path.resolve(__dirname, `../storage/tmp`) + : path.resolve( + process.env.STORAGE_DIR ?? path.resolve(__dirname, `../storage`), + `tmp` + ); + +const FineTuning = { + API_BASE: + process.env.NODE_ENV === "development" + ? process.env.FINE_TUNING_ORDER_API + : "https://finetuning-wxich7363q-uc.a.run.app", + recommendedMinDataset: 50, + standardPrompt: + "Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed.", + + /** + * Get the information for the Fine-tuning product to display in various frontends + * @returns {Promise<{ + * productDetails: { + * name: string, + * description: string, + * icon: string, + * active: boolean, + * }, + * pricing: { + * usd: number, + * }, + * availableBaseModels: string[] + * }>} + */ + getInfo: async function () { + return fetch(`${this.API_BASE}/info`, { + method: "GET", + headers: { + Accepts: "application/json", + }, + }) + .then((res) => { + if (!res.ok) + throw new Error("Could not fetch fine-tuning information endpoint"); + return res.json(); + }) + .catch((e) => { + console.error(e); + return null; + }); + }, + /** + * Get the Dataset size for a training set. + * @param {string[]} workspaceSlugs + * @param {boolean|null} feedback + * @returns {Promise} + */ + datasetSize: async function (workspaceSlugs = [], feedback = null) { + const workspaceIds = await Workspace.where({ + slug: { + in: workspaceSlugs.map((slug) => String(slug)), + }, + }).then((results) => results.map((res) => res.id)); + + const count = await WorkspaceChats.count({ + workspaceId: { + in: workspaceIds, + }, + ...(feedback === true ? { feedback: 1 } : {}), + }); + return count; + }, + + _writeToTempStorage: function (data) { + const tmpFilepath = path.resolve(tmpStorage, `${uuidv4()}.json`); + if (!fs.existsSync(tmpStorage)) + fs.mkdirSync(tmpStorage, { recursive: true }); + fs.writeFileSync(tmpFilepath, JSON.stringify(data, null, 4)); + return tmpFilepath; + }, + + _rmTempDatafile: function (datafileLocation) { + if (!datafileLocation || !fs.existsSync(datafileLocation)) return; + fs.rmSync(datafileLocation); + }, + + _uploadDatafile: async function (datafileLocation, uploadConfig) { + try { + const fileBuffer = fs.readFileSync(datafileLocation); + const formData = new FormData(); + Object.entries(uploadConfig.fields).forEach(([key, value]) => + formData.append(key, value) + ); + formData.append("file", fileBuffer); + const response = await fetch(uploadConfig.url, { + method: "POST", + body: formData, + }); + + console.log("File upload returned code:", response.status); + return true; + } catch (error) { + console.error("Error uploading file:", error.message); + return false; + } + }, + + _buildSystemPrompt: function (chat, prompt = null) { + const sources = safeJsonParse(chat.response)?.sources || []; + const contextTexts = sources.map((source) => source.text); + const context = + sources.length > 0 + ? "\nContext:\n" + + contextTexts + .map((text, i) => { + return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`; + }) + .join("") + : ""; + return `${prompt ?? this.standardPrompt}${context}`; + }, + + _createTempDataFile: async function ({ slugs, feedback }) { + const workspacePromptMap = {}; + const workspaces = await Workspace.where({ + slug: { + in: slugs.map((slug) => String(slug)), + }, + }); + workspaces.forEach((ws) => { + workspacePromptMap[ws.id] = ws.openAiPrompt ?? this.standardPrompt; + }); + + const chats = await WorkspaceChats.whereWithData({ + workspaceId: { + in: workspaces.map((ws) => ws.id), + }, + ...(feedback === true ? { feedback: 1 } : {}), + }); + const preparedData = chats.map((chat) => { + const responseJson = safeJsonParse(chat.response); + return { + instruction: this._buildSystemPrompt( + chat, + workspacePromptMap[chat.workspaceId] + ), + input: chat.prompt, + output: responseJson.text, + }; + }); + + const tmpFile = this._writeToTempStorage(preparedData); + return tmpFile; + }, + + /** + * Generate fine-tune order request + * @param {object} data + * @returns {Promise<{jobId:string, uploadParams: object, configReady: boolean, checkoutUrl:string}>} + */ + _requestOrder: async function (data = {}) { + return await fetch(`${this.API_BASE}/order/new`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accepts: "application/json", + }, + body: JSON.stringify(data), + }) + .then((res) => { + if (!res.ok) throw new Error("Could not create fine-tune order"); + return res.json(); + }) + .catch((e) => { + console.error(e); + return { + jobId: null, + uploadParams: null, + configReady: null, + checkoutUrl: null, + }; + }); + }, + + /** + * Sanitizes the slugifies the model name to prevent issues during processing. + * only a-zA-Z0-9 are okay for model names. If name is totally invalid it becomes a uuid. + * @param {string} modelName - provided model name + * @returns {string} + */ + _cleanModelName: function (modelName = "") { + if (!modelName) return uuidv4(); + const sanitizedName = modelName.replace(/[^a-zA-Z0-9]/g, " "); + return slugify(sanitizedName); + }, + + newOrder: async function ({ email, baseModel, modelName, trainingData }) { + const datafileLocation = await this._createTempDataFile(trainingData); + const order = await this._requestOrder({ + email, + baseModel, + modelName: this._cleanModelName(modelName), + orderExtras: { platform: Telemetry.runtime() }, + }); + const uploadComplete = await this._uploadDatafile( + datafileLocation, + order.uploadParams + ); + if (!uploadComplete) + throw new Error("Data file upload failed. Order could not be created."); + this._rmTempDatafile(datafileLocation); + return { jobId: order.jobId, checkoutUrl: order.checkoutUrl }; + }, +}; + +module.exports = { FineTuning };