From d040f9932d807332c4963f97bc19529051faa6c1 Mon Sep 17 00:00:00 2001 From: y4my4my4m <8145020+y4my4my4m@users.noreply.github.com> Date: Mon, 14 Aug 2023 19:52:20 +0900 Subject: [PATCH] skynet --- src/Home/Net/Programs/Skynet/Skynet.ZC | 165 +++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 src/Home/Net/Programs/Skynet/Skynet.ZC diff --git a/src/Home/Net/Programs/Skynet/Skynet.ZC b/src/Home/Net/Programs/Skynet/Skynet.ZC new file mode 100644 index 00000000..50285b1e --- /dev/null +++ b/src/Home/Net/Programs/Skynet/Skynet.ZC @@ -0,0 +1,165 @@ +#define SERVER_ADDR "mymiddleware.server.com" +#define SERVER_PORT 9000 + +U8* ExtractGPTCompletion(U8 *response) { + U8 *start = StrFind("\"text\": \"", response); + if (!start) { + return NULL; // Pattern not found + } + + start += 10; // Move pointer after '"text": "' + + U8 *end = StrFind("\",", start); // Find the closing double quote + if (!end) { + return NULL; + } + + *end = 0; // Null terminate the completion string + + return start; +} + +public I64 Skynet(U8 *prompt) +{ + U8 responseBuf[8192]; + I64 sock; + + sock = TCPConnectionCreate(SERVER_ADDR, SERVER_PORT); + if (sock <= 0) + { + PrintErr("Failed to connect to middleware server"); + return sock; + } + + U8 *payload = StrPrint(NULL, "{\"model\": \"text-davinci-003\",\"prompt\": \"%s\",\"max_tokens\": 100,\"temperature\": 1}", prompt); + U8 *requestHeader = StrPrint(NULL, + "POST /gpt3 HTTP/1.1\r\n" + "Host: %s:%d\r\n" + "User-Agent: ZealOSClient/1.0\r\n" + "Accept: */*\r\n" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n", + SERVER_ADDR, + SERVER_PORT, + StrLen(payload) + ); + U8 *fullRequest = StrPrint(NULL, "%s%s\n\n", requestHeader, payload); + + TCPSocketSendString(sock, fullRequest); + Free(requestHeader); + Free(payload); + Free(fullRequest); + + + I64 responseLength = TCPSocketReceive(sock, responseBuf, sizeof(responseBuf) - 1); // -1 to ensure space for null terminator + + responseBuf[responseLength] = 0; // Null-terminate the response + + // Assuming the headers and payload are separated by two newline sequences (standard HTTP) + U8 *jsonPayload = StrFind("\r\n\r\n", responseBuf); + if (!jsonPayload) { + TCPSocketClose(sock); + return -1; // or some error code + } + jsonPayload += 4; // Move past the header separator + + U8 *completion = ExtractGPTCompletion(jsonPayload); + Print("$$RED$$Skynet: $$YELLOW$$%s$$FG$$\n", completion); + + TCPSocketClose(sock); + return 0; +} + +U8* ExtractGPT4Completion(U8 *response) +{ + U8 *start = StrFind("\"content\": \"", response); + if (!start) + { + return NULL; // Pattern not found + } + + start += 12; // Move pointer after '"content": "' + + U8 *end = StrFind("\"", start); // Find the closing double quote + if (!end) + { + return NULL; + } + + *end = 0; // Null terminate the completion string + + return start; +} + + +public I64 Skynet4(U8 *message, U8 *model="gpt-3.5-turbo") // gpt-3.5-turbo || gpt-4 +{ + U8 responseBuf[8192]; + I64 sock; + + sock = TCPConnectionCreate(SERVER_ADDR, SERVER_PORT); + if (sock <= 0) + { + PrintErr("Failed to connect to middleware server"); + return sock; + } + + U8 *payload = StrPrint(NULL, + "{" + "\"model\": \"%s\"," + "\"messages\": [" + "{" + "\"role\": \"system\"," + "\"content\": \"You are a helpful assistant.\"" + "}," + "{" + "\"role\": \"user\"," + "\"content\": \"%s\"" + "}" + "]" + "}", + model, + message + ); + + U8 *requestHeader = StrPrint(NULL, + "POST /chat HTTP/1.1\r\n" + "Host: %s:%d\r\n" + "User-Agent: ZealOSClient/1.0\r\n" + "Accept: */*\r\n" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n", + SERVER_ADDR, + SERVER_PORT, + StrLen(payload) + ); + + U8 *fullRequest = StrPrint(NULL, "%s%s", requestHeader, payload); + + TCPSocketSendString(sock, fullRequest); + Free(requestHeader); + Free(payload); + Free(fullRequest); + + I64 responseLength = TCPSocketReceive(sock, responseBuf, sizeof(responseBuf) - 1); + responseBuf[responseLength] = 0; + + // "HTTP Response:\r\n"; + // "%s",responseBuf; + // "\r\nPARSED:\r\n"; + + U8 *jsonPayload = StrFind("\r\n\r\n", responseBuf); + if (!jsonPayload) { + TCPSocketClose(sock); + return -1; + } + jsonPayload += 4; + + U8 *completion = ExtractGPT4Completion(jsonPayload); + Print("$$RED$$Skynet: $$YELLOW$$%s$$FG$$\n", completion); + + TCPSocketClose(sock); + return 0; +} \ No newline at end of file