Skip to content

Commit

Permalink
feat: script for automatically copying values
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Jan 3, 2025
1 parent 8d55107 commit c294240
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 9 deletions.
11 changes: 9 additions & 2 deletions ai-assistant-settings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ This is a repository of AI assistant settings values for which we want simple ve

## Directory Structure

Each assistant in the DAVAI project has its own subdirectory containing text files for each specific setting we track. For example, instructions.txt is for the assistant's Instructions setting value.
Each assistant in the DAVAI project has its own subdirectory containing text files for the specific settings we track. The assistant's Instructions setting value is saved to instructions.txt. Each of the asistant's defined Functions are saved to individual text files in the assistant's `functions` subdirectory.

## Workflow

Since non-developers can modify settings but are unlikely to update this repository, developers should follow these steps to ensure the repository stays reasonably up-to-date:

1. Before making changes, copy the value(s) from platform.openai.com and commit any changes not already recorded here.
1. Before making changes, copy the value(s) from platform.openai.com and commit any changes not already recorded here. (The sync-settings.mts script can be used to automatically copy the values from platform.openai.com via the OpenAI API.)
2. Make your changes on platform.openai.com, then update the corresponding files here and commit the changes.

## Scripts

sync-settings.mts in the `scripts` directory can be used to automatically update the assistant settings files with values from platform.openai.com via the OpenAI API.

### Usage:
`node --loader ts-node/esm sync-settings.mts`
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "create_request",
"description": "Create a request to send to the CODAP Data Interactive API",
"strict": false,
"parameters": {
"type": "object",
"properties": {
Expand All @@ -23,5 +22,6 @@
"action",
"resource"
]
}
},
"strict": false
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "create_request",
"description": "Create a request to send to the CODAP Data Interactive API",
"strict": false,
"parameters": {
"type": "object",
"properties": {
Expand All @@ -23,5 +22,6 @@
"action",
"resource"
]
}
},
"strict": false
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "create_request",
"description": "Create a request to send to the CODAP Data Interactive API",
"strict": false,
"parameters": {
"type": "object",
"properties": {
Expand All @@ -23,5 +22,6 @@
"action",
"resource"
]
}
}
},
"strict": false
}
76 changes: 76 additions & 0 deletions ai-assistant-settings/scripts/sync-settings.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env ts-node

/**
* Updates the local settings files with values from the AI Assistants' settings at platform.openai.com.
*
* Usage:
* node --loader ts-node/esm sync-settings.mts
*
* TODO: Make it so `--loader ts-node/esm` isn't required for this to run since Custom ESM Loaders is an
* experimental feature and could change.
*/

import { OpenAI } from "openai";
import fs from "fs";
import process from "node:process";
import path from "path";
import dotenv from "dotenv";

dotenv.config({ path: "../../.env" });

const requiredEnvVars = ["REACT_APP_OPENAI_API_KEY", "REACT_APP_OPENAI_BASE_URL"];
const missingEnvVars = requiredEnvVars.filter((key) => !process.env[key]);

if (missingEnvVars.length > 0) {
console.error("Missing required environment variables:");
missingEnvVars.forEach((key) => console.error(`- ${key}`));
process.exit(1);
}

const openai = new OpenAI({
apiKey: process.env.REACT_APP_OPENAI_API_KEY || "fake-key",
baseURL: process.env.REACT_APP_OPENAI_BASE_URL,
organization: "org-jbU1egKECzYlQI73HMMi7EOZ",
project: "proj_VsykADfoZHvqcOJUHyVAYoDG",
});

const getAssistants = async () => {
try {
const { data } = await openai.beta.assistants.list();
return data;
} catch (err) {
console.error("Error fetching assistants:", err);
return [];
}
};

const writeAssistantFiles = (assistant: any) => {
const assistantDir = path.resolve("../assistants", assistant.id);

fs.mkdirSync(assistantDir, { recursive: true });
fs.writeFileSync(path.join(assistantDir, "instructions.txt"), assistant.instructions);

const functionsDir = path.join(assistantDir, "functions");
fs.mkdirSync(functionsDir, { recursive: true });
assistant.tools
.filter((tool: any) => tool.type === "function")
.forEach((func: any) => {
const filePath = path.join(functionsDir, `${func.function.name}.txt`);
fs.writeFileSync(filePath, JSON.stringify(func.function, null, 2));
});
};

const syncSettings = async () => {
const assistants = await getAssistants();
const assistantsDir = path.resolve("../assistants");

fs.rmSync(assistantsDir, { recursive: true, force: true });
fs.mkdirSync(assistantsDir);

assistants.forEach(writeAssistantFiles);
};

syncSettings().catch((err) => {
console.error("Error during sync:", err);
process.exit(1);
});

0 comments on commit c294240

Please sign in to comment.