Skip to content

Commit

Permalink
T-12 gpt code added
Browse files Browse the repository at this point in the history
  • Loading branch information
rontap committed May 8, 2023
1 parent b08c8f3 commit 98109ab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion app/api/.env_EXAMPLE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
GRAPHENE_GPT_ORG="org-???"
GRAPHENE_GPT_KEY="sk-???"
GRAPHENE_GPT_KEY="sk-???"
GRAPHENE_GPT_VERSION="1"
29 changes: 9 additions & 20 deletions app/api/gpt4.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import time
from http.server import HTTPServer, BaseHTTPRequestHandler

import cgi
from decouple import config
import requests
import asyncio
import websockets
import openai
import io
import os
import subprocess
import selectors
import sys
from urllib.parse import urlparse, parse_qs

from http.server import BaseHTTPRequestHandler, HTTPServer

ORG = config('GRAPHENE_GPT_ORG')
KEY = config('GRAPHENE_GPT_KEY')
Expand All @@ -23,8 +11,10 @@
openai.organization = ORG
openai.api_key = KEY

if (ORG is None or KEY is None):
raise "No GRAPHENE_GPT_ORG and _KEY are added. Please create a .env-file"
if ORG is None or KEY is None:
raise "No environment variable GRAPHENE_GPT_ORG and _KEY are added. Please create a .env-file and get valid keys " \
"from openAI"


async def echo(websocket, path):
async for message in websocket:
Expand All @@ -44,12 +34,9 @@ async def send_message(websocket, message):


async def gpt_chat_int(prompt, websocket):
start_time = time.time()
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{'role': 'user', 'content': prompt}
],
messages=[{'role': 'user', 'content': prompt}],
temperature=0,
stream=True
)
Expand All @@ -67,7 +54,7 @@ async def gpt_chat_int(prompt, websocket):
print(f"Full response received")


def gpt_chat(prompt):
def gpt_chat_stat(prompt):
start_time = time.time()
response = openai.ChatCompletion.create(
model="gpt-4",
Expand All @@ -78,13 +65,15 @@ def gpt_chat(prompt):
stream=False
)
message = response.choices[0].message["content"].strip()
print("!! {}", message)
print(message)
return message


async def main():
print("Starting GPT WS Service...")
gpt_chat_stat("Write this message back: GPT Connection works.")
async with websockets.serve(echo, "localhost", 8765, max_size=10 * 1024 * 1024):
print("WS Service is active at: ws://localhost:8765")
await asyncio.Future()


Expand Down
3 changes: 2 additions & 1 deletion app/src/dynamic/groups/nodes-query/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
./*
./*
chatgpt.prompt
27 changes: 16 additions & 11 deletions app/src/ui/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ export default function Header({toggleBg, graph}: {
}

const initGPT = async () => {
setUseGPT(GPTStatus.PREPARING);
let userPrompt = window.prompt("Write ChatGPT prompt here...");
if (!userPrompt) return;
try {
setGptContent("Initialising...");
const basePromptPath = await NodeGroup.fetchCurrentGPTPrompt();

const basePromptPath = await NodeGroup.fetchCurrentGPTPrompt();
const basePrompt = await basePromptPath.text();
const wsc = new WebSocket(CONST.chatGPTWS)
let userPrompt = window.prompt("Write ChatGPT prompt here...");

if (!userPrompt) return;
const finalQuery = basePrompt.slice(16).replace("%QUERY%", userPrompt);

const wsc = new WebSocket(CONST.chatGPTWS)
setGptContent("Initialising...");
setUseGPT(GPTStatus.WORKING);
let allData = ""
wsc.onmessage = (msg) => {
Expand Down Expand Up @@ -91,7 +91,8 @@ export default function Header({toggleBg, graph}: {
wsc.send(finalQuery);
}
wsc.onerror = (error) => {
setGptContent("A websocket Error occurred");
setGptContent("A websocket Error occurred. Is the middleware running?");
setUseGPT(GPTStatus.ERROR);
}

} catch
Expand All @@ -105,7 +106,6 @@ export default function Header({toggleBg, graph}: {

return <span id={"header"}>

{/*<Button className={"blue"}>Run</Button>*/}
&nbsp;&nbsp;
{graph && <>
<BtnGroup>
Expand All @@ -121,16 +121,21 @@ export default function Header({toggleBg, graph}: {
</Button>

{" "}
<Button onClick={initGPT}>
<Button onClick={initGPT} disabled={useGPT === GPTStatus.WORKING}>
Use GPT
</Button>

<div className={"gptContent " + (useGPT === GPTStatus.WORKING ? "show" : "hide")}>
<div className={"gptContent " + (useGPT !== GPTStatus.IDLE ? "show" : "hide")}>
<div className={"gptTitle"}>
Generating Graph... ({gptContent.length}) characters
{useGPT === GPTStatus.ERROR && <>
<Button small className={"ml10 blue"} onClick={() => setUseGPT(GPTStatus.IDLE)}>Close</Button>
</>}
</div>
<br/>
<div className={"animateRow"}>&nbsp;</div>
{
useGPT === GPTStatus.WORKING && <div className={"animateRow"}>&nbsp;</div>
}
<br/>
<code id={"gptCode"}>
{gptContent}
Expand Down
13 changes: 10 additions & 3 deletions app/src/ui/styles/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ body:has(._white) {
/**
GPT
*/
.ml10 {
float: right !important;
margin-top: -4px !important;
display: inline-block !important;
}

.gptContent {
position: fixed;
Expand All @@ -558,11 +563,13 @@ GPT
max-height: 100px;
transition: all .4s;
}

.show.gptContent {
bottom: -1px;
max-height: 400px;

}

.gptTitle {
border-bottom: 1px solid #1976D2;
padding: 15px 15px 15px 15px;
Expand All @@ -577,21 +584,21 @@ GPT
font-family: monospace;
line-break: normal;
white-space: pre-wrap;
font-size:15px;
font-size: 15px;
max-height: 300px;
overflow: auto;
display: block;
}

.animateRow {
height: 3px;
margin:-7px -10px 3px -15px;
margin: -7px -10px 3px -15px;
}

.animateRow::before {
content: '';
animation: indeterminateAnimation 1.8s infinite linear;
background:whitesmoke;
background: whitesmoke;
transform-origin: 0% 50%;
width: 100%;
height: 100%;
Expand Down

0 comments on commit 98109ab

Please sign in to comment.