]> git.basschouten.com Git - openhab-addons.git/commitdiff
[chatgpt] Add support for LocalAI and other compatible services (#15385)
authorKai Kreuzer <kai@openhab.org>
Tue, 17 Oct 2023 10:30:52 +0000 (12:30 +0200)
committerGitHub <noreply@github.com>
Tue, 17 Oct 2023 10:30:52 +0000 (12:30 +0200)
* Add support for LocalAI and other compatible services

--------

Signed-off-by: Kai Kreuzer <kai@openhab.org>
bundles/org.openhab.binding.chatgpt/README.md
bundles/org.openhab.binding.chatgpt/src/main/java/org/openhab/binding/chatgpt/internal/ChatGPTBindingConstants.java
bundles/org.openhab.binding.chatgpt/src/main/java/org/openhab/binding/chatgpt/internal/ChatGPTConfiguration.java
bundles/org.openhab.binding.chatgpt/src/main/java/org/openhab/binding/chatgpt/internal/ChatGPTHandler.java
bundles/org.openhab.binding.chatgpt/src/main/resources/OH-INF/i18n/chatgpt.properties
bundles/org.openhab.binding.chatgpt/src/main/resources/OH-INF/thing/thing-types.xml

index cf38c95244db3a43a829bfc649fa7c141f9a545c..d48a0735208c7fe9b66f36c13c651cccc70be7e8 100644 (file)
@@ -14,9 +14,13 @@ The binding supports a single thing type `account`, which corresponds to the Ope
 The `account` thing requires a single configuration parameter, which is the API key that allows accessing the account.
 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       |
+| 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
 
index 3d07d6cb63320c93457da2931e0361c54db2e393..87635b04c10e812164055686e27e6bd29354b074 100644 (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";
 }
index b09b5d80683eab0e16768f9752efe5e6e9627348..e2618c4d392db895874a86fd3aeb7313e7a6be03 100644 (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";
 }
index e39b1bc11908538e4aedfaa80e0b0619fc46fe18..e04ee01d00760bbfb51ede8582bc6b9b313ba13a 100644 (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);
index 48390a3b8cc34e01ebbae5309db455030008071e..6c96866ecf47c6da8bb4e0051b2fea8b6811f2c2 100644 (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
+offline.configuration-error = No API key configured
+offline.communication-error = Could not connect to OpenAI API
index 95d2ee7e26a84cdef2257109d4d9f54dd365a24f..f7aedc576269c657a0db22cb5d53001f692b6022 100644 (file)
                                <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>