├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── LICENSE ├── README.md └── magic-text ├── .eslintrc.json ├── .gitignore ├── .vscode └── settings.json ├── components.json ├── next.config.js ├── package-lock.json ├── package.json ├── postcss.config.js ├── prisma └── schema.prisma ├── public ├── favicon.ico ├── vercel.svg ├── welcome.png └── youtube.png ├── src ├── app │ ├── api │ │ └── citations │ │ │ └── route.tsx │ ├── citations │ │ ├── components │ │ │ └── CitationDisplay.tsx │ │ ├── layout.tsx │ │ └── page.tsx │ ├── components │ │ ├── Footer.tsx │ │ ├── Header.tsx │ │ ├── Hero.tsx │ │ ├── MagicLinks.tsx │ │ ├── links.tsx │ │ └── ui │ │ │ └── tabs.tsx │ ├── data │ │ ├── components │ │ │ └── Divider.tsx │ │ ├── layout.tsx │ │ ├── page.tsx │ │ └── prompt.tsx │ ├── demo │ │ ├── components │ │ │ ├── Notification.tsx │ │ │ ├── TitleCard.tsx │ │ │ └── analytics.tsx │ │ ├── div │ │ │ ├── head.tsx │ │ │ ├── page.tsx │ │ │ └── prompt.tsx │ │ ├── layout.tsx │ │ ├── sql │ │ │ ├── head.tsx │ │ │ ├── page.tsx │ │ │ └── prompt.tsx │ │ ├── survey │ │ │ ├── head.tsx │ │ │ ├── page.tsx │ │ │ └── prompt.tsx │ │ ├── text │ │ │ ├── components.tsx │ │ │ ├── head.tsx │ │ │ ├── page.tsx │ │ │ └── prompt.tsx │ │ └── youtube │ │ │ ├── head.tsx │ │ │ ├── page.tsx │ │ │ ├── utils.ts │ │ │ └── youtube.tsx │ ├── globals.css │ ├── layout.tsx │ ├── page.tsx │ ├── text │ │ ├── components.tsx │ │ ├── page.tsx │ │ └── prompt.tsx │ └── youtube │ │ ├── components │ │ ├── Divider.tsx │ │ └── Youtube.tsx │ │ ├── layout.tsx │ │ ├── page.tsx │ │ └── utils.ts ├── lib │ └── utils.ts └── pages │ └── api │ ├── cached_summary.ts │ ├── generate.ts │ ├── shorten.ts │ └── summary.ts ├── tailwind.config.js └── tsconfig.json /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | .vercel 106 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jason Liu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Magic](http://magic.jxnl.co/) 2 | 3 | Magic is a showcase of the AI tools I've been playing with. I've made the source code available on Github under jxnl/magic-text 4 | 5 | I'm using this project as an opportunity to learn and grow as a machine learning engineer, by building in public. I'm diving into React/NextJS while tackling some low hanging fruit, and I have big plans for what's to come. My goal with Magic is to inspire others to explore the possibilities of the OpenAI API and to show that with a little creativity, the sky's the limit. 6 | 7 | ## [Magic Text](https://magic.jxnl.co/demo/text) 8 | 9 | Simply highlight any text in the text area, and you'll be able to see the brush options in real time. No need to wait for any loading spinners, as Magic will immediately begin rewriting your selection to your specifications. 10 | 11 | ## [Magic Youtube](https://magic.jxnl.co/demo/youtube) 12 | 13 | Input a youtube video url and get a summary in markdown format. If it does not have a transcript, it will use whisper but it may be less accurate than the transcript and will take longer to generate. 14 | 15 | ## [Magic SQL](https://magic.jxnl.co/demo/sql) 16 | 17 | Use the default schema or add your own and ask questions in plain english and let Magic SQL do its best to explain the answer. You can also Magic to add tables or columns and the schema will automatically update. 18 | 19 | ## [Magic Div](https://magic.jxnl.co/demo/div) 20 | 21 | Magic Div is a simple tool to help you create HTML divs. You can use natrual language to describe the content and Magic will do its best to create the layout you're looking for using tailwindcss. 22 | 23 | ## [Magic Survey](https://magic.jxnl.co/demo/survey) 24 | 25 | Magic survey takes a json schema and defines conversational agent that helps users and complete survery and sets the data into a object in the browser. 26 | 27 | # Development 28 | 29 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). 30 | 31 | ## Getting Started 32 | 33 | First, run the development server: 34 | 35 | ```bash 36 | npm run dev 37 | # or 38 | yarn dev 39 | # or 40 | pnpm dev 41 | ``` 42 | 43 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 44 | 45 | ## Learn More 46 | 47 | To learn more about Next.js, take a look at the following resources: 48 | 49 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 50 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 51 | 52 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 53 | 54 | ## Deploy on Vercel 55 | 56 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. 57 | 58 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 59 | -------------------------------------------------------------------------------- /magic-text/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /magic-text/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | .pnpm-debug.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | next-env.d.ts 37 | -------------------------------------------------------------------------------- /magic-text/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules/typescript/lib", 3 | "typescript.enablePromptUseWorkspaceTsdk": true 4 | } -------------------------------------------------------------------------------- /magic-text/components.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://ui.shadcn.com/schema.json", 3 | "style": "default", 4 | "rsc": true, 5 | "tsx": true, 6 | "tailwind": { 7 | "config": "tailwind.config.js", 8 | "css": "src/app/globals.css", 9 | "baseColor": "slate", 10 | "cssVariables": true 11 | }, 12 | "aliases": { 13 | "components": "@/components", 14 | "utils": "@/lib/utils" 15 | } 16 | } -------------------------------------------------------------------------------- /magic-text/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | experimental: { 4 | appDir: true, 5 | }, 6 | async redirects() { 7 | return [ 8 | { 9 | source: "/youtube", 10 | destination: "https://youtubechapters.app/", 11 | permanent: true, 12 | }, 13 | ]; 14 | }, 15 | }; 16 | 17 | module.exports = nextConfig; 18 | -------------------------------------------------------------------------------- /magic-text/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magic-text", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint", 10 | "postinstall": "prisma generate" 11 | }, 12 | "dependencies": { 13 | "@headlessui/react": "^1.7.13", 14 | "@heroicons/react": "^2.0.16", 15 | "@next/font": "13.1.6", 16 | "@prisma/client": "^4.10.1", 17 | "@radix-ui/react-slot": "^1.0.2", 18 | "@radix-ui/react-tabs": "^1.0.4", 19 | "@tailwindcss/forms": "^0.5.3", 20 | "@types/node": "18.11.18", 21 | "@types/react": "18.0.27", 22 | "@types/react-dom": "18.0.10", 23 | "@vercel/analytics": "^0.1.10", 24 | "class-variance-authority": "^0.7.0", 25 | "clsx": "^2.0.0", 26 | "eslint": "8.33.0", 27 | "eventsource-parser": "^0.1.0", 28 | "framer-motion": "^9.0.1", 29 | "html-react-parser": "^3.0.8", 30 | "lucide-react": "^0.263.1", 31 | "next": "^13.4.12", 32 | "prisma": "^4.10.1", 33 | "react": "^18.2.0", 34 | "react-dom": "^18.2.0", 35 | "react-markdown": "^8.0.5", 36 | "react-syntax-highlighter": "^15.5.0", 37 | "react-toastify": "^9.1.1", 38 | "tailwind-merge": "^1.14.0", 39 | "tailwindcss-animate": "^1.0.6", 40 | "typescript": "4.9.5" 41 | }, 42 | "devDependencies": { 43 | "@tailwindcss/typography": "^0.5.9", 44 | "@types/react-syntax-highlighter": "^15.5.6", 45 | "autoprefixer": "^10.4.13", 46 | "eslint-config-next": "^13.3.4", 47 | "postcss": "^8.4.21", 48 | "tailwindcss": "^3.2.4" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /magic-text/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /magic-text/prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | } 4 | 5 | datasource db { 6 | provider = "postgresql" 7 | url = env("DB_URL") 8 | } 9 | 10 | model summary { 11 | id Int @id @default(autoincrement()) 12 | url String? @db.VarChar 13 | video_id String? @db.VarChar 14 | created_at DateTime? @db.Timestamp(6) 15 | summary_markdown String? @db.VarChar 16 | 17 | @@index([url], map: "ix_summary_url") 18 | @@index([video_id], map: "ix_summary_video_id") 19 | } 20 | -------------------------------------------------------------------------------- /magic-text/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnl/magic-text/c3fbc8286956749865253dcefe818d1ed72bb609/magic-text/public/favicon.ico -------------------------------------------------------------------------------- /magic-text/public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /magic-text/public/welcome.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnl/magic-text/c3fbc8286956749865253dcefe818d1ed72bb609/magic-text/public/welcome.png -------------------------------------------------------------------------------- /magic-text/public/youtube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnl/magic-text/c3fbc8286956749865253dcefe818d1ed72bb609/magic-text/public/youtube.png -------------------------------------------------------------------------------- /magic-text/src/app/api/citations/route.tsx: -------------------------------------------------------------------------------- 1 | import {NextRequest, NextResponse} from "next/server"; 2 | 3 | if (!process.env.CITATIONS_URL) { 4 | throw new Error("Require CITATIONS_URL") 5 | } 6 | if (!process.env.OPENAI_API_KEY) { 7 | throw new Error("Require OPENAI_API_KEY") 8 | } 9 | 10 | export type ICitationLocation = [number, number] 11 | 12 | export type ICitationData = { 13 | body: string, 14 | spans: ICitationLocation[], 15 | citation: string[], 16 | } 17 | 18 | 19 | export async function POST(request: NextRequest) { 20 | const {context, query} = await request.json() 21 | 22 | const response = await fetch(process.env.CITATIONS_URL!, { 23 | method: "POST", 24 | body: JSON.stringify({ 25 | context, query 26 | }), 27 | headers: { 28 | "Content-Type": "application/json", 29 | Authorization: `Bearer ${process.env.OPENAI_API_KEY}` 30 | } 31 | }) 32 | if (!response.body) { 33 | console.error("No body found!") 34 | return NextResponse.json({}, {status: 500}) 35 | } 36 | 37 | const reader = response.body.getReader() 38 | const encoder = new TextEncoder(); 39 | const decoder = new TextDecoder(); 40 | 41 | const stream = new ReadableStream({ 42 | start: (controller) => { 43 | return pump(); 44 | 45 | async function pump(): Promise { 46 | const {done, value} = await reader.read(); 47 | if (done) { 48 | controller.close(); 49 | return; 50 | } 51 | const data = decoder.decode(value); 52 | const prefix = data.substring(0, 6) 53 | if (prefix !== "data: ") { 54 | throw new Error(`Incorrect prefix in backend response: "${prefix}"`) 55 | } 56 | const postfix = data.substring(6) 57 | if (postfix !== "[DONE]") { 58 | // Spaces at the end are VERY important 59 | // Otherwise, Vercel will NOT send full chunk 60 | const enc = encoder.encode(postfix + " ") 61 | controller.enqueue(enc); 62 | } 63 | 64 | return pump(); 65 | } 66 | } 67 | }) 68 | 69 | return new Response(stream, { 70 | status: 200, 71 | headers: { 72 | 'Content-Type': 'text/plain; charset=utf-8', 73 | } 74 | }) 75 | } -------------------------------------------------------------------------------- /magic-text/src/app/citations/components/CitationDisplay.tsx: -------------------------------------------------------------------------------- 1 | import { useMemo, useState } from "react"; 2 | 3 | type ICitationLocation = [number, number]; 4 | 5 | type ICitationData = { 6 | body: string; 7 | spans: ICitationLocation[]; 8 | citation: string[]; 9 | }; 10 | 11 | const Citation = ({ 12 | citation, 13 | onHover, 14 | }: { 15 | citation: ICitationData; 16 | onHover: (location: ICitationLocation[]) => void; 17 | }) => { 18 | return ( 19 | onHover(citation.spans)} 21 | onMouseLeave={() => onHover([])} 22 | className="cursor-pointer block hover:underline-offset-1 hover:underline" 23 | > 24 | {citation.body}{" "} 25 | 26 | ); 27 | }; 28 | 29 | const CitationDisplay = ({ 30 | context, 31 | citations, 32 | }: { 33 | context: string; 34 | citations: ICitationData[]; 35 | }) => { 36 | const [highlight, setHighlight] = useState([]); 37 | 38 | const highlightedContext = useMemo(() => { 39 | if (highlight.length === 0) { 40 | return context; 41 | } 42 | let result = context; 43 | for (let loc of Array.from(highlight).sort((a,b) => b[0] - a[0])) { 44 | result = 45 | result.slice(0, loc[0]) + 46 | `` + 47 | result.slice(loc[0], loc[1]) + 48 | "" + 49 | result.slice(loc[1]); 50 | } 51 | return result; 52 | }, [context, highlight]); 53 | 54 | return ( 55 |
56 |
57 |
58 | {citations.length > 0 ? ( 59 | <> 60 | 61 | Statements 62 | 63 | 64 | ) : ( 65 | Loading... 66 | )} 67 |
    68 | {citations.map((c) => ( 69 |
  • 70 | 71 |
  • 72 | ))} 73 |
74 | 75 | Context with highlights 76 | 77 |
78 |

82 |

83 |
84 |
85 |
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 |
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 |

111 |
112 |
113 | 119 | 128 |
129 | 138 |
139 |
140 |
141 |
142 | 147 | 148 | Input 149 | 150 | Preview 151 | 152 | 153 | 154 |
155 | 161 |