File size: 10,227 Bytes
6e1a53e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"use client";

import { Textarea } from "@/components/ui/textarea";
import { useState } from "react";
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import Link from "next/link";
import { useToast } from "@/components/ui/use-toast";

const formSchema = z.object({
  dataType: z.string().min(3).max(50),
  dataValue: z.string().min(3),
  metadata: z.string().optional(),
  envVariables: z.string().optional(),
});

function AddDataSourceForm() {
  const [selectedTypeDescription, setSelectedTypeDescription] = useState("");
  const { toast } = useToast();
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      dataType: "",
      dataValue: "",
      metadata: "",
      envVariables: "",
    },
  });
  function handleDataTypeChange(value: string) {
    const selectedDataType = dataTypes.find(
      (dataType) => dataType.value === value,
    );
    setSelectedTypeDescription(selectedDataType?.description || "");
    form.setValue("dataType", value);
  }

  const dataTypes = [
    {
      value: "pdf_file",
      docsLink: "https://docs.embedchain.ai/components/data-sources/pdf_file",
      label: "PDF File",
      description: "/path/to/file.pdf",
    },
    {
      value: "csv",
      docsLink: "https://docs.embedchain.ai/components/data-sources/csv",
      label: "CSV",
      description: "/path/to/file.csv",
    },
    {
      value: "directory",
      docsLink: "https://docs.embedchain.ai/components/data-sources/directory",
      label: "Directory",
      description: "/path/to/directory",
    },
    {
      value: "text",
      docsLink: "https://docs.embedchain.ai/components/data-sources/text-file",
      label: "Text",
      description: "String of text",
    },
    {
      value: "unstructured",
      docsLink:
        "https://docs.embedchain.ai/components/data-sources/unstructured",
      label: "Unstructured",
      description:
        "Unstructured data. Uses unstructured.io loader to load data.",
    },
    {
      value: "web_page",
      docsLink: "https://docs.embedchain.ai/components/data-sources/web_page",
      label: "Web Page",
      description: "https://www.forbes.com/profile/elon-musk",
    },
    {
      value: "beehiiv",
      docsLink: "https://docs.embedchain.ai/components/data-sources/beehiiv",
      label: "Beehiiv",
      description: "https://aibreakfast.beehiiv.com",
    },
    {
      value: "docx",
      docsLink: "https://docs.embedchain.ai/components/data-sources/docx",
      label: "DOCX",
      description: "https://example.com/content/intro.docx",
    },
    {
      value: "discord",
      docsLink: "https://docs.embedchain.ai/components/data-sources/discord",
      label: "Discord",
      description: "Discord channel id such as '1177296711023075338'",
    },
    {
      value: "docs_site",
      docsLink: "https://docs.embedchain.ai/components/data-sources/docs-site",
      label: "Docs Site",
      description: "https://docs.embedchain.ai/",
    },
    {
      value: "dropbox",
      docsLink: "https://docs.embedchain.ai/components/data-sources/dropbox",
      label: "Dropbox",
      description: "Dropbox file path such as '/path/to/file'",
    },
    {
      value: "gmail",
      docsLink: "https://docs.embedchain.ai/components/data-sources/gmail",
      label: "Gmail",
      description: "to: me label:inbox",
    },
    {
      value: "google_drive",
      docsLink:
        "https://docs.embedchain.ai/components/data-sources/google-drive",
      label: "Google Drive",
      description: "https://drive.google.com/drive/u/0/folders/xxx-xxx",
    },
    {
      value: "image",
      docsLink: "https://docs.embedchain.ai/components/data-sources/image",
      label: "Image",
      description: "Path to image file such as '/path/to/image.png'",
    },
    {
      value: "json",
      docsLink: "https://docs.embedchain.ai/components/data-sources/json",
      label: "JSON",
      description: "JSON file path such as '/path/to/file.json'",
    },
    {
      value: "mdx",
      docsLink: "https://docs.embedchain.ai/components/data-sources/mdx",
      label: "MDX",
      description: "Path to MDX file such as '/path/to/file.mdx'",
    },
    {
      value: "notion",
      docsLink: "https://docs.embedchain.ai/components/data-sources/notion",
      label: "Notion",
      description:
        "A Notion page link such as 'https://www.notion.so/my-page-cfbc134ca6464fc980d0391613959196'",
    },
    {
      value: "openapi",
      docsLink: "https://docs.embedchain.ai/components/data-sources/openapi",
      label: "OpenAPI",
      description:
        "https://github.com/openai/openai-openapi/blob/master/openapi.yaml",
    },
    {
      value: "sitemap",
      docsLink: "https://docs.embedchain.ai/components/data-sources/sitemap",
      label: "Sitemap",
      description: "https://nextjs.org/sitemap.xml",
    },
    {
      value: "slack",
      docsLink: "https://docs.embedchain.ai/components/data-sources/slack",
      label: "Slack",
      description: "in:general",
    },
    {
      value: "substack",
      docsLink: "https://docs.embedchain.ai/components/data-sources/substack",
      label: "Substack",
      description: "https://www.lennysnewsletter.com",
    },
    {
      value: "xml",
      docsLink: "https://docs.embedchain.ai/components/data-sources/xml",
      label: "XML",
      description: "content/data.xml",
    },
    {
      value: "youtube_channel",
      docsLink:
        "https://docs.embedchain.ai/components/data-sources/youtube-channel",
      label: "YouTube Channel",
      description: "@embedchain",
    },
    {
      value: "youtube_video",
      docsLink:
        "https://docs.embedchain.ai/components/data-sources/youtube_video",
      label: "YouTube Video",
      description: "https://www.youtube.com/watch?v=ea_XktsFU1Q",
    },
  ];

  async function onSubmit(values: z.infer<typeof formSchema>) {
    const response = await fetch("/api/v1/admin/data_sources", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(values),
    });
    const response_json = await response.json();
    if (response?.ok) {
      toast({
        title: "Success",
        description: response_json.message,
      });
      form.reset();
    } else {
      toast({
        title: "Failed",
        description: response_json.message,
      });
    }
  }

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
        <FormField
          control={form.control}
          name="dataType"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Data type</FormLabel>
              <Select
                onValueChange={handleDataTypeChange}
                defaultValue={field.value}
              >
                <FormControl>
                  <SelectTrigger>
                    <SelectValue placeholder="Select a data type" />
                  </SelectTrigger>
                </FormControl>
                <SelectContent>
                  <SelectGroup>
                    {dataTypes.map((dataType) => (
                      <SelectItem key={dataType.value} value={dataType.value}>
                        {dataType.label}
                      </SelectItem>
                    ))}
                  </SelectGroup>
                </SelectContent>
              </Select>
              <FormDescription>
                You can manage data sources in your{" "}
                <Link href="/admin/data">data sources page</Link>.
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <FormField
          control={form.control}
          name="dataValue"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Data value</FormLabel>
              <Textarea {...field} />
              <FormDescription>
                Example: {selectedTypeDescription}
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <FormField
          control={form.control}
          name="metadata"
          render={({ field }) => (
            <FormItem>
              <FormLabel>(Optional) Metadata</FormLabel>
              <Textarea {...field} />
              <FormDescription>
                Metadata to store. Useful for metadata filtering in RAG apps.{" "}
                <br />
                Enter valid JSON only.
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <FormField
          control={form.control}
          name="envVariables"
          render={({ field }) => (
            <FormItem>
              <FormLabel>(Optional) Environment variables</FormLabel>
              <Textarea {...field} />
              <FormDescription>
                Environment variables to set for adding data source such as
                `DROPBOX_ACCESS_TOKEN` etc. <br />
                Enter valid JSON only. Key is the env variable name and value is
                the env variable value.
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit">Save</Button>
      </form>
    </Form>
  );
}

export default function Page() {
  return (
    <div className="mt-20 flex justify-center items-stretch">
      <div className="max-w-screen-lg w-full bg-background">
        <div className="md:p-4 flex flex-col">
          <h1 className="text-2xl font-semibold tracking-tight">
            Add data source
          </h1>
        </div>
        <div className="md:p-4 flex flex-col space-y-4">
          <AddDataSourceForm />
        </div>
      </div>
    </div>
  );
}