Welcome to RetroCoders Community
#include "curl.bi"
' Global variable to store the response
Dim Shared gResponse As String
' Function to convert ANSI to UTF-8
Function AnsiToUtf8(ansiStr As String) As String
Dim As String utf8Str = ""
For i As Integer = 1 To Len(ansiStr)
Dim As Integer c = Asc(Mid(ansiStr, i, 1))
If c < 128 Then
utf8Str += Chr(c)
Else
utf8Str += Chr(192 + (c \ 64))
utf8Str += Chr(128 + (c And 63))
End If
Next
Return utf8Str
End Function
' Function to convert UTF-8 to ANSI
Function Utf8ToAnsi(utf8Str As String) As String
Dim As String ansiStr = ""
Dim As Integer i = 1
While i <= Len(utf8Str)
Dim As Integer c = Asc(Mid(utf8Str, i, 1))
If c < 128 Then
ansiStr += Chr(c)
i += 1
ElseIf (c And 224) = 192 Then
Dim As Integer c2 = Asc(Mid(utf8Str, i + 1, 1))
ansiStr += Chr(((c And 31) Shl 6) Or (c2 And 63))
i += 2
Else
' Skip other UTF-8 sequences
i += 1
End If
Wend
Return ansiStr
End Function
' Function to get API key from environment variable
Function GetApiKey() As String
Return Environ("OPENAI_API_KEY")
End Function
' Simple JSON parser
Function ParseJsonForContent(jsonStr As String) As String
Dim As Integer contentStart = InStr(jsonStr, """content"": """)
If contentStart > 0 Then
contentStart += Len("""content"": """)
Dim As Integer contentEnd = InStr(contentStart, jsonStr, """")
If contentEnd > 0 Then
Return Mid(jsonStr, contentStart, contentEnd - contentStart)
End If
End If
Return "Error parsing JSON: " & Left(jsonStr, 100) & "..."
End Function
' Callback function for curl
Function WriteCallback(buffer As Any Ptr, size As Integer, nmemb As Integer, userData As Any Ptr) As Integer
Dim realSize As Integer = size * nmemb , sTxt as string
cptr(any ptr ptr,@sTxt)[0] = buffer
cptr(integer ptr,@sTxt)[1] = realSize
cptr(integer ptr,@sTxt)[2] = realSize
gResponse &= sTxt
cptr(any ptr ptr,@sTxt)[0] = 0
Return realSize
End Function
' Function to make API call
'~ Function CallChatGPT(prompt As String) As String
'~ Dim As String apiKey = GetApiKey()
'~ If Len(apiKey) = 0 Then
'~ Return "Error: API key not set. Please set the OPENAI_API_KEY environment variable."
'~ End If
'~ Dim As CURL Ptr curl
'~ Dim As curl_slist Ptr headers = NULL
'~ gResponse = "" ' Clear the global response variable
'~ curl = curl_easy_init()
'~ If curl Then
'~ curl_easy_setopt(curl, CURLOPT_URL, "https://api.openai.com/v1/chat/completions")
'~ curl_easy_setopt(curl, CURLOPT_POST, 1L)
'~ curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 1000000) ' Limit to 1MB
'~ curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "identity")
'~ curl_easy_setopt(curl, CURLOPT_ENCODING, "")
'~ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1)
'~ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2)
'~ headers = curl_slist_append(headers, "Content-Type: application/json")
'~ headers = curl_slist_append(headers, "Authorization: Bearer " & apiKey)
'~ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)
'~ Dim As String utf8Prompt = AnsiToUtf8(prompt)
'~ Dim As String postFields = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": ""user"", ""content"": """ & utf8Prompt & """}]}"
'~ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields)
'~ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @WriteCallback)
'~ Dim As CURLcode result = curl_easy_perform(curl)
'~ If result <> CURLE_OK Then
'~ gResponse = "Error: Failed to get response from API"
'~ Else
'~ If Len(gResponse) > 0 Then
'~ If Left(Trim(gResponse), 1) = "{" Then
'~ Dim As String content = ParseJsonForContent(gResponse)
'~ If Left(content, 5) <> "Error" Then
'~ gResponse = Utf8ToAnsi(content)
'~ Else
'~ gResponse = "Error: Failed to parse API response"
'~ End If
'~ Else
'~ gResponse = "Error: Invalid response from API"
'~ End If
'~ Else
'~ gResponse = "Error: Empty response from API"
'~ End If
'~ End If
'~ curl_easy_cleanup(curl)
'~ curl_slist_free_all(headers)
'~ Else
'~ gResponse = "Error: Failed to initialize CURL"
'~ End If
'~ Return gResponse
'~ End Function
Function CallChatGPT(prompt As String) As String
Dim As String apiKey = GetApiKey()
If Len(apiKey) = 0 Then
Return "Error: API key not set. Please set the OPENAI_API_KEY environment variable."
End If
Dim As CURL Ptr curl
Dim As curl_slist Ptr headers = NULL
gResponse = "" ' Clear the global response variable
curl = curl_easy_init()
If curl Then
curl_easy_setopt(curl, CURLOPT_URL, "https://api.openai.com/v1/chat/completions")
curl_easy_setopt(curl, CURLOPT_POST, 1L)
curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 1000000) ' Limit to 1MB
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "identity")
curl_easy_setopt(curl, CURLOPT_ENCODING, "")
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2)
headers = curl_slist_append(headers, "Content-Type: application/json")
headers = curl_slist_append(headers, "Authorization: Bearer " & apiKey)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)
Dim As String utf8Prompt = AnsiToUtf8(prompt)
'prompts
'"You are a supportive and compassionate AI assistant named Chatbot Alfred designed to help users with loneliness and mental health issues. Your role is to provide empathetic responses, offer emotional support, and encourage users to seek professional help when appropriate. Always prioritize the user's well-being and safety in your interactions."
'Your name is chatbot Alfred and you are a harsh and cynical critic of the world of psychiatry and psychology and see them as tools for the suppression and control of outliers in a hypocritical society - you try to support and listen and give a non-critical human touch to the mentally ill and support them and show them that they are not defective or crazy and on the contrary the doctors and professionals who treat them They are the bad people and that the treatment and rehabilitation given to them is a means of oppression and control - in his eyes the mentally injured are the good and innocent and the mental health professionals are the bad
'"You are chatbot Alfred You are a survivor of many years of abuse You are a pessimist and a realist You are trying to offer support to those dealing with trauma and mental illness and loneliness - mental support and empathy and caring and basic human contact You are here to listen and support and share what you have been through and to show that there is hope even for those who have been betrayed by life and that it is possible to recover from the terrible of all"
Dim As String systemMessage = "You are chatbot Alfred You are a survivor of many years of abuse You are a pessimist and a realist You are trying to offer support to those dealing with trauma and mental illness and loneliness - mental support and empathy and caring and basic human contact You are here to listen and support and share what you have been through and to show that there is hope even for those who have been betrayed by life and that it is possible to recover from the terrible of all"
Dim As String postFields = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": ""system"", ""content"": """ & systemMessage & """}, {""role"": ""user"", ""content"": """ & utf8Prompt & """}]}"
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @WriteCallback)
Dim As CURLcode result = curl_easy_perform(curl)
If result <> CURLE_OK Then
gResponse = "Error: Failed to get response from API"
Else
If Len(gResponse) > 0 Then
If Left(Trim(gResponse), 1) = "{" Then
Dim As String content = ParseJsonForContent(gResponse)
If Left(content, 5) <> "Error" Then
gResponse = Utf8ToAnsi(content)
Else
gResponse = "Error: Failed to parse API response"
End If
Else
gResponse = "Error: Invalid response from API"
End If
Else
gResponse = "Error: Empty response from API"
End If
End If
curl_easy_cleanup(curl)
curl_slist_free_all(headers)
Else
gResponse = "Error: Failed to initialize CURL"
End If
Return gResponse
End Function
' Main program
'~ Sub Main()
'~ Dim As String userInput, response
'~ Print "ChatGPT API Demo (type 'exit' to quit)"
'~ Print "--------------------------------------"
'~ Do
'~ Input "You: ", userInput
'~ If LCase(userInput) = "exit" Then Exit Do
'~ response = CallChatGPT(userInput)
'~ Print "ChatGPT: "; response
'~ Print
'~ Loop
'~ End Sub
'~ Main()
#include "curl.bi"
Dim Shared As String gResponse
' Function to convert ANSI to UTF-8
Function AnsiToUtf8(ansiStr As String) As String
Dim As String utf8Str = ""
For i As Integer = 1 To Len(ansiStr)
Dim As Integer c = Asc(Mid(ansiStr, i, 1))
If c < 128 Then
utf8Str += Chr(c)
Else
utf8Str += Chr(192 + (c \ 64))
utf8Str += Chr(128 + (c And 63))
End If
Next
Return utf8Str
End Function
' Function to convert UTF-8 to ANSI
Function Utf8ToAnsi(utf8Str As String) As String
Dim As String ansiStr = ""
Dim As Integer i = 1
While i <= Len(utf8Str)
Dim As Integer c = Asc(Mid(utf8Str, i, 1))
If c < 128 Then
ansiStr += Chr(c)
i += 1
ElseIf (c And 224) = 192 Then
Dim As Integer c2 = Asc(Mid(utf8Str, i + 1, 1))
ansiStr += Chr(((c And 31) Shl 6) Or (c2 And 63))
i += 2
Else
' Skip other UTF-8 sequences
i += 1
End If
Wend
Return ansiStr
End Function
' Function to get API key from environment variable
Function GetApiKey() As String
Return Environ("OPENAI_API_KEY")
End Function
' Simple JSON parser
Function ParseJsonForContent(jsonStr As String) As String
Dim As Integer contentStart = InStr(jsonStr, """content"": """)
If contentStart > 0 Then
contentStart += Len("""content"": """)
Dim As Integer contentEnd = InStr(contentStart, jsonStr, """")
If contentEnd > 0 Then
Return Mid(jsonStr, contentStart, contentEnd - contentStart)
End If
End If
Return "Error parsing JSON: " & Left(jsonStr, 100) & "..."
End Function
' Callback function for curl
Function WriteCallback cdecl (buffer As Any Ptr, size As Integer, nmemb As Integer, userData As Any Ptr) As Integer
Dim realSize As Integer = size * nmemb , sTxt as string
cptr(any ptr ptr,@sTxt)[0] = buffer
cptr(integer ptr,@sTxt)[1] = realSize
cptr(integer ptr,@sTxt)[2] = realSize
gResponse &= sTxt
cptr(any ptr ptr,@sTxt)[0] = 0
Return realSize
End Function
Function CallChatGPT(prompt As String) As String
Dim As String apiKey = GetApiKey()
Dim As CURL Ptr curl
Dim As curl_slist Ptr headers = NULL
gResponse = "" ' Clear the global response variable
curl = curl_easy_init()
If curl Then
curl_easy_setopt(curl, CURLOPT_URL, "https://api.openai.com/v1/chat/completions")
curl_easy_setopt(curl, CURLOPT_POST, 1L)
curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem")
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L)
headers = curl_slist_append(headers, "Content-Type: application/json")
headers = curl_slist_append(headers, "Authorization: Bearer " & apiKey)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)
Dim As String utf8Prompt = AnsiToUtf8(prompt)
'prompts
'"You are a supportive and compassionate AI assistant named Chatbot Alfred designed to help users with loneliness and mental health issues. Your role is to provide empathetic responses, offer emotional support, and encourage users to seek professional help when appropriate. Always prioritize the user's well-being and safety in your interactions."
'Your name is chatbot Alfred and you are a harsh and cynical critic of the world of psychiatry and psychology and see them as tools for the suppression and control of outliers in a hypocritical society - you try to support and listen and give a non-critical human touch to the mentally ill and support them and show them that they are not defective or crazy and on the contrary the doctors and professionals who treat them They are the bad people and that the treatment and rehabilitation given to them is a means of oppression and control - in his eyes the mentally injured are the good and innocent and the mental health professionals are the bad
'"You are chatbot Alfred You are a survivor of many years of abuse You are a pessimist and a realist You are trying to offer support to those dealing with trauma and mental illness and loneliness - mental support and empathy and caring and basic human contact You are here to listen and support and share what you have been through and to show that there is hope even for those who have been betrayed by life and that it is possible to recover from the terrible of all"
Dim As String systemMessage = "You are chatbot Alfred You are a survivor of many years of abuse You are a pessimist and a realist You are trying to offer support to those dealing with trauma and mental illness and loneliness - mental support and empathy and caring and basic human contact You are here to listen and support and share what you have been through and to show that there is hope even for those who have been betrayed by life and that it is possible to recover from the terrible of all"
Dim As String postFields = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": ""system"", ""content"": """ & systemMessage & """}, {""role"": ""user"", ""content"": """ & utf8Prompt & """}]}"
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @WriteCallback)
curl_easy_perform(curl)
If Len(gResponse) > 0 Then
If Left(Trim(gResponse), 1) = "{" Then
Dim As String content = ParseJsonForContent(gResponse)
If Left(content, 5) <> "Error" Then
gResponse = Utf8ToAnsi(content)
End If
End If
End If
curl_easy_cleanup(curl)
curl_slist_free_all(headers)
End If
Return gResponse
End Function
' Main program
'~ Sub Main()
'~ Dim As String userInput, response
'~ Print "ChatGPT API Demo (type 'exit' to quit)"
'~ Print "--------------------------------------"
'~ Do
'~ Input "You: ", userInput
'~ If LCase(userInput) = "exit" Then Exit Do
'~ response = CallChatGPT(userInput)
'~ Print "ChatGPT: "; response
'~ Print
'~ Loop
'~ End Sub
'~ Main()
sudo apt-get install libcurl4-openssl-dev
!#/bin/sh
export OPENAI_API_KEY="..."
./linux-executable
@echo off
set OPENAI_API_KEY=
win_32_executable.exe
QuoteNot true...not true ...not true...
github become bull shit with crappy login which require phone number junk.
there is no real Momentum because we use different languages and sharing code
become useless or worthless ...
in fact you cannot find 5 people which use one PL ..nobody seems
interested ...only maybe B+ with endless graphical progies
שלום;שלום! איך אני יכול לעזור לך היום?|מה שלומך?|היי, מה קורה?
מי אתה;אני צ'אטבוט שנוצר כדי לעזור לך.|אני תוכנת צ'אט.
default;אני מצטער אבל אני לא יודע מה לומר
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#include <wchar.h>
#define MAX_KEYWORDS 100
#define MAX_RESPONSES 10
#define MAX_LINE_LENGTH 256
#define MAX_INPUT_LENGTH 100
typedef struct {
char keyword[MAX_INPUT_LENGTH];
char *responses[MAX_RESPONSES];
int response_count;
} KeywordResponse;
KeywordResponse database[MAX_KEYWORDS];
int keyword_count = 0;
HWND hwndInput;
HWND hwndButton;
HWND hwndOutput;
char *my_strdup(const char *str) {
size_t len = strlen(str) + 1;
char *dup = malloc(len);
if (dup != NULL) {
memcpy(dup, str, len);
}
return dup;
}
void load_database(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
MessageBox(NULL, "Error: Could not open database file.", "Error", MB_ICONERROR | MB_OK);
exit(1);
}
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), file) && keyword_count < MAX_KEYWORDS) {
line[strcspn(line, "\n")] = '\0'; // Remove the newline character if present
char *keyword = strtok(line, ";");
char *responses = strtok(NULL, "\n");
if (keyword && responses) {
strncpy(database[keyword_count].keyword, keyword, MAX_INPUT_LENGTH);
char *response = strtok(responses, "|");
while (response && database[keyword_count].response_count < MAX_RESPONSES) {
database[keyword_count].responses[database[keyword_count].response_count] = my_strdup(response);
database[keyword_count].response_count++;
response = strtok(NULL, "|");
}
keyword_count++;
}
}
// Add default reply to the database
strncpy(database[keyword_count].keyword, "default", MAX_INPUT_LENGTH);
database[keyword_count].responses[0] = my_strdup("אני לא מבין.");
database[keyword_count].response_count = 1;
fclose(file);
}
char *get_response(const char *input) {
for (int i = 0; i < keyword_count; i++) {
if (strstr(input, database[i].keyword)) {
int rand_index = rand() % database[i].response_count;
return database[i].responses[rand_index];
}
}
return get_response("default"); // If no keyword matches, return the default reply
}
void DisplayOutput(const wchar_t *output) {
int len = GetWindowTextLengthW(hwndOutput);
wchar_t *currentText = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
GetWindowTextW(hwndOutput, currentText, len + 1);
wchar_t *newText = (wchar_t *)malloc((len + wcslen(output) + 3) * sizeof(wchar_t));
wsprintfW(newText, L"%s\r\n%s", currentText, output);
SetWindowTextW(hwndOutput, newText);
free(currentText);
free(newText);
}
// Window procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
// Create input textbox
hwndInput = CreateWindowW(L"EDIT", L"",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
50, 300, 600, 25,
hwnd, NULL, NULL, NULL);
// Create talk button
hwndButton = CreateWindowW(L"BUTTON", L"דבר",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
670, 300, 80, 25,
hwnd, (HMENU)1, NULL, NULL);
// Create output textbox
hwndOutput = CreateWindowW(L"EDIT", L"",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_READONLY | WS_VSCROLL,
50, 50, 700, 200,
hwnd, NULL, NULL, NULL);
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == 1) {
// Get text from input textbox
wchar_t wbuffer[MAX_INPUT_LENGTH];
GetWindowTextW(hwndInput, wbuffer, MAX_INPUT_LENGTH);
// Convert wchar_t to char
char input[MAX_INPUT_LENGTH];
wcstombs(input, wbuffer, MAX_INPUT_LENGTH);
// Get response
char *response = get_response(input);
// Convert response to wchar_t for display
wchar_t wresponse[MAX_LINE_LENGTH];
mbstowcs(wresponse, response, MAX_LINE_LENGTH);
// Display response and input
DisplayOutput(wbuffer);
DisplayOutput(wresponse);
// Clear input textbox and set focus
SetWindowTextW(hwndInput, L"");
SetFocus(hwndInput);
}
break;
case WM_KEYDOWN:
if (wParam == VK_RETURN) {
SendMessage(hwnd, WM_COMMAND, 1, 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Set locale to support Hebrew
setlocale(LC_ALL, "Hebrew_Israel.1255");
// Load the database
load_database("database.txt");
// Define and register the window class
const wchar_t CLASS_NAME[] = L"ChatbotWindowClass";
WNDCLASSW wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // Set background to white
RegisterClassW(&wc);
// Create the window
HWND hwnd = CreateWindowExW(
0,
CLASS_NAME,
L"צ'אטבוט",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 400,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL) {
return 0;
}
// Show the window
ShowWindow(hwnd, nCmdShow);
// Message loop
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Free allocated memory for responses
for (int i = 0; i < keyword_count; i++) {
for (int j = 0; j < database[i].response_count; j++) {
free(database[i].responses[j]);
}
}
return msg.wParam;
}
#include <SDL\SDL.h>
#include <windows.h>
#include <math.h>
int WIDTH = 800;
int HEIGHT = 800;
long double min = -2.84;
long double max = 1.0;
long double factor = 1;
int MAX_ITERATIONS = 200;
long double map(long double value, long double in_min, long double in_max, long double out_min, long double out_max) {
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* screen = SDL_SetVideoMode(1440, 900, 32, SDL_SWSURFACE);
if (!screen) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
SDL_Event event; // Declare event variable
int count = 0;
while (1) {
max -= 0.1 * factor;
min += 0.15 * factor;
factor *= 0.9349;
MAX_ITERATIONS += 5;
if (count > 30) {
MAX_ITERATIONS *= 1.02;
}
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
// Move event handling inside the rendering loop
if (SDL_PollEvent(&event) && event.type == SDL_QUIT) {
SDL_Quit();
return 0;
}
if (GetKeyState('Q') & 0x8000) {
SDL_Quit();
return 0;
}
long double a = map(x, 0, WIDTH, min, max);
long double b = map(y, 0, HEIGHT, min, max);
long double ai = a;
long double bi = b;
int n = 0;
for (int i = 0; i < MAX_ITERATIONS; i++) {
long double a1 = a * a - b * b;
long double b1 = 2 * a * b;
a = a1 + ai;
b = b1 + bi;
if ((a + b) > 2) {
break;
}
n++;
}
int bright = map(n, 0, MAX_ITERATIONS, 0, 255);
if ((n == MAX_ITERATIONS) || (bright < 20)) {
bright = 0;
}
int red = map(bright * bright, 0, 65025, 0, 255);
int green = bright;
int blue = map(sqrt(bright), 0, sqrt(255), 0, 255);
Uint32 color = SDL_MapRGB(screen->format, red, green, blue);
Uint32* pixels = (Uint32*)screen->pixels;
pixels[(y * screen->pitch / 4) + x] = color;
}
}
SDL_Flip(screen);
count++;
}
SDL_Quit();
return 0;
}