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

fix: prevent models being entered when none are available. #2

Merged
merged 1 commit into from
Nov 11, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/nivthefox/foundryvtt-aide)
### Fixed
- Resolved an issue where models could be set prematurely. (#1)

## [1.1.0](https://github.com/nivthefox/foundryvtt-aide/releases/tag/1.1.0) - 2024-11-09
### Added
Expand Down
25 changes: 18 additions & 7 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@

// Initialize Clients and Stores
const providerSettings = this.settings.getProviderSettings();
if (providerSettings.chat.provider === 'default' || providerSettings.embedding.provider === 'default'
|| providerSettings.chat.provider === '' || providerSettings.embedding.provider === '') {
this.logger.error('Chat and Embedding providers must be set in the settings');
return;
}

this.chatClient = Client.create(providerSettings.chat);
this.embeddingClient = Client.create(providerSettings.embedding);

// Register model choices
const chatModels = await this.chatClient.getChatModels();
this.settings.setChoices('ChatModel', chatModels);

const embeddingModels = await this.embeddingClient.getEmbeddingModels();
this.settings.setChoices('EmbeddingModel', embeddingModels);

if (!providerSettings.chat.model || !providerSettings.embedding.model) {
this.logger.error('Chat and Embedding models must be set in the settings');
return

Check warning on line 60 in src/app/app.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check warning on line 60 in src/app/app.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
}
this.conversationStore = new ConversationStore(ctx);

const lookups = game.settings.get('aide', 'VectorStoreLookups');
Expand All @@ -57,12 +75,5 @@

// todo: only rebuild if necessary
await this.documentManager.rebuildVectorStore();

// Register model choices
const chatModels = await this.chatClient.getChatModels();
this.settings.setChoices('ChatModel', chatModels);

const embeddingModels = await this.embeddingClient.getEmbeddingModels();
this.settings.setChoices('EmbeddingModel', embeddingModels);
}
}
4 changes: 2 additions & 2 deletions src/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ aide:
hint: The API key for the chat service you want to use.
ChatBaseURL:
name: Chat Base URL
hint: The base URL for the chat service you want to use.
hint: The base URL for the chat service you want to use, if different from the default.
ChatModel:
name: Chat Model
hint: The model to use for your chat generation requests.
Expand All @@ -25,7 +25,7 @@ aide:
hint: The API key for the embedding service you want to use.
EmbeddingBaseURL:
name: Embedding Base URL
hint: The base URL for the embedding service you want to use.
hint: The base URL for the embedding service you want to use, if different from the default.
EmbeddingModel:
name: Embedding Model
hint: The model to use for your embedding requests.
Expand Down
31 changes: 22 additions & 9 deletions src/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,43 @@ export class Settings {
}

getProviderSettings() {
return {
const settings = {
chat: {
provider: this.#context.game.settings.get(this.#module, 'ChatProvider'),
apiKey: this.#context.game.settings.get(this.#module, 'ChatAPIKey'),
baseURL: this.#context.game.settings.get(this.#module, 'ChatBaseURL'),
model: this.#context.game.settings.get(this.#module, 'ChatModel'),
},
embedding: {
provider: this.#context.game.settings.get(this.#module, 'EmbeddingProvider'),
apiKey: this.#context.game.settings.get(this.#module, 'EmbeddingAPIKey'),
baseURL: this.#context.game.settings.get(this.#module, 'EmbeddingBaseURL'),
model: this.#context.game.settings.get(this.#module, 'EmbeddingModel'),
}
};

try {
settings.chat.model = this.#context.game.settings.get(this.#module, 'ChatModel');
settings.embedding.model = this.#context.game.settings.get(this.#module, 'EmbeddingModel');
} catch (e) {
// ignore
}

return settings;
}

registerSettings() {
Object.entries(SETTINGS_REGISTRY).forEach(([key, config]) => {
this.#registerSetting(key, config);
});
Object.entries(SETTINGS_REGISTRY)
.forEach(([key, config]) => {
if ((key === 'ChatModel'
&& (this.ChatProvider === 'default' || this.ChatProvider === ''))
|| (key === 'EmbeddingModel'
&& (this.EmbeddingProvider === 'default' || this.EmbeddingProvider === ''))) {
return;
}

Object.entries(SETTINGS_REGISTRY).forEach(([key, config]) => {
this[key] = this.#context.game.settings.get(this.#module, key);
});
this.#registerSetting(key, config);
this[key] = this.#context.game.settings.get(this.#module, key);

});
}

setChoices(key, values) {
Expand Down
52 changes: 27 additions & 25 deletions src/settings/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"ChatProvider": {
"type": "String",
"default": "",
"scope": "client",
"choices": {
"default": "",
"openai": "OpenAI",
"deepinfra": "DeepInfra"
}
},
"ChatAPIKey": {
"type": "String",
"default": "",
Expand All @@ -14,15 +24,31 @@
"default": "",
"scope": "client"
},
"ChatProvider": {
"EmbeddingProvider": {
"type": "String",
"default": "",
"scope": "client",
"choices": {
"default": "",
"openai": "OpenAI",
"deepinfra": "DeepInfra"
}
},
"EmbeddingAPIKey": {
"type": "String",
"default": "",
"scope": "client"
},
"EmbeddingBaseURL": {
"type": "String",
"default": "",
"scope": "client"
},
"EmbeddingModel": {
"type": "StringField",
"default": "",
"scope": "client"
},
"ChunkSize": {
"type": "Number",
"default": 512,
Expand All @@ -43,30 +69,6 @@
"step": 16
}
},
"EmbeddingAPIKey": {
"type": "String",
"default": "",
"scope": "client"
},
"EmbeddingBaseURL": {
"type": "String",
"default": "",
"scope": "client"
},
"EmbeddingModel": {
"type": "StringField",
"default": "",
"scope": "client"
},
"EmbeddingProvider": {
"type": "String",
"default": "",
"scope": "client",
"choices": {
"openai": "OpenAI",
"deepinfra": "DeepInfra"
}
},
"VectorStoreLookups": {
"type": "Number",
"default": 3,
Expand Down
Loading