bramses / chatgpt-md

A (nearly) seamless integration of ChatGPT into Obsidian.
MIT License
824 stars 61 forks source link

OpenRouter AI support #97

Open t-low-505 opened 7 months ago

t-low-505 commented 7 months ago

Hi and thank you for the useful plugin!

Unfortunately, it's not supported custom URLs and I can't use my OpenRouter account with this plugin. I've done a quick and dirty patch to workaround that + fixed some parsing errors. Maybe it would be helpful for someone. (The patch should be applied to .obsidian/plugins/chatgpt-md/main.js)

--- main-orig.js    2023-11-26 08:57:58.669377200 +0300
+++ main.js 2023-11-26 09:36:22.537777000 +0300
@@ -130,15 +130,21 @@
           this._setReadyState(this.OPEN);
         }
         var data = this.xhr.responseText.substring(this.progress);
-        this.progress += data.length;
-        data.split(/(\r\n|\r|\n){2}/g).forEach(function(part) {
-          if (part.trim().length === 0) {
-            this.dispatchEvent(this._parseEventChunk(this.chunk.trim()));
-            this.chunk = "";
-          } else {
-            this.chunk += part;
+        if (data.length > 0) {
+          this.progress += data.length;
+          if (data.trim() !== ": OPENROUTER PROCESSING") {
+            data.split(/(\r\n|\r|\n){2}/g).forEach(function(part) {
+              if (part.trim() !== ": OPENROUTER PROCESSING") {
+                if (part.trim().length === 0) {
+                  this.dispatchEvent(this._parseEventChunk(this.chunk.trim()));
+                  this.chunk = "";
+                } else {
+                  this.chunk += part;
+                }
+              }
+            }.bind(this));
           }
-        }.bind(this));
+        }
       };
       this._onStreamLoaded = function(e) {
         this._onStreamProgress(e);
@@ -362,7 +368,7 @@
             initialCursorPosLine = newCursor.line;
           });
           source.addEventListener("message", (e) => {
-            if (e.data != "[DONE]") {
+            if (e.data != "[DONE]" && e.data != "") {
               const payload = JSON.parse(e.data);
               const text = payload.choices[0].delta.content;
               if (!text) {
@@ -463,7 +469,7 @@
   headingLevel: 0,
   inferTitleLanguage: "English"
 };
-var DEFAULT_URL = `https://api.openai.com/v1/chat/completions`;
+var DEFAULT_URL = `https://openrouter.ai/api/v1/chat/completions`;
 var ChatGPT_MD = class extends import_obsidian3.Plugin {
   async callOpenAIAPI(streamManager, editor, messages, model = "gpt-3.5-turbo", max_tokens = 250, temperature = 0.3, top_p = 1, presence_penalty = 0.5, frequency_penalty = 0.5, stream = true, stop = null, n = 1, logit_bias = null, user = null, url = DEFAULT_URL) {
     try {
@@ -731,7 +737,7 @@
         }
       ];
       const responseUrl = await (0, import_obsidian3.requestUrl)({
-        url: `https://api.openai.com/v1/chat/completions`,
+        url: `https://openrouter.ai/api/v1/chat/completions`,
         method: "POST",
         headers: {
           Authorization: `Bearer ${this.settings.apiKey}`,