diff --git a/rpylib.c b/rpylib.c new file mode 100644 index 0000000..1451a00 --- /dev/null +++ b/rpylib.c @@ -0,0 +1,76 @@ +/* Written by retoor@molodetz.nl */ + +/* +This C extension for Python provides a simple API for communication with an OpenAI service. +It includes functions to return a "Hello World" string, conduct a chat session through OpenAI, and reset the message history. +*/ + +/* +Summary of used imports: +- <Python.h>: Includes necessary Python headers to create a C extension. +- "openai.h": Assumes an external library for OpenAI interaction. +*/ + +/* +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#define PY_SSIZE_T_CLEAN +#include <Python.h> +#include "openai.h" + +static PyObject* mymodule_hello(PyObject *self, PyObject *args) { + return PyUnicode_FromString("Hello, World from C!"); +} + +static PyObject* rpylib_reset(PyObject *self, PyObject *args) { + return PyUnicode_FromString("True"); +} + +static PyObject* rpylib_chat(PyObject *self, PyObject *args) { + const char *role, *message; + if (!PyArg_ParseTuple(args, "ss", &role, &message)) { + return NULL; + } + char *result = openai_chat(role, message); + PyObject *py_result = PyUnicode_FromString(result); + free(result); + return py_result; +} + +static PyMethodDef MyModuleMethods[] = { + {"hello", mymodule_hello, METH_NOARGS, "Returns a Hello World string"}, + {"chat", rpylib_chat, METH_VARARGS, "Chat with OpenAI."}, + {"reset", rpylib_reset, METH_NOARGS, "Reset message history."}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef rpylib = { + PyModuleDef_HEAD_INIT, + "rpylib", + "A simple C extension module for Python", + -1, + MyModuleMethods +}; + +PyMODINIT_FUNC PyInit_rpylib(void) { + return PyModule_Create(&rpylib); +} \ No newline at end of file