Skip to content

Commit

Permalink
feat: add t3 example
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jan 29, 2024
1 parent 24cb48f commit a4382f1
Show file tree
Hide file tree
Showing 26 changed files with 1,366 additions and 135 deletions.
15 changes: 15 additions & 0 deletions examples/llamaindex-t3/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Since the ".env" file is gitignored, you can use the ".env.example" file to
# build a new ".env" file when you clone the repo. Keep this file up-to-date
# when you add new variables to `.env`.

# This file will be committed to version control, so make sure not to have any
# secrets in it. If you are cloning this repo, create a copy of this file named
# ".env" and populate it with your secrets.

# When adding additional environment variables, the schema in "/src/env.js"
# should be updated accordingly.

# Example:
# SERVERVAR="foo"
# NEXT_PUBLIC_CLIENTVAR="bar"
OPENAI_API_KEY=
42 changes: 42 additions & 0 deletions examples/llamaindex-t3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# database
/prisma/db.sqlite
/prisma/db.sqlite-journal

# next.js
/.next/
/out/
next-env.d.ts

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
# do not commit any .env files to git, except for the .env.example file. https://create.t3.gg/en/usage/env-variables#using-environment-variables
.env
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
1 change: 1 addition & 0 deletions examples/llamaindex-t3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Llamaindex T3 Example
20 changes: 20 additions & 0 deletions examples/llamaindex-t3/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
await import("./src/env.js");

/** @type {import("next").NextConfig} */
const config = {
webpack: (config) => {
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
};

return config;
},
};

export default config;
47 changes: 47 additions & 0 deletions examples/llamaindex-t3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "llamaindex-t3",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"build": "next build",
"dev": "next dev",
"lint": "next lint",
"start": "next start"
},
"dependencies": {
"@t3-oss/env-nextjs": "^0.7.1",
"@tanstack/react-query": "^4.36.1",
"@trpc/client": "^10.43.6",
"@trpc/next": "^10.43.6",
"@trpc/react-query": "^10.43.6",
"@trpc/server": "^10.43.6",
"llamaindex": "latest",
"next": "^14.0.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"server-only": "^0.0.1",
"superjson": "^2.2.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/eslint": "^8.44.7",
"@types/node": "^18.17.0",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.54.0",
"eslint-config-next": "^14.0.4",
"postcss": "^8.4.31",
"prettier": "^3.1.0",
"prettier-plugin-tailwindcss": "^0.5.7",
"tailwindcss": "^3.3.5",
"typescript": "^5.1.6"
},
"ct3aMetadata": {
"initVersion": "7.26.0"
},
"packageManager": "[email protected]"
}
8 changes: 8 additions & 0 deletions examples/llamaindex-t3/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const config = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};

module.exports = config;
6 changes: 6 additions & 0 deletions examples/llamaindex-t3/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
const config = {
plugins: ["prettier-plugin-tailwindcss"],
};

export default config;
Binary file added examples/llamaindex-t3/public/favicon.ico
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { type NextRequest } from "next/server";

import { env } from "~/env";

Check failure on line 4 in examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/env' or its corresponding type declarations.
import { appRouter } from "~/server/api/root";

Check failure on line 5 in examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/server/api/root' or its corresponding type declarations.
import { createTRPCContext } from "~/server/api/trpc";

Check failure on line 6 in examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/server/api/trpc' or its corresponding type declarations.

/**
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when
* handling a HTTP request (e.g. when you make requests from Client Components).
*/
const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
});
};

const handler = (req: NextRequest) =>
fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext: () => createContext(req),
onError:
env.NODE_ENV === "development"
? ({ path, error }) => {

Check failure on line 26 in examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts

View workflow job for this annotation

GitHub Actions / typecheck

Binding element 'path' implicitly has an 'any' type.

Check failure on line 26 in examples/llamaindex-t3/src/app/api/trpc/[trpc]/route.ts

View workflow job for this annotation

GitHub Actions / typecheck

Binding element 'error' implicitly has an 'any' type.
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
);
}
: undefined,
});

