├── src ├── vite-env.d.ts ├── aiChat │ ├── styles.css │ ├── TrashIcon.tsx │ ├── CloseIcon.tsx │ ├── SendIcon.tsx │ ├── SizeIcon.tsx │ ├── InfoCircled.tsx │ └── index.tsx ├── main.tsx ├── components │ ├── typography │ │ └── link.tsx │ └── ui │ │ └── button.tsx ├── lib │ └── utils.tsx ├── App.tsx └── index.css ├── convex ├── langchain │ └── db.ts ├── _generated │ ├── api.js │ ├── api.d.ts │ ├── dataModel.d.ts │ ├── server.js │ └── server.d.ts ├── tsconfig.json ├── schema.ts ├── helpers.ts ├── serve.ts ├── messages.ts ├── ingest │ └── load.ts └── README.md ├── screenshot.png ├── postcss.config.js ├── .gitignore ├── tsconfig.node.json ├── vite.config.ts ├── components.json ├── index.html ├── tsconfig.json ├── .eslintrc.cjs ├── tailwind.config.js ├── package.json ├── README.md └── LICENSE /src/vite-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /convex/langchain/db.ts: -------------------------------------------------------------------------------- 1 | export * from "@langchain/community/utils/convex"; 2 | -------------------------------------------------------------------------------- /src/aiChat/styles.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/get-convex/convex-ai-chat-langchain/HEAD/screenshot.png -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | !**/glob-import/dir/node_modules 2 | .DS_Store 3 | .idea 4 | *.cpuprofile 5 | *.local 6 | *.log 7 | /.vscode/ 8 | /docs/.vitepress/cache 9 | dist 10 | dist-ssr 11 | explorations 12 | node_modules 13 | playground-temp 14 | temp 15 | TODOs.md 16 | .eslintcache 17 | -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "composite": true, 4 | "skipLibCheck": true, 5 | "module": "ESNext", 6 | "moduleResolution": "bundler", 7 | "allowSyntheticDefaultImports": true 8 | }, 9 | "include": ["vite.config.ts"] 10 | } 11 | -------------------------------------------------------------------------------- /src/main.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom/client"; 3 | import App from "./App"; 4 | import "./index.css"; 5 | 6 | ReactDOM.createRoot(document.getElementById("root")!).render( 7 | 8 | 9 | 10 | ); 11 | -------------------------------------------------------------------------------- /src/components/typography/link.tsx: -------------------------------------------------------------------------------- 1 | import { se } from "@/lib/utils"; 2 | import { AnchorHTMLAttributes } from "react"; 3 | 4 | export const Link = se< 5 | HTMLAnchorElement, 6 | AnchorHTMLAttributes 7 | >( 8 | "a", 9 | "font-medium text-primary underline underline-offset-4 hover:no-underline" 10 | ); 11 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import path from "path"; 2 | import react from "@vitejs/plugin-react"; 3 | import { defineConfig } from "vite"; 4 | 5 | // https://vitejs.dev/config/ 6 | export default defineConfig({ 7 | plugins: [react()], 8 | resolve: { 9 | alias: { 10 | "@": path.resolve(__dirname, "./src"), 11 | }, 12 | }, 13 | }); 14 | -------------------------------------------------------------------------------- /components.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://ui.shadcn.com/schema.json", 3 | "style": "new-york", 4 | "rsc": false, 5 | "tsx": true, 6 | "tailwind": { 7 | "config": "tailwind.config.js", 8 | "css": "src/index.css", 9 | "baseColor": "zinc", 10 | "cssVariables": true 11 | }, 12 | "aliases": { 13 | "components": "@/components", 14 | "utils": "@/lib/utils" 15 | } 16 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Convex + React (Vite) 8 | 9 | 10 |
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /convex/_generated/api.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /** 3 | * Generated `api` utility. 4 | * 5 | * THIS CODE IS AUTOMATICALLY GENERATED. 6 | * 7 | * Generated by convex@1.13.2. 8 | * To regenerate, run `npx convex dev`. 9 | * @module 10 | */ 11 | 12 | import { anyApi } from "convex/server"; 13 | 14 | /** 15 | * A utility for referencing Convex functions in your app's API. 16 | * 17 | * Usage: 18 | * ```js 19 | * const myFunctionReference = api.myModule.myFunction; 20 | * ``` 21 | */ 22 | export const api = anyApi; 23 | export const internal = anyApi; 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "useDefineForClassFields": true, 5 | "lib": ["ES2020", "DOM", "DOM.Iterable"], 6 | "module": "ESNext", 7 | "skipLibCheck": true, 8 | 9 | /* Bundler mode */ 10 | "moduleResolution": "bundler", 11 | "allowImportingTsExtensions": true, 12 | "resolveJsonModule": true, 13 | "isolatedModules": true, 14 | "noEmit": true, 15 | "jsx": "react-jsx", 16 | 17 | /* Linting */ 18 | "strict": true, 19 | "noUnusedLocals": true, 20 | "noUnusedParameters": true, 21 | "noFallthroughCasesInSwitch": true, 22 | 23 | "baseUrl": ".", 24 | "paths": { 25 | "@/*": ["./src/*"] 26 | } 27 | }, 28 | "references": [{ "path": "./tsconfig.node.json" }] 29 | } 30 | -------------------------------------------------------------------------------- /src/aiChat/TrashIcon.tsx: -------------------------------------------------------------------------------- 1 | export function TrashIcon({ className }: { className?: string }) { 2 | return ( 3 | 11 | 17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /convex/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | /* This TypeScript project config describes the environment that 3 | * Convex functions run in and is used to typecheck them. 4 | * You can modify it, but some settings required to use Convex. 5 | */ 6 | "compilerOptions": { 7 | /* These settings are not required by Convex and can be modified. */ 8 | "allowJs": true, 9 | "strict": true, 10 | 11 | /* These compiler options are required by Convex */ 12 | "target": "ESNext", 13 | "lib": ["ES2021", "dom"], 14 | "forceConsistentCasingInFileNames": true, 15 | "allowSyntheticDefaultImports": true, 16 | "module": "ESNext", 17 | "moduleResolution": "Node", 18 | "isolatedModules": true, 19 | "skipLibCheck": true, 20 | "noEmit": true 21 | }, 22 | "include": ["./**/*"], 23 | "exclude": ["./_generated"] 24 | } 25 | -------------------------------------------------------------------------------- /src/aiChat/CloseIcon.tsx: -------------------------------------------------------------------------------- 1 | export function CloseIcon({ className }: { className?: string }) { 2 | return ( 3 | 11 | 17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /src/aiChat/SendIcon.tsx: -------------------------------------------------------------------------------- 1 | export function SendIcon({ className }: { className?: string }) { 2 | return ( 3 | 11 | 17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /src/lib/utils.tsx: -------------------------------------------------------------------------------- 1 | import { type ClassValue, clsx } from "clsx"; 2 | import { ForwardRefRenderFunction, forwardRef } from "react"; 3 | import { twMerge } from "tailwind-merge"; 4 | 5 | export function cn(...inputs: ClassValue[]) { 6 | return twMerge(clsx(inputs)); 7 | } 8 | 9 | // forward refs 10 | export function fr>( 11 | component: ForwardRefRenderFunction 12 | ) { 13 | const wrapped = forwardRef(component); 14 | wrapped.displayName = component.name; 15 | return wrapped; 16 | } 17 | 18 | // styled element 19 | export function se< 20 | T = HTMLElement, 21 | P extends React.HTMLAttributes = React.HTMLAttributes 22 | >(Tag: keyof React.ReactHTML, ...classNames: ClassValue[]) { 23 | const component = fr(({ className, ...props }, ref) => ( 24 | // @ts-expect-error Too complicated for TypeScript 25 | 26 | )); 27 | component.displayName = Tag[0].toUpperCase() + Tag.slice(1); 28 | return component; 29 | } 30 | -------------------------------------------------------------------------------- /convex/schema.ts: -------------------------------------------------------------------------------- 1 | import { defineSchema, defineTable } from "convex/server"; 2 | import { v } from "convex/values"; 3 | 4 | export default defineSchema({ 5 | cache: defineTable({ 6 | key: v.string(), 7 | value: v.any(), 8 | }).index("byKey", ["key"]), 9 | documents: defineTable({ 10 | embedding: v.array(v.number()), 11 | text: v.string(), 12 | metadata: v.any(), 13 | }).vectorIndex("byEmbedding", { 14 | vectorField: "embedding", 15 | dimensions: 1536, 16 | }), 17 | messages: defineTable({ 18 | sessionId: v.string(), 19 | message: v.object({ 20 | type: v.string(), 21 | data: v.object({ 22 | content: v.string(), 23 | role: v.optional(v.string()), 24 | name: v.optional(v.string()), 25 | additional_kwargs: v.optional(v.any()), 26 | response_metadata: v.optional(v.any()), 27 | id: v.optional(v.string()), 28 | invalid_tool_calls: v.optional(v.array(v.any())), 29 | tool_calls: v.optional(v.array(v.any())), 30 | }), 31 | }), 32 | }).index("bySessionId", ["sessionId"]), 33 | }); 34 | -------------------------------------------------------------------------------- /src/aiChat/SizeIcon.tsx: -------------------------------------------------------------------------------- 1 | export function SizeIcon({ className }: { className?: string }) { 2 | return ( 3 | 11 | 17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import { ConvexAiChat } from "@/aiChat"; 2 | import { Link } from "@/components/typography/link"; 3 | import { Button } from "@/components/ui/button"; 4 | 5 | function App() { 6 | return ( 7 |
8 |

9 | AI Chat using Convex and LangChain 10 |

11 |

Click the button to open the chat window

12 |

13 | ( 19 | 20 | )} 21 | /> 22 |

23 |

24 | Check out{" "} 25 | 26 | Convex docs 27 | 28 |

29 |
30 | ); 31 | } 32 | 33 | export default App; 34 | -------------------------------------------------------------------------------- /convex/helpers.ts: -------------------------------------------------------------------------------- 1 | import { PaginationResult } from "convex/server"; 2 | import { internal } from "./_generated/api"; 3 | import { Doc, TableNames } from "./_generated/dataModel"; 4 | import { ActionCtx, QueryCtx, internalQuery } from "./_generated/server"; 5 | 6 | export async function paginate( 7 | ctx: ActionCtx, 8 | table: T, 9 | batchSize: number, 10 | callback: (documents: Doc[]) => Promise 11 | ): Promise { 12 | let isDone = false; 13 | let cursor = null; 14 | while (!isDone) { 15 | const result: PaginationResult> = (await ctx.runQuery( 16 | internal.helpers.paginateQuery, 17 | { 18 | table, 19 | cursor, 20 | numItems: batchSize, 21 | } 22 | )) as any; 23 | await callback(result.page); 24 | ({ isDone, continueCursor: cursor } = result); 25 | } 26 | } 27 | 28 | export const paginateQuery = internalQuery( 29 | async ( 30 | ctx: QueryCtx, 31 | args: { table: T; cursor: any; numItems: number } 32 | ) => { 33 | return await ctx.db 34 | .query(args.table) 35 | .paginate({ cursor: args.cursor, numItems: args.numItems }); 36 | } 37 | ); 38 | -------------------------------------------------------------------------------- /convex/_generated/api.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /** 3 | * Generated `api` utility. 4 | * 5 | * THIS CODE IS AUTOMATICALLY GENERATED. 6 | * 7 | * Generated by convex@1.13.2. 8 | * To regenerate, run `npx convex dev`. 9 | * @module 10 | */ 11 | 12 | import type { 13 | ApiFromModules, 14 | FilterApi, 15 | FunctionReference, 16 | } from "convex/server"; 17 | import type * as helpers from "../helpers.js"; 18 | import type * as ingest_load from "../ingest/load.js"; 19 | import type * as langchain_db from "../langchain/db.js"; 20 | import type * as messages from "../messages.js"; 21 | import type * as serve from "../serve.js"; 22 | 23 | /** 24 | * A utility for referencing Convex functions in your app's API. 25 | * 26 | * Usage: 27 | * ```js 28 | * const myFunctionReference = api.myModule.myFunction; 29 | * ``` 30 | */ 31 | declare const fullApi: ApiFromModules<{ 32 | helpers: typeof helpers; 33 | "ingest/load": typeof ingest_load; 34 | "langchain/db": typeof langchain_db; 35 | messages: typeof messages; 36 | serve: typeof serve; 37 | }>; 38 | export declare const api: FilterApi< 39 | typeof fullApi, 40 | FunctionReference 41 | >; 42 | export declare const internal: FilterApi< 43 | typeof fullApi, 44 | FunctionReference 45 | >; 46 | -------------------------------------------------------------------------------- /src/aiChat/InfoCircled.tsx: -------------------------------------------------------------------------------- 1 | export function InfoCircled({ className }: { className?: string }) { 2 | return ( 3 | 11 | 17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /convex/serve.ts: -------------------------------------------------------------------------------- 1 | import { v } from "convex/values"; 2 | import { ConversationalRetrievalQAChain } from "langchain/chains"; 3 | import { ChatOpenAI } from "@langchain/openai"; 4 | import { OpenAIEmbeddings } from "@langchain/openai"; 5 | import { BufferMemory } from "langchain/memory"; 6 | import { ConvexChatMessageHistory } from "@langchain/community/stores/message/convex"; 7 | import { ConvexVectorStore } from "@langchain/community/vectorstores/convex"; 8 | import { internalAction } from "./_generated/server"; 9 | 10 | const OPENAI_MODEL = "gpt-3.5-turbo"; 11 | 12 | export const answer = internalAction({ 13 | args: { 14 | sessionId: v.string(), 15 | message: v.string(), 16 | }, 17 | handler: async (ctx, { sessionId, message }) => { 18 | const vectorStore = new ConvexVectorStore(new OpenAIEmbeddings(), { ctx }); 19 | 20 | const model = new ChatOpenAI({ modelName: OPENAI_MODEL }); 21 | const memory = new BufferMemory({ 22 | chatHistory: new ConvexChatMessageHistory({ sessionId, ctx }), 23 | memoryKey: "chat_history", 24 | outputKey: "text", 25 | returnMessages: true, 26 | }); 27 | const chain = ConversationalRetrievalQAChain.fromLLM( 28 | model, 29 | vectorStore.asRetriever(), 30 | { memory } 31 | ); 32 | 33 | await chain.call({ question: message }); 34 | }, 35 | }); 36 | -------------------------------------------------------------------------------- /convex/messages.ts: -------------------------------------------------------------------------------- 1 | import { v } from "convex/values"; 2 | import { mutation } from "./_generated/server"; 3 | import { query } from "./_generated/server"; 4 | import { internal } from "./_generated/api"; 5 | 6 | export const list = query({ 7 | args: { 8 | sessionId: v.string(), 9 | }, 10 | handler: async (ctx, args) => { 11 | return ( 12 | await ctx.db 13 | .query("messages") 14 | .withIndex("bySessionId", (q) => q.eq("sessionId", args.sessionId)) 15 | .collect() 16 | ).map(({ message: { data, type }, ...fields }) => ({ 17 | ...fields, 18 | isViewer: type === "human", 19 | text: data.content, 20 | })); 21 | }, 22 | }); 23 | 24 | export const send = mutation({ 25 | args: { 26 | message: v.string(), 27 | sessionId: v.string(), 28 | }, 29 | handler: async (ctx, { message, sessionId }) => { 30 | await ctx.scheduler.runAfter(0, internal.serve.answer, { 31 | sessionId, 32 | message, 33 | }); 34 | }, 35 | }); 36 | 37 | export const clear = mutation({ 38 | args: { 39 | sessionId: v.string(), 40 | }, 41 | handler: async (ctx, args) => { 42 | const messages = await ctx.db 43 | .query("messages") 44 | .withIndex("bySessionId", (q) => q.eq("sessionId", args.sessionId)) 45 | .collect(); 46 | await Promise.all(messages.map((message) => ctx.db.delete(message._id))); 47 | }, 48 | }); 49 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { browser: true, es2020: true, node: true }, 4 | extends: [ 5 | "eslint:recommended", 6 | "plugin:@typescript-eslint/recommended-type-checked", 7 | "plugin:react-hooks/recommended", 8 | ], 9 | ignorePatterns: [ 10 | "dist", 11 | "convex/_generated", 12 | ".eslintrc.cjs", 13 | "tailwind.config.js", 14 | // There are currently ESLint errors in shadcn/ui 15 | "src/components/ui", 16 | ], 17 | parser: "@typescript-eslint/parser", 18 | parserOptions: { 19 | project: true, 20 | tsconfigRootDir: __dirname, 21 | }, 22 | plugins: ["react-refresh"], 23 | rules: { 24 | "react-refresh/only-export-components": [ 25 | "warn", 26 | { allowConstantExport: true }, 27 | ], 28 | 29 | // All of these overrides ease getting into 30 | // TypeScript, and can be removed for stricter 31 | // linting down the line. 32 | 33 | // Allow escaping the compiler 34 | "@typescript-eslint/ban-ts-comment": "error", 35 | 36 | // Allow explicit `any`s 37 | "@typescript-eslint/no-explicit-any": "off", 38 | 39 | // START: Allow implicit `any`s 40 | "@typescript-eslint/no-unsafe-argument": "off", 41 | "@typescript-eslint/no-unsafe-assignment": "off", 42 | "@typescript-eslint/no-unsafe-call": "off", 43 | "@typescript-eslint/no-unsafe-member-access": "off", 44 | "@typescript-eslint/no-unsafe-return": "off", 45 | // END: Allow implicit `any`s 46 | 47 | // Allow async functions without await 48 | // for consistency (esp. Convex `handler`s) 49 | "@typescript-eslint/require-await": "off", 50 | }, 51 | }; 52 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | @layer base { 6 | :root { 7 | --background: 0 0% 100%; 8 | --foreground: 240 10% 3.9%; 9 | 10 | --card: 0 0% 100%; 11 | --card-foreground: 240 10% 3.9%; 12 | 13 | --popover: 0 0% 100%; 14 | --popover-foreground: 240 10% 3.9%; 15 | 16 | --primary: 240 5.9% 10%; 17 | --primary-foreground: 0 0% 98%; 18 | 19 | --secondary: 240 4.8% 95.9%; 20 | --secondary-foreground: 240 5.9% 10%; 21 | 22 | --muted: 240 4.8% 95.9%; 23 | --muted-foreground: 240 3.8% 46.1%; 24 | 25 | --accent: 240 4.8% 95.9%; 26 | --accent-foreground: 240 5.9% 10%; 27 | 28 | --destructive: 0 84.2% 60.2%; 29 | --destructive-foreground: 0 0% 98%; 30 | 31 | --border: 240 5.9% 90%; 32 | --input: 240 5.9% 90%; 33 | --ring: 240 10% 3.9%; 34 | 35 | --radius: 0.5rem; 36 | } 37 | 38 | @media (prefers-color-scheme: dark) { 39 | :root { 40 | --background: 240 10% 3.9%; 41 | --foreground: 0 0% 98%; 42 | 43 | --card: 240 10% 3.9%; 44 | --card-foreground: 0 0% 98%; 45 | 46 | --popover: 240 10% 3.9%; 47 | --popover-foreground: 0 0% 98%; 48 | 49 | --primary: 0 0% 98%; 50 | --primary-foreground: 240 5.9% 10%; 51 | 52 | --secondary: 240 3.7% 15.9%; 53 | --secondary-foreground: 0 0% 98%; 54 | 55 | --muted: 240 3.7% 15.9%; 56 | --muted-foreground: 240 5% 64.9%; 57 | 58 | --accent: 240 3.7% 15.9%; 59 | --accent-foreground: 0 0% 98%; 60 | 61 | --destructive: 0 62.8% 30.6%; 62 | --destructive-foreground: 0 0% 98%; 63 | 64 | --border: 240 3.7% 15.9%; 65 | --input: 240 3.7% 15.9%; 66 | --ring: 240 4.9% 83.9%; 67 | } 68 | } 69 | } 70 | 71 | @layer base { 72 | * { 73 | @apply border-border; 74 | } 75 | body { 76 | @apply bg-background text-foreground; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /convex/_generated/dataModel.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /** 3 | * Generated data model types. 4 | * 5 | * THIS CODE IS AUTOMATICALLY GENERATED. 6 | * 7 | * Generated by convex@1.13.2. 8 | * To regenerate, run `npx convex dev`. 9 | * @module 10 | */ 11 | 12 | import type { 13 | DataModelFromSchemaDefinition, 14 | DocumentByName, 15 | TableNamesInDataModel, 16 | SystemTableNames, 17 | } from "convex/server"; 18 | import type { GenericId } from "convex/values"; 19 | import schema from "../schema.js"; 20 | 21 | /** 22 | * The names of all of your Convex tables. 23 | */ 24 | export type TableNames = TableNamesInDataModel; 25 | 26 | /** 27 | * The type of a document stored in Convex. 28 | * 29 | * @typeParam TableName - A string literal type of the table name (like "users"). 30 | */ 31 | export type Doc = DocumentByName< 32 | DataModel, 33 | TableName 34 | >; 35 | 36 | /** 37 | * An identifier for a document in Convex. 38 | * 39 | * Convex documents are uniquely identified by their `Id`, which is accessible 40 | * on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids). 41 | * 42 | * Documents can be loaded using `db.get(id)` in query and mutation functions. 43 | * 44 | * IDs are just strings at runtime, but this type can be used to distinguish them from other 45 | * strings when type checking. 46 | * 47 | * @typeParam TableName - A string literal type of the table name (like "users"). 48 | */ 49 | export type Id = 50 | GenericId; 51 | 52 | /** 53 | * A type describing your Convex data model. 54 | * 55 | * This type includes information about what tables you have, the type of 56 | * documents stored in those tables, and the indexes defined on them. 57 | * 58 | * This type is used to parameterize methods like `queryGeneric` and 59 | * `mutationGeneric` to make them type-safe. 60 | */ 61 | export type DataModel = DataModelFromSchemaDefinition; 62 | -------------------------------------------------------------------------------- /convex/ingest/load.ts: -------------------------------------------------------------------------------- 1 | "use node"; 2 | 3 | import { load } from "cheerio"; 4 | import { v } from "convex/values"; 5 | import { CheerioWebBaseLoader } from "langchain/document_loaders/web/cheerio"; 6 | import { CacheBackedEmbeddings } from "langchain/embeddings/cache_backed"; 7 | import { OpenAIEmbeddings } from "@langchain/openai"; 8 | OpenAIEmbeddings; 9 | import { ConvexKVStore } from "@langchain/community/storage/convex"; 10 | import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; 11 | import { ConvexVectorStore } from "@langchain/community/vectorstores/convex"; 12 | import { map } from "modern-async"; 13 | import { internal } from "../_generated/api"; 14 | import { internalAction } from "../_generated/server"; 15 | 16 | export const scrapeAndEmbedSite = internalAction({ 17 | args: { 18 | sitemapUrl: v.string(), 19 | limit: v.optional(v.number()), 20 | }, 21 | handler: async (ctx, { sitemapUrl, limit }) => { 22 | const response = await fetch(sitemapUrl); 23 | const xml = await response.text(); 24 | const $ = load(xml, { xmlMode: true }); 25 | const urls = $("url > loc") 26 | .map((_i, elem) => $(elem).text()) 27 | .get() 28 | .slice(0, limit); 29 | await map(urls, (url) => 30 | ctx.runAction(internal.ingest.load.fetchAndEmbedSingle, { url }) 31 | ); 32 | }, 33 | }); 34 | 35 | export const fetchAndEmbedSingle = internalAction({ 36 | args: { 37 | url: v.string(), 38 | }, 39 | handler: async (ctx, { url }) => { 40 | const loader = new CheerioWebBaseLoader(url); 41 | const data = await loader.load(); 42 | const textSplitter = new RecursiveCharacterTextSplitter({ 43 | chunkSize: 1000, 44 | chunkOverlap: 200, 45 | }); 46 | 47 | const splitDocs = await textSplitter.splitDocuments(data); 48 | 49 | const embeddings = new CacheBackedEmbeddings({ 50 | underlyingEmbeddings: new OpenAIEmbeddings(), 51 | documentEmbeddingStore: new ConvexKVStore({ ctx }), 52 | }); 53 | 54 | await ConvexVectorStore.fromDocuments(splitDocs, embeddings, { ctx }); 55 | }, 56 | }); 57 | -------------------------------------------------------------------------------- /src/components/ui/button.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | import { Slot } from "@radix-ui/react-slot" 3 | import { cva, type VariantProps } from "class-variance-authority" 4 | 5 | import { cn } from "@/lib/utils" 6 | 7 | const buttonVariants = cva( 8 | "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50", 9 | { 10 | variants: { 11 | variant: { 12 | default: 13 | "bg-primary text-primary-foreground shadow hover:bg-primary/90", 14 | destructive: 15 | "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90", 16 | outline: 17 | "border border-input bg-transparent shadow-sm hover:bg-accent hover:text-accent-foreground", 18 | secondary: 19 | "bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80", 20 | ghost: "hover:bg-accent hover:text-accent-foreground", 21 | link: "text-primary underline-offset-4 hover:underline", 22 | }, 23 | size: { 24 | default: "h-9 px-4 py-2", 25 | sm: "h-8 rounded-md px-3 text-xs", 26 | lg: "h-10 rounded-md px-8", 27 | icon: "h-9 w-9", 28 | }, 29 | }, 30 | defaultVariants: { 31 | variant: "default", 32 | size: "default", 33 | }, 34 | } 35 | ) 36 | 37 | export interface ButtonProps 38 | extends React.ButtonHTMLAttributes, 39 | VariantProps { 40 | asChild?: boolean 41 | } 42 | 43 | const Button = React.forwardRef( 44 | ({ className, variant, size, asChild = false, ...props }, ref) => { 45 | const Comp = asChild ? Slot : "button" 46 | return ( 47 | 52 | ) 53 | } 54 | ) 55 | Button.displayName = "Button" 56 | 57 | export { Button, buttonVariants } 58 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | darkMode: ["class"], 4 | content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"], 5 | theme: { 6 | container: { 7 | center: true, 8 | padding: "2rem", 9 | screens: { 10 | "2xl": "1400px", 11 | }, 12 | }, 13 | extend: { 14 | colors: { 15 | border: "hsl(var(--border))", 16 | input: "hsl(var(--input))", 17 | ring: "hsl(var(--ring))", 18 | background: "hsl(var(--background))", 19 | foreground: "hsl(var(--foreground))", 20 | primary: { 21 | DEFAULT: "hsl(var(--primary))", 22 | foreground: "hsl(var(--primary-foreground))", 23 | }, 24 | secondary: { 25 | DEFAULT: "hsl(var(--secondary))", 26 | foreground: "hsl(var(--secondary-foreground))", 27 | }, 28 | destructive: { 29 | DEFAULT: "hsl(var(--destructive))", 30 | foreground: "hsl(var(--destructive-foreground))", 31 | }, 32 | muted: { 33 | DEFAULT: "hsl(var(--muted))", 34 | foreground: "hsl(var(--muted-foreground))", 35 | }, 36 | accent: { 37 | DEFAULT: "hsl(var(--accent))", 38 | foreground: "hsl(var(--accent-foreground))", 39 | }, 40 | popover: { 41 | DEFAULT: "hsl(var(--popover))", 42 | foreground: "hsl(var(--popover-foreground))", 43 | }, 44 | card: { 45 | DEFAULT: "hsl(var(--card))", 46 | foreground: "hsl(var(--card-foreground))", 47 | }, 48 | }, 49 | borderRadius: { 50 | lg: "var(--radius)", 51 | md: "calc(var(--radius) - 2px)", 52 | sm: "calc(var(--radius) - 4px)", 53 | }, 54 | keyframes: { 55 | "accordion-down": { 56 | from: { height: 0 }, 57 | to: { height: "var(--radix-accordion-content-height)" }, 58 | }, 59 | "accordion-up": { 60 | from: { height: "var(--radix-accordion-content-height)" }, 61 | to: { height: 0 }, 62 | }, 63 | }, 64 | animation: { 65 | "accordion-down": "accordion-down 0.2s ease-out", 66 | "accordion-up": "accordion-up 0.2s ease-out", 67 | }, 68 | }, 69 | }, 70 | plugins: [require("tailwindcss-animate")], 71 | }; 72 | -------------------------------------------------------------------------------- /convex/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to your Convex functions directory! 2 | 3 | Write your Convex functions here. See 4 | https://docs.convex.dev/using/writing-convex-functions for more. 5 | 6 | A query function that takes two arguments looks like: 7 | 8 | ```ts 9 | // functions.js 10 | import { query } from "./_generated/server"; 11 | import { v } from "convex/values"; 12 | 13 | export const myQueryFunction = query({ 14 | // Validators for arguments. 15 | args: { 16 | first: v.number(), 17 | second: v.string(), 18 | }, 19 | 20 | // Function implementation. 21 | handler: async (ctx, args) => { 22 | // Read the database as many times as you need here. 23 | // See https://docs.convex.dev/database/reading-data. 24 | const documents = await ctx.db.query("tablename").collect(); 25 | 26 | // Arguments passed from the client are properties of the args object. 27 | console.log(args.first, args.second); 28 | 29 | // Write arbitrary JavaScript here: filter, aggregate, build derived data, 30 | // remove non-public properties, or create new objects. 31 | return documents; 32 | }, 33 | }); 34 | ``` 35 | 36 | Using this query function in a React component looks like: 37 | 38 | ```ts 39 | const data = useQuery(api.functions.myQueryFunction, { 40 | first: 10, 41 | second: "hello", 42 | }); 43 | ``` 44 | 45 | A mutation function looks like: 46 | 47 | ```ts 48 | // functions.js 49 | import { mutation } from "./_generated/server"; 50 | import { v } from "convex/values"; 51 | 52 | export const myMutationFunction = mutation({ 53 | // Validators for arguments. 54 | args: { 55 | first: v.string(), 56 | second: v.string(), 57 | }, 58 | 59 | // Function implementation. 60 | handler: async (ctx, args) => { 61 | // Insert or modify documents in the database here. 62 | // Mutations can also read from the database like queries. 63 | // See https://docs.convex.dev/database/writing-data. 64 | const message = { body: args.first, author: args.second }; 65 | const id = await ctx.db.insert("messages", message); 66 | 67 | // Optionally, return a value from your mutation. 68 | return await ctx.db.get(id); 69 | }, 70 | }); 71 | ``` 72 | 73 | Using this mutation function in a React component looks like: 74 | 75 | ```ts 76 | const mutation = useMutation(api.functions.myMutationFunction); 77 | function handleButtonPress() { 78 | // fire and forget, the most common way to use mutations 79 | mutation({ first: "Hello!", second: "me" }); 80 | // OR 81 | // use the result once the mutation has completed 82 | mutation({ first: "Hello!", second: "me" }).then((result) => 83 | console.log(result) 84 | ); 85 | } 86 | ``` 87 | 88 | Use the Convex CLI to push your functions to a deployment. See everything 89 | the Convex CLI can do by running `npx convex -h` in your project root 90 | directory. To learn more, launch the docs with `npx convex docs`. 91 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "convex-ai-chat-langchain", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "npm-run-all --parallel dev:frontend dev:backend", 8 | "dev:frontend": "vite --open", 9 | "dev:backend": "convex dev", 10 | "predev": "convex dev --until-success && convex dashboard", 11 | "build": "tsc && vite build", 12 | "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", 13 | "preview": "vite preview" 14 | }, 15 | "dependencies": { 16 | "@hookform/resolvers": "^3.3.2", 17 | "@langchain/community": "^0.2.20", 18 | "@radix-ui/react-accordion": "^1.1.2", 19 | "@radix-ui/react-alert-dialog": "^1.0.5", 20 | "@radix-ui/react-aspect-ratio": "^1.0.3", 21 | "@radix-ui/react-avatar": "^1.0.4", 22 | "@radix-ui/react-checkbox": "^1.0.4", 23 | "@radix-ui/react-collapsible": "^1.0.3", 24 | "@radix-ui/react-context-menu": "^2.1.5", 25 | "@radix-ui/react-dialog": "^1.0.5", 26 | "@radix-ui/react-dropdown-menu": "^2.0.6", 27 | "@radix-ui/react-hover-card": "^1.0.7", 28 | "@radix-ui/react-icons": "^1.3.0", 29 | "@radix-ui/react-label": "^2.0.2", 30 | "@radix-ui/react-menubar": "^1.0.4", 31 | "@radix-ui/react-navigation-menu": "^1.1.4", 32 | "@radix-ui/react-popover": "^1.0.7", 33 | "@radix-ui/react-progress": "^1.0.3", 34 | "@radix-ui/react-radio-group": "^1.1.3", 35 | "@radix-ui/react-scroll-area": "^1.0.5", 36 | "@radix-ui/react-select": "^2.0.0", 37 | "@radix-ui/react-separator": "^1.0.3", 38 | "@radix-ui/react-slider": "^1.1.2", 39 | "@radix-ui/react-slot": "^1.0.2", 40 | "@radix-ui/react-switch": "^1.0.3", 41 | "@radix-ui/react-tabs": "^1.0.4", 42 | "@radix-ui/react-toast": "^1.1.5", 43 | "@radix-ui/react-toggle": "^1.0.3", 44 | "@radix-ui/react-tooltip": "^1.0.7", 45 | "cheerio": "^1.0.0-rc.12", 46 | "class-variance-authority": "^0.7.0", 47 | "clsx": "^2.0.0", 48 | "cmdk": "^0.2.0", 49 | "convex": "^1.13.2", 50 | "date-fns": "^2.30.0", 51 | "langchain": "^0.2.11", 52 | "modern-async": "^1.1.4", 53 | "openai": "^4.19.0", 54 | "react": "^18.2.0", 55 | "react-day-picker": "^8.9.1", 56 | "react-dom": "^18.2.0", 57 | "react-hook-form": "^7.47.0", 58 | "tailwind-merge": "^1.14.0", 59 | "tailwindcss-animate": "^1.0.7", 60 | "zod": "^3.22.4" 61 | }, 62 | "devDependencies": { 63 | "@types/node": "^20.7.0", 64 | "@types/react": "^18.2.21", 65 | "@types/react-dom": "^18.2.7", 66 | "@typescript-eslint/eslint-plugin": "^6.7.0", 67 | "@typescript-eslint/parser": "^6.7.0", 68 | "@vitejs/plugin-react": "^4.0.4", 69 | "autoprefixer": "^10.4.16", 70 | "eslint": "^8.49.0", 71 | "eslint-plugin-react-hooks": "^4.6.0", 72 | "eslint-plugin-react-refresh": "^0.4.3", 73 | "npm-run-all": "^4.1.5", 74 | "postcss": "^8.4.30", 75 | "tailwindcss": "^3.3.3", 76 | "typescript": "^5.2.2", 77 | "vite": "^4.4.9" 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /convex/_generated/server.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /** 3 | * Generated utilities for implementing server-side Convex query and mutation functions. 4 | * 5 | * THIS CODE IS AUTOMATICALLY GENERATED. 6 | * 7 | * Generated by convex@1.13.2. 8 | * To regenerate, run `npx convex dev`. 9 | * @module 10 | */ 11 | 12 | import { 13 | actionGeneric, 14 | httpActionGeneric, 15 | queryGeneric, 16 | mutationGeneric, 17 | internalActionGeneric, 18 | internalMutationGeneric, 19 | internalQueryGeneric, 20 | } from "convex/server"; 21 | 22 | /** 23 | * Define a query in this Convex app's public API. 24 | * 25 | * This function will be allowed to read your Convex database and will be accessible from the client. 26 | * 27 | * @param func - The query function. It receives a {@link QueryCtx} as its first argument. 28 | * @returns The wrapped query. Include this as an `export` to name it and make it accessible. 29 | */ 30 | export const query = queryGeneric; 31 | 32 | /** 33 | * Define a query that is only accessible from other Convex functions (but not from the client). 34 | * 35 | * This function will be allowed to read from your Convex database. It will not be accessible from the client. 36 | * 37 | * @param func - The query function. It receives a {@link QueryCtx} as its first argument. 38 | * @returns The wrapped query. Include this as an `export` to name it and make it accessible. 39 | */ 40 | export const internalQuery = internalQueryGeneric; 41 | 42 | /** 43 | * Define a mutation in this Convex app's public API. 44 | * 45 | * This function will be allowed to modify your Convex database and will be accessible from the client. 46 | * 47 | * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. 48 | * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. 49 | */ 50 | export const mutation = mutationGeneric; 51 | 52 | /** 53 | * Define a mutation that is only accessible from other Convex functions (but not from the client). 54 | * 55 | * This function will be allowed to modify your Convex database. It will not be accessible from the client. 56 | * 57 | * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. 58 | * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. 59 | */ 60 | export const internalMutation = internalMutationGeneric; 61 | 62 | /** 63 | * Define an action in this Convex app's public API. 64 | * 65 | * An action is a function which can execute any JavaScript code, including non-deterministic 66 | * code and code with side-effects, like calling third-party services. 67 | * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive. 68 | * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}. 69 | * 70 | * @param func - The action. It receives an {@link ActionCtx} as its first argument. 71 | * @returns The wrapped action. Include this as an `export` to name it and make it accessible. 72 | */ 73 | export const action = actionGeneric; 74 | 75 | /** 76 | * Define an action that is only accessible from other Convex functions (but not from the client). 77 | * 78 | * @param func - The function. It receives an {@link ActionCtx} as its first argument. 79 | * @returns The wrapped function. Include this as an `export` to name it and make it accessible. 80 | */ 81 | export const internalAction = internalActionGeneric; 82 | 83 | /** 84 | * Define a Convex HTTP action. 85 | * 86 | * @param func - The function. It receives an {@link ActionCtx} as its first argument, and a `Request` object 87 | * as its second. 88 | * @returns The wrapped endpoint function. Route a URL path to this function in `convex/http.js`. 89 | */ 90 | export const httpAction = httpActionGeneric; 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI Chat using LangChain and Convex 2 | 3 | An example of building AI-powered chat interface using 4 | [LangChain](https://www.langchain.com/) and its Convex integration. 5 | 6 | ![Screenshot of a website with AI chat modal open](./screenshot.png "AI chat UI") 7 | 8 | ## Overview: 9 | 10 | This app demonstrates how you can add a chat bot to an existing website, powered 11 | by Convex and LangChain. 12 | 13 | - The chat is trigged by a button in [`App.tsx`](./src/App.tsx) 14 | - The chat frontend is all in [`src/aiChat`](./src/aiChat/index.tsx) 15 | - An example of web scraping is in [`convex/ingest`](./convex/ingest/load.ts) 16 | - The public endpoints for the backend are in 17 | [`convex/messages.ts`](./convex/messages.ts) 18 | - The answering logic is in [`convex/serve.ts`](./convex/serve.ts) 19 | 20 | ## Running the App 21 | 22 | ``` 23 | npm install 24 | npm run dev 25 | ``` 26 | 27 | This will configure a Convex project if you don't already have one, open the 28 | Convex dashboard and the web app running on `localhost`. 29 | 30 | For the chat itself to work, you must configure the following environment 31 | variable on the Convex dashboard: 32 | 33 | - `OPENAI_API_KEY` set to an [OpenAI](https://platform.openai.com/) API key 34 | (should start with `sk-`) 35 | 36 | You can change the LLM identifier `OPENAI_MODEL` in 37 | [`convex/serve.ts`](./convex/serve.ts) to `"gpt-4-32k"` if you're paying for 38 | OpenAI to improve the quality of responses. 39 | 40 | # What is Convex? 41 | 42 | [Convex](https://convex.dev) is a hosted backend platform with a built-in 43 | database that lets you write your 44 | [database schema](https://docs.convex.dev/database/schemas) and 45 | [server functions](https://docs.convex.dev/functions) in 46 | [TypeScript](https://docs.convex.dev/typescript). Server-side database 47 | [queries](https://docs.convex.dev/functions/query-functions) automatically 48 | [cache](https://docs.convex.dev/functions/query-functions#caching--reactivity) 49 | and [subscribe](https://docs.convex.dev/client/react#reactivity) to data, 50 | powering a 51 | [realtime `useQuery` hook](https://docs.convex.dev/client/react#fetching-data) 52 | in our [React client](https://docs.convex.dev/client/react). There are also 53 | [Python](https://docs.convex.dev/client/python), 54 | [Rust](https://docs.convex.dev/client/rust), 55 | [ReactNative](https://docs.convex.dev/client/react-native), and 56 | [Node](https://docs.convex.dev/client/javascript) clients, as well as a 57 | straightforward 58 | [HTTP API](https://github.com/get-convex/convex-js/blob/main/src/browser/http_client.ts#L40). 59 | 60 | The database support 61 | [NoSQL-style documents](https://docs.convex.dev/database/document-storage) with 62 | [relationships](https://docs.convex.dev/database/document-ids) and 63 | [custom indexes](https://docs.convex.dev/database/indexes/) (including on fields 64 | in nested objects). 65 | 66 | The [`query`](https://docs.convex.dev/functions/query-functions) and 67 | [`mutation`](https://docs.convex.dev/functions/mutation-functions) server 68 | functions have transactional, low latency access to the database and leverage 69 | our [`v8` runtime](https://docs.convex.dev/functions/runtimes) with 70 | [determinism guardrails](https://docs.convex.dev/functions/runtimes#using-randomness-and-time-in-queries-and-mutations) 71 | to provide the strongest ACID guarantees on the market: immediate consistency, 72 | serializable isolation, and automatic conflict resolution via 73 | [optimistic multi-version concurrency control](https://docs.convex.dev/database/advanced/occ) 74 | (OCC / MVCC). 75 | 76 | The [`action` server functions](https://docs.convex.dev/functions/actions) have 77 | access to external APIs and enable other side-effects and non-determinism in 78 | either our [optimized `v8` runtime](https://docs.convex.dev/functions/runtimes) 79 | or a more 80 | [flexible `node` runtime](https://docs.convex.dev/functions/runtimes#nodejs-runtime). 81 | 82 | Functions can run in the background via 83 | [scheduling](https://docs.convex.dev/scheduling/scheduled-functions) and 84 | [cron jobs](https://docs.convex.dev/scheduling/cron-jobs). 85 | 86 | Development is cloud-first, with 87 | [hot reloads for server function](https://docs.convex.dev/cli#run-the-convex-dev-server) 88 | editing via the [CLI](https://docs.convex.dev/cli). There is a 89 | [dashbord UI](https://docs.convex.dev/dashboard) to 90 | [browse and edit data](https://docs.convex.dev/dashboard/deployments/data), 91 | [edit environment variables](https://docs.convex.dev/production/environment-variables), 92 | [view logs](https://docs.convex.dev/dashboard/deployments/logs), 93 | [run server functions](https://docs.convex.dev/dashboard/deployments/functions), 94 | and more. 95 | 96 | There are built-in features for 97 | [reactive pagination](https://docs.convex.dev/database/pagination), 98 | [file storage](https://docs.convex.dev/file-storage), 99 | [reactive search](https://docs.convex.dev/text-search), 100 | [https endpoints](https://docs.convex.dev/functions/http-actions) (for 101 | webhooks), 102 | [streaming import/export](https://docs.convex.dev/database/import-export/), and 103 | [runtime data validation](https://docs.convex.dev/database/schemas#validators) 104 | for [function arguments](https://docs.convex.dev/functions/args-validation) and 105 | [database data](https://docs.convex.dev/database/schemas#schema-validation). 106 | 107 | Everything scales automatically, and it’s 108 | [free to start](https://www.convex.dev/plans). 109 | -------------------------------------------------------------------------------- /convex/_generated/server.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /** 3 | * Generated utilities for implementing server-side Convex query and mutation functions. 4 | * 5 | * THIS CODE IS AUTOMATICALLY GENERATED. 6 | * 7 | * Generated by convex@1.13.2. 8 | * To regenerate, run `npx convex dev`. 9 | * @module 10 | */ 11 | 12 | import { 13 | ActionBuilder, 14 | HttpActionBuilder, 15 | MutationBuilder, 16 | QueryBuilder, 17 | GenericActionCtx, 18 | GenericMutationCtx, 19 | GenericQueryCtx, 20 | GenericDatabaseReader, 21 | GenericDatabaseWriter, 22 | } from "convex/server"; 23 | import type { DataModel } from "./dataModel.js"; 24 | 25 | /** 26 | * Define a query in this Convex app's public API. 27 | * 28 | * This function will be allowed to read your Convex database and will be accessible from the client. 29 | * 30 | * @param func - The query function. It receives a {@link QueryCtx} as its first argument. 31 | * @returns The wrapped query. Include this as an `export` to name it and make it accessible. 32 | */ 33 | export declare const query: QueryBuilder; 34 | 35 | /** 36 | * Define a query that is only accessible from other Convex functions (but not from the client). 37 | * 38 | * This function will be allowed to read from your Convex database. It will not be accessible from the client. 39 | * 40 | * @param func - The query function. It receives a {@link QueryCtx} as its first argument. 41 | * @returns The wrapped query. Include this as an `export` to name it and make it accessible. 42 | */ 43 | export declare const internalQuery: QueryBuilder; 44 | 45 | /** 46 | * Define a mutation in this Convex app's public API. 47 | * 48 | * This function will be allowed to modify your Convex database and will be accessible from the client. 49 | * 50 | * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. 51 | * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. 52 | */ 53 | export declare const mutation: MutationBuilder; 54 | 55 | /** 56 | * Define a mutation that is only accessible from other Convex functions (but not from the client). 57 | * 58 | * This function will be allowed to modify your Convex database. It will not be accessible from the client. 59 | * 60 | * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. 61 | * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. 62 | */ 63 | export declare const internalMutation: MutationBuilder; 64 | 65 | /** 66 | * Define an action in this Convex app's public API. 67 | * 68 | * An action is a function which can execute any JavaScript code, including non-deterministic 69 | * code and code with side-effects, like calling third-party services. 70 | * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive. 71 | * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}. 72 | * 73 | * @param func - The action. It receives an {@link ActionCtx} as its first argument. 74 | * @returns The wrapped action. Include this as an `export` to name it and make it accessible. 75 | */ 76 | export declare const action: ActionBuilder; 77 | 78 | /** 79 | * Define an action that is only accessible from other Convex functions (but not from the client). 80 | * 81 | * @param func - The function. It receives an {@link ActionCtx} as its first argument. 82 | * @returns The wrapped function. Include this as an `export` to name it and make it accessible. 83 | */ 84 | export declare const internalAction: ActionBuilder; 85 | 86 | /** 87 | * Define an HTTP action. 88 | * 89 | * This function will be used to respond to HTTP requests received by a Convex 90 | * deployment if the requests matches the path and method where this action 91 | * is routed. Be sure to route your action in `convex/http.js`. 92 | * 93 | * @param func - The function. It receives an {@link ActionCtx} as its first argument. 94 | * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up. 95 | */ 96 | export declare const httpAction: HttpActionBuilder; 97 | 98 | /** 99 | * A set of services for use within Convex query functions. 100 | * 101 | * The query context is passed as the first argument to any Convex query 102 | * function run on the server. 103 | * 104 | * This differs from the {@link MutationCtx} because all of the services are 105 | * read-only. 106 | */ 107 | export type QueryCtx = GenericQueryCtx; 108 | 109 | /** 110 | * A set of services for use within Convex mutation functions. 111 | * 112 | * The mutation context is passed as the first argument to any Convex mutation 113 | * function run on the server. 114 | */ 115 | export type MutationCtx = GenericMutationCtx; 116 | 117 | /** 118 | * A set of services for use within Convex action functions. 119 | * 120 | * The action context is passed as the first argument to any Convex action 121 | * function run on the server. 122 | */ 123 | export type ActionCtx = GenericActionCtx; 124 | 125 | /** 126 | * An interface to read from the database within Convex query functions. 127 | * 128 | * The two entry points are {@link DatabaseReader.get}, which fetches a single 129 | * document by its {@link Id}, or {@link DatabaseReader.query}, which starts 130 | * building a query. 131 | */ 132 | export type DatabaseReader = GenericDatabaseReader; 133 | 134 | /** 135 | * An interface to read from and write to the database within Convex mutation 136 | * functions. 137 | * 138 | * Convex guarantees that all writes within a single mutation are 139 | * executed atomically, so you never have to worry about partial writes leaving 140 | * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control) 141 | * for the guarantees Convex provides your functions. 142 | */ 143 | export type DatabaseWriter = GenericDatabaseWriter; 144 | -------------------------------------------------------------------------------- /src/aiChat/index.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | ConvexProvider, 3 | ConvexReactClient, 4 | useMutation, 5 | useQuery, 6 | } from "convex/react"; 7 | import { 8 | FormEvent, 9 | ReactNode, 10 | useCallback, 11 | useEffect, 12 | useMemo, 13 | useRef, 14 | useState, 15 | } from "react"; 16 | import { createPortal } from "react-dom"; 17 | import { api } from "../../convex/_generated/api.js"; 18 | import { CloseIcon } from "./CloseIcon.js"; 19 | import { InfoCircled } from "./InfoCircled.js"; 20 | import { SendIcon } from "./SendIcon.js"; 21 | import { SizeIcon } from "./SizeIcon.js"; 22 | import { TrashIcon } from "./TrashIcon.js"; 23 | 24 | export function ConvexAiChat({ 25 | convexUrl, 26 | infoMessage, 27 | name, 28 | welcomeMessage, 29 | renderTrigger, 30 | }: { 31 | convexUrl: string; 32 | name: string; 33 | infoMessage: ReactNode; 34 | welcomeMessage: string; 35 | renderTrigger: (onClick: () => void) => ReactNode; 36 | }) { 37 | const [hasOpened, setHasOpened] = useState(false); 38 | const [dialogOpen, setDialogOpen] = useState(false); 39 | 40 | const handleCloseDialog = useCallback(() => { 41 | setDialogOpen(false); 42 | }, []); 43 | 44 | return ( 45 | <> 46 | {renderTrigger(() => { 47 | setHasOpened(true); 48 | setDialogOpen(!dialogOpen); 49 | })} 50 | {hasOpened 51 | ? createPortal( 52 | , 60 | document.body 61 | ) 62 | : null} 63 | 64 | ); 65 | } 66 | 67 | export function ConvexAiChatDialog({ 68 | convexUrl, 69 | infoMessage, 70 | isOpen, 71 | name, 72 | welcomeMessage, 73 | onClose, 74 | }: { 75 | convexUrl: string; 76 | infoMessage: ReactNode; 77 | isOpen: boolean; 78 | name: string; 79 | welcomeMessage: string; 80 | onClose: () => void; 81 | }) { 82 | const client = useMemo(() => new ConvexReactClient(convexUrl), [convexUrl]); 83 | 84 | return ( 85 | 86 | 93 | 94 | ); 95 | } 96 | 97 | export function Dialog({ 98 | infoMessage, 99 | isOpen, 100 | name, 101 | welcomeMessage, 102 | onClose, 103 | }: { 104 | infoMessage: ReactNode; 105 | isOpen: boolean; 106 | name: string; 107 | welcomeMessage: string; 108 | onClose: () => void; 109 | }) { 110 | const sessionId = useSessionId(); 111 | const remoteMessages = useQuery(api.messages.list, { sessionId }); 112 | const messages = useMemo( 113 | () => 114 | [{ isViewer: false, text: welcomeMessage, _id: "0" }].concat( 115 | (remoteMessages ?? []) as { 116 | isViewer: boolean; 117 | text: string; 118 | _id: string; 119 | }[] 120 | ), 121 | [remoteMessages, welcomeMessage] 122 | ); 123 | const sendMessage = useMutation(api.messages.send); 124 | const clearMesages = useMutation(api.messages.clear); 125 | 126 | const [expanded, setExpanded] = useState(false); 127 | const [isScrolled, setScrolled] = useState(false); 128 | 129 | const [input, setInput] = useState(""); 130 | 131 | const handleExpand = () => { 132 | setExpanded(!expanded); 133 | setScrolled(false); 134 | }; 135 | 136 | const handleSend = async (event: FormEvent) => { 137 | event.preventDefault(); 138 | await sendMessage({ message: input, sessionId }); 139 | setInput(""); 140 | setScrolled(false); 141 | }; 142 | 143 | const handleClearMessages = async () => { 144 | await clearMesages({ sessionId }); 145 | setScrolled(false); 146 | }; 147 | 148 | const listRef = useRef(null); 149 | 150 | useEffect(() => { 151 | if (isScrolled) { 152 | return; 153 | } 154 | // Using `setTimeout` to make sure scrollTo works on button click in Chrome 155 | setTimeout(() => { 156 | listRef.current?.scrollTo({ 157 | top: listRef.current.scrollHeight, 158 | behavior: "smooth", 159 | }); 160 | }, 0); 161 | }, [messages, isScrolled]); 162 | 163 | return ( 164 |
176 |
177 | 191 | 197 | 203 | 209 |
210 |
{ 214 | setScrolled(true); 215 | }} 216 | > 217 | {remoteMessages === undefined ? ( 218 | <> 219 |
220 |
221 | 222 | ) : ( 223 | messages.map((message) => ( 224 |
225 |
231 | {message.isViewer ? <>You : <>{name}} 232 |
233 | {message.text === "" ? ( 234 |
235 | ) : ( 236 |
247 | {message.text} 248 |
249 | )} 250 |
251 | )) 252 | )} 253 |
254 |
void handleSend(event)} 257 | > 258 | setInput(event.target.value)} 265 | /> 266 | 272 |
273 |
274 | ); 275 | } 276 | 277 | const STORE = (typeof window === "undefined" ? null : window)?.sessionStorage; 278 | const STORE_KEY = "ConvexSessionId"; 279 | 280 | function useSessionId() { 281 | const [sessionId] = useState( 282 | () => STORE?.getItem(STORE_KEY) ?? crypto.randomUUID() 283 | ); 284 | 285 | // Get or set the ID from our desired storage location, whenever it changes. 286 | useEffect(() => { 287 | STORE?.setItem(STORE_KEY, sessionId); 288 | }, [sessionId]); 289 | 290 | return sessionId; 291 | } 292 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2024 Convex, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------