Skip to content

Commit 35aed99

Browse files
authored
Add support for LocalAI Processor (#11)
1 parent 923feaa commit 35aed99

File tree

22 files changed

+845
-27
lines changed

22 files changed

+845
-27
lines changed

base/apis.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ def patch(self, request):
123123
).decode('utf-8')
124124
else:
125125
profile.azure_openai_api_key = ''
126+
if 'localai_api_key' in request.data and flag_enabled('CAN_ADD_KEYS', request=request):
127+
should_update = True
128+
localai_api_key = request.data.get('localai_api_key')
129+
if localai_api_key and len(localai_api_key) > 0:
130+
profile.localai_api_key = profile.encrypt_value(
131+
localai_api_key,
132+
).decode('utf-8')
133+
else:
134+
profile.localai_api_key = ''
135+
136+
if 'localai_base_url' in request.data and flag_enabled('CAN_ADD_KEYS', request=request):
137+
should_update = True
138+
localai_base_url = request.data.get('localai_base_url')
139+
if localai_base_url and len(localai_base_url) > 0:
140+
profile.localai_base_url = localai_base_url
141+
else:
142+
profile.localai_base_url = ''
126143

127144
if 'logo' in request.data:
128145
should_update = True
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 4.2.1 on 2023-08-16 22:18
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('base', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='defaultprofile',
15+
name='localai_api_key',
16+
field=models.CharField(blank=True, default=None, help_text='LocalAI API key to use with LocalAI backend', max_length=256, null=True),
17+
),
18+
migrations.AddField(
19+
model_name='defaultprofile',
20+
name='localai_base_url',
21+
field=models.CharField(blank=True, default=None, help_text='LocalAI base URL to use with LocalAI processors', max_length=256, null=True),
22+
),
23+
]

base/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ class Meta:
7979
aws_default_region = models.CharField(
8080
max_length=64, default=None, help_text='AWS default region to use with AWS backend', null=True, blank=True,
8181
)
82+
localai_api_key = models.CharField(
83+
max_length=256, default=None, help_text='LocalAI API key to use with LocalAI backend', null=True, blank=True,
84+
)
85+
localai_base_url = models.CharField(
86+
max_length=256, default=None, help_text='LocalAI base URL to use with LocalAI processors', null=True, blank=True,
87+
)
8288
logo = models.TextField(
8389
default='', help_text='Logo to use for the user', null=True, blank=True,
8490
)
@@ -147,13 +153,17 @@ def _vendor_key_or_promptly_default(self, attrname, api_key_value):
147153
elif attrname == 'google_service_account_json_key':
148154
return self.decrypt_value(api_key_value) if api_key_value else settings.DEFAULT_GOOGLE_SERVICE_ACCOUNT_JSON_KEY
149155
elif attrname == 'aws_secret_access_key':
150-
return self.decrypt_value(api_key_value) if api_key_value else None
156+
return self.decrypt_value(api_key_value) if api_key_value else settings.DEFAULT_AWS_SECRET_ACCESS_KEY
151157
elif attrname == 'aws_default_region':
152-
return self.decrypt_value(api_key_value) if api_key_value else None
158+
return self.decrypt_value(api_key_value) if api_key_value else settings.DEFAULT_AWS_DEFAULT_REGION
153159
elif attrname == 'azure_openai_endpoint':
154-
return self.decrypt_value(api_key_value) if api_key_value else None
160+
return self.decrypt_value(api_key_value) if api_key_value else settings.DEFAULT_AZURE_OPENAI_ENDPOINT
155161
elif attrname in ['aws_access_key_id']:
156-
return api_key_value
162+
return api_key_value if api_key_value else settings.DEFAULT_AWS_ACCESS_KEY_ID
163+
elif attrname == 'localai_api_key':
164+
return self.decrypt_value(api_key_value) if api_key_value else settings.DEFAULT_LOCALAI_API_KEY
165+
elif attrname == 'localai_base_url':
166+
return api_key_value if api_key_value else settings.DEFAULT_LOCALAI_BASE_URL
157167
else:
158168
return None
159169

@@ -188,6 +198,8 @@ def get_vendor_env(self):
188198
'aws_secret_access_key': self.get_vendor_key('aws_secret_access_key'),
189199
'aws_default_region': self.get_vendor_key('aws_default_region'),
190200
'azure_openai_endpoint': self.get_vendor_key('azure_openai_endpoint'),
201+
'localai_api_key': self.get_vendor_key('localai_key'),
202+
'localai_base_url': self.get_vendor_key('localai_base_url'),
191203
'weaviate_url': self.weaviate_url,
192204
'weaviate_api_key': self.weaviate_api_key,
193205
'weaviate_embedding_endpoint': self.vectostore_embedding_endpoint,

base/serializers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class ProfileSerializer(serializers.ModelSerializer):
1313
elevenlabs_key = serializers.SerializerMethodField()
1414
google_service_account_json_key = serializers.SerializerMethodField()
1515
azure_openai_api_key = serializers.SerializerMethodField()
16+
localai_api_key = serializers.SerializerMethodField()
17+
localai_base_url = serializers.SerializerMethodField()
18+
1619
avatar = serializers.SerializerMethodField()
1720

1821
def get_openai_key(self, obj):
@@ -35,6 +38,12 @@ def get_google_service_account_json_key(self, obj):
3538

3639
def get_azure_openai_api_key(self, obj):
3740
return obj.decrypt_value(obj.azure_openai_api_key)
41+
42+
def get_localai_api_key(self, obj):
43+
return obj.decrypt_value(obj.localai_api_key)
44+
45+
def get_localai_base_url(self, obj):
46+
return obj.localai_base_url
3847

