CoreFlow 1.0.0
A modern orchestration and execution runtime
Loading...
Searching...
No Matches
chatbot_sample.cpp

Interactove Sample with AI Chatbot.

Interactove Sample with AI Chatbot

Version
0.1
Date
2025-01-20
#include <cstdlib>
#include <iostream>
#include <string>
#include <COREFLOW/all.hpp>
using namespace coreflow;
int main()
{
std::cout << "AI Chatbot - Type 'q' or 'quit' or 'exit' to stop" << std::endl;
std::cout << "==========================================" << std::endl;
// Create context
auto context = Context::createContext();
if (Error::getStatus(context) != VX_SUCCESS)
{
std::cerr << "Failed to create Context" << std::endl;
return EXIT_FAILURE;
}
// Create graph
auto graph = Graph::createGraph(context);
{
std::cerr << "Failed to create Graph" << std::endl;
return EXIT_FAILURE;
}
// Create input string array
auto input_string = Array::createArray(context, VX_TYPE_CHAR, VX_MAX_STRING_LENGTH);
if (Error::getStatus(input_string) != VX_SUCCESS)
{
std::cerr << "Failed to create input string array" << std::endl;
return EXIT_FAILURE;
}
// Create output string array
auto output_string = Array::createArray(context, VX_TYPE_CHAR, VX_MAX_STRING_LENGTH);
if (Error::getStatus(output_string) != VX_SUCCESS)
{
std::cerr << "Failed to create output string array" << std::endl;
return EXIT_FAILURE;
}
// Get AI chatbot kernel
if (Error::getStatus(kernel) != VX_SUCCESS)
{
std::cerr << "Failed to get AI chatbot kernel. Make sure AI server target is loaded." << std::endl;
return EXIT_FAILURE;
}
// Create node
auto node = Node::createNode(graph, kernel, {input_string, output_string});
{
std::cerr << "Failed to create chatbot node" << std::endl;
return EXIT_FAILURE;
}
// Main conversation loop
std::string query;
while (true)
{
std::cout << "\nYou: ";
std::getline(std::cin, query);
// Check for exit commands
if (query == "quit" || query == "exit" || query == "q")
{
std::cout << "Goodbye!" << std::endl;
break;
}
if (query.empty())
{
std::cout << "Please enter a question or type 'quit' to exit." << std::endl;
continue;
}
// Clear previous input from array
if (input_string->truncate(0) != VX_SUCCESS)
{
std::cerr << "Failed to clear input array" << std::endl;
continue;
}
// Clear previous output
if (output_string->truncate(0) != VX_SUCCESS)
{
std::cerr << "Failed to clear output array" << std::endl;
continue;
}
// Add new query to the input array
if (input_string->addItems(query.length() + 1, query.c_str(), sizeof(char)) != VX_SUCCESS)
{
std::cerr << "Failed to add input query to array" << std::endl;
continue;
}
// Process graph
if (graph->process() != VX_SUCCESS)
{
std::cerr << "Failed to process graph" << std::endl;
continue;
}
// Read output
auto num_items = output_string->numItems();
if (num_items > 0)
{
void *data_ptr = nullptr;
size_t stride = 0;
if (output_string->accessArrayRange(0, num_items, &stride, &data_ptr, VX_READ_ONLY) == VX_SUCCESS)
{
// Direct access to the underlying string data - no copying needed!
const char *response = static_cast<const char*>(data_ptr);
std::cout << "AI: " << std::string(response, num_items) << std::endl;
}
else
{
std::cerr << "Failed to access output data" << std::endl;
}
}
else
{
std::cout << "AI: No response received from AI server" << std::endl;
}
}
return EXIT_SUCCESS;
}
CoreVX single-include header for C++ development.
int main()
Definition blur_pipeline.cpp:15
@ VX_TYPE_CHAR
A vx_char.
Definition vx_types.h:435
@ VX_SUCCESS
No error.
Definition vx_types.h:543
@ VX_READ_ONLY
The memory shall be treated by the system as if it were read-only. If the User writes to this memory,...
Definition vx_types.h:1515
@ VX_KERNEL_AIS_CHATBOT
The AI Model Server Chatbot kernel.
Definition vx_corevx_ext.h:243
static vx_array createArray(vx_context context, vx_enum item_type, vx_size capacity, vx_bool is_virtual=vx_false_e, vx_enum type=VX_TYPE_ARRAY)
Create a Array object.
static vx_context createContext()
Create a new context.
static vx_status getStatus(vx_reference ref)
Provides a generic API to return status values from Object constructors if they fail.
static vx_graph createGraph(vx_context context)
Create a graph.
static vx_kernel getKernelByEnum(vx_context context, vx_enum kernelenum)
Get the Kernel By Enum.
static vx_node createNode(vx_graph graph, vx_kernel kernel)
Create a new node.
The internal representation of a vx_array.
Definition vx_array.h:34