Compare commits
3 Commits
7e10be7821
...
bb1c1667c7
Author | SHA1 | Date | |
---|---|---|---|
bb1c1667c7 | |||
eab4e4d7f1 | |||
a2eccb4e5a |
5
Makefile
5
Makefile
@ -1,5 +1,10 @@
|
|||||||
all: build run
|
all: build run
|
||||||
|
|
||||||
|
|
||||||
|
build_rpylib:
|
||||||
|
gcc -shared -o rpylib.so -fPIC rpylib.c -lpython3.12 `python3-config --includes` -I/usr/include/CL -ljson-c -lcurl
|
||||||
|
|
||||||
|
|
||||||
build:
|
build:
|
||||||
# -lpython3.14 -I/usr/include/python3.14
|
# -lpython3.14 -I/usr/include/python3.14
|
||||||
gcc main.c -Ofast -o r -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm
|
gcc main.c -Ofast -o r -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm
|
||||||
|
1
auth.h
1
auth.h
@ -22,7 +22,6 @@ const char *resolve_api_key() {
|
|||||||
if (api_key) {
|
if (api_key) {
|
||||||
return api_key;
|
return api_key;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\nThere is no API key configured in the environment.\n");
|
|
||||||
api_key = "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";
|
api_key = "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";
|
||||||
return api_key;
|
return api_key;
|
||||||
}
|
}
|
||||||
|
13
messages.h
13
messages.h
@ -24,15 +24,22 @@ struct json_object *message_list() {
|
|||||||
}
|
}
|
||||||
return message_array;
|
return message_array;
|
||||||
}
|
}
|
||||||
|
bool messages_remove_last() {
|
||||||
void messages_remove_last() {
|
|
||||||
struct json_object *messages = message_list();
|
struct json_object *messages = message_list();
|
||||||
int size = json_object_array_length(messages);
|
int size = json_object_array_length(messages);
|
||||||
if (size) {
|
if (size) {
|
||||||
json_object_array_del_idx(messages, size - 1, 1);
|
json_object_array_del_idx(messages, size - 1, 1);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void messages_remove(){
|
||||||
|
while(messages_remove_last())
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
struct json_object *message_add_tool_call(struct json_object *message) {
|
struct json_object *message_add_tool_call(struct json_object *message) {
|
||||||
struct json_object *messages = message_list();
|
struct json_object *messages = message_list();
|
||||||
json_object_array_add(messages, message);
|
json_object_array_add(messages, message);
|
||||||
@ -75,4 +82,4 @@ void message_free() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
76
rpylib.c
Normal file
76
rpylib.c
Normal file
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user