From ebb76ac31e0b1f50aa8681fe00ef7cef5c531fb0 Mon Sep 17 00:00:00 2001 From: ElishaKay Date: Sat, 7 Sep 2024 20:22:46 +0000 Subject: [PATCH] querying the backend with websockets --- .env.example | 2 +- README.md | 2 +- .../docs/gpt-researcher/context/local-docs.md | 2 +- .../gptr/querying-the-backend.md | 106 ++++++++++++++++++ docs/sidebars.js | 1 + 5 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 docs/docs/gpt-researcher/gptr/querying-the-backend.md diff --git a/.env.example b/.env.example index edb96a977..f86255e5a 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ OPENAI_API_KEY= TAVILY_API_KEY= -DOC_PATH=./docs/my-docs \ No newline at end of file +DOC_PATH=./my-docs \ No newline at end of file diff --git a/README.md b/README.md index f5379da4b..a8b66818d 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ You can instruct the GPT Researcher to run research tasks based on your local do Step 1: Add the env variable `DOC_PATH` pointing to the folder where your documents are located. ```bash -export DOC_PATH="./docs/my-docs" +export DOC_PATH="./my-docs" ``` Step 2: diff --git a/docs/docs/gpt-researcher/context/local-docs.md b/docs/docs/gpt-researcher/context/local-docs.md index 9c8115c5a..31f53277d 100644 --- a/docs/docs/gpt-researcher/context/local-docs.md +++ b/docs/docs/gpt-researcher/context/local-docs.md @@ -5,7 +5,7 @@ You can instruct the GPT Researcher to run research tasks based on your local do Step 1: Add the env variable `DOC_PATH` pointing to the folder where your documents are located. ```bash -export DOC_PATH="./docs/my-docs" +export DOC_PATH="./my-docs" ``` Step 2: diff --git a/docs/docs/gpt-researcher/gptr/querying-the-backend.md b/docs/docs/gpt-researcher/gptr/querying-the-backend.md new file mode 100644 index 000000000..0bee895a6 --- /dev/null +++ b/docs/docs/gpt-researcher/gptr/querying-the-backend.md @@ -0,0 +1,106 @@ +# Querying the Backend + +## Introduction + +In this section, we will discuss how to query the GPTR backend server. The GPTR backend server is a Python server that runs the GPTR Python package. The server listens for WebSocket connections and processes incoming messages to generate reports, streaming back logs and results to the client. + +An example WebSocket client is implemented in the `gptr-webhook.js` file below. + +This function sends a Webhook Message to the GPTR Python backend running on localhost:8000, but this example can also be modified to query a [GPTR Server hosted on Linux](https://docs.gptr.dev/docs/gpt-researcher/getting-started/linux-deployment). + +// gptr-webhook.js + +```javascript + +const WebSocket = require('ws'); + +let socket = null; +let responseCallback = null; + +async function initializeWebSocket() { + if (!socket) { + const host = 'localhost:8000'; + const ws_uri = `ws://${host}/ws`; + + socket = new WebSocket(ws_uri); + + socket.onopen = () => { + console.log('WebSocket connection established'); + }; + + socket.onmessage = (event) => { + const data = JSON.parse(event.data); + console.log('WebSocket data received:', data); + + if (data.content === 'dev_team_result' + && data.output.rubber_ducker_thoughts != undefined + && data.output.tech_lead_review != undefined) { + if (responseCallback) { + responseCallback(data.output); + responseCallback = null; // Clear callback after use + } + } else { + console.log('Received data:', data); + } + }; + + socket.onclose = () => { + console.log('WebSocket connection closed'); + socket = null; + }; + + socket.onerror = (error) => { + console.error('WebSocket error:', error); + }; + } +} + +async function sendWebhookMessage(message) { + return new Promise((resolve, reject) => { + if (!socket || socket.readyState !== WebSocket.OPEN) { + initializeWebSocket(); + } + + const data = { + task: message, + report_type: 'dev_team', + report_source: 'web', + tone: 'Objective', + headers: {}, + repo_name: 'elishakay/gpt-researcher' + }; + + const payload = "start " + JSON.stringify(data); + + responseCallback = (response) => { + resolve(response); // Resolve the promise with the WebSocket response + }; + + if (socket.readyState === WebSocket.OPEN) { + socket.send(payload); + console.log('Message sent:', payload); + } else { + socket.onopen = () => { + socket.send(payload); + console.log('Message sent after connection:', payload); + }; + } + }); +} + +module.exports = { + sendWebhookMessage +}; +``` + +And here's how you can leverage this helper function: + +```javascript +const { sendWebhookMessage } = require('./gptr-webhook'); + +async function main() { + const message = 'What are the thoughts of the rubber duck?'; + const response = await sendWebhookMessage(message); + console.log('Response:', response); +} +``` \ No newline at end of file diff --git a/docs/sidebars.js b/docs/sidebars.js index 0bf3fe1fc..688f47cf0 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -32,6 +32,7 @@ items: [ 'gpt-researcher/gptr/pip-package', 'gpt-researcher/gptr/example', + 'gpt-researcher/gptr/querying-the-backend', 'gpt-researcher/gptr/config', 'gpt-researcher/gptr/automated-tests', 'gpt-researcher/gptr/troubleshooting',