Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. OpenAI Coding

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    OpenAI Coding

    This topic has been deleted. Only users with topic management privileges can see it.
    • Christoph1337
      Christoph1337 last edited by

      Ich habe heute mal mit OpenAI gespielt. Selber kann ich nicht JavaScript programmieren. Also haben ich OpenAI nach einem Script gefragt, welches mir ermöglicht Anfragen an OpenAI zu senden und diese in je einem DP für Frage und Antwort zu bekommen.

      Aktuell sieht das Script dazu so aus:

      const ACCESS_TOKEN = "API-TOKEN";
      const MODEL_ID = "davinci";
      const QUESTION_DP_ID = "0_userdata.0.GlobalVars.OpenAIQuestion";
      const ANSWER_DP_ID = "0_userdata.0.GlobalVars.OpenAIAnswer";
      
      const http = require('http');
      
      on({id: QUESTION_DP_ID, change: "ne"}, function (obj) {
        // Hole den aktuellen Wert des Datenpunkts für die Frage
        const question = getState(QUESTION_DP_ID).val;
        console.log(`Frage: ${question}`);
      
        // Setze den Wert des Datenpunkts für die Antwort auf "keine Antwort verfügbar"
        setState(ANSWER_DP_ID, "keine Antwort verfügbar");
      
        // Erstelle die HTTP-POST-Optionen
        const options = {
          host: 'api.openai.com',
          path: '/v1/completions',
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${ACCESS_TOKEN}`
          }
        };
      
        // Erstelle den HTTP-POST-Request
        const req = http.request(options, (res) => {
          console.log(`Status: ${res.statusCode}`);
          console.log(`Headers: ${JSON.stringify(res.headers)}`);
          res.setEncoding('utf8');
          let responseData = '';
          res.on('data', (chunk) => {
            responseData += chunk;
          });
          res.on('end', () => {
            console.log(`Antwort: ${responseData}`);
            const responseJson = JSON.parse(responseData);
            if (responseJson.hasOwnProperty('choices')) {
              const answer = responseJson.choices[0].text;
              // Setze den Wert des Datenpunkts für die Antwort auf die erste Antwort
              setState(ANSWER_DP_ID, answer);
            }
          });
        });
      
        req.on('error', (e) => {
          console.error(`Problem mit dem Request: ${e.message}`);
        });
      
        // Sende den HTTP-POST-Request
        req.write(JSON.stringify({
          "model": MODEL_ID,
          "prompt": question,
          "max_tokens": 128
        }));
        req.end();
      });
      
      

      Ich hab keine Ahnung ob der Code Sinn ergibt. Leider konnte ich ihn bis jetzt nicht zum laufen bringen, da immer irgendwelche Module zu Fehlern geführt haben. OpenAI hat hier aber viele verschiedene Scripte generiert und versucht Hilfestellung zu leisten. Schon gruselig der Kram.

      Vielleicht hat ja jemand von euch eine Idee wie man so ein Script bauen könnte. Da gibt es sicher spannende SPielereien für.

      Christoph1337 1 Reply Last reply Reply Quote 0
      • Christoph1337
        Christoph1337 @Christoph1337 last edited by

        nach ein paar weiteren fragen klappt das script nun 😄 ...

        const ACCESS_TOKEN = "token";
        const MODEL_ID = "text-davinci-002";
        const QUESTION_DP_ID = "0_userdata.0.GlobalVars.OpenAIQuestion";
        const ANSWER_DP_ID = "0_userdata.0.GlobalVars.OpenAIAnswer";
        
        const https = require('https');
        
        on({id: QUESTION_DP_ID, change: "ne"}, function (obj) {
          // Get the current value of the question datapoint
          const question = getState(QUESTION_DP_ID).val;
          console.log(`Question: ${question}`);
        
          // Set the value of the answer datapoint to "no answer available"
          setState(ANSWER_DP_ID, "no answer available");
        
          // Create the HTTP POST options
          const options = {
            host: 'api.openai.com',
            path: '/v1/completions',
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'Authorization': `Bearer ${ACCESS_TOKEN}`
            }
          };
        
          // Create the HTTP POST request
          const req = https.request(options, (res) => {
            console.log(`Status: ${res.statusCode}`);
            console.log(`Headers: ${JSON.stringify(res.headers)}`);
            res.setEncoding('utf8');
            let responseData = '';
            res.on('data', (chunk) => {
              responseData += chunk;
            });
            res.on('end', () => {
              console.log(`Answer: ${responseData}`);
              const responseJson = JSON.parse(responseData);
              if (responseJson.hasOwnProperty('choices')) {
                const answer = responseJson.choices[0].text;
                // Set the value of the answer datapoint to the first answer
                setState(ANSWER_DP_ID, answer);
              }
            });
          });
        
          req.on('error', (e) => {
            console.error(`Problem with request: ${e.message}`);
          });
        
          // Send the HTTP POST request
          req.write(JSON.stringify({
            "model": MODEL_ID,
            "prompt": question,
            "max_tokens": 128
          }));
          req.end();
        });
        
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post

        Support us

        ioBroker
        Community Adapters
        Donate

        817
        Online

        31.8k
        Users

        80.0k
        Topics

        1.3m
        Posts

        1
        2
        448
        Loading More Posts
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes
        Reply
        • Reply as topic
        Log in to reply
        Community
        Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
        The ioBroker Community 2014-2023
        logo