Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mirrored_repos/machinelearning/run-llama/tool
1 result
Show changes
Commits on Source (2)
Showing
with 460 additions and 2 deletions
......@@ -2,7 +2,7 @@
"type": "module",
"dependencies": {
"@llamaindex/tool": "workspace:*",
"llamaindex": "^0.2.10",
"llamaindex": "^0.3.0",
"openai": "^4.33.0"
},
"scripts": {
......
import * as Tools from './index.llama.js'
import * as Tools from './index.tool.js'
import { convertTools, registerTools } from '@llamaindex/tool'
import { OpenAI } from 'openai'
import { inspect } from 'node:util'
......
# Rename this file to `.env.local` to use environment variables locally with `next dev`
# https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
MY_HOST="example.com"
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
This is a [LlamaIndex](https://www.llamaindex.ai/) project using [Next.js](https://nextjs.org/) bootstrapped with [`create-llama`](https://github.com/run-llama/LlamaIndexTS/tree/main/packages/create-llama).
## Getting Started
First, install the dependencies:
```
npm install
```
Second, run the development server:
```
npm run dev
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
## Learn More
To learn more about LlamaIndex, take a look at the following resources:
- [LlamaIndex Documentation](https://docs.llamaindex.ai) - learn about LlamaIndex (Python features).
- [LlamaIndexTS Documentation](https://ts.llamaindex.ai) - learn about LlamaIndex (Typescript features).
You can check out [the LlamaIndexTS GitHub repository](https://github.com/run-llama/LlamaIndexTS) - your feedback and contributions are welcome!
'use server'
import { OpenAIAgent } from 'llamaindex/agent/openai'
// import your tools on top, that's it
import '@/tool/index.tool'
import { convertTools } from '@llamaindex/tool'
import type { JSX } from 'react'
import {
createStreamableUI,
} from 'ai/rsc'
import { runWithStreamableUI } from '@/context'
export async function chatWithAI (
message: string,
): Promise<JSX.Element> {
const agent = new OpenAIAgent(
{
tools: convertTools('llamaindex')
}
)
const uiStream = createStreamableUI()
runWithStreamableUI(uiStream, () => agent.chat({
stream: true,
message,
}).then(async (responseStream) => {
return responseStream.pipeTo(
new WritableStream({
start: () => {
uiStream.append('\n')
},
write: async (message) => {
uiStream.append(message.response.delta)
},
close: () => {
uiStream.done()
}
})
)
}
)).catch(uiStream.error)
return uiStream.value
}
\ No newline at end of file
examples/02_nextjs/app/favicon.ico

15 KiB

@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 222.2 47.4% 11.2%;
--muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%;
--popover: 0 0% 100%;
--popover-foreground: 222.2 47.4% 11.2%;
--border: 214.3 31.8% 91.4%;
--input: 214.3 31.8% 91.4%;
--card: 0 0% 100%;
--card-foreground: 222.2 47.4% 11.2%;
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;
--secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%;
--accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%;
--destructive: 0 100% 50%;
--destructive-foreground: 210 40% 98%;
--ring: 215 20.2% 65.1%;
--radius: 0.5rem;
}
.dark {
--background: 224 71% 4%;
--foreground: 213 31% 91%;
--muted: 223 47% 11%;
--muted-foreground: 215.4 16.3% 56.9%;
--accent: 216 34% 17%;
--accent-foreground: 210 40% 98%;
--popover: 224 71% 4%;
--popover-foreground: 215 20.2% 65.1%;
--border: 216 34% 17%;
--input: 216 34% 17%;
--card: 224 71% 4%;
--card-foreground: 213 31% 91%;
--primary: 210 40% 98%;
--primary-foreground: 222.2 47.4% 1.2%;
--secondary: 222.2 47.4% 11.2%;
--secondary-foreground: 210 40% 98%;
--destructive: 0 63% 31%;
--destructive-foreground: 210 40% 98%;
--ring: 216 34% 17%;
--radius: 0.5rem;
}
}
@layer base {
* {
@apply border-border;
}
body {
@apply bg-background text-foreground;
font-feature-settings:
"rlig" 1,
"calt" 1;
}
.background-gradient {
background-color: #fff;
background-image: radial-gradient(
at 21% 11%,
rgba(186, 186, 233, 0.53) 0,
transparent 50%
),
radial-gradient(at 85% 0, hsla(46, 57%, 78%, 0.52) 0, transparent 50%),
radial-gradient(at 91% 36%, rgba(194, 213, 255, 0.68) 0, transparent 50%),
radial-gradient(at 8% 40%, rgba(251, 218, 239, 0.46) 0, transparent 50%);
}
}
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'
import { Toaster } from 'sonner'
const inter = Inter({ subsets: ['latin'] })
export const metadata: Metadata = {
title: 'Create Llama App',
description: 'Generated by create-llama'
}
export default function RootLayout ({
children
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={inter.className}>
<Toaster/>
{children}
</body>
</html>
)
}
import { ChatSection } from '@/components/chat-section'
export const runtime = "edge";
export default function Home () {
return (
<main
className="flex min-h-screen flex-col items-center gap-10 p-24 background-gradient"
>
<ChatSection/>
</main>
)
}
'use client'
import { ReactNode, useActionState } from 'react'
import { toast } from 'sonner'
import { chatWithAI } from '@/actions'
export function ChatSection () {
const [state, formAction] = useActionState<ReactNode | null, FormData>(
async (state, payload) => {
const input = payload.get('input') as string | null
if (!input) {
toast.error('Please type a message')
return null
}
return chatWithAI(input)
}, null)
return (
<form>
<div
className="border border-gray-400 p-2 max-w-md"
>
{state}
</div>
<input
className="border border-gray-400 p-2"
type="text"
name="input"
placeholder="Type your message here"
/>
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
formAction={formAction}
>
Chat
</button>
</form>
)
}
\ No newline at end of file
export function LocationCard() {
return (
<div
className="border border-gray-400 p-2 max-w-md"
>
<h1>Weather</h1>
<p>San Francisco, CA</p>
<p>Sunny</p>
</div>
)
}
\ No newline at end of file
export function Spinner () {
return (
<div role="status">
<svg aria-hidden="true"
className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"/>
</svg>
<span className="sr-only">Loading...</span>
</div>
)
}
\ No newline at end of file
import { AsyncLocalStorage } from 'node:async_hooks'
import type { createStreamableUI } from 'ai/rsc'
type StreamableUI = ReturnType<typeof createStreamableUI>
const streamUIAsyncLocalStorage = new AsyncLocalStorage<StreamableUI>()
export function getCurrentStreamableUI () {
return streamUIAsyncLocalStorage.getStore()
}
export function runWithStreamableUI<T> (
streamUI: StreamableUI,
fn: () => T
): T {
return streamUIAsyncLocalStorage.run(streamUI, fn)
}
\ No newline at end of file
import { withNext } from '@llamaindex/tool/next'
/** @type {import('next').NextConfig} */
const nextConfig = {};
export default withNext(nextConfig);
{
"name": "02_nextjs",
"version": "0.1.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@llamaindex/tool": "workspace:^",
"@radix-ui/react-slot": "^1.0.2",
"ai": "^3.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^1.2.1",
"dotenv": "^16.3.1",
"llamaindex": "0.3.0",
"lucide-react": "^0.294.0",
"next": "^14.3.0-canary.33",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^8.0.7",
"react-syntax-highlighter": "^15.5.0",
"remark": "^14.0.3",
"remark-code-import": "^1.2.0",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"sonner": "^1.4.41",
"supports-color": "^9.4.0",
"tailwind-merge": "^2.1.0"
},
"devDependencies": {
"@types/node": "^20.10.3",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.11",
"autoprefixer": "^10.4.16",
"cross-env": "^7.0.3",
"postcss": "^8.4.32",
"tailwindcss": "^3.3.6",
"typescript": "^5.3.2"
}
}
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
examples/02_nextjs/public/llama.png

36.1 KiB

import type { Config } from "tailwindcss";
import { fontFamily } from "tailwindcss/defaultTheme";
const config: Config = {
darkMode: ["class"],
content: ["app/**/*.{ts,tsx}", "components/**/*.{ts,tsx}"],
theme: {
container: {
center: true,
padding: "2rem",
screens: {
"2xl": "1400px",
},
},
extend: {
colors: {
border: "hsl(var(--border))",
input: "hsl(var(--input))",
ring: "hsl(var(--ring))",
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
primary: {
DEFAULT: "hsl(var(--primary))",
foreground: "hsl(var(--primary-foreground))",
},
secondary: {
DEFAULT: "hsl(var(--secondary))",
foreground: "hsl(var(--secondary-foreground))",
},
destructive: {
DEFAULT: "hsl(var(--destructive) / <alpha-value>)",
foreground: "hsl(var(--destructive-foreground) / <alpha-value>)",
},
muted: {
DEFAULT: "hsl(var(--muted))",
foreground: "hsl(var(--muted-foreground))",
},
accent: {
DEFAULT: "hsl(var(--accent))",
foreground: "hsl(var(--accent-foreground))",
},
popover: {
DEFAULT: "hsl(var(--popover))",
foreground: "hsl(var(--popover-foreground))",
},
card: {
DEFAULT: "hsl(var(--card))",
foreground: "hsl(var(--card-foreground))",
},
},
borderRadius: {
xl: `calc(var(--radius) + 4px)`,
lg: `var(--radius)`,
md: `calc(var(--radius) - 2px)`,
sm: "calc(var(--radius) - 4px)",
},
fontFamily: {
sans: ["var(--font-sans)", ...fontFamily.sans],
},
keyframes: {
"accordion-down": {
from: { height: "0" },
to: { height: "var(--radix-accordion-content-height)" },
},
"accordion-up": {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: "0" },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
},
},
},
plugins: [],
};
export default config;