- OpenAI Assistants API Wrapper
This API wrapper provides a comprehensive interface to interact with OpenAI's Assistants API. It offers endpoints for managing assistants, threads, messages, runs, vector stores, and file operations, making it easier to integrate OpenAI's powerful AI capabilities into your applications.
-
Clone the repository:
git clone https://github.com/RaheesAhmed/openai-assistants-api-v2.git cd openai-assistants-api-v2
-
Install dependencies:
npm install
-
Set up environment variables in a
.env
file and add OPENAI_API_KEY other are not necessary:OPENAI_API_KEY=your_openai_api_key PORT=3000 DEFAULT_ASSISTANT_ID=your_default_assistant_id DEFAULT_THREAD_ID=your_default_thread_id DEFAULT_MESSAGE_ID=your_default_message_id DEFAULT_RUN_ID=your_default_run_id DEFAULT_FILE_ID=your_default_file_id DEFAULT_VECTOR_STORE_ID=your_default_vector_store_id
-
Start the server:
npm start
- Create Assistant:
POST /create-assistant
- List Assistants:
GET /list-assistants
- Retrieve Assistant:
GET /retrieve-assistant/:assistant_id
- Update Assistant:
PATCH /update-assistant/:assistant_id
- Delete Assistant:
DELETE /delete-assistant/:assistant_id
- Create Thread:
POST /create-thread
- Retrieve Thread:
GET /retrieve-thread/:thread_id
- Update Thread:
PATCH /update-thread/:thread_id
- Delete Thread:
DELETE /delete-thread/:thread_id
- Create Message:
POST /create-message/:thread_id
- List Messages:
GET /list-messages/:thread_id
- Retrieve Message:
GET /retrieve-message/:thread_id/:message_id
- Update Message:
PATCH /update-message/:thread_id/:message_id
- Create Run:
POST /create-run/:thread_id
- Retrieve Run:
GET /retrieve-run/:thread_id/:run_id
- Cancel Run:
POST /cancel-run/:thread_id/:run_id
- Submit Tool Outputs:
POST /submit-tool-outputs/:thread_id/:run_id
- Create Vector Store:
POST /create-vector-store
- List Vector Stores:
GET /list-vector-stores
- Retrieve Vector Store:
GET /retrieve-vector-store/:vector_store_id
- Update Vector Store:
PATCH /update-vector-store/:vector_store_id
- Delete Vector Store:
DELETE /delete-vector-store/:vector_store_id
- Upload File:
POST /upload-file
- List Files:
GET /list-files
- Retrieve File:
GET /retrieve-file/:file_id
- Delete File:
DELETE /delete-file/:file_id
- Retrieve File Content:
GET /retrieve-file-content/:file_id
- List Run Steps:
GET /list-run-steps/:thread_id/:run_id
- Retrieve Run Step:
GET /retrieve-run-step/:thread_id/:run_id/:step_id
- Stream Run:
POST /stream-run/:thread_id/:run_id
- Execute Tools:
POST /execute-tools/:thread_id/:run_id
- Update Rate Limit:
POST /update-rate-limit
- Update File Size Limit:
POST /update-file-size
- Get API Defaults:
GET /api-defaults
- Set API Key:
POST /set-api-key
The API uses a global error handling middleware to catch and process errors. Errors are returned in the following format:
{
"error": {
"message": "Error message"
}
}
- Uses
helmet
for setting various HTTP headers - Implements rate limiting to prevent abuse
- Sanitizes file uploads and limits file size
- Uses CORS for cross-origin resource sharing
For production deployment, implement additional security measures such as authentication and input validation.
Here are detailed examples of how to use the main endpoints:
const createAssistant = async () => {
const response = await fetch('http://localhost:3000/create-assistant', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: "Math Tutor",
instructions: "You are a helpful math tutor.",
model: "gpt-4-1106-preview"
})
});
const data = await response.json();
console.log('Assistant created:', data);
};
const listAssistants = async () => {
const response = await fetch('http://localhost:3000/list-assistants');
const data = await response.json();
console.log('Assistants:', data);
};
const createThread = async () => {
const response = await fetch('http://localhost:3000/create-thread', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [{ role: "user", content: "Hello, I need help with math." }]
})
});
const data = await response.json();
console.log('Thread created:', data);
};
const createMessage = async (threadId) => {
const response = await fetch(`http://localhost:3000/create-message/${threadId}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
role: "user",
content: "What is the formula for the area of a circle?"
})
});
const data = await response.json();
console.log('Message created:', data);
};
const createRun = async (threadId, assistantId) => {
const response = await fetch(`http://localhost:3000/create-run/${threadId}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ assistant_id: assistantId })
});
const data = await response.json();
console.log('Run created:', data);
};
const streamRun = async (threadId, runId) => {
const response = await fetch(`http://localhost:3000/stream-run/${threadId}/${runId}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ stream: true })
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { value, done } = await reader.read();
if (done) break;
console.log('Streamed data:', decoder.decode(value));
}
};
const uploadFile = async (file) => {
const formData = new FormData();
formData.append('file', file);
formData.append('purpose', 'assistants');
const response = await fetch('http://localhost:3000/upload-file', {
method: 'POST',
body: formData
});
const data = await response.json();
console.log('File uploaded:', data);
};
const updateRateLimit = async () => {
const response = await fetch('http://localhost:3000/update-rate-limit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ windowMs: 900000, max: 100 })
});
const data = await response.json();
console.log('Rate limit updated:', data);
};
const updateFileSizeLimit = async (fileSize) => {
const response = await fetch('http://localhost:3000/update-file-size', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ fileSize })
});
const data = await response.json();
console.log('File size limit updated:', data);
};
const getApiDefaults = async () => {
const response = await fetch('http://localhost:3000/api-defaults');
const data = await response.json();
console.log('API defaults:', data);
};
const setApiKey = async (apiKey) => {
const response = await fetch('http://localhost:3000/set-api-key', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ apiKey })
});
const data = await response.json();
console.log('API key set:', data);
};
Remember to replace placeholder values (like asst_abc123
, thread_abc123
, etc.) with actual IDs when using these requests.