Skip to content

Commit

Permalink
feat(settings): add temp, topp, topk, etc. values
Browse files Browse the repository at this point in the history
  • Loading branch information
nivthefox committed Nov 13, 2024
1 parent ae69134 commit f1dd418
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/nivthefox/foundryvtt-aide)
### Added
- Document embeddings will be updated when another user updates a document.
- New settings are now available to control the LLM's creativity and consistency.

### Fixed
- Resolved an issue where models could be set prematurely. (#1)
Expand Down
20 changes: 14 additions & 6 deletions src/ai/provider/deepinfra.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ export class DeepInfra {
* @param {AIProviderSettings} config
*/
constructor(config) {
this.#apiKey = config.apiKey;
delete config.provider;

if (config.apiKey) {
this.#apiKey = config.apiKey;
delete config.apiKey;
}

if (config.baseURL) {
this.#baseUrl = config.baseURL;
delete config.baseURL;
}

this.config = config;
}

/**
Expand Down Expand Up @@ -108,11 +120,7 @@ export class DeepInfra {
body: JSON.stringify({
model: model,
messages: query,
temperature: 0.7,
top_p: 0.9,
top_k: 0,
presence_penalty: 0.0,
frequency_penalty: 0.0,
...this.config,
stream
})
});
Expand Down
16 changes: 10 additions & 6 deletions src/ai/provider/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ export class OpenAI {
* @param {AIProviderSettings} config
*/
constructor(config) {
this.#apiKey = config.apiKey;
delete config.provider;

if (config.apiKey) {
this.#apiKey = config.apiKey;
delete config.apiKey;
}

if (config.baseURL) {
this.#baseUrl = config.baseURL;
delete config.baseURL;
}

this.config = config;
}

/**
Expand Down Expand Up @@ -88,11 +96,7 @@ export class OpenAI {
body: JSON.stringify({
model,
messages: query,
temperature: 0.7,
top_p: 0.9,
top_k: 0,
presence_penalty: 0.0,
frequency_penalty: 0.0,
...this.config,
stream
})
});
Expand Down
8 changes: 3 additions & 5 deletions src/conversation/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ export class Store {
await this.#createDirectoryIfMissing();
await this.#fetchConversations();

this.#emitter.on('conversation.create', (id) =>
this.#conversations.set(id, {id, loaded: false}));
this.#emitter.on('conversation.delete', (id) =>
this.#conversations.delete(id));
this.#emitter.on('conversation.update', (id) => {
this.#emitter.on('conversation.create', id => this.#conversations.set(id, {id, loaded: false}));
this.#emitter.on('conversation.delete', id => this.#conversations.delete(id));
this.#emitter.on('conversation.update', id => {
const {userId} = this.#conversations.get(id);
this.#conversations.set(id, {id, loaded: false});
this.get(userId, id);
Expand Down
2 changes: 1 addition & 1 deletion src/event/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export class Emitter {
this.#logger.debug('Received event %s', event.name);
listener(...event.args);
}
})
});
}
}
6 changes: 6 additions & 0 deletions src/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export class Settings {
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'),
temperature: this.#context.game.settings.get(this.#module, 'ChatTemperature'),
maxTokens: this.#context.game.settings.get(this.#module, 'ChatMaxTokens'),
topP: this.#context.game.settings.get(this.#module, 'ChatTopP'),
topK: this.#context.game.settings.get(this.#module, 'ChatTopK'),
frequencyPenalty: this.#context.game.settings.get(this.#module, 'ChatFrequencyPenalty'),
presencePenalty: this.#context.game.settings.get(this.#module, 'ChatPresencePenalty'),
},
embedding: {
provider: this.#context.game.settings.get(this.#module, 'EmbeddingProvider'),
Expand Down
59 changes: 59 additions & 0 deletions src/settings/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,65 @@
"default": "",
"scope": "client"
},
"ChatTemperature": {
"type": "Number",
"default": 0.7,
"scope": "client",
"range": {
"min": 0,
"max": 2,
"step": 0.05
}
},
"ChatTopP": {
"type": "Number",
"default": 0.9,
"scope": "client",
"range": {
"min": 0,
"max": 1,
"step": 0.05
}
},
"ChatTopK": {
"type": "Number",
"default": 0,
"scope": "client",
"range": {
"min": 0,
"max": 32,
"step": 1
}
},
"ChatPresencePenalty": {
"type": "Number",
"default": 0,
"scope": "client",
"range": {
"min": 0,
"max": 2,
"step": 0.05
}
},
"ChatFrequencyPenalty": {
"type": "Number",
"default": 0,
"scope": "client",
"range": {
"min": 0,
"max": 2,
"step": 0.05
}
},
"ChatMaxTokens": {
"type": "Number",
"default": 150,
"scope": "client",
"range": {
"min": 1,
"step": 1
}
},
"EmbeddingProvider": {
"type": "String",
"default": "",
Expand Down
18 changes: 18 additions & 0 deletions src/ui/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ export class ChatSettings extends HandlebarsApplicationMixin(ApplicationV2) {

const chatModel = html.querySelector('.chat-model').value;
const embeddingModel = html.querySelector('.embedding-model').value;
const temperature = html.querySelector('.temperature').value;
const maxTokens = html.querySelector('.max-tokens').value;
const topP = html.querySelector('.top-p').value;
const topK = html.querySelector('.top-k').value;
const frequencyPenalty = html.querySelector('.frequency-penalty').value;
const presencePenalty = html.querySelector('.presence-penalty').value;

game.settings.set('aide', 'ChatModel', chatModel);
game.settings.set('aide', 'EmbeddingModel', embeddingModel);
game.settings.set('aide', 'ChatTemperature', temperature);
game.settings.set('aide', 'ChatMaxTokens', maxTokens);
game.settings.set('aide', 'ChatTopP', topP);
game.settings.set('aide', 'ChatTopK', topK);
game.settings.set('aide', 'ChatFrequencyPenalty', frequencyPenalty);
game.settings.set('aide', 'ChatPresencePenalty', presencePenalty);

this.close();
}
Expand All @@ -46,6 +58,12 @@ export class ChatSettings extends HandlebarsApplicationMixin(ApplicationV2) {
acc[model] = model;
return acc;
}, {}),
temperature: game.settings.get('aide', 'ChatTemperature'),
maxTokens: game.settings.get('aide', 'ChatMaxTokens'),
topP: game.settings.get('aide', 'ChatTopP'),
topK: game.settings.get('aide', 'ChatTopK'),
frequencyPenalty: game.settings.get('aide', 'ChatFrequencyPenalty'),
presencePenalty: game.settings.get('aide', 'ChatPresencePenalty'),
embeddingModel: game.settings.get('aide', 'EmbeddingModel'),
embeddingModels: (await aide.embeddingClient.getEmbeddingModels())
.reduce((acc, model) => {
Expand Down
25 changes: 25 additions & 0 deletions templates/applications/Settings.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
<!-- todo: i18n -->
<section class="settings">
<label>
<span>Chat Model</span>
<select class="chat-model">
{{selectOptions chatModels selected=chatModel}}
</select>
</label>
<label>
<span>Temperature</span>
<input type="number" class="temperature" step="0.05" value={{temperature}}>
</label>
<label>
<span>Max Tokens</span>
<input type="number" class="max-tokens" step="1" value={{maxTokens}}>
</label>
<label>
<span>Top P</span>
<input type="number" class="top-p" step="0.05" value={{topP}}>
</label>
<label>
<span>Top K</span>
<input type="number" class="top-k" step="1" value={{topK}}>
</label>
<label>
<span>Frequency Penalty</span>
<input type="number" class="frequency-penalty" step="0.05" value={{frequencyPenalty}}>
</label>
<label>
<span>Presence Penalty</span>
<input type="number" class="presence-penalty" step="0.05" value={{presencePenalty}}>
</label>
<label>
<span>Embedding Model</span>
<select class="embedding-model">
Expand Down

0 comments on commit f1dd418

Please sign in to comment.