Enhance Makefile and rpylib.c:

- Updated Makefile to streamline build process for main application and Python extension.
- Implemented C extension in rpylib.c for OpenAI communication, including chat and reset functionalities.
This commit is contained in:
retoor 2025-03-20 04:13:26 +01:00
parent c645645155
commit 71606a6c22
2 changed files with 26 additions and 6 deletions

View File

@ -1,4 +1,4 @@
all: build run
all: build build_rpylib run
# Variables for compiler and flags
CC = gcc
@ -9,12 +9,9 @@ build:
$(CC) main.c $(CFLAGS) -o r
publish r
build_free:
$(CC) main.c -DFREE_VERSION $(CFLAGS) -o rf
publish rf
build_rpylib:
$(CC) -shared -o rpylib.so -fPIC rpylib.c -lpython3.12 `python3-config --includes` -I/usr/include/CL -ljson-c -lcurl
publish rpylib.so
run:
./r

View File

@ -51,9 +51,32 @@ static PyObject* rpylib_chat(PyObject *self, PyObject *args) {
free(result);
return py_result;
}
static PyObject* rpylib_prompt(PyObject *self, PyObject *args) {
const char *role = "user";
const char *message;
if (!PyArg_ParseTuple(args, "s", &message)) {
return NULL;
}
char *result = openai_chat(role, message);
PyObject *py_result = PyUnicode_FromString(result);
free(result);
return py_result;
}
static PyObject* rpylib_system(PyObject *self, PyObject *args) {
const char *role = "system";
const char *message;
if (!PyArg_ParseTuple(args, "s", &message)) {
return NULL;
}
char *result = openai_chat(role, message);
PyObject *py_result = PyUnicode_FromString(result);
free(result);
return py_result;
}
static PyMethodDef MyModuleMethods[] = {
{"chat", rpylib_chat, METH_VARARGS, "Chat with OpenAI."},
{"prompt", rpylib_prompt, METH_VARARGS, "Prompt to OpenAI."},
{"system", rpylib_system, METH_VARARGS, "Add system message."},
{"reset", rpylib_reset, METH_NOARGS, "Reset message history."},
{NULL, NULL, 0, NULL}
};