Skip to content

Instantly share code, notes, and snippets.

@Lukas1h
Last active July 30, 2023 14:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lukas1h/4a8559d0aa209dd4fec09aa5463e9cbe to your computer and use it in GitHub Desktop.
Save Lukas1h/4a8559d0aa209dd4fec09aa5463e9cbe to your computer and use it in GitHub Desktop.

ChatGPT Telnet

  • Download the files into a directory.
  • Replace the API key with your own in gpt.js
  • Run npm i
  • Run node .
  • Connect to it using telnet <Your IP address>
import { ChatGPTClient } from "@waylaidwanderer/chatgpt-api";
import axios from "axios";
let context = `
Please keep your responces brief. Dont use markdown.
`
const chatGptClient = new ChatGPTClient(
"sk-ABCDEFG" // your key here
);
export class GPT {
gptResponse = {};
async ask(query) {
let conversationId = this.gptResponse?.conversationId;
let parentMessageId = this.gptResponse?.messageId;
if (typeof conversationId === "undefined") {
console.log(
`conversationID (${conversationId}) is undefined. Sending new message.`
);
this.gptResponse = await chatGptClient.sendMessage(context + query);
} else {
console.log(
`conversationID (${conversationId}) is good. Sending following message.`
);
this.gptResponse = await chatGptClient.sendMessage(query, {
conversationId: conversationId,
parentMessageId: parentMessageId,
});
}
return this.gptResponse.response
}
static async getUsage(){
const res = await axios
.get(
"https://api.openai.com/dashboard/billing/usage?end_date=2023-05-01&start_date=2023-04-01",
{
headers: {
Authorization:
"Bearer "+key,
},
}
)
return Number(res.data.total_usage).toFixed(2);
}
}
import net from "net"
import { GPT } from "./gpt.js";
const PORT = process.argv[2] || 23;
// Create a new TCP server
const server = net.createServer();
let gpt = new GPT();
function containsLetters(str) {
const regex = /[a-zA-Z]/;
return regex.test(str);
}
// When a client connects to the server
server.on("connection", async (client) => {
function sendMessage(string) {client.write(string+"\r\n")}
console.log(
`Client connected from ${client.remoteAddress}:${client.remotePort}`
);
// Send a welcome message to the client
client.write(
`\r\n\r\n \u001b[7mWelcome To ChatGPT\u001b[0m\r\n`
);
client.write("\u001b[1m> ");
client.on("data", async (data) => {
const message = data.toString().trim();
if (!containsLetters(message)) {
console.log("ignoring ",message)
return;
}
console.log("allowing ",message)
client.write("\u001b[0m");
if (message.startsWith("!usage")) {
let usageString = `You have used $${(await GPT.getUsage()) / 100} this month.`;
sendMessage(usageString);
client.write("\u001b[1m> ");
return;
}
console.log(message);
var responce = await gpt.ask(message);
console.log(responce);
sendMessage(responce);
client.write("\u001b[1m> ");
});
// When the client disconnects from the server
client.on("end", () => {
console.log(
`Client disconnected from ${client.remoteAddress}:${client.remotePort}`
);
});
});
// Start listening for connections on the defined port
server.listen(PORT, () => {
console.log(`Telnet server listening on port ${PORT}`);
});
{
"name": "chatgpt",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@waylaidwanderer/chatgpt-api": "^1.35.0",
"axios": "^1.3.5",
"chatgpt": "^5.2.2",
"chatgpt-api-wrapper": "^0.1.2",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"http": "^0.0.1-security",
"openai": "^3.2.1",
"replicate": "^0.10.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment