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:
@@ -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}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user