forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,366 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Llamaindex T3 Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const config = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
import { appRouter } from "~/server/api/root"; | ||
import { createTRPCContext } from "~/server/api/trpc"; | ||
|
||
/** | ||
* 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
|
||
console.error( | ||
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`, | ||
); | ||
} | ||
: undefined, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { postRouter } from "~/server/api/routers/post"; | ||
import { createTRPCRouter } from "~/server/api/trpc"; | ||
|
||
/** | ||
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
const openai = new OpenAI(); | ||
|
||
export const postRouter = createTRPCRouter({ | ||
ask: publicProcedure | ||
.input(z.object({ text: z.string() })) | ||
.query(async ({ input }) => { | ||
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, | ||
}; | ||
}), | ||
}); |
Oops, something went wrong.