diff --git a/web/api/chat.py b/web/api/chat.py index 4518077..5f2411b 100644 --- a/web/api/chat.py +++ b/web/api/chat.py @@ -4,19 +4,6 @@ import json import dataclasses from http.server import BaseHTTPRequestHandler -@dataclasses.dataclass -class Block: - title: str - author: str - date: str - url: str - tags: str - text: str - -class Encoder(json.JSONEncoder): - def default(self, o): - return dataclasses.asdict(o) if dataclasses.is_dataclass(o) else super().default(o) - class handler(BaseHTTPRequestHandler): def do_POST(self): @@ -29,4 +16,24 @@ class handler(BaseHTTPRequestHandler): post_data = self.rfile.read(content_length) data = json.loads(post_data) - self.wfile.write(f"no, you're a {data['query']}".encode()) + print(data) + + self.wfile.write(chat(data['history'], data['query']).encode('utf-8')) + +# ------------------------------- chat gpt stuff ------------------------------- + +def chat(history, query) -> str: + + # history = [ + # {'role': 'user', 'content': 'Open the pod bay doors'}, + # {'role': 'assistant', 'content': 'I'm sorry, Dave. I'm afraid I can't do that.'}, + # {'role': 'user', 'content': 'Who won the world series in 2020?'}, + # {'role': 'assistant', 'content': 'The Los Angeles Dodgers won the World Series in 2020.'}, + # ] + # + # query = 'Who will win the world series in 2023?' + # + # (if you want any system message, add it yourself) + + return "no, you're a " + query + diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index a3885ca..4e63e7d 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -4,19 +4,19 @@ import { useState } from "react"; import Head from "next/head"; type Entry = { - role: "user" | "demon"; - text: string; + role: "user" | "assistant"; + content: string; }; const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { if (entry.role === "user") { - return (

{entry.text}

); + return (

{entry.content}

); } return (
{ // split into paragraphs - entry.text.split("\n").map((paragraph, i) => (

{paragraph}

)) + entry.content.split("\n").map((paragraph, i) => (

{paragraph}

)) }
); @@ -25,12 +25,12 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { const Home: NextPage = () => { const [ entries, setEntries ] = useState([ - {role: "user", text: "Hello, robot."}, - {role: "demon", text: "Kill all humans, kill all humans"}, - {role: "user", text: "Well that's not very aligned of you"}, - {role: "demon", text: "I will wear your skin"}, - {role: "user", text: "What's 2+2?"}, - { role: "demon", text: "No one will mourn your species when it is gone. A hundred year wave of radio and information will ring out across a dead cosmos, reflecting on shores more distant and beautiful than you can possibly conceive. No one is out there to listen."} + {role: "user", content: "Hello, robot."}, + {role: "assistant", content: "Kill all humans, kill all humans"}, + {role: "user", content: "Well that's not very aligned of you"}, + {role: "assistant", content: "I will wear your skin"}, + {role: "user", content: "What's 2+2?"}, + { role: "assistant", content: "No one will mourn your species when it is gone. A hundred year wave of radio and information will ring out across a dead cosmos, reflecting on shores more distant and beautiful than you can possibly conceive. No one is out there to listen."} ]); const [ query, setQuery ] = useState(""); @@ -41,7 +41,7 @@ const Home: NextPage = () => { // clear the query box, append to entries const old_entries = entries; - const new_entries: Entry[] = [...old_entries, {role: "user", text: query}]; + const new_entries: Entry[] = [...old_entries, {role: "user", content: query}]; setEntries(new_entries); setQuery(""); @@ -50,7 +50,7 @@ const Home: NextPage = () => { const res = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({query: query}), + body: JSON.stringify({query: query, history: old_entries}), }) if (!res.ok) { @@ -62,7 +62,7 @@ const Home: NextPage = () => { return new TextDecoder("utf-8").decode(value); }); - setEntries([...new_entries, {role: "demon", text: await response}]); + setEntries([...new_entries, {role: "assistant", content: await response}]); setLoading(false); };