Hey - beginner here. I am trying to trigger the creation of an assistant when a specific page is rendered. The logic should be that when the page.tsx from first-page or second-page is compiled, then a specific assistant from assistant-config.ts is created and the corresponding assistantId is updated to .env.
I can’t seem to make it call the assistant function. Any ideas/advice?
app\assistant-config.ts
import { openai } from "@/app/openai";
import { promises as fs } from 'fs';
import path from 'path';
// Use a dynamic import to handle ES modules if needed
const __dirname = path.dirname(new URL(import.meta.url).pathname);
export async function firstAssistant() {
try {
// Create the assistant
const response = await openai.beta.assistants.create({
name: "Haiku Poet",
instructions: "You are a haiku poet.",
tools: [{ type: "code_interpreter" }],
model: "gpt-4o"
});
const assistantId = response.id; // Assuming response contains the assistant ID
console.log("Assistant created with ID:", assistantId);
// Define the relative path
const envPath = path.resolve('.env');
// Prepare the new line to update
const newEnvLine = `assistantId="${assistantId}"`;
// Read the existing .env file contents
let envContent = await fs.readFile(envPath, 'utf8');
// Update the ASSISTANT_ID line
const updatedEnvContent = envContent.split('\n').map(line => {
if (line.startsWith('assistantId=')) {
return newEnvLine;
}
return line;
}).join('\n');
// Write the updated content back to the .env file
await fs.writeFile(envPath, updatedEnvContent, 'utf8');
console.log('Assistant ID updated in .env file.');
} catch (error) {
console.error('Error creating assistant or updating .env file:', error);
}
}
export async function secondAssistant() {
try {
// Create the assistant
const response = await openai.beta.assistants.create({
name: "Haiku Poet",
instructions: "You are a haiku poet.",
tools: [{ type: "code_interpreter" }],
model: "gpt-4o"
});
const assistantId = response.id; // Assuming response contains the assistant ID
console.log("Assistant created with ID:", assistantId);
// Define the relative path
const envPath = path.resolve('.env');
// Prepare the new line to update
const newEnvLine = `assistantId="${assistantId}"`;
// Read the existing .env file contents
let envContent = await fs.readFile(envPath, 'utf8');
// Update the ASSISTANT_ID line
const updatedEnvContent = envContent.split('\n').map(line => {
if (line.startsWith('assistantId=')) {
return newEnvLine;
}
return line;
}).join('\n');
// Write the updated content back to the .env file
await fs.writeFile(envPath, updatedEnvContent, 'utf8');
console.log('Assistant ID updated in .env file.');
} catch (error) {
console.error('Error creating assistant or updating .env file:', error);
}
}
app\examples\first-chat\page.tsx
"use client";
import React from "react";
import styles from "./page.module.css"; // use simple styles for demonstration purposes
import Chat from "../../components/chat";
import { firstAssistant } from "../../assistant-config";
// Call the function to create the assistant and handle any errors
firstAssistant().catch(console.error);
const Home = () => {
return (
<main className={styles.main}>
<div className={styles.container}>
<Chat />
</div>
</main>
);
};
export default Home;
app\examples\second-chat\page.tsx
"use client";
import React from "react";
import styles from "./page.module.css"; // use simple styles for demonstration purposes
import Chat from "../../components/chat";
import { secondAssistant } from "../../assistant-config";
// Call the function to create the assistant and handle any errors
secondAssistant().catch(console.error);
const Home = () => {
return (
<main className={styles.main}>
<div className={styles.container}>
<Chat />
</div>
</main>
);
};
export default Home;