Skip to content

Commit

Permalink
fix importing non existing assistants - assitants are now created on …
Browse files Browse the repository at this point in the history
…open ai if they dont exist by id
  • Loading branch information
nascarjake committed Dec 15, 2024
1 parent fedd9aa commit 3d5efc8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/app/services/open-ai-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class OpenAiApiService {
.toPromise();
}

public createAssistant(assistant: Partial<OAAssistant>): Promise<OAAssistant> {
public async createAssistant(assistant: Partial<OAAssistant>): Promise<OAAssistant> {
console.log('Creating assistant with payload:', assistant);

// Ensure all required fields are present
Expand All @@ -115,7 +115,7 @@ export class OpenAiApiService {
payload['top_p'] = assistant.top_p;
}

return this.http.post<OAAssistant>(`${this.apiUrl}/assistants`, payload, { headers: this.getHeaders() })
const createdAssistant = await this.http.post<OAAssistant>(`${this.apiUrl}/assistants`, payload, { headers: this.getHeaders() })
.pipe(
catchError((error) => {
console.error('Error creating assistant:', error);
Expand All @@ -124,6 +124,9 @@ export class OpenAiApiService {
})
)
.toPromise();

console.log('Created assistant:', createdAssistant);
return createdAssistant;
}

public getThread(id: string): Promise<OAThread> {
Expand Down
32 changes: 28 additions & 4 deletions src/app/services/profile-export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,55 @@ export class ProfileExportService {
model,
instructions,
tools,
file_ids,
metadata,
response_format,
temperature,
top_p
} = assistant.openai;

// Create the assistant with all OpenAI fields
await this.openAiService.createAssistant({
const createdAssistant = await this.openAiService.createAssistant({
name,
description,
model,
instructions,
tools,
file_ids: file_ids || [],
metadata,
response_format,
temperature,
top_p
});

// Get the old and new file paths
const oldAssistantId = assistant.openai.id;
const newAssistantId = createdAssistant.id;
const oldFilename = `assistant-${profile.id}-${oldAssistantId}.json`;
const newFilename = `assistant-${profile.id}-${newAssistantId}.json`;

const oldPath = await window.electron.path.join(this.baseDir, oldFilename);
const newPath = await window.electron.path.join(this.baseDir, newFilename);

// Update the assistant's OpenAI ID
assistant.openai.id = newAssistantId;

// Save the updated assistant configuration
await window.electron.fs.writeTextFile(newPath, JSON.stringify(assistant, null, 2));

// Delete the old file
if (oldPath !== newPath) {
try {
await window.electron.fs.removeTextFile(oldPath);
} catch (error) {
console.error('Error removing old assistant file:', error);
}
}

console.log(`Assistant created with ID ${newAssistantId}`);
}
}
} catch (error) {
console.error('Error creating assistants in OpenAI:', error);
throw new Error(`Failed to create assistants in OpenAI: ${error instanceof Error ? error.message : String(error)}`);
throw error;
}
}
}

0 comments on commit 3d5efc8

Please sign in to comment.