Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always select a model if it's the only one in the list #33

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions sbai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function selectImageModelFromConfig() {
}

/**
* Prompts the user for a custom prompt to send to the LLM. If the user has text selected, the selected text is used as the note content.
* Prompts the user for a custom prompt to send to the LLM. If the user has text selected, the selected text is used as the note content.
* If the user has no text selected, the entire note is used as the note content.
* The response is streamed to the cursor position.
*/
Expand All @@ -114,7 +114,7 @@ export async function callOpenAIwithNote() {
{
role: "system",
content:
"You are an AI note assistant. Follow all user instructions and use the note context and note content to help follow those instructions. Use Markdown for any formatting.",
"You are an AI note assistant. Follow all user instructions and use the note context and note content to help follow those instructions. Use Markdown for any formatting.",
},
{
role: "user",
Expand Down Expand Up @@ -156,7 +156,7 @@ export async function summarizeNote() {

/**
* Uses a built-in prompt to ask the LLM for a summary of either the entire note, or the selected
* text. Inserts the summary at the cursor's position.
* text. Inserts the summary at the cursor's position.
*/
export async function insertSummary() {
const { summary, selectedTextInfo } = await summarizeNote();
Expand All @@ -170,7 +170,7 @@ export async function insertSummary() {

/**
* Uses a built-in prompt to ask the LLM for a summary of either the entire note, or the selected
* text. Opens the resulting summary in a temporary right pane.
* text. Opens the resulting summary in a temporary right pane.
*/
export async function openSummaryPanel() {
const { summary } = await summarizeNote();
Expand Down Expand Up @@ -399,6 +399,11 @@ export async function streamChatOnPage() {
export async function promptAndGenerateImage() {
await initIfNeeded();

if (!aiSettings.imageModels || aiSettings.imageModels.length === 0) {
await editor.flashNotification("No image models available.", "error");
return;
}

try {
const prompt = await editor.prompt("Enter a prompt for DALL·E:");
if (!prompt || !prompt.trim()) {
Expand Down Expand Up @@ -433,7 +438,7 @@ export async function promptAndGenerateImage() {
await space.writeAttachment(prefix + finalFileName, decodedImage);

// And then insert it with the prompt dall-e rewrote for us
// TODO: This uses the original prompt as alt-text, but sometimes it's kind of long. I'd like to let the user provide a template for how this looks.
// TODO: This uses the original prompt as alt-text, but sometimes it's kind of long. I'd like to let the user provide a template for how this looks.
const markdownImg =
`![${finalFileName}](${finalFileName})\n*${revisedPrompt}*`;
await editor.insertAtCursor(markdownImg);
Expand All @@ -460,7 +465,7 @@ export async function queryOpenAI(
await initIfNeeded();
const messages: ChatMessage[] = [];
const defaultSystemPrompt =
"You are an AI note assistant helping to render content for a note. Please follow user instructions and keep your response short and concise.";
"You are an AI note assistant helping to render content for a note. Please follow user instructions and keep your response short and concise.";
messages.push({
role: "system",
content: systemPrompt || defaultSystemPrompt,
Expand Down
56 changes: 16 additions & 40 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,11 @@ export type AISettings = {
promptInstructions: PromptInstructions;

// These are deprecated and will be removed in a future release
summarizePrompt: string;
tagPrompt: string;
imagePrompt: string;
temperature: number;
maxTokens: number;
defaultTextModel: string;
openAIBaseUrl: string;
dallEBaseUrl: string;
requireAuth: boolean;
secretName: string;
provider: Provider;
backwardsCompat: boolean;
// Above is left for backwards compatibility
};

Expand Down Expand Up @@ -184,9 +177,7 @@ export async function configureSelectedModel(model: ModelConfig) {
if (!model) {
throw new Error("No model provided to configure");
}
if (model.requireAuth === undefined) {
model.requireAuth = aiSettings.requireAuth;
}
model.requireAuth = model.requireAuth ?? aiSettings.requireAuth;
if (model.requireAuth) {
const newApiKey = await readSecret(model.secretName || "OPENAI_API_KEY");
if (newApiKey !== apiKey) {
Expand Down Expand Up @@ -228,14 +219,14 @@ export async function configureSelectedImageModel(model: ImageModelConfig) {

async function loadAndMergeSettings() {
const defaultSettings = {
defaultTextModel: "gpt-3.5-turbo",
openAIBaseUrl: "https://api.openai.com/v1",
dallEBaseUrl: "https://api.openai.com/v1",
requireAuth: true,
secretName: "OPENAI_API_KEY",
provider: "OpenAI",
chat: {},
promptInstructions: {},
imageModels: [],
};
const defaultChatSettings: ChatSettings = {
userInformation: "",
Expand All @@ -250,11 +241,6 @@ async function loadAndMergeSettings() {
tagRules: "",
};
const newSettings = await readSetting("ai", {});
if (newSettings.defaultTextModel) {
newSettings.backwardsCompat = true;
} else {
newSettings.backwardsCompat = false;
}
const newCombinedSettings = { ...defaultSettings, ...newSettings };
newCombinedSettings.chat = {
...defaultChatSettings,
Expand All @@ -272,31 +258,11 @@ export async function initializeOpenAI(configure = true) {
const newCombinedSettings = await loadAndMergeSettings();

let errorMessage = "";
if (!newCombinedSettings.textModels && newCombinedSettings.defaultTextModel) {
// Backwards compatibility - if there isn't a textModels object, use the old behavior of config
newCombinedSettings.textModels = [
{
name: "default",
description: "Default model",
modelName: newCombinedSettings.defaultTextModel,
provider: newCombinedSettings.provider,
secretName: newCombinedSettings.secretName,
},
];
await setSelectedTextModel(newCombinedSettings.textModels[0]);
} else if (
newCombinedSettings.textModels.length > 0 &&
newCombinedSettings.backwardsCompat
) {
errorMessage =
"Both textModels and defaultTextModel found in ai settings. Please remove defaultTextModel.";
} else if (
!newCombinedSettings.textModels && !newCombinedSettings.defaultTextModel
) {
if (!newCombinedSettings.textModels) {
errorMessage = "No textModels found in ai settings";
}

if (errorMessage !== "") {
if (errorMessage) {
console.error(errorMessage);
// await editor.flashNotification(errorMessage, "error");
throw new Error(errorMessage);
Expand All @@ -310,9 +276,19 @@ export async function initializeOpenAI(configure = true) {
console.log("aiSettings unchanged", aiSettings);
}

if (aiSettings.textModels.length === 1) {
// If there's only one text model, set it as the selected model
await setSelectedTextModel(aiSettings.textModels[0]);
}

if (aiSettings.imageModels.length === 1) {
// If there's only one image model, set it as the selected model
await setSelectedImageModel(aiSettings.imageModels[0]);
}

if (configure) {
await getAndConfigureModel();
if (aiSettings.imageModels && aiSettings.imageModels.length > 0) {
if (aiSettings.imageModels.length > 0) {
await getAndConfigureImageModel();
}
}
Expand All @@ -324,7 +300,7 @@ export async function initializeOpenAI(configure = true) {
};
if (aiSettings.chat.userInformation) {
chatSystemPrompt.content +=
`\nThe user has provided the following information about their self: ${aiSettings.chat.userInformation}`;
`\nThe user has provided the following information about themselves: ${aiSettings.chat.userInformation}`;
}
if (aiSettings.chat.userInstructions) {
chatSystemPrompt.content +=
Expand Down
Loading