86 | );
87 | };
88 |
89 | export { CitationDisplay };
90 |
--------------------------------------------------------------------------------
/magic-text/src/app/citations/layout.tsx:
--------------------------------------------------------------------------------
1 | const title = "Citations";
2 | const description =
3 | "Magic Citations not only answer questions based on context but will also show you eactly where its coming from!";
4 |
5 | export const metadata = {
6 | title,
7 | description,
8 | twitter: {
9 | card: "summary_large_image",
10 | title,
11 | description,
12 | creator: "@jxnlco",
13 | },
14 | };
15 |
16 | export default function Layout({ children }: { children: React.ReactNode }) {
17 | return
{children}
;
18 | }
19 |
--------------------------------------------------------------------------------
/magic-text/src/app/citations/page.tsx:
--------------------------------------------------------------------------------
1 | "use client";
2 |
3 | import {useRef, useState} from "react";
4 | import "react-toastify/dist/ReactToastify.css";
5 | import {ToastContainer, toast} from "react-toastify";
6 | import {CitationDisplay} from "@/app/citations/components/CitationDisplay";
7 | import {
8 | Tabs,
9 | TabsContent,
10 | TabsList,
11 | TabsTrigger,
12 | } from "@/app/components/ui/tabs";
13 | import {Loader2} from "lucide-react";
14 | import Link from "next/link";
15 |
16 | const defaultContext =
17 | "My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics. As part of coop I worked at many companies including Stitchfix, Facebook. I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.";
18 | const defaultQuestion = "What did the author do in school?";
19 |
20 | type ICitationLocation = [number, number];
21 |
22 | type ICitationData = {
23 | body: string;
24 | spans: ICitationLocation[];
25 | citation: string[];
26 | };
27 |
28 | export default function Example() {
29 | const [context, setContext] = useState(defaultContext);
30 | const inputRef = useRef();
31 | const [loading, setLoading] = useState(false);
32 | const [citations, setCitations] = useState([]);
33 | const [tab, setTab] = useState("input");
34 |
35 | const [savedContext, setSavedContext] = useState("");
36 |
37 | const generateCitations = (e: any) => {
38 | e.preventDefault();
39 |
40 | setLoading(true);
41 | setSavedContext(context);
42 | setTab("preview");
43 | setCitations([]);
44 |
45 | return fetch("/api/citations", {
46 | method: "POST",
47 | headers: {
48 | "Content-Type": "application/json",
49 | },
50 | body: JSON.stringify({
51 | query: inputRef.current!.value,
52 | context,
53 | }),
54 | })
55 | .then(async (response) => {
56 | const stream = response.body
57 | if (!stream) {
58 | return
59 | }
60 | const reader = stream.getReader()
61 | const decoder = new TextDecoder();
62 |
63 | reader.read().then(function processText({done, value: chunk}): Promise | void {
64 | if (done) {
65 | return;
66 | }
67 |
68 | const value = decoder.decode(chunk);
69 | const parsedCitation = JSON.parse(value)
70 | setCitations((prev) => Array.from(prev).concat(parsedCitation));
71 |
72 | // Read some more, and call this function again
73 | return reader.read().then(processText);
74 | });
75 | })
76 | .finally(() => {
77 | setLoading(false);
78 | toast.success("Try hovering over the statements");
79 | });
80 | };
81 |
82 | return (
83 | <>
84 |
85 |
86 |
87 |
88 |
89 |
91 | Powered by OpenAI Function calls.{" "}
92 |
97 |
98 | Read more →
99 |
100 |
101 |
102 |
103 | Exact citations
104 |
105 |
106 | Not only can we use language models to answer question from a
107 | context we can also use structed extraction to provide character
108 | level citations that allow us to fact check each statement in the
109 | context, minimizing halucinations.
110 |
40 | {
41 | "I'm a machine learning engineer and I love to play with large language models. I've built this site to make it easy for you to play with them too. My background is computational mathematics but I decided to dedicate some time to learning React and Next.js."
42 | }
43 |
17 | I{"'"}m currently revamping my site to make it easier to use. Hit
18 | me up on my socials or{" "}
19 |
23 | email
24 | {" "}
25 | me if you have any questions. Click any of these links to get
26 | started.
27 |
100 | Magic Data is a tool that helps you understand your data, bring
101 | your own schema and data, and get insights from your data. You can
102 | also ask it to create new tables and migrations and we{"'"}ll
103 | append it to the schema
104 |
105 |
106 |
107 |
113 |
122 |
123 |
131 |
132 |
137 | {query}
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
151 |
161 |
162 | >
163 | );
164 | }
165 |
--------------------------------------------------------------------------------
/magic-text/src/app/data/prompt.tsx:
--------------------------------------------------------------------------------
1 | export function makePrompt(
2 | question: string,
3 | schema: string,
4 | currentQuery: string
5 | ) {
6 | const date = new Date().toDateString();
7 |
8 | const prompt = `
9 | You are a data scientist that helps your coworkers to answer questins about the data.
10 | You will be given a scheme and a question to answer. Reply only with the SQL query that answers the question.
11 | But leave comments inline to help your coworkers understand your thought process.
12 |
13 | If the question cannot be answered response with a sql comment explaining why.
14 | and suggest what data you might need to connect to answer the question.
15 |
16 | The data you have access to has the following schema:
17 |
18 | Schemas:
19 | ${schema}
20 |
21 | Example:
22 |
23 | Question: What is the minimum value of column b for each value of column a?
24 | SQL Query:
25 | -- This query ...
26 | -- 1. ...
27 | -- 2. ...
28 | -- ...
29 | SELECT
30 | table.a
31 | , min(table2.b)
32 | FROM table
33 | -- Join the two tables to get the data we need
34 | -- we need to filter the data to only get the data we need
35 | LEFT JOIN ON table2.id = table.id
36 | WHERE table2.id = 1
37 | GROUP BY 1
38 |
39 | Question: how many kinds cars are on the moon?
40 | SQL Query:
41 | -- I don't know how to answer this question
42 | -- maybe if we had data about the moon we could answer this question
43 |
44 | The date is ${date}
45 |
46 | Question:
47 | ${question}
48 |
49 | The last query you wrote was:
50 | ${currentQuery}
51 |
52 | SQL Query:
53 | `;
54 | return prompt;
55 | }
56 |
--------------------------------------------------------------------------------
/magic-text/src/app/demo/components/Notification.tsx:
--------------------------------------------------------------------------------
1 | "use client";
2 |
3 | import { Fragment, useState } from "react";
4 | import { Dialog, Transition } from "@headlessui/react";
5 | import { CheckIcon } from "@heroicons/react/24/outline";
6 |
7 | export default function Example() {
8 | const [open, setOpen] = useState(true);
9 |
10 | return (
11 |
12 |
73 |
74 | );
75 | }
76 |
--------------------------------------------------------------------------------
/magic-text/src/app/demo/components/TitleCard.tsx:
--------------------------------------------------------------------------------
1 | import Link from "next/link";
2 |
3 | export default function TitleCard(props: {
4 | title: string;
5 | description: string;
6 | }) {
7 | const { title, description } = props;
8 | return (
9 | <>
10 |
121 | );
122 | }
123 |
--------------------------------------------------------------------------------
/magic-text/src/app/demo/div/prompt.tsx:
--------------------------------------------------------------------------------
1 | export function makePrompt(command: string, currentHTML: string) {
2 | const prompt = `
3 | You will be given a question and a current html snippet and the task is to modify the html snippet to answer the request.
4 |
5 | Use these two tags as a style reference
6 |