3948
def get_avatar(self, obj):
4049
return obj.user.socialaccount_set.first().get_avatar_url() if obj.user.socialaccount_set.first() else None
@@ -46,5 +55,5 @@ class Meta:
4655
model = Profile
4756
fields = [
4857
'name', 'user_email', 'token', 'openai_key',
49-
'stabilityai_key', 'cohere_key', 'forefrontai_key', 'elevenlabs_key', 'google_service_account_json_key', 'azure_openai_api_key', 'logo', 'organization', 'avatar',
58+
'stabilityai_key', 'cohere_key', 'forefrontai_key', 'elevenlabs_key', 'google_service_account_json_key', 'azure_openai_api_key', 'localai_api_key', 'localai_base_url', 'logo', 'organization', 'avatar',
5059
]
66.7 KB
Loading
66.7 KB
Loading

client/src/components/MuiCustomSelect.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ export default function MuiCustomSelect(props) {
66
props.value || props?.schema?.default || "",
77
);
88
const [options, setOptions] = useState(
9-
(props?.options?.enumOptions || []).map((o) => o.value),
9+
(
10+
props?.options?.enumOptions ||
11+
props?.uiSchema?.["ui:options"]?.["enumOptions"] ||
12+
[]
13+
).map((o) => o.value),
1014
);
1115

1216
useEffect(() => {

client/src/components/apps/AppStepCard.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import elevenLabsIcon_light from "../../assets/images/elevenlabs-icon-light.png"
1313
import elevenLabsIcon_dark from "../../assets/images/elevenlabs-icon-dark.png";
1414
import vertexAiIcon_light from "../../assets/images/vertexai-icon-light.png";
1515
import vertexAiIcon_dark from "../../assets/images/vertexai-icon-dark.png";
16+
import localAiIcon_light from "../../assets/images/localai-icon-light.png";
17+
import localAiIcon_dark from "../../assets/images/localai-icon-dark.png";
1618

1719
const getIconImage = (icon, isActive) => {
1820
switch (icon?.replaceAll(" ", "").toLowerCase()) {
@@ -30,6 +32,8 @@ const getIconImage = (icon, isActive) => {
3032
return isActive ? elevenLabsIcon_dark : elevenLabsIcon_light;
3133
case "google":
3234
return isActive ? vertexAiIcon_dark : vertexAiIcon_light;
35+
case "localai":
36+
return isActive ? localAiIcon_dark : localAiIcon_light;
3337
default:
3438
return promptlyIcon_light;
3539
}

client/src/pages/organization.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ const settingsSchema = {
175175
description: "AWS default region",
176176
title: "AWS Default Region",
177177
},
178+
localai_api_key: {
179+
type: ["string", "null"],
180+
description: "LocalAI API key",
181+
title: "LocalAI API Key",
182+
},
183+
localai_base_url: {
184+
type: ["string", "null"],
185+
description: "LocalAI base URL",
186+
title: "LocalAI Base URL",
187+
},
178188
vectorstore_weaviate_url: {
179189
type: ["string", "null"],
180190
description: "Vectorstore Weaviate URL",

client/src/pages/setting.jsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const SettingPage = () => {
4040
cohere_key: "",
4141
forefrontai_key: "",
4242
elevenlabs_key: "",
43+
localai_api_key: "",
44+
localai_base_url: "",
4345
logo: "",
4446
});
4547
const [loading, setLoading] = useState(true);
@@ -62,6 +64,8 @@ const SettingPage = () => {
6264
google_service_account_json_key:
6365
profile.google_service_account_json_key,
6466
azure_openai_api_key: profile.azure_openai_api_key,
67+
localai_api_key: profile.localai_api_key,
68+
localai_base_url: profile.localai_base_url,
6569
logo: profile.logo,
6670
user_email: profile.user_email,
6771
});
@@ -92,6 +96,8 @@ const SettingPage = () => {
9296
google_service_account_json_key:
9397
profile.google_service_account_json_key,
9498
azure_openai_api_key: profile.azure_openai_api_key,
99+
localai_api_key: profile.localai_api_key,
100+
localai_base_url: profile.localai_base_url,
95101
logo: profile.logo,
96102
});
97103
setLoading(false);
@@ -266,6 +272,50 @@ const SettingPage = () => {
266272
</Form.Item>
267273
</Col>
268274
</Row>
275+
<Row>
276+
<Col xs={24} md={14}>
277+
<Form.Item label="LocalAI Base URL">
278+
<Row>
279+
<Col xs={24} md={16}>
280+
{" "}
281+
<Input
282+
value={formData.localai_base_url}
283+
disabled={!profileFlags.CAN_ADD_KEYS}
284+
onChange={(e) => {
285+
setFormData({
286+
...formData,
287+
localai_base_url: e.target.value,
288+
});
289+
setUpdateKeys(updateKeys.add("localai_base_url"));
290+
}}
291+
/>
292+
</Col>
293+
</Row>
294+
</Form.Item>
295+
</Col>
296+
</Row>
297+
<Row>
298+
<Col xs={24} md={14}>
299+
<Form.Item label="LocalAI API Key">
300+
<Row>
301+
<Col xs={24} md={16}>
302+
{" "}
303+
<Input.Password
304+
value={formData.localai_api_key}
305+
disabled={!profileFlags.CAN_ADD_KEYS}
306+
onChange={(e) => {
307+
setFormData({
308+
...formData,
309+
localai_api_key: e.target.value,
310+
});
311+
setUpdateKeys(updateKeys.add("localai_api_key"));
312+
}}
313+
/>
314+
</Col>
315+
</Row>
316+
</Form.Item>
317+
</Col>
318+
</Row>
269319
{process.env.REACT_APP_ENABLE_SUBSCRIPTION_MANAGEMENT ===
270320
"true" && (
271321
<>

0 commit comments

Comments
 (0)