export { handler as GET, handler as POST };
30 changes: 30 additions & 0 deletions examples/llamaindex-t3/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "~/styles/globals.css";

import { Inter } from "next/font/google";

import { TRPCReactProvider } from "~/trpc/react";

const inter = Inter({
subsets: ["latin"],
variable: "--font-sans",
});

export const metadata = {
title: "Create T3 App",
description: "Generated by create-t3-app",
icons: [{ rel: "icon", url: "/favicon.ico" }],
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={`font-sans ${inter.variable}`}>
<TRPCReactProvider>{children}</TRPCReactProvider>
</body>
</html>
);
}
12 changes: 12 additions & 0 deletions examples/llamaindex-t3/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { unstable_noStore as noStore } from "next/cache";
import { AskQuestion } from "~/components/ask-question";

export default async function Home() {
noStore();

return (
<main>
<AskQuestion />
</main>
);
}
36 changes: 36 additions & 0 deletions examples/llamaindex-t3/src/components/ask-question.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";
import { api } from "~/trpc/react";
import { Suspense, useState } from "react";

export function AskQuestion() {
const [question, setQuestion] = useState<string | null>(null);
return (
<div>
<h1></h1>
AskQuestion
<form
action={(formData) => {
setQuestion(formData.get("question") as string);
}}
>
<input name="question" type="text" />
<button type="submit">Ask</button>
</form>
<Suspense fallback="Asking...">
{question && <Response question={question} />}
</Suspense>
</div>
);
}

type ResponseProps = {
question: string;
};

const Response = (props: ResponseProps) => {
const [{ text }] = api.post.ask.useSuspenseQuery({
text: props.question,
});

return <div>{text}</div>;
};
42 changes: 42 additions & 0 deletions examples/llamaindex-t3/src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

export const env = createEnv({
/**
* Specify your server-side environment variables schema here. This way you can ensure the app
* isn't built with invalid env vars.
*/
server: {
NODE_ENV: z.enum(["development", "test", "production"]),
OPENAI_API_KEY: z.string(),
},

/**
* Specify your client-side environment variables schema here. This way you can ensure the app
* isn't built with invalid env vars. To expose them to the client, prefix them with
* `NEXT_PUBLIC_`.
*/
client: {
// NEXT_PUBLIC_CLIENTVAR: z.string(),
},

/**
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
* middlewares) or client-side so we need to destruct manually.
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
* useful for Docker builds.
*/
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
/**
* Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
* `SOME_VAR=''` will throw an error.
*/
emptyStringAsUndefined: true,
});
14 changes: 14 additions & 0 deletions examples/llamaindex-t3/src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { postRouter } from "~/server/api/routers/post";

Check failure on line 1 in examples/llamaindex-t3/src/server/api/root.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/server/api/routers/post' or its corresponding type declarations.
import { createTRPCRouter } from "~/server/api/trpc";

Check failure on line 2 in examples/llamaindex-t3/src/server/api/root.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/server/api/trpc' or its corresponding type declarations.

/**
* This is the primary router for your server.
*
* All routers added in /api/routers should be manually added here.
*/
export const appRouter = createTRPCRouter({
post: postRouter,
});

// export type definition of API
export type AppRouter = typeof appRouter;
29 changes: 29 additions & 0 deletions examples/llamaindex-t3/src/server/api/routers/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from "zod";
import { OpenAI } from "llamaindex";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

Check failure on line 4 in examples/llamaindex-t3/src/server/api/routers/post.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '~/server/api/trpc' or its corresponding type declarations.

const openai = new OpenAI();

export const postRouter = createTRPCRouter({
ask: publicProcedure
.input(z.object({ text: z.string() }))
.query(async ({ input }) => {

Check failure on line 11 in examples/llamaindex-t3/src/server/api/routers/post.ts

View workflow job for this annotation

GitHub Actions / typecheck

Binding element 'input' implicitly has an 'any' type.
const response = await openai.chat({
stream: false,
messages: [
{
role: "system",
content: "You are a helpful assistant",
},
{
role: "user",
content: input.text,
},
],
});
return {
text: response.message.content,
};
}),
});
Loading

0 comments on commit a4382f1

Please sign in to comment.