Skip to content

Commit

Permalink
feat(openai): Allow to set custom models using the API (not in UI)
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Oct 24, 2023
1 parent 21abec1 commit 858f48b
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions lib/routes-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ const cachedTemplates = {
testSend: fs.readFileSync(pathlib.join(__dirname, '..', 'views', 'partials', 'test_send.hbs'), 'utf-8')
};

const getOpenAiModels = (models, selectedModel) => {
let modelList = pfStructuredClone(models);

if (selectedModel && !models.find(model => model.key === selectedModel)) {
modelList.push({
name: selectedModel,
key: selectedModel
});
}

return modelList.map(model => {
model.selected = model.key === selectedModel;
return model;
});
};

const FIELD_TYPES = [
{
type: 'keyword',
Expand Down Expand Up @@ -1394,10 +1410,7 @@ return true;`

hasOpenAiAPIKey,
openAiError,
openAiModels: pfStructuredClone(OPEN_AI_MODELS).map(model => {
model.selected = model.key === openAiModel;
return model;
}),
openAiModels: getOpenAiModels(OPEN_AI_MODELS, openAiModel),

scriptEnvJson: JSON.stringify((await settings.get('scriptEnv')) || '{}'),
examplePayloadsJson: JSON.stringify(
Expand Down Expand Up @@ -1496,10 +1509,7 @@ return true;`

hasOpenAiAPIKey,
openAiError,
openAiModels: pfStructuredClone(OPEN_AI_MODELS).map(model => {
model.selected = model.key === request.payload.openAiModel;
return model;
}),
openAiModels: getOpenAiModels(OPEN_AI_MODELS, request.payload.openAiModel),

scriptEnvJson: JSON.stringify((await settings.get('scriptEnv')) || '{}'),
examplePayloadsJson: JSON.stringify(
Expand Down Expand Up @@ -1563,10 +1573,7 @@ return true;`

hasOpenAiAPIKey,
openAiError,
openAiModels: pfStructuredClone(OPEN_AI_MODELS).map(model => {
model.selected = model.key === request.payload.openAiModel;
return model;
}),
openAiModels: getOpenAiModels(OPEN_AI_MODELS, request.payload.openAiModel),

scriptEnvJson: JSON.stringify((await settings.get('scriptEnv')) || '{}'),
examplePayloadsJson: JSON.stringify(
Expand Down Expand Up @@ -7581,10 +7588,7 @@ ${Buffer.from(data.content, 'base64url').toString('base64')}
hasOpenAiAPIKey: !!(await settings.get('openAiAPIKey')),
indexInfo: await settings.get('embeddings:index'),

openAiModels: pfStructuredClone(OPEN_AI_MODELS_CHAT).map(model => {
model.selected = model.key === documentStoreChatModel;
return model;
}),
openAiModels: getOpenAiModels(OPEN_AI_MODELS_CHAT, documentStoreChatModel),

values: {
documentStoreGenerateEmbeddings: (await settings.get(`documentStoreGenerateEmbeddings`)) || false
Expand Down Expand Up @@ -7624,10 +7628,7 @@ ${Buffer.from(data.content, 'base64url').toString('base64')}
hasOpenAiAPIKey: !!(await settings.get('openAiAPIKey')),
indexInfo: await settings.get('embeddings:index'),

openAiModels: pfStructuredClone(OPEN_AI_MODELS_CHAT).map(model => {
model.selected = model.key === request.payload.documentStoreChatModel;
return model;
})
openAiModels: getOpenAiModels(OPEN_AI_MODELS_CHAT, request.payload.documentStoreChatModel)
},
{
layout: 'app'
Expand Down Expand Up @@ -7669,10 +7670,7 @@ ${Buffer.from(data.content, 'base64url').toString('base64')}
hasOpenAiAPIKey: !!(await settings.get('openAiAPIKey')),
indexInfo: await settings.get('embeddings:index'),

openAiModels: pfStructuredClone(OPEN_AI_MODELS_CHAT).map(model => {
model.selected = model.key === request.payload.documentStoreChatModel;
return model;
}),
openAiModels: getOpenAiModels(OPEN_AI_MODELS_CHAT, request.payload.documentStoreChatModel),

errors
},
Expand Down

0 comments on commit 858f48b

Please sign in to comment.