[chatgpt] Add support for LocalAI and other compatible services (#15385)

* Add support for LocalAI and other compatible services

--------

Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer 2023-10-17 12:30:52 +02:00 committed by GitHub
parent 062f7b13eb
commit 1fca7bfdc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 14 deletions

View File

@ -15,8 +15,12 @@ The `account` thing requires a single configuration parameter, which is the API
API keys can be created and managed under <https://platform.openai.com/account/api-keys>.
| Name | Type | Description | Default | Required | Advanced |
|-----------------|---------|-----------------------------------------|---------|----------|----------|
|-----------------|---------|-----------------------------------------------------------|--------------------------------------------|----------|----------|
| apiKey | text | The API key to be used for the requests | N/A | yes | no |
| apiUrl | text | The server API where to reach the AI service | https://api.openai.com/v1/chat/completions | no | yes |
| modelUrl | text | The model url where to retrieve the available models from | https://api.openai.com/v1/models | no | yes |
The advanced parameters `apiUrl` and `modelUrl` can be used, if any other ChatGPT-compatible service is used, e.g. a local installation of [LocalAI](https://github.com/go-skynet/LocalAI).
## Channels

View File

@ -31,7 +31,4 @@ public class ChatGPTBindingConstants {
// List of all Channel ids
public static final String CHANNEL_CHAT = "chat";
public static final String OPENAI_API_URL = "https://api.openai.com/v1/chat/completions";
public static final String OPENAI_MODELS_URL = "https://api.openai.com/v1/models";
}

View File

@ -23,4 +23,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
public class ChatGPTConfiguration {
public String apiKey = "";
public String apiUrl = "https://api.openai.com/v1/chat/completions";
public String modelUrl = "https://api.openai.com/v1/models";
}

View File

@ -12,8 +12,6 @@
*/
package org.openhab.binding.chatgpt.internal;
import static org.openhab.binding.chatgpt.internal.ChatGPTBindingConstants.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -63,6 +61,9 @@ public class ChatGPTHandler extends BaseThingHandler {
private Gson gson = new Gson();
private String apiKey = "";
private String apiUrl = "";
private String modelUrl = "";
private String lastPrompt = "";
private List<String> models = List.of();
@ -123,7 +124,7 @@ public class ChatGPTHandler extends BaseThingHandler {
root.add("messages", messages);
String queryJson = gson.toJson(root);
Request request = httpClient.newRequest(OPENAI_API_URL).method(HttpMethod.POST)
Request request = httpClient.newRequest(apiUrl).method(HttpMethod.POST)
.header("Content-Type", "application/json").header("Authorization", "Bearer " + apiKey)
.content(new StringContentProvider(queryJson));
logger.trace("Query '{}'", queryJson);
@ -158,12 +159,15 @@ public class ChatGPTHandler extends BaseThingHandler {
}
this.apiKey = apiKey;
this.apiUrl = config.apiUrl;
this.modelUrl = config.modelUrl;
updateStatus(ThingStatus.UNKNOWN);
scheduler.execute(() -> {
try {
Request request = httpClient.newRequest(OPENAI_MODELS_URL).method(HttpMethod.GET)
.header("Authorization", "Bearer " + apiKey);
Request request = httpClient.newRequest(modelUrl).method(HttpMethod.GET).header("Authorization",
"Bearer " + apiKey);
ContentResponse response = request.send();
if (response.getStatus() == 200) {
updateStatus(ThingStatus.ONLINE);

View File

@ -12,6 +12,10 @@ thing-type.chatgpt.account.description = Account at OpenAI that is used for acce
thing-type.config.chatgpt.account.apiKey.label = API Key
thing-type.config.chatgpt.account.apiKey.description = API key to access the account
thing-type.config.chatgpt.account.apiUrl.label = API URL
thing-type.config.chatgpt.account.apiUrl.description = The server API where to reach the AI service.
thing-type.config.chatgpt.account.modelUrl.label = Model URL
thing-type.config.chatgpt.account.modelUrl.description = The model url where to retrieve the available models from.
# channel types
@ -29,7 +33,7 @@ channel-type.config.chatgpt.chat.systemMessage.description = The system message
channel-type.config.chatgpt.chat.temperature.label = Temperature
channel-type.config.chatgpt.chat.temperature.description = Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
# Status messages
# status messages
offline.configuration-error = No API key configured
offline.communication-error = Could not connect to OpenAI API

View File

@ -19,6 +19,26 @@
<label>API Key</label>
<description>API key to access the account</description>
</parameter>
<parameter name="apiUrl" type="text" required="false">
<label>API URL</label>
<description>The server API where to reach the AI service.</description>
<default>https://api.openai.com/v1/chat/completions</default>
<advanced>true</advanced>
<options>
<option value="https://api.openai.com/v1/chat/completions">https://api.openai.com/v1/chat/completions</option>
</options>
<limitToOptions>false</limitToOptions>
</parameter>
<parameter name="modelUrl" type="text" required="false">
<label>Model URL</label>
<description>The model url where to retrieve the available models from.</description>
<default>https://api.openai.com/v1/models</default>
<advanced>true</advanced>
<options>
<option value="https://api.openai.com/v1/models">https://api.openai.com/v1/models</option>
</options>
<limitToOptions>false</limitToOptions>
</parameter>
</config-description>
</thing-type>