Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigio committed May 1, 2024
0 parents commit 15cc623
Show file tree
Hide file tree
Showing 37 changed files with 3,320 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DO NOT COMMIT THIS FILE - IT WILL CONTAIN YOUR SENSITIVE KEYS.
# Instead, rename this file to ".env" instead of ".env.example". (.env will not be committed)

# Create an OpenAI API key at https://platform.openai.com/account/api-keys
OPENAI_API_KEY="sk-123..."
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# 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
.env

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
openai-v4.29.0-beta.1.tgz
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 OpenAI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# OpenAI Assistants API Quickstart
A quick-start template using the OpenAI [Assistants API](https://platform.openai.com/docs/assistants/overview) with [Next.js](https://nextjs.org/docs).
<br/>
<br/>
![openai_assistants_quickstart](https://github.com/openai/openai-assistants-quickstart-internal/assets/25421602/3ea9c3d9-2bc6-410a-a7bf-dcae03cae899)



## Quickstart Setup

### 1. Clone repo
```shell
git clone https://github.com/openai/openai-assistants-quickstart.git
cd openai-assistants-quickstart
```

### 2. Set your [OpenAI API key](https://platform.openai.com/api-keys)
```shell
export OPENAI_API_KEY="sk_..."
```
(or in `.env.example` and rename it to `.env`).

### 3. Install dependencies
```shell
npm install
```

### 4. Run
```shell
npm run dev
```

### 5. Navigate to [http:https://localhost:3000](http:https://localhost:3000).

## Overview

This project is intended to serve as a template for using the Assistants API in Next.js with [streaming](https://platform.openai.com/docs/assistants/overview/step-4-create-a-run), tool use ([code interpreter](https://platform.openai.com/docs/assistants/tools/code-interpreter) and [file search](https://platform.openai.com/docs/assistants/tools/file-search)), and [function calling](https://platform.openai.com/docs/assistants/tools/function-calling). While there are multiple pages to demonstrate each of these capabilities, they all use the same underlying assistant with all capabilities enabled.

The main logic for chat will be found in the `Chat` component in `app/components/chat.tsx`, and the handlers starting with `api/assistants/threads` (found in `api/assistants/threads/...`). Feel free to start your own project and copy some of this logic in! The `Chat` component itself can be copied and used directly, provided you copy the styling from `app/components/chat.module.css` as well.

### Pages

- Basic Chat Example: [http:https://localhost:3000/examples/basic-chat](http:https://localhost:3000/examples/basic-chat)
- Function Calling Example: [http:https://localhost:3000/examples/function-calling](http:https://localhost:3000/examples/function-calling)
- File Search Example: [http:https://localhost:3000/examples/file-search](http:https://localhost:3000/examples/file-search)
- Full-featured Example: [http:https://localhost:3000/examples/all](http:https://localhost:3000/examples/all)

### Main Components

- `app/components/chat.tsx` - handles chat chat rendering, [streaming](https://platform.openai.com/docs/assistants/overview?context=with-streaming), and [function call](https://platform.openai.com/docs/assistants/tools/function-calling/quickstart?context=streaming&lang=node.js) forwarding
- `app/components/file-viewer.tsx` - handles uploading, fetching, and deleting files for [file search](https://platform.openai.com/docs/assistants/tools/file-search)

### Endpoints

- `api/assistants` - `POST`: create assistant (only used at startup)
- `api/assistants/threads` - `POST`: create new thread
- `api/assistants/threads/[threadId]/messages` - `POST`: send message to assistant
- `api/assistants/threads/[threadId]/actions` - `POST`: inform assistant of the result of a function it decided to call
- `api/assistants/files` - `GET`/`POST`/`DELETE`: fetch, upload, and delete assistant files for file search

## Feedback

Let us know if you have any thoughts, questions, or feedback in [this form](https://docs.google.com/forms/d/e/1FAIpQLSdquOq8U8cvqL4EVCXuJ3oPag2w7KySsC0PLxv6VAHon6smrw/viewform?usp=sf_link)!

79 changes: 79 additions & 0 deletions app/api/assistants/files/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import OpenAI from "openai";
import { assistantId } from "../../../assistant-config";

const openai = new OpenAI();

// upload file to assistant's vector store
export async function POST(request) {
const formData = await request.formData(); // process file as FormData
const file = formData.get("file"); // retrieve the single file from FormData
const vectorStoreId = await getOrCreateVectorStore(); // get or create vector store

// upload using the file stream
const openaiFile = await openai.files.create({
file: file,
purpose: "assistants",
});

// add file to vector store
await openai.beta.vectorStores.files.create(vectorStoreId, {
file_id: openaiFile.id,
});
return new Response();
}

// list files in assistant's vector store
export async function GET() {
const vectorStoreId = await getOrCreateVectorStore(); // get or create vector store
const fileList = await openai.beta.vectorStores.files.list(vectorStoreId);

const filesArray = await Promise.all(
fileList.data.map(async (file) => {
const fileDetails = await openai.files.retrieve(file.id);
const vectorFileDetails = await openai.beta.vectorStores.files.retrieve(
vectorStoreId,
file.id
);
return {
file_id: file.id,
filename: fileDetails.filename,
status: vectorFileDetails.status,
};
})
);
return new Response(JSON.stringify(filesArray));
}

// delete file from assistant's vector store
export async function DELETE(request) {
const body = await request.json();
const fileId = body.fileId;

const vectorStoreId = await getOrCreateVectorStore(); // get or create vector store
await openai.beta.vectorStores.files.del(vectorStoreId, fileId); // delete file from vector store

return new Response();
}

/* Helper functions */

const getOrCreateVectorStore = async () => {
const assistant = await openai.beta.assistants.retrieve(assistantId);

// if the assistant already has a vector store, return it
if (assistant.tool_resources?.file_search?.vector_store_ids?.length > 0) {
return assistant.tool_resources.file_search.vector_store_ids[0];
}
// otherwise, create a new vector store and attatch it to the assistant
const vectorStore = await openai.beta.vectorStores.create({
name: "sample-assistant-vector-store",
});
await openai.beta.assistants.update(assistantId, {
tool_resources: {
file_search: {
vector_store_ids: [vectorStore.id],
},
},
});
return vectorStore.id;
};
39 changes: 39 additions & 0 deletions app/api/assistants/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import OpenAI from "openai";
const openai = new OpenAI();

export const runtime = "nodejs";

// Create a new assistant
export async function POST() {
const assistant = await openai.beta.assistants.create({
instructions: "You are a helpful assistant.",
name: "Quickstart Assistant",
model: "gpt-4-turbo-preview",
tools: [
{ type: "code_interpreter" },
{
type: "function",
function: {
name: "get_weather",
description: "Determine weather in my location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state e.g. San Francisco, CA",
},
unit: {
type: "string",
enum: ["c", "f"],
},
},
required: ["location"],
},
},
},
{ type: "file_search" },
],
});
return new Response(JSON.stringify({ assistantId: assistant.id }));
}
21 changes: 21 additions & 0 deletions app/api/assistants/threads/[threadId]/actions/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import OpenAI from "openai";
const openai = new OpenAI();

export const runtime = "nodejs";

// Send a new message to a thread
export async function POST(request, { params }) {
const body = await request.json();
const toolCallOutputs = body.toolCallOutputs;
const runId = body.runId;
const threadId = params.threadId;

const stream = openai.beta.threads.runs.submitToolOutputsStream(
threadId,
runId,
// { tool_outputs: [{ output: result, tool_call_id: toolCallId }] },
{ tool_outputs: toolCallOutputs }
);

return new Response(stream.toReadableStream());
}
23 changes: 23 additions & 0 deletions app/api/assistants/threads/[threadId]/messages/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import OpenAI from "openai";
import { assistantId } from "../../../../../assistant-config";
const openai = new OpenAI();

export const runtime = "nodejs";

// Send a new message to a thread
export async function POST(request, { params }) {
const body = await request.json();
const content = body.content;
const threadId = params.threadId;

await openai.beta.threads.messages.create(threadId, {
role: "user",
content: content,
});

const stream = openai.beta.threads.runs.createAndStream(threadId, {
assistant_id: assistantId,
});

return new Response(stream.toReadableStream());
}
10 changes: 10 additions & 0 deletions app/api/assistants/threads/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import OpenAI from "openai";
const openai = new OpenAI();

export const runtime = "nodejs";

// Create a new thread
export async function POST() {
const thread = await openai.beta.threads.create();
return new Response(JSON.stringify({ threadId: thread.id }));
}
1 change: 1 addition & 0 deletions app/assistant-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const assistantId = ""; // set your assistant ID here

0 comments on commit 15cc623

Please sign in to comment.