-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add vue composables and nuxt implementation (#84)
Co-authored-by: Max Leiter <maxwell.leiter@gmail.com>
- Loading branch information
Showing
20 changed files
with
7,942 additions
and
1,655 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,5 @@ | ||
--- | ||
'ai': minor | ||
--- | ||
|
||
This adds Vue support for `ai` via the `ai/vue` subpath export. Vue composables `useChat` and `useCompletion` are provided. |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.next | ||
.nuxt | ||
node_modules | ||
dist | ||
dist |
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
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 @@ | ||
NUXT_OPENAI_API_KEY=xxxxxxx |
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,23 @@ | ||
# Nuxt dev/build outputs | ||
.output | ||
.nuxt | ||
.nitro | ||
.cache | ||
dist | ||
|
||
# Node dependencies | ||
node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
||
# Misc | ||
.DS_Store | ||
.fleet | ||
.idea | ||
|
||
# Local env files | ||
.env | ||
.env.* | ||
!.env.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,34 @@ | ||
# Vercel AI SDK, Nuxt and OpenAI Chat Example | ||
|
||
This example shows how to use the [Vercel AI SDK](https://sdk.vercel.ai/docs) with [Nuxt](https://nuxt.com/), and [OpenAI](https://openai.com) to create a ChatGPT-like AI-powered streaming chat bot. | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=ai-sdk-example): | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel-labs%2Fai%2Ftree%2Fmain%2Fexamples%2Fnuxt-openai&env=NUXT_OPENAI_API_KEY&envDescription=OpenAI%20API%20Key&envLink=https%3A%2F%2Fplatform.openai.com%2Faccount%2Fapi-keys&project-name=ai-chat&repository-name=nuxt-ai-chat) | ||
|
||
## How to use | ||
|
||
Execute `nuxi` to bootstrap the example: | ||
|
||
```bash | ||
npx nuxi@latest init -t github:vercel-labs/ai/examples/nuxt-openai nuxt-openai | ||
``` | ||
|
||
To run the example locally you need to: | ||
|
||
1. Sign up at [OpenAI's Developer Platform](https://platform.openai.com/signup). | ||
2. Go to [OpenAI's dashboard](https://platform.openai.com/account/api-keys) and create an API KEY. | ||
3. Set the required OpenAI environment variable as the token value as shown [the example env file](./.env.example) but in a new file called `.env`. | ||
4. `pnpm install` to install the required dependencies. | ||
5. `pnpm dev` to launch the development server. | ||
|
||
## Learn More | ||
|
||
To learn more about OpenAI, Nuxt, and the Vercel AI SDK take a look at the following resources: | ||
|
||
- [Vercel AI SDK docs](https://sdk.vercel.ai/docs) - learn mode about the Vercel AI SDK | ||
- [Vercel AI Playground](https://play.vercel.ai) - compare and tune 20+ AI models side-by-side | ||
- [OpenAI Documentation](https://platform.openai.com/docs) - learn about OpenAI features and API. | ||
- [Nuxt Documentation](https://nuxt.com/docs) - learn about Nuxt features and API. |
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,23 @@ | ||
<script setup> | ||
import { useChat } from 'ai/vue' | ||
const { messages, input, handleInputChange, handleSubmit } = useChat() | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
<div v-for="m in messages" key="m.id" class="whitespace-pre-wrap"> | ||
{{ m.role === 'user' ? 'User: ' : 'AI: ' }} | ||
{{ m.content }} | ||
</div> | ||
|
||
<form @submit="handleSubmit"> | ||
<input | ||
class="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
v-model="input" | ||
placeholder="Say something..." | ||
@change="handleInputChange" | ||
/> | ||
</form> | ||
</div> | ||
</template> |
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 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
devtools: { enabled: true }, | ||
modules: ['@nuxtjs/tailwindcss'], | ||
runtimeConfig: { | ||
openaiApiKey: '' | ||
} | ||
}) |
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,26 @@ | ||
{ | ||
"name": "nuxt-openai", | ||
"private": true, | ||
"scripts": { | ||
"build": "nuxt build", | ||
"dev": "nuxt dev", | ||
"generate": "nuxt generate", | ||
"preview": "nuxt preview", | ||
"postinstall": "nuxt prepare" | ||
}, | ||
"devDependencies": { | ||
"@nuxt/devtools": "latest", | ||
"@nuxtjs/tailwindcss": "^6.7.2", | ||
"@types/node": "^18", | ||
"@vue/reactivity": "^3.3.4", | ||
"@vue/runtime-core": "^3.3.4", | ||
"@vue/runtime-dom": "^3.3.4", | ||
"@vue/shared": "^3.3.4", | ||
"ai": "2.0.1", | ||
"nuxt": "^3.5.2", | ||
"openai-edge": "^0.5.1", | ||
"tailwindcss": "^3.3.2", | ||
"unctx": "^2.3.1", | ||
"vue": "^3.3.4" | ||
} | ||
} |
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 @@ | ||
// ./api/chat.ts | ||
import { Configuration, OpenAIApi } from 'openai-edge' | ||
import { OpenAIStream, streamToResponse } from 'ai' | ||
|
||
// Create an OpenAI API client (that's edge friendly!) | ||
const config = new Configuration({ | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
apiKey: useRuntimeConfig().openaiApiKey | ||
}) | ||
const openai = new OpenAIApi(config) | ||
|
||
export default defineEventHandler(async event => { | ||
// Extract the `prompt` from the body of the request | ||
const { messages } = await readBody(event) | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.createChatCompletion({ | ||
model: 'gpt-3.5-turbo', | ||
stream: true, | ||
messages: messages.map((message: any) => ({ | ||
content: message.content, | ||
role: message.role | ||
})) | ||
}) | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response) | ||
// Respond with the stream | ||
streamToResponse(stream, event.node.res) | ||
}) |
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,3 @@ | ||
{ | ||
"extends": "../.nuxt/tsconfig.server.json" | ||
} |
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,4 @@ | ||
{ | ||
// https://nuxt.com/docs/guide/concepts/typescript | ||
"extends": "./.nuxt/tsconfig.json" | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"extends": "@vercel/ai-tsconfig/react-library.json", | ||
"extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
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
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,2 @@ | ||
export * from './use-chat' | ||
export * from './use-completion' |
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,10 @@ | ||
{ | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": "./dist/index.mjs", | ||
"private": true, | ||
"peerDependencies": { | ||
"vue": "*" | ||
} | ||
} |
Oops, something went wrong.
71f9c51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ai – ./
ai-vercel-labs.vercel.app
ai-git-main-vercel-labs.vercel.app
ai-three-xi.vercel.app