Understanding piping

I've been looking for nearly a week for an API to do simple piping that I'm sure is out there. What I have is a simple home assistant project, in the event the home assistant fails to find a suitable solution to user input, it passes the user input to Ollama.

For instance:
User: "Turn on Living room lights"
Assistant: (turns on lights)

User: "What's the population of Tokyo?"
Assistant: (pass to AI)

The API (that HAS to exist somewhere) would be some version of this:

//
string to_AI = ""What's the population of Tokyo?";
pid_t pid = FunctionIAlreadyHaveToGetPID("ollama");

SendToProcessRunningInAnotherTerminal(pid, to_AI); // <---- This!


//

Does anybody know a library to do such a thing or similar?

I don't need to get output from Ollama, since speed is not of the essence Ollama can just log, and I can grab the last line of the log and pull it in as a string.

Thanks for taking the time to read, hopefully somebody can point me in the right direction...
Running Ubuntu(ish)
Looks like you want a FIFO, aka "named pipe", for inter-process communication (IPC).

Create the FIFO file (in the shell):
mkfifo /tmp/myfifo

Reader process:
1
2
3
4
const char *fifo = "/tmp/myfifo";
int fd = open(fifo, O_RDONLY);   // blocks until writer opens
char buf[128];
int n = read(fd, buf, sizeof(buf));

Writer process:
1
2
3
4
const char *fifo = "/tmp/myfifo";
int fd = open(fifo, O_WRONLY);   // blocks until reader opens
write(fd, "Hello from C\n", 13);
close(fd);


Note: A FIFO (named pipe) is a one-way communication channel, but you could create two (or more) of them, if you need the communication to work in both directions.

See also:
https://www.geeksforgeeks.org/cpp/named-pipe-fifo-example-c-program/

______


If a FIFO is too much limited for your use case, look into Unix Domain Sockets:
https://en.wikipedia.org/wiki/Unix_domain_socket

This essentially allows you to use the socket (network) API for inter-process communication.
Last edited on
WHEW!!!

I got it with ollama.hpp! It works with Ollama-server if anybody finds this looking for the same thing.

ollama.hpp actually sends and returns in one line so it's more convenient than what I had planned.

I'll be having offline verbal conversations with my computer on my next day off...



Thank you for your help though kigar64551!
Topic archived. No new replies allowed.