Skip to content

Commit

Permalink
querying the backend with websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
ElishaKay committed Sep 7, 2024
1 parent 8dd4d26 commit ebb76ac
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
OPENAI_API_KEY=
TAVILY_API_KEY=
DOC_PATH=./docs/my-docs
DOC_PATH=./my-docs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/gpt-researcher/context/local-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
106 changes: 106 additions & 0 deletions docs/docs/gpt-researcher/gptr/querying-the-backend.md
Original file line number Diff line number Diff line change
@@ -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);
}
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ebb76ac

Please sign in to comment.