├── mbase ├── pc │ ├── pc_termination_handler.cpp │ ├── pc_stream_manager.cpp │ └── pc_program.cpp ├── inference │ ├── inf_client.cpp │ ├── inf_t2t_proc_diagnostics.cpp │ ├── inf_common.cpp │ └── inf_maip_peer_base.cpp └── mcp │ ├── mcp_server_stdio.cpp │ ├── mcp_server_client_stdio.cpp │ ├── mcp_server_features.cpp │ ├── mcp_client_server_stdio.cpp │ └── mcp_server_client_http_streamable.cpp ├── external └── double-conversion │ ├── .gitignore │ ├── SConscript │ ├── README.vendor │ ├── AUTHORS │ ├── BUILD.mk │ ├── LICENSE │ ├── double-conversion.h │ └── fixed-dtoa.h ├── favicon.ico ├── docs ├── source │ ├── favicon.png │ ├── core │ │ └── index.rst │ ├── inference │ │ ├── info-reference │ │ │ ├── on-sampling.rst │ │ │ ├── client-in-detail.rst │ │ │ ├── images │ │ │ │ ├── PSM-EXAMPLE-TIMING.png │ │ │ │ ├── chat_template_ex1.png │ │ │ │ └── gguf_file_content.png │ │ │ ├── inference-workflow.rst │ │ │ └── index.rst │ │ ├── quickstart │ │ │ ├── embeddings_ex │ │ │ │ ├── hf_download_step1.png │ │ │ │ ├── hf_download_step2.png │ │ │ │ ├── about.rst │ │ │ │ └── downloading_model.rst │ │ │ ├── single_prompt_ex │ │ │ │ ├── hf_download_step1.png │ │ │ │ ├── hf_download_step2.png │ │ │ │ ├── about.rst │ │ │ │ └── downloading_model.rst │ │ │ ├── project_setup.rst │ │ │ └── dialogue_ex │ │ │ │ ├── about.rst │ │ │ │ └── context_lines.rst │ │ ├── links.rst │ │ ├── programs │ │ │ ├── index.rst │ │ │ ├── typo-fixer │ │ │ │ └── about.rst │ │ │ ├── retrieval │ │ │ │ └── about.rst │ │ │ └── embedding │ │ │ │ └── about.rst │ │ └── quick_start.rst │ ├── std │ │ └── index.rst │ ├── mcp │ │ ├── info-reference │ │ │ └── mcp_object_diagram.png │ │ ├── information_reference.rst │ │ ├── quickstart │ │ │ ├── what_now.rst │ │ │ └── project_setup.rst │ │ └── quick_start.rst │ ├── setting-up │ │ └── about.rst │ ├── program_identification.rst │ ├── copyright.rst │ ├── conf.py │ └── index.rst ├── maip │ ├── multiple_keys.png │ ├── protocol_model.png │ ├── packet_schematic.png │ ├── standard_start.html │ ├── framework_start.html │ ├── general.css │ └── navig_generator.js ├── program_doc_template.rst ├── Makefile └── make.bat ├── .gitmodules ├── python ├── pyproject.toml ├── mbasepy │ ├── __init__.py │ └── _core.cpp ├── setup.py └── README.md ├── scripts ├── __pycache__ │ ├── maip_builder.cpython-312.pyc │ ├── maip_context.cpython-312.pyc │ ├── maip_resolver.cpython-312.pyc │ └── maip_operation_object.cpython-312.pyc ├── maip_operation_object.py └── maip_builder.py ├── .gitignore ├── description.txt ├── RELEASE.md ├── include └── mbase │ ├── mbase.h │ ├── inference │ ├── inf_context_line.h │ ├── inf_t2t_proc_diagnostics.h │ ├── inf_embedder_client.h │ ├── inf_client.h │ ├── inf_sampling_set.h │ ├── inf_maip_peer_base.h │ ├── inf_maip_peer_t2t.h │ ├── inf_device_desc.h │ ├── inf_common.h │ ├── inf_common.h.in │ ├── inf_t2t_client.h │ └── inf_maip_server.h │ ├── mcp │ ├── mcp_server_stdio.h │ ├── mcp_server_client_stdio.h │ ├── mcp_server_arguments.h │ ├── mcp_server_descriptions.h │ ├── mcp_client_server_stdio.h │ ├── mcp_client_arguments.h │ ├── mcp_server_client_http_streamable.h │ ├── mcp_client_server_http.h │ ├── mcp_notifications.h │ ├── mcp_common.h │ ├── mcp_server_responses.h │ ├── mcp_client_callbacks.h │ ├── mcp_server_to_client_requests.h │ ├── mcp_server_features.h │ ├── mcp_server_http_streamable.h │ └── mcp_server_client_state.h │ ├── framework │ ├── object_watcher.h │ ├── handler_base.h │ └── event_handler.h │ ├── wsa_init.h │ ├── pc │ ├── pc_termination_handler.h │ ├── pc_stream_manager.h │ ├── pc_program.h │ ├── pc_diagnostics.h │ └── pc_config.h │ ├── mbase_std.h │ ├── platform.h │ ├── argument_get_value.h │ ├── synchronization.h │ ├── algorithm.h │ ├── http_parser.h │ └── behaviors.h ├── legal ├── cuda_redis_disclaimer └── LICENSE-full ├── cmake ├── mbase_lib_cmake_template ├── MBASEFrameworkSys.cmake ├── MBASECoreSys.cmake ├── MBASEJsonSys.cmake └── mbase_packaging.cmake ├── examples ├── retrieval │ └── CMakeLists.txt ├── typo-fixer │ └── CMakeLists.txt ├── benchmark │ └── CMakeLists.txt ├── embedding │ └── CMakeLists.txt ├── simple-conversation │ └── CMakeLists.txt └── openai-server │ ├── global_state.h │ ├── CMakeLists.txt │ ├── input_list.txt │ ├── processor.h │ ├── model.h │ ├── openai_errors.h │ ├── processor.cpp │ └── client.h ├── .github └── workflows │ ├── docs_deploy.yml │ ├── docs_build.yml │ ├── build.yml │ └── release.yml ├── cmake_config_in ├── mbase.libs-config.cmake.in ├── mbase-mcp-config.cmake.in ├── mbase-pc-config.cmake.in ├── mbase-json-config.cmake.in ├── mbase-std-config.cmake.in └── mbase-inference-config.cmake.in ├── LICENSE ├── SECURITY.md ├── README.md └── CMakeLists.txt /mbase/pc/pc_termination_handler.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /external/double-conversion/.gitignore: -------------------------------------------------------------------------------- 1 | *.os 2 | -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/favicon.ico -------------------------------------------------------------------------------- /docs/source/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/favicon.png -------------------------------------------------------------------------------- /docs/source/core/index.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | About Core 3 | ========== 4 | 5 | Work in progress... -------------------------------------------------------------------------------- /docs/maip/multiple_keys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/maip/multiple_keys.png -------------------------------------------------------------------------------- /docs/maip/protocol_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/maip/protocol_model.png -------------------------------------------------------------------------------- /docs/maip/packet_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/maip/packet_schematic.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "llama.cpp"] 2 | path = llama.cpp 3 | url = https://github.com/ggerganov/llama.cpp.git 4 | -------------------------------------------------------------------------------- /docs/source/inference/info-reference/on-sampling.rst: -------------------------------------------------------------------------------- 1 | ============== 2 | About Sampling 3 | ============== 4 | 5 | -------------------------------------------------------------------------------- /python/pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | -------------------------------------------------------------------------------- /docs/source/inference/info-reference/client-in-detail.rst: -------------------------------------------------------------------------------- 1 | ======================= 2 | Client Object in Detail 3 | ======================= -------------------------------------------------------------------------------- /docs/source/std/index.rst: -------------------------------------------------------------------------------- 1 | ====================== 2 | About Standard Library 3 | ====================== 4 | 5 | Work in progress... -------------------------------------------------------------------------------- /python/mbasepy/__init__.py: -------------------------------------------------------------------------------- 1 | from ._core import get_sys_name_total, cosine_similarity 2 | __all__ = ["get_sys_name_total", "cosine_similarity"] 3 | -------------------------------------------------------------------------------- /scripts/__pycache__/maip_builder.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/scripts/__pycache__/maip_builder.cpython-312.pyc -------------------------------------------------------------------------------- /scripts/__pycache__/maip_context.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/scripts/__pycache__/maip_context.cpython-312.pyc -------------------------------------------------------------------------------- /mbase/inference/inf_client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MBASE_BEGIN 4 | 5 | InfClientBase::~InfClientBase(){} 6 | 7 | MBASE_END -------------------------------------------------------------------------------- /scripts/__pycache__/maip_resolver.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/scripts/__pycache__/maip_resolver.cpython-312.pyc -------------------------------------------------------------------------------- /docs/source/mcp/info-reference/mcp_object_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/mcp/info-reference/mcp_object_diagram.png -------------------------------------------------------------------------------- /scripts/__pycache__/maip_operation_object.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/scripts/__pycache__/maip_operation_object.cpython-312.pyc -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/ 2 | .vscode/ 3 | out/ 4 | build/ 5 | build_cpu/ 6 | build_cuda/ 7 | docs/build/ 8 | scripts/__pycache__ 9 | change_log.txt 10 | output/ 11 | .DS_Store -------------------------------------------------------------------------------- /description.txt: -------------------------------------------------------------------------------- 1 | MBASE SDK is a set of libraries, tools and technologies primarily developed for 2 | bulding high-performance non-blocking applications with easy LLM integration. -------------------------------------------------------------------------------- /docs/source/inference/info-reference/images/PSM-EXAMPLE-TIMING.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/info-reference/images/PSM-EXAMPLE-TIMING.png -------------------------------------------------------------------------------- /docs/source/inference/info-reference/images/chat_template_ex1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/info-reference/images/chat_template_ex1.png -------------------------------------------------------------------------------- /docs/source/inference/info-reference/images/gguf_file_content.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/info-reference/images/gguf_file_content.png -------------------------------------------------------------------------------- /docs/source/inference/quickstart/embeddings_ex/hf_download_step1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/quickstart/embeddings_ex/hf_download_step1.png -------------------------------------------------------------------------------- /docs/source/inference/quickstart/embeddings_ex/hf_download_step2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/quickstart/embeddings_ex/hf_download_step2.png -------------------------------------------------------------------------------- /docs/source/inference/quickstart/single_prompt_ex/hf_download_step1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/quickstart/single_prompt_ex/hf_download_step1.png -------------------------------------------------------------------------------- /docs/source/inference/quickstart/single_prompt_ex/hf_download_step2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emreerdog/mbase/HEAD/docs/source/inference/quickstart/single_prompt_ex/hf_download_step2.png -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # MBASE SDK Release 2 | 3 | ## v0.3.1 Latest - 2025-06-26 4 | 5 | ### SDK Fix 6 | 7 | - MBASE server application now working on windows fixed. 8 | 9 | - important fix on processor `update_t` loop 1fc6b66 10 | 11 | ### SDK Added 12 | -------------------------------------------------------------------------------- /docs/source/inference/links.rst: -------------------------------------------------------------------------------- 1 | .. _llama.cpp: https://github.com/ggerganov/llama.cpp 2 | .. _mbase_main: https://github.com/Emreerdog/mbase 3 | .. _llama.cpp.cuda: https://github.com/ggerganov/llama.cpp/blob/master/docs/build.md#cuda 4 | .. _ggml: https://github.com/ggerganov/ggml/tree/master -------------------------------------------------------------------------------- /docs/source/setting-up/about.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | Setting-Up 3 | ========== 4 | 5 | Method 1: :doc:`Compiling from source ` 6 | 7 | Method 2: :doc:`Download binaries ` 8 | 9 | .. toctree:: 10 | :hidden: 11 | 12 | Compiling From Source 13 | Download Binaries -------------------------------------------------------------------------------- /docs/source/program_identification.rst: -------------------------------------------------------------------------------- 1 | * Author: |pi_author| 2 | * Maintainer: |pi_maintainer| 3 | * Email: |pi_email| 4 | * Name: |pi_name| 5 | * Version: |pi_version| 6 | * Type: |pi_type| 7 | * Network Usage: |pi_net_usage| 8 | * Lib Depends: |pi_lib_depends| 9 | * Repo location: |pi_repo_location| 10 | -------------------------------------------------------------------------------- /mbase/inference/inf_t2t_proc_diagnostics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MBASE_BEGIN 4 | 5 | InfProcT2TDiagnostics::InfProcT2TDiagnostics(): 6 | loadTimeInMilliseconds(0), 7 | ppTokensPerSecond(0), 8 | evalTokensPerSecond(0) 9 | { 10 | } 11 | 12 | MBASE_END -------------------------------------------------------------------------------- /external/double-conversion/SConscript: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | double_conversion_sources = [ 3 | 'bignum.cc', 4 | 'bignum-dtoa.cc', 5 | 'cached-powers.cc', 6 | 'double-to-string.cc', 7 | 'fast-dtoa.cc', 8 | 'fixed-dtoa.cc', 9 | 'string-to-double.cc', 10 | 'strtod.cc' 11 | ] 12 | Return('double_conversion_sources') 13 | -------------------------------------------------------------------------------- /scripts/maip_operation_object.py: -------------------------------------------------------------------------------- 1 | class MaipOperationResult : 2 | def __init__(self, in_data = None, in_result_code = 0) -> None: 3 | self.result_code = in_result_code 4 | self.result_data = in_data 5 | 6 | def get_result_code(self): 7 | return self.result_code 8 | def get_result_data(self): 9 | return self.result_data -------------------------------------------------------------------------------- /include/mbase/mbase.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_H 2 | #define MBASE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #endif // !MBASE_H 12 | -------------------------------------------------------------------------------- /external/double-conversion/README.vendor: -------------------------------------------------------------------------------- 1 | DESCRIPTION 2 | 3 | Converts floating point numbers to strings 4 | 5 | ORIGIN 6 | 7 | git@github.com:google/double-conversion.git 8 | 75b48d66ac835da2c1678926f7d61d6cb2992922 9 | Tue May 21 10:45:11 2024 +0200 10 | 11 | LICENSE 12 | 13 | BSD-3 14 | 15 | LOCAL MODIFICATIONS 16 | 17 | - Ignored compiler warning 18 | -------------------------------------------------------------------------------- /legal/cuda_redis_disclaimer: -------------------------------------------------------------------------------- 1 | This software includes NVIDIA CUDA components – specifically, cudart64_12.dll, cublas64_12.dll, and cublasLt64_12.dll – which are redistributed under the terms of the NVIDIA CUDA Toolkit End User License Agreement. These files are provided “as is” and must not be modified or stripped of their proprietary notices. For full details of the license terms, please refer to the NVIDIA CUDA EULA available at: 2 | https://docs.nvidia.com/cuda/eula/index.html -------------------------------------------------------------------------------- /cmake/mbase_lib_cmake_template: -------------------------------------------------------------------------------- 1 | ### 2 | 3 | For this example, we are assuming the given library name: 4 | 5 | Library name: foo 6 | 7 | --- Header-Only --- 8 | 9 | include_guard(GLOBAL) 10 | 11 | include(MBASECommonConfig) # Including common cmake configuration variables 12 | include(MBASEStdSys) # Including standard library 13 | # Or more if necessary 14 | 15 | set(MBASE_FOO_SYS_STRING "MBASE Foo") 16 | set(MBASE_FOO_LIB_NAME "foo") 17 | 18 | ### TODO, WRITE THE REST 19 | 20 | ### -------------------------------------------------------------------------------- /examples/retrieval/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_retrieval retrieval.cpp) 2 | 3 | target_compile_definitions(mbase_retrieval PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 4 | target_compile_options(mbase_retrieval PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 5 | target_include_directories(mbase_retrieval PUBLIC mb_inference) 6 | target_link_libraries(mbase_retrieval PRIVATE ${MBASE_STD_LIBS} mb_inference mb_json) 7 | 8 | install(TARGETS mbase_retrieval RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -------------------------------------------------------------------------------- /include/mbase/inference/inf_context_line.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_CONTEXT_LINE_H 2 | #define MBASE_CONTEXT_LINE_H 3 | 4 | #include 5 | #include 6 | 7 | MBASE_BEGIN 8 | 9 | enum class context_role { 10 | SYSTEM, 11 | ASSISTANT, 12 | USER, 13 | NONE 14 | }; 15 | 16 | struct context_line { 17 | context_role mRole = context_role::NONE; 18 | mbase::string mMessage = ""; 19 | U32 mMessageIndex; 20 | }; 21 | 22 | MBASE_END 23 | 24 | #endif // !MBASE_CONTEXT_LINE_H 25 | -------------------------------------------------------------------------------- /examples/typo-fixer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_typo_fixer typo-fixer.cpp) 2 | 3 | target_compile_definitions(mbase_typo_fixer PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 4 | target_compile_options(mbase_typo_fixer PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 5 | target_include_directories(mbase_typo_fixer PUBLIC mb_pc mb_inference) 6 | target_link_libraries(mbase_typo_fixer PRIVATE ${MBASE_STD_LIBS} mb_pc mb_inference llama) 7 | 8 | install(TARGETS mbase_typo_fixer RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -------------------------------------------------------------------------------- /python/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, Extension 2 | from pathlib import Path 3 | import sys 4 | 5 | # Simple setuptools build using Python C API 6 | 7 | mbase_include = Path(__file__).resolve().parent.parent / 'include' 8 | 9 | ext = Extension( 10 | 'mbasepy._core', 11 | sources=['mbasepy/_core.cpp'], 12 | include_dirs=[str(mbase_include)], 13 | language='c++' 14 | ) 15 | 16 | setup( 17 | name='mbasepy', 18 | version='0.1.0', 19 | packages=['mbasepy'], 20 | ext_modules=[ext], 21 | ) 22 | -------------------------------------------------------------------------------- /.github/workflows/docs_deploy.yml: -------------------------------------------------------------------------------- 1 | name: MBASE Documentation Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - documents 7 | 8 | jobs: 9 | doc_build: 10 | runs-on: mbase-amd64-ubuntu-p4r4-1 11 | steps: 12 | - name: Checkout the documentation branch 13 | uses: actions/checkout@v4 14 | with: 15 | ref: ${{ github.ref }} 16 | 17 | - name: Build and deploy documents 18 | run: | 19 | sphinx-build -M html ./docs/source ./docs/build 20 | ~/docs_update.sh -------------------------------------------------------------------------------- /examples/benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_benchmark_t2t benchmark.cpp) 2 | 3 | target_compile_definitions(mbase_benchmark_t2t PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 4 | target_compile_options(mbase_benchmark_t2t PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 5 | target_include_directories(mbase_benchmark_t2t PUBLIC mb_pc mb_inference mb_json) 6 | target_link_libraries(mbase_benchmark_t2t PRIVATE ${MBASE_STD_LIBS} mb_pc mb_inference mb_json llama) 7 | 8 | install(TARGETS mbase_benchmark_t2t RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -------------------------------------------------------------------------------- /examples/embedding/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_embedding_simple embedding.cpp) 2 | 3 | target_compile_definitions(mbase_embedding_simple PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 4 | target_compile_options(mbase_embedding_simple PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 5 | target_include_directories(mbase_embedding_simple PUBLIC mb_inference mb_json) 6 | target_link_libraries(mbase_embedding_simple PRIVATE ${MBASE_STD_LIBS} mb_inference mb_json) 7 | 8 | install(TARGETS mbase_embedding_simple RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 9 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_t2t_proc_diagnostics.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_T2T_PROC_DIAGNOSTICS 2 | #define MBASE_INF_T2T_PROC_DIAGNOSTICS 3 | 4 | #include 5 | #include 6 | 7 | MBASE_BEGIN 8 | 9 | class MBASE_API InfProcT2TDiagnostics : public mbase::PcDiagnostics { 10 | public: 11 | InfProcT2TDiagnostics(); 12 | 13 | I64 loadTimeInMilliseconds; 14 | F32 ppTokensPerSecond; 15 | F32 evalTokensPerSecond; 16 | }; 17 | 18 | MBASE_END 19 | 20 | #endif // MBASE_INF_T2T_PROC_DIAGNOSTICS -------------------------------------------------------------------------------- /examples/simple-conversation/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_simple_conversation simple-conversation.cpp) 2 | 3 | target_compile_definitions(mbase_simple_conversation PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 4 | target_compile_options(mbase_simple_conversation PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 5 | target_include_directories(mbase_simple_conversation PUBLIC mb_pc mb_inference) 6 | target_link_libraries(mbase_simple_conversation PRIVATE ${MBASE_STD_LIBS} mb_pc mb_inference llama) 7 | 8 | install(TARGETS mbase_simple_conversation RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -------------------------------------------------------------------------------- /external/double-conversion/AUTHORS: -------------------------------------------------------------------------------- 1 | # Below is a list of people and organizations that have contributed 2 | # to the double-conversion project. Names should be added to the 3 | # list like so: 4 | # 5 | # Name/Organization 6 | 7 | Google Inc. 8 | Mozilla Foundation 9 | 10 | Jeff Muizelaar 11 | Mike Hommey 12 | Martin Olsson 13 | Kent Williams 14 | Elan Ruusamäe 15 | Colin Hirsch 16 | Zhenyi Peng 17 | -------------------------------------------------------------------------------- /docs/maip/standard_start.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | MBASE Standard Doc 7 | 8 | 9 | 10 |
11 |
12 |
13 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /docs/maip/framework_start.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | MBASE Framework Doc 7 | 8 | 9 | 10 |
11 |
12 |
13 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /docs/source/mcp/information_reference.rst: -------------------------------------------------------------------------------- 1 | ===================== 2 | Information Reference 3 | ===================== 4 | 5 | .. toctree:: 6 | 7 | MCP SDK Design 8 | MCP Server in Detail 9 | MCP Client in Detail 10 | MCP HTTP Server Details 11 | Sending/Reading Notifications 12 | Implementing Custom MCP Methods 13 | Overwriting the Dispatcher -------------------------------------------------------------------------------- /mbase/mcp/mcp_server_stdio.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | MBASE_BEGIN 5 | 6 | McpServerStdio::McpServerStdio(const mbase::string& in_server_name, const mbase::string& in_version_string) : McpServerBase(in_server_name, in_version_string, mcp_transport_method::STDIO), mSingleStdioClient(this) 7 | { 8 | //mSingleStdioClient.start_processor(); 9 | } 10 | 11 | McpServerStdio::~McpServerStdio() 12 | { 13 | this->stop_processor(); 14 | } 15 | 16 | GENERIC McpServerStdio::update_t() 17 | { 18 | mSingleStdioClient.update_t(); 19 | } 20 | 21 | MBASE_END -------------------------------------------------------------------------------- /.github/workflows/docs_build.yml: -------------------------------------------------------------------------------- 1 | name: Documentation Build 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - documents 7 | 8 | jobs: 9 | doc_build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout the documentation branch 13 | uses: actions/checkout@v4 14 | with: 15 | ref: ${{ github.ref }} 16 | 17 | - name: Build sphinx docs 18 | uses: ammaraskar/sphinx-action@master 19 | with: 20 | docs-folder: "docs/" 21 | pre-build-command: "pip install furo sphinx_design" 22 | build-command: "sphinx-build -M html ./source ./build" -------------------------------------------------------------------------------- /cmake_config_in/mbase.libs-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_LIBS_BIN_DIR "@PACKAGE_MBASE_LIBS_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_LIBS_LIB_DIR "@PACKAGE_MBASE_LIBS_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_LIBS_INCLUDE_DIRS "@PACKAGE_MBASE_LIBS_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_LIBS_NAME_DESCRIPTIVE "@MBASE_LIBS_LIB_NAME@ @MBASE_LIBS_VERSION@") 10 | set(MBASE_CURRENT_CONFIG_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) 11 | foreach(MBASE_LIB_COMPONENT ${mbase.libs_FIND_COMPONENTS}) 12 | include("${MBASE_CURRENT_CONFIG_DIRECTORY}/mbase-${MBASE_LIB_COMPONENT}-config.cmake") 13 | endforeach() 14 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_STDIO_H 2 | #define MBASE_MCP_SERVER_STDIO_H 3 | 4 | #include // McpServerBase 5 | #include // McpServerStdioClient 6 | 7 | MBASE_BEGIN 8 | 9 | class MBASE_API McpServerStdio : public mbase::McpServerBase { 10 | public: 11 | McpServerStdio(const mbase::string& in_server_name, const mbase::string& in_version_string); 12 | ~McpServerStdio(); 13 | GENERIC update_t() override; 14 | 15 | private: 16 | mbase::McpServerStdioClient mSingleStdioClient; 17 | }; 18 | 19 | MBASE_END 20 | 21 | #endif // MBASE_MCP_SERVER_STDIO_H -------------------------------------------------------------------------------- /docs/program_doc_template.rst: -------------------------------------------------------------------------------- 1 | =========== 2 | 3 | =========== 4 | 5 | ---------------------- 6 | Program Identification 7 | ---------------------- 8 | 9 | .. |pi_author| replace:: 10 | .. |pi_maintainer| replace:: 11 | .. |pi_email| replace:: 12 | .. |pi_name| replace:: 13 | .. |pi_version| replace:: 14 | .. |pi_type| replace:: 15 | .. |pi_net_usage| replace:: 16 | .. |pi_lib_depends| replace:: 17 | .. |pi_repo_location| replace:: 18 | 19 | .. include:: path_to_program_identification_rst -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # mbasepy 2 | 3 | This directory contains experimental Python bindings for the C++ MBASE SDK. 4 | 5 | `mbasepy` exposes a very small portion of the inference API to Python. The 6 | bindings are implemented using the Python C API and require the C++ sources to 7 | be built. Currently only `get_sys_name_total` and `cosine_similarity` from the 8 | inference library are available. 9 | 10 | To build a wheel locally: 11 | 12 | ```bash 13 | python -m pip install wheel 14 | python setup.py bdist_wheel 15 | ``` 16 | 17 | The build requires a valid MBASE C++ build environment. Missing dependencies 18 | (such as the `llama.cpp` submodule) will cause the build to fail. 19 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_client_stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_STDIO_CLIENT_H 2 | #define MBASE_MCP_SERVER_STDIO_CLIENT_H 3 | 4 | #include 5 | #include // McpServerClient 6 | 7 | MBASE_BEGIN 8 | 9 | class MBASE_API McpServerStdioClient : public mbase::McpServerClient { 10 | public: 11 | McpServerStdioClient(McpServerBase* in_server_instance); 12 | ~McpServerStdioClient(); 13 | GENERIC send_mcp_payload(const mbase::string& in_payload) override; 14 | GENERIC update_t() override; 15 | 16 | private: 17 | mbase::mutex mStdioMutex; 18 | }; 19 | 20 | MBASE_END 21 | 22 | #endif // MBASE_MCP_SERVER_STDIO_CLIENT_H -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_arguments.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_ARGUMENTS_H 2 | #define MBASE_MCP_SERVER_ARGUMENTS_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | enum class McpValueType { 9 | NUMBER, 10 | BOOL, 11 | STRING, 12 | ARRAY, 13 | JSON 14 | }; 15 | 16 | struct McpPromptArgument { 17 | mbase::string mArgumentName; 18 | mbase::string mDescription; 19 | mbase::vector mCompletionStrings; 20 | bool mIsRequired = false; 21 | }; 22 | 23 | struct McpToolArgument { 24 | mbase::string mArgumentName; 25 | mbase::string mDescription; 26 | McpValueType mArgType; 27 | bool mIsRequired = false; 28 | }; 29 | 30 | MBASE_END 31 | 32 | #endif // MBASE_MCP_SERVER_ARGUMENTS_H -------------------------------------------------------------------------------- /external/double-conversion/BUILD.mk: -------------------------------------------------------------------------------- 1 | #-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ 2 | #── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘ 3 | 4 | PKGS += DOUBLE_CONVERSION 5 | 6 | DOUBLE_CONVERSION_FILES := $(wildcard double-conversion/*) 7 | DOUBLE_CONVERSION_HDRS = $(filter %.h,$(DOUBLE_CONVERSION_FILES)) 8 | DOUBLE_CONVERSION_SRCS = $(filter %.cc,$(DOUBLE_CONVERSION_FILES)) 9 | DOUBLE_CONVERSION_OBJS = $(DOUBLE_CONVERSION_SRCS:%.cc=o/$(MODE)/%.o) 10 | 11 | o/$(MODE)/double-conversion/double-conversion.a: $(DOUBLE_CONVERSION_OBJS) 12 | 13 | $(DOUBLE_CONVERSION_OBJS): double-conversion/BUILD.mk 14 | 15 | .PHONY: o/$(MODE)/double-conversion 16 | o/$(MODE)/double-conversion: o/$(MODE)/double-conversion/double-conversion.a 17 | -------------------------------------------------------------------------------- /include/mbase/framework/object_watcher.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OBJECT_WATCHER_H 2 | #define MBASE_OBJECT_WATCHER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | MBASE_BEGIN 9 | 10 | template 11 | struct object_watcher { 12 | IteratorType mItSelf; 13 | mutable TargetObject* mSubject = NULL; 14 | }; 15 | 16 | template 17 | struct list_object_watcher { 18 | typename mbase::list>::iterator mItSelf; 19 | mutable TargetObject* mSubject = NULL; 20 | }; 21 | 22 | template 23 | struct vector_object_watcher { 24 | typename mbase::list>::iterator mItSelf; 25 | mutable TargetObject* mSubject = NULL; 26 | }; 27 | 28 | MBASE_END 29 | 30 | #endif // MBASE_OBJECT_WATCHER_H -------------------------------------------------------------------------------- /docs/maip/general.css: -------------------------------------------------------------------------------- 1 | #flexContainer { 2 | display: grid; 3 | grid-template-columns: 4fr 1fr; 4 | grid-gap: 5px; 5 | margin-top: 0; 6 | } 7 | #textSection { 8 | /* box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); */ 9 | padding: 1%; 10 | } 11 | #pageNavigation { 12 | border: 1px solid rgba(0, 0, 0, .4); 13 | border-radius: 5px; 14 | padding-left: 4%; 15 | padding-bottom: 4%; 16 | padding-top: 2%; 17 | align-self: start; 18 | position: sticky; 19 | top: 1rem; 20 | visibility: hidden; 21 | 22 | } 23 | 24 | .stgroup { 25 | margin-bottom: 2em; 26 | } 27 | 28 | a:link { 29 | color: red; 30 | text-decoration: none; 31 | } 32 | 33 | a:hover { 34 | color: blue; 35 | text-decoration: underline; 36 | } 37 | 38 | 39 | a:visited { 40 | color: purple; 41 | text-decoration: none; 42 | } -------------------------------------------------------------------------------- /docs/source/inference/quickstart/embeddings_ex/about.rst: -------------------------------------------------------------------------------- 1 | ================== 2 | Embeddings Example 3 | ================== 4 | 5 | In this example, we will write a program that will read a user query and 6 | generate embeddings using an open-source LLM model. 7 | 8 | ------------------- 9 | What will you learn 10 | ------------------- 11 | 12 | After you finish this example, you will learn: 13 | 14 | * How to download and access to open-source embedder models. 15 | * Initializing and running embedder models. 16 | * How the embedder works in general. 17 | 18 | .. toctree:: 19 | :hidden: 20 | 21 | Acquiring Model 22 | Initializing Model 23 | Initializing Processor 24 | Initializing Client 25 | Preparing Input and Execution 26 | Complete Program -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_descriptions.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_DESCRIPTIONS_H 2 | #define MBASE_MCP_SERVER_DESCRIPTIONS_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | struct McpResourceDescription { 9 | mbase::string mUri; 10 | mbase::string mName; 11 | mbase::string mDescription; // Optional 12 | mbase::string mMimeType; // Optional 13 | mbase::SIZE_T mSize = 0; // Optional 14 | }; 15 | 16 | struct McpPromptDescription { 17 | mbase::string mName; 18 | mbase::string mDescription; // Optional 19 | mbase::vector mArguments; // Optional 20 | }; 21 | 22 | struct McpToolDescription { 23 | mbase::string mName; 24 | mbase::string mDescription; // Optional 25 | mbase::vector mArguments; // Optional 26 | }; 27 | 28 | MBASE_END 29 | 30 | #endif // MBASE_MCP_SERVER_DESCRIPTIONS_H -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_client_server_stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_CLIENT_SERVER_STDIO_H 2 | #define MBASE_MCP_CLIENT_SERVER_STDIO_H 3 | 4 | #include 5 | #include // McpServerStateBase 6 | #include // gStdin, gStdout 7 | #include // mbase::subprocess manager 8 | 9 | MBASE_BEGIN 10 | 11 | class MBASE_API McpClientServerStdio : public McpServerStateBase { 12 | public: 13 | McpClientServerStdio(const mbase::McpServerStdioInit& in_init); 14 | bool is_subprocess_alive() const noexcept; 15 | GENERIC send_mcp_payload(const mbase::string& in_payload) override; 16 | GENERIC update_t() override; 17 | private: 18 | bool mIsSubprocessAlive = false; 19 | mbase::subprocess mSubProcessManager; 20 | mbase::mutex writerSync; 21 | }; 22 | 23 | MBASE_END 24 | 25 | #endif // MBASE_MCP_CLIENT_SERVER_STDIO_H -------------------------------------------------------------------------------- /include/mbase/inference/inf_embedder_client.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_EMBEDDER_CLIENT_H 2 | #define MBASE_INF_EMBEDDER_CLIENT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | MBASE_BEGIN 9 | 10 | class MBASE_API InfClientEmbedder : public InfClientBase { 11 | public: 12 | /* ===== INTERFACE METHODS BEGIN ===== */ 13 | virtual GENERIC on_batch_processed(InfEmbedderProcessor* out_processor, const U32& out_proc_batch_length) = 0; 14 | virtual GENERIC on_write(InfEmbedderProcessor* out_processor, PTRF32 out_embeddings, const U32& out_cursor, bool out_is_finished) = 0; 15 | virtual GENERIC on_finish(InfEmbedderProcessor* out_processor, const size_type& out_total_processed_embeddings) = 0; 16 | /* ===== INTERFACE METHODS END ===== */ 17 | }; 18 | 19 | MBASE_END 20 | 21 | #endif // !MBASE_INF_EMBEDDER_CLIENT_H -------------------------------------------------------------------------------- /include/mbase/wsa_init.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_WSAINIT_H 2 | #define MBASE_WSAINIT_H 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | // Need to link with Ws2_32.lib 11 | #pragma comment (lib, "Ws2_32.lib") 12 | #pragma comment (lib, "Mswsock.lib") 13 | #pragma comment (lib, "AdvApi32.lib") 14 | 15 | MBASE_STD_BEGIN 16 | 17 | class wsa_initializer { 18 | public: 19 | wsa_initializer() noexcept { 20 | wsaInit = WSAStartup(MAKEWORD(2, 2), &wsaData); 21 | } 22 | 23 | ~wsa_initializer() noexcept { 24 | WSACleanup(); 25 | } 26 | 27 | MBASE_ND(MBASE_OBS_IGNORE) I32 get_init_result() const noexcept { 28 | return wsaInit; 29 | } 30 | 31 | private: 32 | WSADATA wsaData; 33 | int wsaInit; 34 | }; 35 | 36 | static wsa_initializer wsaInitializer; 37 | 38 | MBASE_STD_END 39 | 40 | #endif // !MBASE_WSAINIT_H -------------------------------------------------------------------------------- /include/mbase/pc/pc_termination_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_TERMINATION_HANDLER_H 2 | #define MBASE_TERMINATION_HANDLER_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | class PcTerminationHandler { 9 | public: 10 | virtual GENERIC on_termination() = 0; 11 | virtual GENERIC on_segfault() = 0; 12 | virtual GENERIC on_interrupt() = 0; 13 | virtual GENERIC on_invalidation() = 0; 14 | virtual GENERIC on_abort() = 0; 15 | }; 16 | 17 | class PcTerminationBasic : public PcTerminationHandler { 18 | public: 19 | 20 | GENERIC on_termination() override 21 | { 22 | 23 | } 24 | 25 | GENERIC on_segfault() override 26 | { 27 | 28 | } 29 | 30 | GENERIC on_interrupt() override 31 | { 32 | 33 | } 34 | 35 | GENERIC on_invalidation() override 36 | { 37 | 38 | } 39 | 40 | GENERIC on_abort() override 41 | { 42 | 43 | } 44 | }; 45 | 46 | MBASE_END 47 | 48 | #endif // !MBASE_TERMINATION_HANDLER_H 49 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_client.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_CLIENT_H 2 | #define MBASE_INF_CLIENT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | // inheriting from InfClientBase seemed unneccesary, so I removed it. 13 | 14 | class InfProcessorBase; 15 | 16 | class MBASE_API InfClientBase { 17 | public: 18 | using size_type = U64; 19 | 20 | /* ===== BUILDER METHODS BEGIN ===== */ 21 | virtual ~InfClientBase(); 22 | /* ===== BUILDER METHODS END ===== */ 23 | 24 | /* ===== INTERFACE METHODS BEGIN ===== */ 25 | virtual GENERIC on_register(InfProcessorBase* out_processor) = 0; 26 | virtual GENERIC on_unregister(InfProcessorBase* out_processor) = 0; 27 | /* ===== INTERFACE METHODS END ===== */ 28 | }; 29 | 30 | MBASE_END 31 | 32 | #endif // !MBASE_INF_CLIENT_H 33 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | 13 | %SPHINXBUILD% >NUL 2>NUL 14 | if errorlevel 9009 ( 15 | echo. 16 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 17 | echo.installed, then set the SPHINXBUILD environment variable to point 18 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 19 | echo.may add the Sphinx directory to PATH. 20 | echo. 21 | echo.If you don't have Sphinx installed, grab it from 22 | echo.https://www.sphinx-doc.org/ 23 | exit /b 1 24 | ) 25 | 26 | if "%1" == "" goto help 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /include/mbase/mbase_std.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_STD_H 2 | #define MBASE_STD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #ifdef MBASE_PLATFORM_WINDOWS 13 | #include 14 | #include 15 | #include 16 | #endif 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | //#include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #endif // !MBASE_STD_H 35 | -------------------------------------------------------------------------------- /examples/openai-server/global_state.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OPENAI_GLOBAL_STATE_H 2 | #define MBASE_OPENAI_GLOBAL_STATE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "model.h" 10 | 11 | MBASE_BEGIN 12 | 13 | struct mbase_openai_program_data { 14 | // Program Arguments 15 | mbase::string apiKey; 16 | mbase::string hostName = "127.0.0.1"; // --hostname || -h; 17 | mbase::string publicKeyFile; 18 | mbase::string privateKeyFile; 19 | int listenPort = 8080; // --port || -p 20 | mbase::Json jsonDescription; // -jsdesc 21 | 22 | // Program data 23 | mbase::vector programModels; 24 | bool serverListening = true; 25 | bool keyFileSet = false; 26 | bool customPortSet = false; 27 | mbase::PcDiagnostics diagnostic; 28 | }; 29 | 30 | MBASE_END 31 | 32 | inline mbase::mbase_openai_program_data gProgramData; 33 | 34 | #endif // MBASE_OPENAI_GLOBAL_STATE_H -------------------------------------------------------------------------------- /include/mbase/platform.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_PLATFORM_H 2 | #define MBASE_PLATFORM_H 3 | 4 | #include 5 | 6 | MBASE_STD_BEGIN 7 | 8 | #ifdef _WIN32 9 | 10 | /* SOCKET PROCS */ 11 | /* IO PROCS */ 12 | /* THREAD PROCS */ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #pragma comment (lib, "Ws2_32.lib") 19 | #pragma comment (lib, "Mswsock.lib") 20 | #pragma comment (lib, "AdvApi32.lib") 21 | 22 | class wsa_initializer { 23 | public: 24 | wsa_initializer() noexcept 25 | { 26 | wsaInit = WSAStartup(MAKEWORD(2, 2), &wsaData); 27 | } 28 | 29 | ~wsa_initializer() noexcept 30 | { 31 | WSACleanup(); 32 | } 33 | 34 | MBASE_ND(MBASE_RESULT_IGNORE) I32 get_init_result() const noexcept 35 | { 36 | return wsaInit; 37 | } 38 | 39 | private: 40 | WSADATA wsaData; 41 | int wsaInit; 42 | }; 43 | 44 | static wsa_initializer wsaInitializer; 45 | 46 | #endif 47 | 48 | MBASE_STD_END 49 | 50 | #endif // !MBASE_PLATFORM_H 51 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_client_arguments.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_CLIENT_ARGUMENTS_H 2 | #define MBASE_MCP_CLIENT_ARGUMENTS_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | using McpPromptMessageArgument = mbase::string; 9 | using McpPromptMessageMap = mbase::unordered_map; 10 | using McpToolMessageArgument = std::variant, std::map>; 11 | using McpToolMessageMap = mbase::unordered_map; 12 | 13 | struct McpServerStdioInit { 14 | mbase::string mServerName; 15 | mbase::string mCommand; 16 | mbase::vector mArguments = mbase::vector(); 17 | mbase::unordered_map mEnvironmentVariables; 18 | }; 19 | 20 | struct McpServerHttpInit { 21 | mbase::string mHostname; 22 | mbase::string mMcpEndpoint = "/mcp"; 23 | mbase::string mApiKey; 24 | }; 25 | 26 | MBASE_END 27 | 28 | #endif // MBASE_MCP_CLIENT_ARGUMENTS_H -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2025 MBASE Yazilim A.S. (https://www.mbasesoftware.com) 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /docs/source/inference/info-reference/inference-workflow.rst: -------------------------------------------------------------------------------- 1 | ============================= 2 | Inference Workflow in General 3 | ============================= 4 | 5 | .. important:: 6 | 7 | This page is incomplete. Make sure to follow the examples such as :doc:`../quickstart/single_prompt_ex/about`, :doc:`../quickstart/dialogue_ex/about` and :doc:`../quickstart/embeddings_ex/about`. 8 | 9 | The inference workflow is same and simple for all types of models including the ones that are not implemented yet. 10 | 11 | Here is the flow of operations user should employ to achieve non-blocking inference: 12 | 13 | - Initialize the model object properly. See :doc:`model-in-detail`, and :doc:`../quickstart/single_prompt_ex/initializing_model`. 14 | - Initialize the processor object properly. See :doc:`processor-in-detail`, and :doc:`../quickstart/single_prompt_ex/initializing_processor`. 15 | - Initialize the client object properly. See :doc:`client-in-detail` and :doc:`../quickstart/single_prompt_ex/initializing_client`. 16 | - Invoke their respective methods by employing the :doc:`parallel-state-machine` -------------------------------------------------------------------------------- /docs/source/copyright.rst: -------------------------------------------------------------------------------- 1 | ========= 2 | Copyright 3 | ========= 4 | 5 | *Copyright (c) 2025 MBASE Yazılım A.Ş. (https://www.mbasesoftware.com)* 6 | 7 | *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:* 8 | 9 | *The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.* 10 | 11 | *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.* 12 | -------------------------------------------------------------------------------- /cmake_config_in/mbase-mcp-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_MCP_BIN_DIR "@PACKAGE_MBASE_MCP_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_MCP_LIB_DIR "@PACKAGE_MBASE_MCP_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_MCP_INCLUDE_DIRS "@PACKAGE_MBASE_MCP_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_MCP_NAME_DESCRIPTIVE "MBASE @MBASE_MCP_LIB_NAME@ library @MBASE_MCP_VERSION@") 10 | 11 | find_library( 12 | MBASE_MCP_LIBRARIES 13 | NAMES mb_mcp 14 | HINTS ${MBASE_MCP_LIB_DIR} 15 | REQUIRED 16 | ) 17 | 18 | include(FindPackageHandleStandardArgs) 19 | find_package_handle_standard_args( 20 | MBASE_MCP 21 | DEFAULT_MSG 22 | MBASE_MCP_LIBRARIES 23 | MBASE_MCP_INCLUDE_DIRS 24 | ) 25 | 26 | if(MBASE_MCP_FOUND) 27 | find_package(mbase-json HINTS ${MBASE_CURRENT_CONFIG_DIRECTORY}) 28 | 29 | list(APPEND MBASE_MCP_LIBRARIES ${MBASE_JSON_LIBRARIES}) 30 | 31 | add_library(mbase-mcp INTERFACE) 32 | target_link_libraries(mbase-mcp INTERFACE ${MBASE_MCP_LIBRARIES}) 33 | target_include_directories(mbase-mcp INTERFACE ${MBASE_MCP_INCLUDE_DIRS}) 34 | 35 | set_property(TARGET mbase-mcp PROPERTY POSITION_INDEPENDENT_CODE ON) 36 | endif() -------------------------------------------------------------------------------- /docs/source/inference/quickstart/single_prompt_ex/about.rst: -------------------------------------------------------------------------------- 1 | =========================== 2 | About Single-Prompt Example 3 | =========================== 4 | 5 | In this example, we will write a program that will read user input (prompt in our context) 6 | from stdin and supply it to the LLM. Then, the LLM will give the response. 7 | 8 | It may look complex at first and the explanations may be dull. 9 | The reason for that is to keep the examples short and to the point. 10 | To get further explanation and better understanding refer to :doc:`../../info-reference/index` 11 | 12 | ------------------- 13 | What will you learn 14 | ------------------- 15 | 16 | After you finish this example, you will understand: 17 | 18 | * How to download and access to open-source models. 19 | * Initializing and running models. 20 | * How the MBASE inference works in general. 21 | 22 | .. toctree:: 23 | :hidden: 24 | 25 | Acquiring Model 26 | Initializing Model 27 | Initializing Processor 28 | Initializing Client 29 | Preparing Input and Execution 30 | Complete Program 31 | 32 | 33 | -------------------------------------------------------------------------------- /cmake_config_in/mbase-pc-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_PC_BIN_DIR "@PACKAGE_MBASE_PC_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_PC_LIB_DIR "@PACKAGE_MBASE_PC_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_PC_INCLUDE_DIRS "@PACKAGE_MBASE_PC_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_PC_NAME_DESCRIPTIVE "MBASE @MBASE_PC_LIB_NAME@ library @MBASE_PC_VERSION@") 10 | 11 | find_library( 12 | MBASE_PC_LIBRARIES 13 | NAMES mb_pc 14 | HINTS ${MBASE_PC_LIB_DIR} 15 | REQUIRED 16 | ) 17 | 18 | include(FindPackageHandleStandardArgs) 19 | find_package_handle_standard_args( 20 | MBASE_PC 21 | DEFAULT_MSG 22 | MBASE_PC_LIBRARIES 23 | MBASE_PC_INCLUDE_DIRS 24 | ) 25 | 26 | if(MBASE_PC_FOUND) 27 | find_package(mbase-std HINTS ${MBASE_CURRENT_CONFIG_DIRECTORY}) 28 | list(APPEND MBASE_PC_LIBRARIES ${MBASE_STD_LIBRARIES}) # In UNIX, there is a dependency on UUID library so, it is necessary 29 | add_library(mbase-pc INTERFACE) 30 | target_link_libraries(mbase-pc INTERFACE ${MBASE_PC_LIBRARIES}) 31 | target_include_directories(mbase-pc INTERFACE ${MBASE_PC_INCLUDE_DIRS}) 32 | 33 | set_property(TARGET mbase-pc PROPERTY POSITION_INDEPENDENT_CODE ON) 34 | endif() 35 | 36 | -------------------------------------------------------------------------------- /docs/source/inference/info-reference/index.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | Inference Information Reference 3 | =============================== 4 | 5 | This chapter is for understanding the inference SDK 6 | more in depth. 7 | 8 | Section contents are usually structured this way: 9 | 10 | 1. Giving a brief summary and remarks about the topic if the topic is universal and not specific to the inference SDK. 11 | 2. Giving a detailed explanation about the topic if the topic is specifically related to the inference SDK. 12 | 3. Showing how the inference SDK is related to the topic, useful objects and their locations and brief examples of how to use it. 13 | 4. Short SDK usage summary. 14 | 5. Header file synopsis. 15 | 16 | -------- 17 | Sections 18 | -------- 19 | 20 | .. toctree:: 21 | 22 | SDK Structure 23 | Parallel State Machine 24 | Inference Workflow in General 25 | Obtaining Hardware Information 26 | About GGUF Files 27 | Message Preparation 28 | Model Object in Detail 29 | Processor Object in Detail 30 | Client Object in Detail 31 | About Sampling 32 | -------------------------------------------------------------------------------- /cmake_config_in/mbase-json-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_JSON_BIN_DIR "@PACKAGE_MBASE_JSON_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_JSON_LIB_DIR "@PACKAGE_MBASE_JSON_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_JSON_INCLUDE_DIRS "@PACKAGE_MBASE_JSON_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_JSON_NAME_DESCRIPTIVE "MBASE @MBASE_JSON_LIB_NAME@ library @MBASE_JSON_VERSION@") 10 | 11 | find_library( 12 | MBASE_JSON_LIBRARIES 13 | NAMES mb_json 14 | HINTS ${MBASE_JSON_LIB_DIR} 15 | REQUIRED 16 | ) 17 | 18 | include(FindPackageHandleStandardArgs) 19 | find_package_handle_standard_args( 20 | MBASE_JSON 21 | DEFAULT_MSG 22 | MBASE_JSON_LIBRARIES 23 | MBASE_JSON_INCLUDE_DIRS 24 | ) 25 | 26 | if(MBASE_JSON_FOUND) 27 | find_package(mbase-std HINTS ${MBASE_CURRENT_CONFIG_DIRECTORY}) 28 | list(APPEND MBASE_JSON_LIBRARIES ${MBASE_STD_LIBRARIES}) # In UNIX, there is a dependency on UUID library so, it is necessary 29 | add_library(mbase-json INTERFACE) 30 | target_link_libraries(mbase-json INTERFACE ${MBASE_JSON_LIBRARIES}) 31 | target_include_directories(mbase-json INTERFACE ${MBASE_JSON_INCLUDE_DIRS}) 32 | 33 | set_property(TARGET mbase-json PROPERTY POSITION_INDEPENDENT_CODE ON) 34 | endif() 35 | 36 | -------------------------------------------------------------------------------- /mbase/mcp/mcp_server_client_stdio.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | MBASE_BEGIN 6 | 7 | McpServerStdioClient::McpServerStdioClient(McpServerBase* in_server_instance) : McpServerClient(in_server_instance) 8 | { 9 | } 10 | 11 | McpServerStdioClient::~McpServerStdioClient() 12 | { 13 | } 14 | 15 | GENERIC McpServerStdioClient::send_mcp_payload(const mbase::string& in_payload) 16 | { 17 | mStdioMutex.acquire(); 18 | gStdout.write_data(in_payload.c_str(), in_payload.size()); 19 | mStdioMutex.release(); 20 | } 21 | 22 | GENERIC McpServerStdioClient::update_t() 23 | { 24 | I32 bytesRead = 1; 25 | while(bytesRead > 0 && mServerInstance->is_processor_running()) 26 | { 27 | char totalMcpRequest[MBASE_MCP_STDIO_BUFFER_LENGTH] = {0}; 28 | bytesRead = gStdin.read_available_data(totalMcpRequest, MBASE_MCP_STDIO_BUFFER_LENGTH - 1); 29 | 30 | mbase::string mcpRequestString(totalMcpRequest, bytesRead); 31 | mbase::vector mcpMessages; 32 | mcpRequestString.split("\n", mcpMessages); 33 | for(const mbase::string& mcpRequest : mcpMessages) 34 | { 35 | this->read_mcp_payload(mcpRequest); 36 | } 37 | } 38 | } 39 | 40 | MBASE_END -------------------------------------------------------------------------------- /examples/openai-server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(mbase_openai_server main.cpp) 2 | 3 | add_library( 4 | openailib STATIC 5 | model.cpp 6 | processor.cpp 7 | client.cpp 8 | ) 9 | 10 | if (WIN32) 11 | set(OPENSSL_MSVC_STATIC_RT "TRUE") 12 | endif() 13 | 14 | set(OPENSSL_USE_STATIC_LIBS "TRUE") 15 | 16 | if(MBASE_SERVER_SSL STREQUAL "ON") 17 | find_package(OpenSSL REQUIRED) 18 | target_compile_definitions(openailib PUBLIC ${MBASE_COMMON_COMPILE_DEFINITIONS} CPPHTTPLIB_OPENSSL_SUPPORT) 19 | else() 20 | target_compile_definitions(openailib PUBLIC ${MBASE_COMMON_COMPILE_DEFINITIONS}) 21 | endif() 22 | 23 | # disclaimer here: even if the openssl is missing, those are empty strings and won't cause a problem at configuration and linking 24 | target_include_directories(openailib PUBLIC mb_pc mb_inference ${CMAKE_SOURCE_DIR}/external mb_json ${OPENSSL_INCLUDE_DIR}) 25 | target_link_libraries(openailib PUBLIC ${MBASE_STD_LIBS} mb_pc mb_inference mb_json ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY}) 26 | 27 | target_compile_definitions(mbase_openai_server PRIVATE openailib) 28 | target_include_directories(mbase_openai_server PUBLIC openailib) 29 | target_link_libraries(mbase_openai_server PRIVATE openailib) 30 | 31 | install(TARGETS mbase_openai_server RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -------------------------------------------------------------------------------- /cmake_config_in/mbase-std-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_STD_BIN_DIR "@PACKAGE_MBASE_STD_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_STD_LIB_DIR "@PACKAGE_MBASE_STD_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_STD_INCLUDE_DIRS "@PACKAGE_MBASE_STD_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_STD_NAME_DESCRIPTIVE "MBASE @MBASE_STD_LIB_NAME_EXCEPTION@ library @MBASE_STD_VERSION@") 10 | 11 | @MBASE_EMBEDDED_FIND_UUID_CMAKE@ 12 | 13 | add_library(mbase-std INTERFACE) 14 | set(MBASE_STD_FOUND TRUE) 15 | 16 | if(APPLE) 17 | find_package(ICONV) 18 | list(APPEND MBASE_STD_INCLUDE_DIRS ${Iconv_INCLUDE_DIRS}) 19 | list(APPEND MBASE_STD_LIBRARIES ${Iconv_LIBRARIES}) 20 | elseif(UNIX) 21 | if(NOT UUID_FOUND) 22 | message(FATAL "Standard library has a dependency on uuid library. On ubuntu, you can install it using 'sudo apt-get install uuid-dev'.") 23 | endif() 24 | find_package(Threads REQUIRED) 25 | list(APPEND MBASE_STD_INCLUDE_DIRS ${UUID_INCLUDE_DIRS}) 26 | list(APPEND MBASE_STD_LIBRARIES ${UUID_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) 27 | endif() 28 | 29 | if(MBASE_STD_LIBRARIES) 30 | target_link_libraries(mbase-std INTERFACE ${MBASE_STD_LIBRARIES}) 31 | target_include_directories(mbase-std INTERFACE ${MBASE_STD_INCLUDE_DIRS}) 32 | endif() 33 | -------------------------------------------------------------------------------- /examples/openai-server/input_list.txt: -------------------------------------------------------------------------------- 1 | --help Print usage. 2 | -v, --version Shows program version. 3 | --api-key Api key. 4 | -h, --hostname Hostname to listen to (default=127.0.0.1). 5 | -p, --port Port to assign to (default=8080). 6 | -jsdesc Path to json program description 7 | 8 | 9 | -al, --access-limit Amount of clients that can access concurrently (default=-1). 10 | -m, --model-path Model file to be hosted. To host multiple models, pass this argument multiple times. 11 | -t, --thread-count Amount of threads to use for output processing (default=16). 12 | -bt, --batch-thread-count Amount of threads to use for initial batch processing (default=8). 13 | -c, --context-length Total context length (default=8192). 14 | -b, --batch-length Batch length (default=4096). 15 | -gl, --gpu-layers GPU layers to offload to (default=999). 16 | 17 | 18 | ====== Model json params ====== 19 | 20 | model_path 21 | host_as_name 22 | processor_count 23 | thread_count 24 | batch_thread_count 25 | context_length 26 | batch_length 27 | gpu_layers 28 | sys_prompt_path // path to file containing the system prompt -------------------------------------------------------------------------------- /examples/openai-server/processor.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OPENAI_PROCESSOR_H 2 | #define MBASE_OPENAI_PROCESSOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "client.h" 9 | 10 | MBASE_BEGIN 11 | 12 | class OpenaiTextToTextProcessor : public mbase::InfProcessorTextToText { 13 | public: 14 | OpenaiTextToTextProcessor(const I32& in_processor_index); 15 | ~OpenaiTextToTextProcessor(); 16 | 17 | GENERIC on_initializing() override; 18 | GENERIC on_initialize_fail(last_fail_code out_code) override; 19 | GENERIC on_initialize() override; 20 | GENERIC on_destroy() override; 21 | 22 | private: 23 | I32 mProcessorIndex = -1; 24 | OpenaiTextToTextClient mTextClient; 25 | }; 26 | 27 | class OpenaiEmbedderProcessor : public mbase::InfEmbedderProcessor { 28 | public: 29 | OpenaiEmbedderProcessor(const I32& in_processor_index); 30 | ~OpenaiEmbedderProcessor(); 31 | 32 | GENERIC on_initializing() override; 33 | GENERIC on_initialize_fail(last_fail_code out_code) override; 34 | GENERIC on_initialize() override; 35 | GENERIC on_destroy() override; 36 | 37 | private: 38 | I32 mProcessorIndex = -1; 39 | OpenaiEmbedderClient mEmbedderClient; 40 | }; 41 | 42 | MBASE_END 43 | 44 | #endif // MBASE_OPENAI_PROCESSOR_H -------------------------------------------------------------------------------- /cmake/MBASEFrameworkSys.cmake: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | include(MBASECommonConfig) 4 | include(MBASEStdSys) 5 | 6 | set(MBASE_FRAMEWORK_SYS_STRING "MBASE Framework") 7 | set(MBASE_FRAMEWORK_LIB_NAME "framework") 8 | 9 | mbase_build_version(1 0 0 ${MBASE_VERSION_STABLE} MBASE_FRAMEWORK_VERSION) 10 | mbase_build_include_install_path(${MBASE_FRAMEWORK_LIB_NAME} MBASE_FRAMEWORK_INCLUDE_INSTALL_PATH) 11 | 12 | list(APPEND MBASE_FRAMEWORK_INCLUDE_DEPENDS ${MBASE_FRAMEWORK_INCLUDES}) 13 | 14 | list(APPEND MBASE_FRAMEWORK_INCLUDE_STABLE_FILES 15 | event_handler.h 16 | event_manager.h 17 | handler_base.h 18 | logical_processing.h 19 | object_watcher.h 20 | thread_pool.h 21 | timer_loop.h 22 | timers.h 23 | ) 24 | 25 | foreach(MB_INCLUDE_FILE IN LISTS MBASE_FRAMEWORK_INCLUDE_STABLE_FILES) 26 | list(APPEND MBASE_FRAMEWORK_INCLUDE_INSTALL_FILES ${MBASE_FRAMEWORK_INCLUDE_INSTALL_PATH}/${MB_INCLUDE_FILE}) 27 | endforeach() 28 | 29 | set(MBASE_FRAMEWORK_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR}) 30 | set(MBASE_FRAMEWORK_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}) 31 | set(MBASE_FRAMEWORK_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}) 32 | 33 | install(FILES ${MBASE_FRAMEWORK_INCLUDE_INSTALL_FILES} 34 | PERMISSIONS 35 | OWNER_READ 36 | OWNER_WRITE 37 | GROUP_READ 38 | WORLD_READ 39 | DESTINATION ${MBASE_FRAMEWORK_INCLUDE_INSTALL_DIR}/mbase/framework) -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_client_http_streamable.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_CLIENT_HTTP_STREAMABLE_H 2 | #define MBASE_MCP_SERVER_CLIENT_HTTP_STREAMABLE_H 3 | 4 | #include 5 | #include // McpServerClient 6 | 7 | namespace httplib 8 | { 9 | struct Response; 10 | } 11 | 12 | MBASE_BEGIN 13 | 14 | class MBASE_API McpServerClientHttpStreamable : public mbase::McpServerClient { 15 | public: 16 | // this is called if the client is stateful 17 | McpServerClientHttpStreamable( 18 | McpServerBase* in_server_instance, 19 | const mbase::string& in_session_id 20 | ); 21 | 22 | // this is called if the client is stateless 23 | McpServerClientHttpStreamable( 24 | McpServerBase* in_server_instance 25 | ); 26 | 27 | bool is_request_processed() const noexcept; 28 | const mbase::string& get_session_id() const noexcept; // returns empty stirng if the client is stateless 29 | GENERIC on_empty_processed_t() override; 30 | GENERIC send_mcp_payload(const mbase::string& in_payload) override; 31 | GENERIC update_t() override; 32 | 33 | GENERIC set_response_object(httplib::Response* in_response); 34 | private: 35 | httplib::Response* mResponse = nullptr; 36 | mbase::string mSessionId; 37 | volatile bool mIsRequestProcessed = false; 38 | }; 39 | 40 | MBASE_END 41 | 42 | #endif // MBASE_MCP_SERVER_CLIENT_HTTP_STREAMABLE_H -------------------------------------------------------------------------------- /include/mbase/pc/pc_stream_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_PC_STREAM_MANAGER_H 2 | #define MBASE_PC_STREAM_MANAGER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | #define MBASE_INVALID_STREAM_HANDLE -1 13 | 14 | static const U32 gDefaultStreamCount = 32; 15 | static const U32 gDefaultStreamSize = 0xfffff; // 1MB 16 | 17 | class MBASE_API PcStreamManager : public non_copymovable { 18 | public: 19 | 20 | using stream_handle = I32; 21 | 22 | enum class flags : U8 { 23 | STREAM_MNG_SUCCESS = 0, 24 | STREAM_ERR_STREAMS_ARE_FULL, 25 | STREAM_ERR_INVALID_HANDLE 26 | }; 27 | 28 | PcStreamManager() = default; 29 | ~PcStreamManager() = default; 30 | 31 | flags get_stream_by_handle(stream_handle& in_stream_handle, char_stream*& out_stream); 32 | U32 get_stream_count(); 33 | U32 get_stream_size(); 34 | 35 | bool initialize(U32 in_stream_count = gDefaultStreamCount, U32 in_stream_size = gDefaultStreamSize); 36 | flags acquire_stream(stream_handle& out_stream_handle); 37 | flags acquire_stream(stream_handle& out_stream_handle, char_stream*& out_stream); 38 | flags release_stream(stream_handle& in_stream_handle); 39 | 40 | private: 41 | mbase::vector mStreams; 42 | mbase::stack mHandleStack; 43 | U32 mStreamCount; 44 | U32 mStreamSize; 45 | }; 46 | 47 | MBASE_END 48 | 49 | #endif // !MBASE_PC_STREAM_MANAGER_H 50 | -------------------------------------------------------------------------------- /docs/source/mcp/quickstart/what_now.rst: -------------------------------------------------------------------------------- 1 | ========= 2 | What Now? 3 | ========= 4 | 5 | From now on, you may refer to :doc:`../information_reference` in order to understand and implement advanced 6 | features using the MBASE MCP SDK. 7 | 8 | MCP is an actively evolving protocol, with new features likely to be introduced in the near future. 9 | Although the fundamental features seem to be matured, minor changes to their usage and API are highly expected. 10 | 11 | For that reason, I will give you some MCP related resources to help you keep up with the latest changes and 12 | news about the MCP protocol. 13 | 14 | ------------------ 15 | Official Resources 16 | ------------------ 17 | 18 | The MCP protocol is introduced by the `Anthropic `_. 19 | The links below are their official resources: 20 | 21 | * `MCP Protocol Website `_ 22 | * `MCP Specification `_: Latest MCP specification. Useful for in-depth knowledge. 23 | * `MCP Github `_: You can navigate about MCP through this repository. 24 | * `MCP Inspector `_: A very useful visual testing tool for testing MCP servers. 25 | * `MCP Community Discussions `_ 26 | * `MCP Servers `_ 27 | * `MCP Registry `_ -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_client_server_http.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_CLIENT_SERVER_HTTP_H 2 | #define MBASE_MCP_CLIENT_SERVER_HTTP_H 3 | 4 | #include // McpServerStateBase 5 | 6 | MBASE_BEGIN 7 | 8 | class MBASE_API McpClientServerHttp : public McpServerStateBase { 9 | public: 10 | McpClientServerHttp(const McpServerHttpInit& in_init); 11 | 12 | const mbase::string& get_api_key() const noexcept; 13 | const mbase::string& get_hostname() const noexcept; 14 | const mbase::string& get_mcp_endpoint() const noexcept; 15 | mbase::string get_header_value(const mbase::string& in_header) const noexcept; 16 | const mbase::unordered_map& get_headers() const noexcept; 17 | 18 | GENERIC set_mcp_endpoint(const mbase::string& in_endpoint); 19 | GENERIC set_hostname(const mbase::string& in_hostname); 20 | GENERIC set_api_key(const mbase::string& in_api_key); 21 | GENERIC add_header(const mbase::string& in_header, const mbase::string& in_value); 22 | GENERIC remove_header(const mbase::string& in_header); 23 | GENERIC send_mcp_payload(const mbase::string& in_payload) override; 24 | GENERIC update_t() override; 25 | private: 26 | mbase::string mHostname; 27 | mbase::string mApiKey; 28 | mbase::string mMcpEndpoint; 29 | mbase::unordered_map mHeadersMap; 30 | mbase::vector mPayloadList; 31 | mbase::mutex mPayloadListSync; 32 | }; 33 | 34 | MBASE_END 35 | 36 | #endif // MBASE_MCP_CLIENT_SERVER_HTTP_H -------------------------------------------------------------------------------- /cmake_config_in/mbase-inference-config.cmake.in: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | @PACKAGE_INIT@ 4 | 5 | set_and_check(MBASE_INFERENCE_BIN_DIR "@PACKAGE_MBASE_INFERENCE_BIN_INSTALL_DIR@") 6 | set_and_check(MBASE_INFERENCE_LIB_DIR "@PACKAGE_MBASE_INFERENCE_LIB_INSTALL_DIR@") 7 | set_and_check(MBASE_INFERENCE_INCLUDE_DIRS "@PACKAGE_MBASE_INFERENCE_INCLUDE_INSTALL_DIR@") 8 | 9 | set(MBASE_INFERENCE_NAME_DESCRIPTIVE "MBASE @MBASE_INFERENCE_LIB_NAME@ library @MBASE_INFERENCE_VERSION@") 10 | 11 | find_library( 12 | MBASE_INFERENCE_LIBRARIES 13 | NAMES mb_inference 14 | HINTS ${MBASE_INFERENCE_LIB_DIR} 15 | REQUIRED 16 | ) 17 | 18 | include(FindPackageHandleStandardArgs) 19 | find_package_handle_standard_args( 20 | MBASE_INFERENCE 21 | DEFAULT_MSG 22 | MBASE_INFERENCE_LIBRARIES 23 | MBASE_INFERENCE_INCLUDE_DIRS 24 | ) 25 | 26 | if(MBASE_INFERENCE_FOUND) 27 | find_package(mbase-pc HINTS ${MBASE_CURRENT_CONFIG_DIRECTORY}) 28 | find_package(llama REQUIRED HINTS ${MBASE_INFERENCE_LIB_DIR}/cmake) 29 | 30 | list(APPEND MBASE_INFERENCE_LIBRARIES ${MBASE_PC_LIBRARIES}) 31 | list(APPEND MBASE_INFERENCE_INCLUDE_DIRS ${LLAMA_INCLUDE_DIR}) 32 | 33 | add_library(mbase-inference INTERFACE) 34 | target_link_libraries(mbase-inference INTERFACE ${MBASE_INFERENCE_LIBRARIES} llama) 35 | target_include_directories(mbase-inference INTERFACE ${MBASE_INFERENCE_INCLUDE_DIRS} llama) 36 | target_compile_definitions(mbase-inference INTERFACE llama) 37 | 38 | set_property(TARGET mbase-inference PROPERTY POSITION_INDEPENDENT_CODE ON) 39 | endif() 40 | -------------------------------------------------------------------------------- /docs/maip/navig_generator.js: -------------------------------------------------------------------------------- 1 | let nvg= document.getElementById("pageNavigation"); 2 | let firstList = document.createElement("ul"); 3 | let sectionArray = []; 4 | function findAllSections(startString, associatedList) 5 | { 6 | associatedList.style.listStyleType = "none"; 7 | startString += 'c'; 8 | for(let i = 0;;i++) 9 | { 10 | const sectionIdString = startString + i.toString(); 11 | let foundSection = document.getElementById(sectionIdString); 12 | 13 | if(foundSection) 14 | { 15 | let newLi = document.createElement("li"); 16 | let newAnchor = document.createElement("a"); 17 | newAnchor.href = '#' + sectionIdString; 18 | newAnchor.innerHTML = foundSection.innerHTML; 19 | newLi.appendChild(newAnchor); 20 | associatedList.appendChild(newLi); 21 | 22 | let newOrderedList = document.createElement("ul"); 23 | associatedList.appendChild(newOrderedList); 24 | sectionArray.push(foundSection.id); 25 | findAllSections(sectionIdString, newOrderedList); 26 | 27 | } 28 | else 29 | { 30 | break; 31 | } 32 | } 33 | } 34 | 35 | findAllSections("", firstList); 36 | if(sectionArray.length) 37 | { 38 | nvg.appendChild(firstList); 39 | nvg.style.visibility = "visible"; 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_notifications.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_NOTIFICATIONS_H 2 | #define MBASE_MCP_NOTIFICATIONS_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | enum class mcp_server_notification : I32 { 9 | MESSAGE, 10 | PROGRESS, 11 | CANCELLED, 12 | PROMPTS_LIST_CHANGED, 13 | RESOURCES_LIST_CHANGED, 14 | RESOURCE_UPDATED, 15 | TOOLS_LIST_CHANGED 16 | }; 17 | 18 | struct McpNotificationLogMessage { 19 | // Spec: https://modelcontextprotocol.io/specification/2024-11-05/server/utilities/logging 20 | mbase::mcp_log_levels mLevel; 21 | mbase::string mLogger; 22 | mbase::string mError; 23 | mbase::Json mDetails; 24 | }; 25 | 26 | struct McpNotificationProgress { 27 | // Spec: https://modelcontextprotocol.io/specification/2024-11-05/basic/utilities/progress 28 | 29 | mbase::string mProgressToken; 30 | mbase::F64 mProgress = 0; 31 | mbase::F64 mProgressTotal = 100; 32 | mbase::string mProgressMessage; 33 | }; 34 | 35 | struct McpNotificationCancellation { 36 | // Spec: https://modelcontextprotocol.io/specification/2024-11-05/basic/utilities/cancellation 37 | 38 | mbase::Json mRequestId; 39 | mbase::string mReason; 40 | }; 41 | 42 | struct McpNotificationCancellationIdStr{ 43 | mbase::string mIdString; 44 | mbase::string mReason; 45 | }; 46 | 47 | struct McpNotificationResourceUpdated{ 48 | // Spec: https://modelcontextprotocol.io/specification/2024-11-05/server/resources#subscriptions 49 | 50 | mbase::string mUri; 51 | }; 52 | 53 | MBASE_END 54 | 55 | #endif // MBASE_MCP_NOTIFICATIONS_H -------------------------------------------------------------------------------- /mbase/mcp/mcp_server_features.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | MBASE_BEGIN 5 | 6 | const McpToolDescription& McpToolFeature::get_tool_description() const noexcept 7 | { 8 | return mToolDescription; 9 | } 10 | 11 | mcp_server_tool_cb McpToolFeature::get_tool_cb() const noexcept 12 | { 13 | return mToolCb; 14 | } 15 | 16 | GENERIC McpToolFeature::update_tool_description(const McpToolDescription& in_desc, mcp_server_tool_cb in_cb) noexcept 17 | { 18 | mToolDescription = in_desc; 19 | mToolCb = in_cb; 20 | } 21 | 22 | const McpPromptDescription& McpPromptFeature::get_prompt_description() const noexcept 23 | { 24 | return mPromptDescription; 25 | } 26 | 27 | mcp_server_prompt_cb McpPromptFeature::get_prompt_cb() const noexcept 28 | { 29 | return mPromptCb; 30 | } 31 | 32 | GENERIC McpPromptFeature::update_prompt_description(const McpPromptDescription& in_desc, mcp_server_prompt_cb in_cb) noexcept 33 | { 34 | mPromptDescription = in_desc; 35 | mPromptCb = in_cb; 36 | } 37 | 38 | const McpResourceDescription& McpResourceFeature::get_resource_description() const noexcept 39 | { 40 | return mResourceDescription; 41 | } 42 | 43 | mcp_server_resource_cb McpResourceFeature::get_resource_cb() const noexcept 44 | { 45 | return mResourceCb; 46 | } 47 | 48 | GENERIC McpResourceFeature::update_resource_description(const McpResourceDescription& in_desc, mcp_server_resource_cb in_cb) noexcept 49 | { 50 | mResourceDescription = in_desc; 51 | mResourceCb = in_cb; 52 | } 53 | 54 | MBASE_END -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: MBASE SDK Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - devel 7 | - configuration 8 | pull_request: 9 | branches: 10 | - devel 11 | - configuration 12 | 13 | jobs: 14 | ubuntu-amd64-build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout the build test branch 18 | uses: actions/checkout@v4 19 | with: 20 | ref: ${{ github.ref }} 21 | submodules: recursive 22 | 23 | - name: Dev dependency install 24 | run: sudo apt install uuid-dev 25 | 26 | - name: MBASE build 27 | run: | 28 | cmake -B build_amd64 -DMBASE_FORCE_BUNDLE=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 29 | cmake --build build_amd64 --config Release -j 30 | 31 | windows-amd64-build: 32 | runs-on: windows-latest 33 | steps: 34 | - name: Checkout the build test branch 35 | uses: actions/checkout@v4 36 | with: 37 | ref: ${{ github.ref }} 38 | submodules: recursive 39 | 40 | - name: MBASE build 41 | run: | 42 | cmake -B build_amd64 -DMBASE_FORCE_BUNDLE=ON 43 | cmake --build build_amd64 --config Release -j 44 | 45 | macos-metal-build: 46 | runs-on: macos-13 47 | steps: 48 | - name: Checkout the build test branch 49 | uses: actions/checkout@v4 50 | with: 51 | ref: ${{ github.ref }} 52 | submodules: recursive 53 | 54 | - name: MBASE build 55 | run: | 56 | cmake -B build -DMBASE_FORCE_BUNDLE=ON 57 | cmake --build build --config Release -j -------------------------------------------------------------------------------- /external/double-conversion/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2006-2011, the V8 project authors. All rights reserved. 2 | Redistribution and use in source and binary forms, with or without 3 | modification, are permitted provided that the following conditions are 4 | met: 5 | 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above 9 | copyright notice, this list of conditions and the following 10 | disclaimer in the documentation and/or other materials provided 11 | with the distribution. 12 | * Neither the name of Google Inc. nor the names of its 13 | contributors may be used to endorse or promote products derived 14 | from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_common.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_COMMON_H 2 | #define MBASE_MCP_COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include // for mbase::mutex 10 | #include 11 | #include 12 | #include 13 | 14 | MBASE_BEGIN 15 | 16 | #define MBASE_MCP_SUCCESS 0 17 | #define MBASE_MCP_PARSE_ERROR -32700 18 | #define MBASE_MCP_INVALID_REQUEST -32600 19 | #define MBASE_MCP_METHOD_NOT_FOUND -32601 20 | #define MBASE_MCP_INVALID_PARAMS -32602 21 | #define MBASE_MCP_INTERNAL_ERROR -32603 22 | #define MBASE_MCP_TIMEOUT -32001 23 | #define MBASE_MCP_RESOURCE_NOT_FOUND -32002 24 | 25 | #ifndef MBASE_MCP_TIMEOUT_DEFAULT 26 | #define MBASE_MCP_TIMEOUT_DEFAULT 10 // in seconds 27 | #endif 28 | 29 | #ifndef MBASE_MCP_STDIO_BUFFER_LENGTH 30 | #define MBASE_MCP_STDIO_BUFFER_LENGTH (64 * 1024) 31 | #endif 32 | 33 | #define MBASE_MCP_DEFAULT_VERSION "2025-03-26" 34 | 35 | static inline mbase::string gMcpVersion = "2025-03-26"; 36 | 37 | enum class mcp_log_levels { 38 | L_DEBUG, 39 | L_INFO, 40 | L_NOTICE, 41 | L_WARNING, 42 | L_ERROR, 43 | L_CRITICAL, 44 | L_ALERT, 45 | L_EMERGENCY 46 | }; 47 | 48 | enum class feature_type : I32 { 49 | TOOL, 50 | PROMPT, 51 | RESOURCE 52 | }; 53 | 54 | enum class mcp_transport_method { 55 | STDIO, 56 | HTTP_STREAMBLE, 57 | INTERNAL 58 | }; 59 | 60 | struct mcp_err_format { 61 | I32 mCode = MBASE_MCP_SUCCESS; 62 | mbase::string mMessage; 63 | mbase::Json mData; 64 | }; 65 | 66 | MBASE_END 67 | 68 | #endif // MBASE_MCP_COMMON_H -------------------------------------------------------------------------------- /include/mbase/inference/inf_sampling_set.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_SAMPLING_SET_H 2 | #define MBASE_INF_SAMPLING_SET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | MBASE_BEGIN 10 | 11 | struct InfSamplingRepetition { 12 | U32 mPenaltyN; 13 | F32 mRepeatPenalty; 14 | F32 mPenaltyFrequency; 15 | F32 mPenaltyPresent; 16 | bool mPenaltyLinefeed; 17 | bool mPenaltyEos; 18 | }; 19 | 20 | struct InfSamplingXTC { 21 | F32 mProbability; 22 | F32 mThreshold; 23 | }; 24 | 25 | struct InfSamplingDRY { 26 | F32 mDryMultiplier; 27 | F32 mDryBase; 28 | I32 mDryAllowedLength; 29 | I32 mDryPenaltyLastN; 30 | }; 31 | 32 | struct InfSamplingMirostatV2 { 33 | F32 mTau; 34 | F32 mEta; 35 | }; 36 | 37 | struct InfSamplerDescription { 38 | enum class SAMPLER : U32 { 39 | RNG, 40 | REPETITION, 41 | DRY, 42 | TOP_K, 43 | TOP_P, 44 | MIN_P, 45 | TYPICAL_P, 46 | TEMP, 47 | XTC, 48 | MIROSTAT_V2 49 | }; 50 | 51 | friend bool operator<(const InfSamplerDescription& in_lhs, const InfSamplerDescription& in_rhs) 52 | { 53 | U32 samplerValueLeft = (U32)in_lhs.mSamplerType; 54 | U32 samplerValueRight = (U32)in_rhs.mSamplerType; 55 | 56 | return samplerValueLeft < samplerValueRight; 57 | } 58 | 59 | SAMPLER mSamplerType; 60 | 61 | union { 62 | U32 mTopK; 63 | F32 mTopP; 64 | F32 mMinP; 65 | F32 mTypicalP; 66 | F32 mTemp; 67 | InfSamplingRepetition mRepetition; 68 | InfSamplingXTC mXtc; 69 | InfSamplingDRY mDry; 70 | InfSamplingMirostatV2 mMiroV2; 71 | U32 mRng; 72 | }; 73 | }; 74 | 75 | using inf_sampling_set = mbase::set; 76 | 77 | MBASE_END 78 | 79 | #endif // MBASE_INF_SAMPLING_SET_H -------------------------------------------------------------------------------- /mbase/mcp/mcp_client_server_stdio.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MBASE_BEGIN 4 | 5 | McpClientServerStdio::McpClientServerStdio(const mbase::McpServerStdioInit& in_init) 6 | : McpServerStateBase(mbase::mcp_transport_method::STDIO), mSubProcessManager(true, in_init.mCommand, in_init.mArguments, in_init.mEnvironmentVariables) 7 | { 8 | } 9 | 10 | bool McpClientServerStdio::is_subprocess_alive() const noexcept 11 | { 12 | return mIsSubprocessAlive; 13 | } 14 | 15 | GENERIC McpClientServerStdio::send_mcp_payload(const mbase::string& in_payload) 16 | { 17 | writerSync.acquire(); 18 | mbase::io_file& writePipe = mSubProcessManager.get_write_pipe(); 19 | if(writePipe.is_operate_ready()) 20 | { 21 | writePipe.write_data(in_payload.c_str(), in_payload.size()); 22 | } 23 | writerSync.release(); 24 | } 25 | 26 | GENERIC McpClientServerStdio::update_t() 27 | { 28 | while(is_processor_running() && mSubProcessManager.get_read_pipe1().is_file_open()) 29 | { 30 | char readBytes[MBASE_MCP_STDIO_BUFFER_LENGTH] = {0}; 31 | I32 byteLength = mSubProcessManager.get_read_pipe1().read_available_data(readBytes, MBASE_MCP_STDIO_BUFFER_LENGTH - 1); 32 | if(byteLength > 0) 33 | { 34 | mbase::string readBytesString(readBytes, byteLength); 35 | mbase::vector seperatedPacket; 36 | readBytesString.split("\n", seperatedPacket); 37 | for(const mbase::string& currentMcpPayload : seperatedPacket) 38 | { 39 | this->read_mcp_payload(currentMcpPayload); 40 | } 41 | } 42 | else 43 | { 44 | break; 45 | } 46 | } 47 | mIsSubprocessAlive = false; 48 | } 49 | 50 | MBASE_END -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | from dataclasses import asdict 2 | 3 | project = 'MBASE Software Documentation Center' 4 | copyright = '2025, MBASE Yazılım A.Ş.' 5 | author = 'M. Emre Erdog' 6 | 7 | extensions = [ 8 | "sphinx.ext.extlinks", 9 | "sphinx_design" 10 | ] 11 | 12 | templates_path = ['_templates'] 13 | exclude_patterns = [] 14 | 15 | html_theme = 'furo' 16 | html_static_path = ['_static'] 17 | html_favicon = 'favicon.png' 18 | 19 | html_title = project 20 | html_theme_options = { 21 | "sidebar_hide_name": True, 22 | "light_logo": "mbase_sdk_logo_white.svg", 23 | "dark_logo": "mbase_sdk_logo_dark.svg", 24 | "source_repository": "https://github.com/Emreerdog/mbase", 25 | "source_branch": "documents/docs/source", 26 | "footer_icons": [ 27 | { 28 | "name": "GitHub", 29 | "url": "https://github.com/Emreerdog/mbase", 30 | "html": """ 31 | 32 | 33 | 34 | """, 35 | "class": "", 36 | }, 37 | ], 38 | } 39 | 40 | pygments_dark_style = "github-dark" 41 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_maip_peer_base.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MAIP_PEER_BASE_H 2 | #define MBASE_MAIP_PEER_BASE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | class MBASE_API InfMaipPeerBase { 13 | public: 14 | using registered_processor_map = std::unordered_map; 15 | using indexed_processor_map = std::unordered_map; 16 | 17 | InfMaipPeerBase(std::shared_ptr in_peer, const mbase::string& in_maip_user, const inf_model_category& in_peer_category); 18 | virtual ~InfMaipPeerBase(); 19 | 20 | std::shared_ptr get_maip_peer() const; 21 | const mbase::string& get_maip_username() const; 22 | const inf_model_category& get_peer_category() const; 23 | InfProcessorBase* get_processor_by_id(const U64& in_id); 24 | mbase::vector get_processor_ids() const; 25 | 26 | GENERIC add_processor(InfProcessorBase* in_address, U64& out_context_id); 27 | GENERIC set_network_peer(std::shared_ptr in_peer); 28 | GENERIC set_maip_username(const mbase::string& in_username); 29 | GENERIC remove_processor_by_address(InfProcessorBase* in_address); 30 | GENERIC remove_processor_by_id(const U64& in_id); 31 | 32 | protected: 33 | GENERIC mandatory_processor_cleanup(); // Must be called on destructor of the derived 34 | registered_processor_map mRegisteredProcMap; 35 | indexed_processor_map mIndexedProcMap; 36 | std::shared_ptr mPeer = NULL; 37 | mbase::string mMaipUsername; 38 | inf_model_category mPeerCategory; 39 | U64 mContextCounter = 1; 40 | }; 41 | 42 | MBASE_END 43 | 44 | #endif // !MBASE_MAIP_PEER_BASE_H 45 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | ======================= 2 | MBASE SDK Documentation 3 | ======================= 4 | 5 | Github link: https://github.com/Emreerdog/mbase 6 | 7 | Welcome to the MBASE SDK documentation. 8 | 9 | MBASE SDK is a set of mostly LLM related libraries actively being maintained and developed by M. Emre Erdog 10 | and the `MBASE Yazılım A.Ş. `_. 11 | 12 | The main purpose of the MBASE SDK is to supply the developer with necessary tools and procedures to easily integrate LLM capabilities into their 13 | C++ applications. 14 | 15 | Here is a list of libraries in the MBASE SDK: 16 | 17 | * :doc:`Inference Library `: An LLM inference library built over https://github.com/ggerganov/llama.cpp library for integrating LLMs into programs. 18 | * :doc:`Model Context Protocol Library `: A C++ MCP client/server library that includes all fundamental features, with support for both STDIO and StreamableHTTP transport methods. 19 | * :doc:`Standard Library ` : A standard library containing STL compatible containers with default serialization support and useful utilities such as built-in uuid generation, timers etc. 20 | * :doc:`JSON Library `: A light-weight json library. 21 | 22 | ------------------- 23 | Supported Platforms 24 | ------------------- 25 | 26 | * Mac OS 27 | * Linux 28 | * Windows 29 | 30 | ---------- 31 | Setting-Up 32 | ---------- 33 | 34 | In order the setup the MBASE SDK, see: :doc:`setting-up ` 35 | 36 | .. toctree:: 37 | :caption: Contents: 38 | 39 | Setting-up 40 | Inference Library 41 | Model Context Protocol Library 42 | Standard Library 43 | JSON Library 44 | Copyright -------------------------------------------------------------------------------- /docs/source/inference/programs/index.rst: -------------------------------------------------------------------------------- 1 | ======== 2 | Programs 3 | ======== 4 | 5 | This chapter contains some useful programs that show the capabilities of the 6 | MBASE library. Their source code directory is given in their program identification section, so dont hesitate 7 | to use their implementation as a reference point for your specific implementations use case. 8 | 9 | 10 | Here are the programs that are implemented: 11 | 12 | :doc:`Benchmark T2T`: It is a program written to measure the performance 13 | of the given T2T LLM and its impact on your main application logic. 14 | 15 | :doc:`Embedding `: An example program for generating the embeddings of the given prompt or prompts. 16 | 17 | :doc:`Openai Server `: An Openai API compatible HTTP/HTTPS server for serving LLMs. 18 | This program provides chat completion API for TextToText models and embeddings API For embedder models. 19 | 20 | :doc:`Retrieval `: An example for calculating the distance between the 21 | given query and multiple text files/documents and applying retrieval operation. 22 | 23 | :doc:`Simple Conversation `: It is a simple executable program where you are having 24 | a dialogue with the LLM you provide. It is useful for examining the answer of the LLM since the system prompt 25 | and sampler values can be altered. 26 | 27 | :doc:`Typo Fixer `: This is an applied example use case of the MBASE library. 28 | The program is reading a user-supplied text file and fixing the typos. 29 | 30 | 31 | .. toctree:: 32 | :hidden: 33 | 34 | Benchmark TextToText 35 | Embedding 36 | Openai Server 37 | Retrieval 38 | Simple Conversation 39 | Typo Fixer -------------------------------------------------------------------------------- /include/mbase/inference/inf_maip_peer_t2t.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_MAIP_PEER_T2T_H 2 | #define MBASE_INF_MAIP_PEER_T2T_H 3 | 4 | #include 5 | #include 6 | 7 | MBASE_BEGIN 8 | 9 | class InfMaipTextToTextProcessor; 10 | class InfMaipPeerTextToText; 11 | 12 | class MBASE_API InfMaipTextToTextProcessor : public mbase::InfProcessorTextToText { 13 | public: 14 | InfMaipTextToTextProcessor(InfMaipPeerTextToText* in_peer); 15 | GENERIC on_initialize_fail(last_fail_code out_code) override; 16 | GENERIC on_initialize() override; 17 | GENERIC on_destroy() override; 18 | private: 19 | InfMaipPeerTextToText* mClientNominee; 20 | }; 21 | 22 | class MBASE_API InfMaipPeerTextToText : public mbase::InfMaipPeerBase, public mbase::InfClientTextToText { 23 | public: 24 | InfMaipPeerTextToText(std::shared_ptr in_peer, const mbase::string& in_maip_user); 25 | ~InfMaipPeerTextToText(); 26 | GENERIC on_init_fail(InfProcessorBase* out_processor, InfProcessorTextToText::last_fail_code out_fail_code); 27 | GENERIC on_register(InfProcessorBase* out_processor) override; 28 | GENERIC on_unregister(InfProcessorBase* out_processor) override; // This function is a one-liner which will only call remove_processor_by_address and then delete the processor 29 | GENERIC on_batch_processed(InfProcessorTextToText* out_processor, const U32& out_proc_batch_length, const bool& out_is_kv_locked) override; 30 | GENERIC on_write(InfProcessorTextToText* out_processor, const inf_text_token_vector& out_token, bool out_is_finish) override; 31 | GENERIC on_finish(InfProcessorTextToText* out_processor, size_type out_total_token_size, InfProcessorTextToText::finish_state out_finish_state) override; 32 | private: 33 | inf_token_description mLastToken; 34 | }; 35 | 36 | MBASE_END 37 | 38 | #endif // MBASE_INF_MAIP_PEER_T2T_H -------------------------------------------------------------------------------- /mbase/mcp/mcp_server_client_http_streamable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | MBASE_BEGIN 5 | 6 | McpServerClientHttpStreamable::McpServerClientHttpStreamable( 7 | McpServerBase* in_server_instance, 8 | const mbase::string& in_session_id 9 | ): 10 | mbase::McpServerClient(in_server_instance), 11 | mSessionId(in_session_id), 12 | mIsRequestProcessed(false) 13 | { 14 | } 15 | 16 | McpServerClientHttpStreamable::McpServerClientHttpStreamable( 17 | McpServerBase* in_server_instance 18 | ): 19 | mbase::McpServerClient(in_server_instance), 20 | mIsRequestProcessed(false) 21 | { 22 | // assume it is already initialized 23 | // as the PR suggests(https://github.com/modelcontextprotocol/modelcontextprotocol/pull/206) 24 | mIsClientInitialized = true; 25 | } 26 | 27 | bool McpServerClientHttpStreamable::is_request_processed() const noexcept 28 | { 29 | return mIsRequestProcessed; 30 | } 31 | 32 | const mbase::string& McpServerClientHttpStreamable::get_session_id() const noexcept 33 | { 34 | return mSessionId; 35 | } 36 | 37 | GENERIC McpServerClientHttpStreamable::on_empty_processed_t() 38 | { 39 | mIsRequestProcessed = true; 40 | } 41 | 42 | GENERIC McpServerClientHttpStreamable::send_mcp_payload(const mbase::string& in_payload) 43 | { 44 | if(mResponse) 45 | { 46 | std::string mcpContent(in_payload.c_str(), in_payload.size()); 47 | mResponse->set_content(mcpContent, "application/json"); 48 | mIsRequestProcessed = true; 49 | mResponse = nullptr; 50 | } 51 | } 52 | 53 | GENERIC McpServerClientHttpStreamable::update_t() 54 | { 55 | // nothing 56 | } 57 | 58 | GENERIC McpServerClientHttpStreamable::set_response_object(httplib::Response* in_response) 59 | { 60 | mIsRequestProcessed = false; 61 | mResponse = in_response; 62 | } 63 | 64 | MBASE_END 65 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_device_desc.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_DEVICE_DESC_H 2 | #define MBASE_INF_DEVICE_DESC_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | MBASE_BEGIN 9 | 10 | class MBASE_API InfDeviceDescription { 11 | public: 12 | enum class device_type { 13 | CPU, 14 | CUSTOM, 15 | GPU, 16 | UNKNOWN 17 | }; 18 | 19 | using size_type = SIZE_T; 20 | 21 | /* ===== BUILDER METHODS BEGIN ===== */ 22 | InfDeviceDescription() noexcept; 23 | InfDeviceDescription(ggml_backend_dev_t in_device, const size_type& in_index) noexcept; 24 | InfDeviceDescription(const InfDeviceDescription& in_description) noexcept; 25 | ~InfDeviceDescription() noexcept; 26 | /* ===== BUILDER METHODS END ===== */ 27 | 28 | /* ===== OPERATOR BUILDER METHODS BEGIN ===== */ 29 | InfDeviceDescription& operator=(const InfDeviceDescription& in_rhs); 30 | /* ===== OPERATOR BUILDER METHODS END ===== */ 31 | 32 | /* ===== OBSERVATION METHODS BEGIN ===== */ 33 | MBASE_ND(MBASE_OBS_IGNORE) mbase::string get_device_name() const; 34 | MBASE_ND(MBASE_OBS_IGNORE) mbase::string get_device_description() const; 35 | MBASE_ND(MBASE_OBS_IGNORE) size_type get_total_memory() const; 36 | MBASE_ND(MBASE_OBS_IGNORE) size_type get_free_memory() const; 37 | MBASE_ND(MBASE_OBS_IGNORE) size_type get_device_index() const; 38 | MBASE_ND(MBASE_OBS_IGNORE) device_type get_device_type() const; 39 | MBASE_ND(MBASE_OBS_IGNORE) ggml_backend_dev_t get_internal_dev_handle(); 40 | /* ===== OBSERVATION METHODS END ===== */ 41 | private: 42 | ggml_backend_dev_t mBackendDevice; 43 | size_type mDeviceIndex; 44 | }; 45 | 46 | MBASE_API mbase::vector inf_query_devices(); 47 | MBASE_API mbase::vector inf_query_gpu_devices(); 48 | 49 | MBASE_END 50 | 51 | #endif // MBASE_INF_DEVICE_DESC_H -------------------------------------------------------------------------------- /include/mbase/argument_get_value.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_ARGUMENT_GET_VALUE_H 2 | #define MBASE_ARGUMENT_GET_VALUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | MBASE_BEGIN 9 | 10 | template 11 | struct argument_get_numeric { 12 | static bool value(const I32& in_index, const I32& in_argc, IBYTEBUFFER* in_argv, Type& out_value) 13 | { 14 | if (in_index >= in_argc || in_index + 1 == in_argc || in_index < 0) 15 | { 16 | // If index is bigger or equal to argc, invocation is invalid 17 | // If index + 1 is equal to argc, it means that it does not have a value to set 18 | // If index is less than 0, invocation is invalid 19 | return false; 20 | } 21 | 22 | out_value = mbase::smart_conversion::apply(in_argv[in_index + 1]); 23 | return true; 24 | } 25 | }; 26 | 27 | struct argument_get_string { 28 | static bool value(const I32& in_index, const I32& in_argc, IBYTEBUFFER* in_argv, mbase::string& out_value) 29 | { 30 | if (in_index >= in_argc || in_index + 1 == in_argc) 31 | { 32 | // If index is bigger or equal to argc, invocation is invalid 33 | // If index + 1 is equal to argc, it means that it does not have a value to set 34 | return false; 35 | } 36 | 37 | out_value = in_argv[in_index + 1]; 38 | return true; 39 | } 40 | }; 41 | 42 | template 43 | struct argument_get { 44 | using get_type = std::conditional_t, argument_get_string, argument_get_numeric>; 45 | static bool value(const I32& in_index, const I32& in_argc, IBYTEBUFFER* in_argv, Type& out_value) 46 | { 47 | return get_type::value(in_index, in_argc, in_argv, out_value); 48 | } 49 | }; 50 | 51 | MBASE_END 52 | 53 | #endif // !MBASE_ARGUMENT_GET_VALUE_H 54 | -------------------------------------------------------------------------------- /cmake/MBASECoreSys.cmake: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | include(MBASECommonConfig) 4 | include(MBASEStdSys) 5 | 6 | set(MBASE_PC_SYS_STRING "MBASE Core") 7 | set(MBASE_PC_LIB_NAME "pc") 8 | 9 | mbase_build_version(1 0 0 ${MBASE_VERSION_ALPHA} MBASE_PC_VERSION) 10 | mbase_build_include_install_path(${MBASE_PC_LIB_NAME} MBASE_PC_INCLUDE_INSTALL_PATH) 11 | mbase_build_lib_path(${MBASE_PC_LIB_NAME} MBASE_PC_LIB_PATH) 12 | 13 | message("${MBASE_PC_SYS_STRING} library version: ${MBASE_PC_VERSION}") 14 | message("${MBASE_PC_SYS_STRING} install source path: ${MBASE_PC_INCLUDE_INSTALL_PATH}") 15 | message("${MBASE_PC_SYS_STRING} lib source path: ${MBASE_PC_LIB_PATH}") 16 | 17 | list(APPEND MBASE_PC_INCLUDE_DEPENDS ${MBASE_STD_INCLUDES}) 18 | list(APPEND MBASE_PC_LIB_DEPENDS ${MBASE_STD_LIBS}) 19 | 20 | list(APPEND MBASE_PC_INCLUDE_STABLE_FILES 21 | pc_config.h 22 | pc_diagnostics.h 23 | pc_io_manager.h 24 | pc_net_manager.h 25 | pc_program.h 26 | pc_state.h 27 | pc_stream_manager.h 28 | pc_termination_handler.h 29 | ) 30 | 31 | add_library(mb_pc 32 | ${MBASE_PC_LIB_PATH}/pc_config.cpp 33 | ${MBASE_PC_LIB_PATH}/pc_state.cpp 34 | ${MBASE_PC_LIB_PATH}/pc_io_manager.cpp 35 | ${MBASE_PC_LIB_PATH}/pc_stream_manager.cpp 36 | ${MBASE_PC_LIB_PATH}/pc_program.cpp 37 | ${MBASE_PC_LIB_PATH}/pc_diagnostics.cpp 38 | ${MBASE_PC_LIB_PATH}/pc_net_manager.cpp 39 | ) 40 | 41 | target_compile_definitions(mb_pc PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 42 | target_compile_options(mb_pc PRIVATE ${MBASE_COMMON_COMPILE_OPTIONS}) 43 | target_include_directories(mb_pc PUBLIC ${MBASE_PC_INCLUDE_DEPENDS}) 44 | target_link_libraries(mb_pc PRIVATE ${MBASE_PC_LIB_DEPENDS}) 45 | 46 | set_property(TARGET mb_pc PROPERTY POSITION_INDEPENDENT_CODE ON) 47 | 48 | foreach(MB_INCLUDE_FILE IN LISTS MBASE_PC_INCLUDE_STABLE_FILES) 49 | list(APPEND MBASE_PC_INCLUDE_INSTALL_FILES ${MBASE_PC_INCLUDE_INSTALL_PATH}/${MB_INCLUDE_FILE}) 50 | endforeach() 51 | -------------------------------------------------------------------------------- /external/double-conversion/double-conversion.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 the V8 project authors. All rights reserved. 2 | // Redistribution and use in source and binary forms, with or without 3 | // modification, are permitted provided that the following conditions are 4 | // met: 5 | // 6 | // * Redistributions of source code must retain the above copyright 7 | // notice, this list of conditions and the following disclaimer. 8 | // * Redistributions in binary form must reproduce the above 9 | // copyright notice, this list of conditions and the following 10 | // disclaimer in the documentation and/or other materials provided 11 | // with the distribution. 12 | // * Neither the name of Google Inc. nor the names of its 13 | // contributors may be used to endorse or promote products derived 14 | // from this software without specific prior written permission. 15 | // 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 29 | #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 30 | 31 | #include "string-to-double.h" 32 | #include "double-to-string.h" 33 | 34 | #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 35 | -------------------------------------------------------------------------------- /include/mbase/synchronization.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_SYNCHRONIZATION_H 2 | #define MBASE_SYNCHRONIZATION_H 3 | 4 | #include 5 | 6 | #ifdef MBASE_PLATFORM_WINDOWS 7 | #include // CreateMutexA, CloseHandle, WaitForSingleObject, ReleaseMutex 8 | #endif 9 | 10 | #ifdef MBASE_PLATFORM_UNIX 11 | #include 12 | #endif 13 | 14 | MBASE_STD_BEGIN 15 | 16 | class mutex { 17 | public: 18 | #ifdef MBASE_PLATFORM_WINDOWS 19 | using raw_handle = HANDLE; 20 | #endif 21 | #ifdef MBASE_PLATFORM_UNIX 22 | using raw_handle = pthread_mutex_t; 23 | #endif 24 | 25 | mutex() noexcept 26 | { // ASSUME 100% SUCCESS 27 | #ifdef MBASE_PLATFORM_WINDOWS 28 | mHandle = CreateMutexA( 29 | nullptr, 30 | false, 31 | nullptr 32 | ); 33 | #endif 34 | #ifdef MBASE_PLATFORM_UNIX 35 | pthread_mutex_init(&mHandle, NULL); 36 | #endif 37 | } 38 | 39 | ~mutex() noexcept 40 | { 41 | this->release(); 42 | #ifdef MBASE_PLATFORM_WINDOWS 43 | CloseHandle(mHandle); 44 | #endif 45 | #ifdef MBASE_PLATFORM_UNIX 46 | pthread_mutex_destroy(&mHandle); 47 | #endif 48 | } 49 | 50 | MBASE_INLINE GENERIC acquire() noexcept 51 | { 52 | #ifdef MBASE_PLATFORM_WINDOWS 53 | WaitForSingleObject(mHandle, INFINITE); 54 | #endif 55 | #ifdef MBASE_PLATFORM_UNIX 56 | pthread_mutex_lock(&mHandle); 57 | #endif 58 | } 59 | 60 | MBASE_INLINE GENERIC release() noexcept 61 | { 62 | #ifdef MBASE_PLATFORM_WINDOWS 63 | ReleaseMutex(mHandle); 64 | #endif 65 | #ifdef MBASE_PLATFORM_UNIX 66 | pthread_mutex_unlock(&mHandle); 67 | #endif 68 | } 69 | 70 | private: 71 | raw_handle mHandle; 72 | }; 73 | 74 | class lock_guard { 75 | public: 76 | MBASE_INLINE lock_guard(mbase::mutex& in_mutex) noexcept 77 | { 78 | mMutex = &in_mutex; 79 | mMutex->acquire(); 80 | } 81 | 82 | ~lock_guard() noexcept 83 | { 84 | mMutex->release(); 85 | } 86 | private: 87 | mbase::mutex* mMutex; 88 | }; 89 | 90 | MBASE_STD_END 91 | 92 | #endif // MBASE_SYNCHRONIZATION_H 93 | -------------------------------------------------------------------------------- /docs/source/inference/quickstart/embeddings_ex/downloading_model.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | Acquiring Model 3 | =============== 4 | 5 | .. tip:: 6 | 7 | If you already have a valid embedder model with GGUF format, you can skip this chapter. 8 | 9 | In order to generate embeddings, you first need to download an open-source 10 | embedder model from huggingface or some place anywhere. 11 | 12 | In our example, we will use the nomic-ai's `nomic-ai/nomic-embed-text-v1-GGUF `_ 13 | because it is popular and useful for demonstrative examples. However, what embedder model you use 14 | won't make a difference for our example, just make sure you have one in your system. 15 | 16 | MBASE Inference engine only supports "gguf" file format so that safetensors or any format that is not gguf, must 17 | be converted into gguf format. 18 | 19 | .. tip:: 20 | 21 | Non GGUF models can be converted into GGUF format. 22 | To see how to do it and in order to have in-depth knowledge of GGUF format, 23 | refer to: :ref:`gguf-convert` 24 | 25 | ------------------------------------ 26 | Downloading a Model from Huggingface 27 | ------------------------------------ 28 | 29 | The gguf file of nomic-embed-text-v1 is already being hosted on nomic-ai's huggingface repository. 30 | 31 | Repo link: https://huggingface.co/nomic-ai/nomic-embed-text-v1-GGUF 32 | 33 | ^^^^^^^^^^^^ 34 | From Website 35 | ^^^^^^^^^^^^ 36 | 37 | Step 1: Click the "Files and versions" tab: 38 | 39 | .. image:: hf_download_step1.png 40 | :align: center 41 | 42 | Step 2: Then download the Q8 quantized version of the model: 43 | 44 | .. image:: hf_download_step2.png 45 | :align: center 46 | 47 | ^^^^^^^^^^ 48 | Using wget 49 | ^^^^^^^^^^ 50 | 51 | Here is a wget command for people who love downloading using terminal: 52 | 53 | .. code-block:: bash 54 | 55 | wget https://huggingface.co/nomic-ai/nomic-embed-text-v1-GGUF/resolve/main/nomic-embed-text-v1.Q8_0.gguf 56 | -------------------------------------------------------------------------------- /include/mbase/framework/handler_base.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_HANDLER_BASE_H 2 | #define MBASE_HANDLER_BASE_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | class handler_base { 9 | public: 10 | using user_data = PTRGENERIC; 11 | 12 | /* ===== BUILDER METHODS BEGIN ===== */ 13 | MBASE_INLINE handler_base() noexcept; 14 | /* ===== BUILDER METHODS END ===== */ 15 | 16 | /* ===== OBSERVATION METHODS BEGIN ===== */ 17 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE user_data get_user_data() const noexcept; 18 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE U32 get_handler_id() const noexcept; 19 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE I32 get_thread_index() const noexcept; 20 | /* ===== OBSERVATION METHODS END ===== */ 21 | 22 | /* ===== STATE-MODIFIER METHODS BEGIN ===== */ 23 | MBASE_INLINE GENERIC set_user_data(user_data in_data) noexcept; 24 | MBASE_INLINE GENERIC _set_thread_index(I32 in_index) noexcept; 25 | virtual GENERIC on_call(user_data in_data) = 0; 26 | /* ===== STATE-MODIFIER METHODS END ===== */ 27 | 28 | protected: 29 | U32 mHandlerId; 30 | I32 mThreadIndex; 31 | user_data mSuppliedData; 32 | }; 33 | 34 | MBASE_INLINE handler_base::handler_base() noexcept : mHandlerId(0), mThreadIndex(-1), mSuppliedData(nullptr) 35 | { 36 | } 37 | 38 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE typename handler_base::user_data handler_base::get_user_data() const noexcept 39 | { 40 | return mSuppliedData; 41 | } 42 | 43 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE U32 handler_base::get_handler_id() const noexcept 44 | { 45 | return mHandlerId; 46 | } 47 | 48 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE I32 handler_base::get_thread_index() const noexcept 49 | { 50 | return mThreadIndex; 51 | } 52 | 53 | MBASE_INLINE GENERIC handler_base::set_user_data(user_data in_data) noexcept 54 | { 55 | mSuppliedData = in_data; 56 | } 57 | 58 | MBASE_INLINE GENERIC handler_base::_set_thread_index(I32 in_index) noexcept 59 | { 60 | mThreadIndex = in_index; 61 | } 62 | 63 | MBASE_END 64 | 65 | #endif // MBASE_HANDLER_BASE_H -------------------------------------------------------------------------------- /docs/source/inference/quickstart/single_prompt_ex/downloading_model.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | Acquiring Model 3 | =============== 4 | 5 | .. tip:: 6 | If you already have a valid text-to-text model with GGUF format, 7 | you can skip this chapter. 8 | 9 | In order to do inference, you first need to download an open-source LLM from huggingface or some place anywhere. 10 | The LLM we download should be Text-To-Text since the library supports only that for now. 11 | 12 | In our example, we will use the Microsoft's Phi-3-medium-128k-instruct LLM for inference because it is small in general which is suitable for our case. 13 | However, what Text-To-Text model you use won't make a difference for our example, just make sure you have one in your system. 14 | 15 | MBASE Inference engine only supports "gguf" file format so that safetensors or any format that is not gguf, must be converted into gguf format. 16 | 17 | .. tip:: 18 | Non GGUF models can be converted into GGUF format. 19 | To see how to do it and in order to have in-depth knowledge of GGUF format, 20 | refer to: :ref:`gguf-convert` 21 | 22 | ------------------------------------ 23 | Downloading a Model from Huggingface 24 | ------------------------------------ 25 | 26 | The gguf file of Phi-3-medium-128k-instruct LLM is already being hosted on MBASE Huggingface repository. 27 | 28 | Repo link: https://huggingface.co/MBASE/Phi-3-medium-128k-instruct-GGUF 29 | 30 | ^^^^^^^^^^^^ 31 | From Website 32 | ^^^^^^^^^^^^ 33 | 34 | Step 1: Click the "Files and versions" tab: 35 | 36 | .. image:: hf_download_step1.png 37 | :align: center 38 | 39 | Step 2: Then download the Q4 quantized version of the model: 40 | 41 | .. image:: hf_download_step2.png 42 | :align: center 43 | 44 | ^^^^^^^^^^ 45 | Using wget 46 | ^^^^^^^^^^ 47 | 48 | Here is a wget command for people who love downloading using terminal: 49 | 50 | .. code-block:: bash 51 | 52 | wget https://huggingface.co/MBASE/Phi-3-medium-128k-instruct-GGUF/resolve/main/Phi-3-mini-128k-instruct-q4_0.gguf 53 | -------------------------------------------------------------------------------- /cmake/MBASEJsonSys.cmake: -------------------------------------------------------------------------------- 1 | include_guard(GLOBAL) 2 | 3 | include(MBASECommonConfig) 4 | include(MBASEStdSys) 5 | 6 | set(MBASE_JSON_SYS_STRING "MBASE Json") 7 | set(MBASE_JSON_LIB_NAME "json") 8 | 9 | mbase_build_version(1 0 0 ${MBASE_VERSION_STABLE} MBASE_JSON_VERSION) 10 | mbase_build_include_install_path(${MBASE_JSON_LIB_NAME} MBASE_JSON_INCLUDE_INSTALL_PATH) 11 | mbase_build_lib_path(${MBASE_JSON_LIB_NAME} MBASE_JSON_LIB_PATH) 12 | 13 | list(APPEND MBASE_JSON_INCLUDE_DEPENDS ${MBASE_STD_INCLUDES} ${CMAKE_CURRENT_SOURCE_DIR}/external) 14 | 15 | add_library(double-conversion STATIC 16 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/bignum.cc 17 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/bignum-dtoa.cc 18 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/cached-powers.cc 19 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/double-to-string.cc 20 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/fast-dtoa.cc 21 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/fixed-dtoa.cc 22 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/string-to-double.cc 23 | ${CMAKE_CURRENT_SOURCE_DIR}/external/double-conversion/strtod.cc 24 | ) 25 | 26 | set_property(TARGET double-conversion PROPERTY POSITION_INDEPENDENT_CODE ON) 27 | 28 | message("JSON Include depends: ${MBASE_JSON_INCLUDE_DEPENDS}") 29 | 30 | list(APPEND MBASE_JSON_INCLUDE_STABLE_FILES 31 | json.h 32 | jtckdint.h 33 | ) 34 | 35 | foreach(MB_INCLUDE_FILE IN LISTS MBASE_JSON_INCLUDE_STABLE_FILES) 36 | list(APPEND MBASE_JSON_INCLUDE_INSTALL_FILES ${MBASE_JSON_INCLUDE_INSTALL_PATH}/${MB_INCLUDE_FILE}) 37 | endforeach() 38 | 39 | add_library(mb_json 40 | ${MBASE_JSON_LIB_PATH}/json.cpp 41 | ) 42 | 43 | target_compile_definitions(mb_json PRIVATE ${MBASE_COMMON_COMPILE_DEFINITIONS}) 44 | target_include_directories(mb_json PUBLIC ${MBASE_JSON_INCLUDE_DEPENDS}) 45 | target_link_libraries(mb_json PRIVATE double-conversion) 46 | 47 | set_property(TARGET mb_json PROPERTY POSITION_INDEPENDENT_CODE ON) -------------------------------------------------------------------------------- /docs/source/mcp/quick_start.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | Quickstart 3 | ========== 4 | 5 | .. tip:: 6 | It is highly recommended that you read the sections in order, as they are not arranged randomly in the document. 7 | 8 | In this chapter, we will cover everything to setup, run and develop using MBASE MCP library. 9 | Even if the setting up section covers everything to setup the MCP library, 10 | the concepts that are implemented in the examples section are just fundamental MCP features which 11 | are mostly sufficient for every-day usage of MCP. 12 | 13 | In order to use and understand the MCP library in detail, make sure you refer to the :doc:`information_reference`. 14 | 15 | --------- 16 | Structure 17 | --------- 18 | 19 | This chapter is composed of six sections which cover topics as follows: 20 | 21 | * :doc:`Setting-up`: Creating a CMake project and linking the MCP library. 22 | 23 | * :doc:`Complete Project Setup`: Properly configured :code:`CMakeLists.txt`, :code:`client.cpp` and :code:`server.cpp` for reference. 24 | 25 | * :doc:`MCP STDIO/HTTP Server Usage`: MCP STDIO/HTTP server implementation example. Codes under this section will be written under the :code:`server.cpp`. 26 | 27 | * :doc:`MCP STDIO/HTTP Client Usage`: MCP STDIO/HTTP client implementation example. Codes under this section will be written under the :code:`client.cpp`. 28 | 29 | * :doc:`Complete Source Code`: A Complete source code of both MCP client and server examples. 30 | 31 | * :doc:`What Now?`: Small-talk about MCP, where to find resources and advanced MCP SDK Usage. 32 | 33 | .. toctree:: 34 | :hidden: 35 | 36 | Setting-up 37 | Complete Project Setup 38 | MCP STDIO/HTTP Server Usage 39 | MCP STDIO/HTTP Client Usage 40 | Complete Source Code 41 | What Now? -------------------------------------------------------------------------------- /docs/source/inference/programs/typo-fixer/about.rst: -------------------------------------------------------------------------------- 1 | ================== 2 | Typo Fixer Program 3 | ================== 4 | 5 | ---------------------- 6 | Program Identification 7 | ---------------------- 8 | 9 | .. |pi_author| replace:: M\. Emre Erdog 10 | .. |pi_maintainer| replace:: M\. Emre Erdog 11 | .. |pi_email| replace:: erdog@mbasesoftware.com 12 | .. |pi_name| replace:: mbase_typo_fixer 13 | .. |pi_version| replace:: v0.1.0 14 | .. |pi_type| replace:: Utility, Example 15 | .. |pi_net_usage| replace:: No 16 | .. |pi_lib_depends| replace:: mbase-std mbase-inference 17 | .. |pi_repo_location| replace:: https://github.com/Emreerdog/mbase/tree/main/examples/typo-fixer 18 | 19 | .. include:: ../../../program_identification.rst 20 | 21 | -------- 22 | Synopsis 23 | -------- 24 | 25 | .. code-block:: bash 26 | 27 | mbase_typo_fixer model_path *[option [value]] 28 | mbase_typo_fixer model.gguf -gl 80 -s typo.txt -o fixed.txt 29 | 30 | ----------- 31 | Description 32 | ----------- 33 | 34 | This is an applied example use case of the MBASE library. 35 | The program is reading a user-supplied text file and fixing the typos. 36 | 37 | Fixed text will be written to a file specified by option :code:`-o` or :code:`--output-file`. 38 | 39 | ------- 40 | Options 41 | ------- 42 | 43 | .. option:: --help 44 | 45 | Print program information. 46 | 47 | .. option:: -v, --version 48 | 49 | Shows program version. 50 | 51 | .. option:: -t count, --thread-count count 52 | 53 | Amount of threads to use for token generation. (default=16) 54 | 55 | .. option:: -bt count, --batch-thread-count count 56 | 57 | Amount of thread to use for initial batch processing. (default=8) 58 | 59 | .. option:: -gl count, --gpu-layers count 60 | 61 | Number of layers too offload to GPU. 62 | Ignored if there is no GPU is present. (default=999) 63 | 64 | .. option:: -s file_path, --source-file file_path 65 | 66 | File containing the text with typos. 67 | 68 | .. option:: -o, file_path, --output-file file_path 69 | 70 | Output file where fixed text will be written to. 71 | -------------------------------------------------------------------------------- /include/mbase/pc/pc_program.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_PC_PROGRAM 2 | #define MBASE_PC_PROGRAM 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | class PcConfig; 13 | class PcIoManager; 14 | class PcNetManager; 15 | class PcDiagnostics; 16 | class PcState; 17 | 18 | struct MBASE_API PcProgramInformation { 19 | U16 mVersionMajor; 20 | U16 mVersionMinor; 21 | U16 mVersionPatch; 22 | mbase::string mProductVersion; 23 | mbase::string mProductId; 24 | mbase::string mProductName; 25 | mbase::string mProductAuthor; 26 | mbase::string mProductLicence; 27 | mbase::string mMbaseVersion; 28 | }; 29 | 30 | class MBASE_API PcProgramBase : public non_copymovable { 31 | public: 32 | using MBDate = U32; // placeholder: not a real date 33 | using size_type = SIZE_T; 34 | 35 | PcProgramBase(); 36 | ~PcProgramBase(); 37 | 38 | PcConfig* get_config() noexcept; 39 | PcDiagnostics* get_diagnostics_manager() noexcept; 40 | PcIoManager* get_io_manager() noexcept; 41 | PcNetManager* get_net_manager() noexcept; 42 | PcProgramInformation get_program_info() const noexcept; 43 | timer_loop* get_timer_loop() noexcept; 44 | bool is_running() const noexcept; 45 | bool is_initialized() const noexcept; 46 | 47 | GENERIC initialize_system( 48 | PcProgramInformation in_program_info, 49 | PcConfig* in_configurator = NULL, 50 | PcDiagnostics* in_diagnostics = NULL, 51 | PcIoManager* in_io_manager = NULL, 52 | PcNetManager* in_net_manager = NULL, 53 | PcState* in_program_state = NULL 54 | ); 55 | virtual GENERIC update() = 0; 56 | GENERIC update_defaults(); 57 | bool halt(); 58 | bool exit(I32 in_code, mbase::string in_message = ""); 59 | 60 | private: 61 | PcConfig* mConfig; 62 | PcDiagnostics* mDiagnostics; 63 | PcIoManager* mIoManager; 64 | PcNetManager* mNetManager; 65 | PcState* mProgramState; 66 | PcProgramInformation mProgramInfo; 67 | timer_loop mTimerLoop; 68 | bool mIsRunning; 69 | bool mIsAuthorized; 70 | bool mIsInitialized; 71 | }; 72 | 73 | MBASE_END 74 | 75 | #endif // !MBASE_PC_PROGRAM 76 | -------------------------------------------------------------------------------- /scripts/maip_builder.py: -------------------------------------------------------------------------------- 1 | class MaipBuilder: 2 | def __init__(self): 3 | self.major=1 4 | self.minor=0 5 | self.identification_line = "" 6 | self.version_name = "MAIP" 7 | self.kvals = {} 8 | self.user_message = "" 9 | self.end = "END" 10 | self.length="" 11 | self.length_word = "LENGTH" 12 | 13 | def set_version(self,major,minor): 14 | self.major=major 15 | self.minor=minor 16 | 17 | def get_version(self): 18 | return f"{self.version_name}{self.major}.{self.minor}" 19 | 20 | def set_request_identification(self, var, inf_acq_model): 21 | formatted_string = f"{self.version_name}{self.major}.{self.minor} {var} {inf_acq_model} " 22 | self.identification_line = formatted_string 23 | return self.identification_line 24 | 25 | def user_input(self,client_input): 26 | self.user_message = client_input 27 | return self.user_message 28 | 29 | def length_of_word_function(self,client_input): 30 | self.length=len(client_input) 31 | length_format = f"{self.length_word}:{self.length}" 32 | return length_format 33 | 34 | def set_kval(self,key,value): 35 | if self.kvals.get(key) == None: 36 | self.kvals[key] = [] 37 | self.kvals[key].append(str(value)) 38 | else: 39 | self.kvals[key].append(";" + str(value)) 40 | 41 | return f"{key}:{value}" 42 | 43 | def end_word(self): 44 | return self.end 45 | 46 | def generate_payload(self, client_input = ""): 47 | generatePayload = self.identification_line + '\n' 48 | dataLength = len(client_input.encode()) 49 | if dataLength: 50 | self.set_kval("LENGTH", dataLength) 51 | self.user_message = client_input 52 | 53 | for msg in self.kvals: 54 | generatePayload += msg + ":" 55 | for msgValue in self.kvals[msg]: 56 | generatePayload += msgValue 57 | generatePayload+='\n' 58 | generatePayload += self.end + '\n' 59 | generatePayload += self.user_message 60 | return generatePayload -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_responses.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_RESPONSES_H 2 | #define MBASE_MCP_SERVER_RESPONSES_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | enum class mcp_response_type { 9 | TEXT, 10 | IMAGE, 11 | BINARY 12 | }; 13 | 14 | struct McpResponseTextResource { 15 | mbase::string mUri; 16 | mbase::string mMimeType; 17 | mbase::string mText; 18 | }; 19 | 20 | struct McpResponseBinaryResource { 21 | mbase::string mUri; 22 | mbase::string mMimeType; 23 | mbase::string mBlob; 24 | }; 25 | 26 | struct McpResponseTextPrompt { 27 | mbase::string mRole = "user"; 28 | mbase::string mType = "text"; 29 | mbase::string mText; 30 | }; 31 | 32 | struct McpResponseImagePrompt { 33 | mbase::string mRole = "user"; 34 | mbase::string mType = "image"; 35 | mbase::string mMimeType; // "image/png" ... 36 | mbase::string mData; // base64-encoded-image-data 37 | }; 38 | 39 | struct McpResponseAudioPrompt { 40 | mbase::string mRole = "user"; 41 | mbase::string mType = "audio"; 42 | mbase::string mMimeType; // audio/wav etc. 43 | mbase::string mData; // base-64-encoded-audio-data 44 | }; 45 | 46 | struct McpResponseTextTool { 47 | mbase::string mType = "text"; 48 | mbase::string mText; 49 | }; 50 | 51 | struct McpResponseImageTool { 52 | mbase::string mType = "image"; 53 | mbase::string mMimeType; // "image/png" ... 54 | mbase::string mData; // base64-encoded-image-data 55 | }; 56 | 57 | struct McpResponseAudioTool { 58 | mbase::string mType = "audio"; 59 | mbase::string mMimeType; // "audio/wav" ... 60 | mbase::string mData; // base64-encoded-audio-data 61 | }; 62 | 63 | struct McpPromptCompilationResult { 64 | mbase::vector mValues; 65 | I32 mTotal = 0; 66 | bool mHasMore = false; 67 | }; 68 | 69 | using McpResponseResource = std::variant; 70 | using McpResponsePrompt = std::variant; 71 | using McpResponseTool = std::variant; 72 | 73 | MBASE_END 74 | 75 | #endif // MBASE_MCP_SERVER_RESPONSES_H -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_client_callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_CLIENT_CALLBACKS_H 2 | #define MBASE_MCP_CLIENT_CALLBACKS_H 3 | 4 | #include 5 | #include // Response objects 6 | #include // Description objects 7 | 8 | MBASE_BEGIN 9 | 10 | class McpClientBase; 11 | class McpServerStateBase; 12 | 13 | using mcp_init_cb = std::function; 14 | using mcp_read_resource_cb = std::function&&)>; 15 | using mcp_get_prompt_cb = std::function&&)>; 16 | using mcp_tool_call_cb = std::function&&, bool /* is error? */)>; 17 | using mcp_list_resources_cb = std::function&&, const mbase::string&)>; 18 | using mcp_list_prompts_cb = std::function&&, const mbase::string&)>; 19 | using mcp_list_tools_cb = std::function&&, const mbase::string&)>; 20 | using mcp_prompt_compilation_cb = std::function; 21 | using mcp_empty_cb = std::function; 22 | 23 | using mcp_response_callback = std::variant< 24 | mcp_init_cb, 25 | mcp_read_resource_cb, 26 | mcp_get_prompt_cb, 27 | mcp_tool_call_cb, 28 | mcp_list_resources_cb, 29 | mcp_list_prompts_cb, 30 | mcp_list_tools_cb, 31 | mcp_prompt_compilation_cb, 32 | mcp_empty_cb>; 33 | 34 | struct McpServerRequestState { 35 | mcp_response_callback responseCallback; 36 | mbase::string mMessageId; 37 | mbase::I64 mTimeoutInSeconds = 10; 38 | mbase::I64 mAttemptCount = 0; 39 | }; 40 | 41 | struct McpServerResponseObject { 42 | mcp_response_callback responseCallback; 43 | mbase::Json mResponseParams; 44 | I32 mErrorCode = MBASE_MCP_SUCCESS; 45 | }; 46 | 47 | MBASE_END 48 | 49 | #endif // MBASE_MCP_CLIENT_CALLBACKS_H -------------------------------------------------------------------------------- /examples/openai-server/model.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OPENAI_MODEL_H 2 | #define MBASE_OPENAI_MODEL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | class OpenaiTextToTextProcessor; 13 | class OpenaiEmbedderProcessor; 14 | 15 | class OpenaiModel : public mbase::InfModelTextToText { 16 | public: 17 | 18 | OpenaiModel(); 19 | 20 | enum init_proc_err { 21 | PROC_SUCCESS, 22 | ERR_INVALID_PROCESSOR_COUNT, 23 | ERR_INVALID_THREAD_COUNT, 24 | ERR_INVALID_BATCH_THREAD_COUNT, 25 | ERR_INVALID_CONTEXT_LENGTH, 26 | ERR_INVALID_BATCH_LENGTH, 27 | }; 28 | U64 get_creation_date_in_epoch(); 29 | init_proc_err initialize_t2t_processors( 30 | const U32& in_processor_count, 31 | const U32& in_thread_count, 32 | const U32& in_batch_thread_count, 33 | const U32& in_context_length, 34 | const U32& in_batch_length, 35 | const inf_sampling_set& in_sampling_set 36 | ); 37 | init_proc_err initialize_embedder_processors( 38 | const U32& in_processor_count, 39 | const U32& in_context_length, 40 | const U32& in_thread_count 41 | ); 42 | bool acquire_processor(OpenaiTextToTextProcessor*& out_processor); 43 | bool acquire_processor(OpenaiEmbedderProcessor*& out_processor); 44 | bool is_init_finished(); 45 | GENERIC release_processor(OpenaiTextToTextProcessor* in_processor); 46 | GENERIC release_processor(OpenaiEmbedderProcessor* out_processor); 47 | GENERIC _incr_processor_count(); 48 | 49 | GENERIC on_initialize_fail(init_fail_code out_fail_code) override; 50 | GENERIC on_initialize() override; 51 | GENERIC on_destroy() override; 52 | 53 | private: 54 | mbase::mutex mProcDistributionSync; 55 | mbase::vector mAvailableT2tProcessors; // will leak memory but its okay. 56 | mbase::vector mAvailableEmbedderProcessors; 57 | U32 mAccessLimit; 58 | U64 mCreationDate; 59 | I32 mProcRgrCounter = 0; 60 | }; 61 | 62 | MBASE_END 63 | 64 | #endif // MBASE_OPENAI_MODEL_H -------------------------------------------------------------------------------- /python/mbasepy/_core.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static PyObject* py_get_sys_name_total(PyObject*, PyObject*) { 6 | mbase::string result = mbase::inf_get_sys_name_total(); 7 | return PyUnicode_FromString(result.c_str()); 8 | } 9 | 10 | static PyObject* py_cosine_similarity(PyObject*, PyObject* args) { 11 | PyObject* list1; PyObject* list2; 12 | if (!PyArg_ParseTuple(args, "OO", &list1, &list2)) 13 | return NULL; 14 | if (!PySequence_Check(list1) || !PySequence_Check(list2)) { 15 | PyErr_SetString(PyExc_TypeError, "arguments must be sequences"); 16 | return NULL; 17 | } 18 | Py_ssize_t len1 = PySequence_Size(list1); 19 | Py_ssize_t len2 = PySequence_Size(list2); 20 | if (len1 != len2) { 21 | PyErr_SetString(PyExc_ValueError, "vectors must be same length"); 22 | return NULL; 23 | } 24 | std::vector v1(len1), v2(len2); 25 | for (Py_ssize_t i = 0; i < len1; ++i) { 26 | PyObject* item1 = PySequence_GetItem(list1, i); 27 | PyObject* item2 = PySequence_GetItem(list2, i); 28 | if (!item1 || !item2) { 29 | Py_XDECREF(item1); 30 | Py_XDECREF(item2); 31 | PyErr_SetString(PyExc_RuntimeError, "invalid sequence element"); 32 | return NULL; 33 | } 34 | v1[i] = static_cast(PyFloat_AsDouble(item1)); 35 | v2[i] = static_cast(PyFloat_AsDouble(item2)); 36 | Py_DECREF(item1); 37 | Py_DECREF(item2); 38 | } 39 | float res = mbase::inf_common_cosine_similarity(v1.data(), v2.data(), (int)len1); 40 | return PyFloat_FromDouble(res); 41 | } 42 | 43 | static PyMethodDef Methods[] = { 44 | {"get_sys_name_total", py_get_sys_name_total, METH_NOARGS, "Return library name"}, 45 | {"cosine_similarity", py_cosine_similarity, METH_VARARGS, "Compute cosine similarity"}, 46 | {NULL, NULL, 0, NULL} 47 | }; 48 | 49 | static struct PyModuleDef moduledef = { 50 | PyModuleDef_HEAD_INIT, 51 | "_core", 52 | "MBASE Python bindings", 53 | -1, 54 | Methods, 55 | NULL, NULL, NULL, NULL 56 | }; 57 | 58 | PyMODINIT_FUNC PyInit__core(void) { 59 | return PyModule_Create(&moduledef); 60 | } 61 | -------------------------------------------------------------------------------- /examples/openai-server/openai_errors.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OPENAI_ERRORS_H 2 | #define MBASE_OPENAI_ERRORS_H 3 | 4 | #include 5 | #include 6 | 7 | MBASE_BEGIN 8 | 9 | mbase::string buildOpenaiError( 10 | const mbase::string& in_message, 11 | const mbase::string& in_type, 12 | const mbase::string& in_code) 13 | { 14 | mbase::Json errorDescription; 15 | errorDescription["message"] = in_message; 16 | errorDescription["type"] = in_type; 17 | errorDescription["code"] = in_code; 18 | 19 | mbase::Json openaiError; 20 | openaiError["error"] = errorDescription; 21 | 22 | return openaiError.toString(); 23 | } 24 | 25 | GENERIC sendOpenaiError( 26 | const httplib::Request& in_req, 27 | httplib::Response& in_resp, 28 | const mbase::string& in_message, 29 | const mbase::string& in_type, 30 | const mbase::string& in_code 31 | ) 32 | { 33 | mbase::string responseString = buildOpenaiError(in_message, in_type, in_code); 34 | in_resp.set_content(responseString.c_str(), responseString.size(), "application/json"); 35 | } 36 | 37 | bool openaiAuthCheck(const httplib::Request& in_req, httplib::Response& in_resp, const mbase::string& in_server_key, mbase::string& out_provided_key) 38 | { 39 | if(!in_server_key.size()) 40 | { 41 | return true; 42 | } 43 | 44 | if(in_req.has_header("Authorization")) 45 | { 46 | std::string authToken = in_req.get_header_value("Authorization"); 47 | mbase::string authTokenField(authToken.c_str(), authToken.size()); 48 | mbase::vector seperatedField; 49 | 50 | authTokenField.split(" ", seperatedField); 51 | if(seperatedField.size() != 2) // 'Bearer' and 'key' 52 | { 53 | return false; 54 | } 55 | 56 | mbase::string bearerString = seperatedField[0]; 57 | out_provided_key = seperatedField[1]; 58 | 59 | if(bearerString != "Bearer") 60 | { 61 | return false; 62 | } 63 | 64 | if(out_provided_key != in_server_key) 65 | { 66 | return false; 67 | } 68 | 69 | return true; 70 | } 71 | 72 | return false; 73 | } 74 | 75 | MBASE_END 76 | 77 | #endif // MBASE_OPENAI_ERRORS_H -------------------------------------------------------------------------------- /examples/openai-server/processor.cpp: -------------------------------------------------------------------------------- 1 | #include "processor.h" 2 | #include "global_state.h" 3 | 4 | MBASE_BEGIN 5 | 6 | OpenaiTextToTextProcessor::OpenaiTextToTextProcessor(const I32& in_processor_index) 7 | { 8 | mProcessorIndex = in_processor_index; 9 | } 10 | 11 | OpenaiTextToTextProcessor::~OpenaiTextToTextProcessor() 12 | { 13 | this->release_inference_client_stacked(); 14 | } 15 | 16 | GENERIC OpenaiTextToTextProcessor::on_initializing() 17 | { 18 | 19 | } 20 | 21 | GENERIC OpenaiTextToTextProcessor::on_initialize_fail(last_fail_code out_code) 22 | { 23 | std::cout << "ERR: TextToText processor " << mProcessorIndex <<" failed to initialize." << std::endl; 24 | std::cout << "INFO: Make sure you have enough memory for such operation." << std::endl; 25 | exit(1); 26 | } 27 | 28 | GENERIC OpenaiTextToTextProcessor::on_initialize() 29 | { 30 | printf("TextToText processor %d is initialized\n", mProcessorIndex); 31 | OpenaiModel* tmpModel = static_cast(this->get_processed_model()); 32 | tmpModel->_incr_processor_count(); 33 | this->set_inference_client(&mTextClient); 34 | } 35 | 36 | GENERIC OpenaiTextToTextProcessor::on_destroy() 37 | { 38 | 39 | } 40 | 41 | OpenaiEmbedderProcessor::OpenaiEmbedderProcessor(const I32& in_processor_index) 42 | { 43 | mProcessorIndex = in_processor_index; 44 | } 45 | 46 | OpenaiEmbedderProcessor::~OpenaiEmbedderProcessor() 47 | { 48 | this->release_inference_client_stacked(); 49 | } 50 | 51 | GENERIC OpenaiEmbedderProcessor::on_initializing() 52 | { 53 | 54 | } 55 | 56 | GENERIC OpenaiEmbedderProcessor::on_initialize_fail(last_fail_code out_code) 57 | { 58 | std::cout << "ERR: Embedder processor " << mProcessorIndex <<" failed to initialize." << std::endl; 59 | std::cout << "INFO: Make sure you have enough memory for such operation." << std::endl; 60 | exit(1); 61 | } 62 | 63 | GENERIC OpenaiEmbedderProcessor::on_initialize() 64 | { 65 | printf("Embedder processor %d is initialized\n", mProcessorIndex); 66 | OpenaiModel* tmpModel = static_cast(this->get_processed_model()); 67 | tmpModel->_incr_processor_count(); 68 | this->set_inference_client(&mEmbedderClient); 69 | } 70 | 71 | GENERIC OpenaiEmbedderProcessor::on_destroy() 72 | { 73 | 74 | } 75 | 76 | 77 | MBASE_END -------------------------------------------------------------------------------- /include/mbase/algorithm.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_ALGORITHM_H 2 | #define MBASE_ALGORITHM_H 3 | 4 | #include 5 | 6 | MBASE_STD_BEGIN 7 | 8 | template 9 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE InputIt find(InputIt in_first, InputIt in_last, const Type& in_value) 10 | { 11 | for(; in_first != in_last; in_first++) 12 | { 13 | if(*in_first == in_value) 14 | { 15 | return in_first; 16 | } 17 | } 18 | return in_last; 19 | } 20 | 21 | #undef max 22 | template 23 | MBASE_INLINE_EXPR const Type& max(const Type& in_left, const Type& in_right) 24 | { 25 | return in_left > in_right ? in_left : in_right; 26 | } 27 | 28 | #undef min 29 | template 30 | MBASE_INLINE_EXPR const Type& min(const Type& in_left, const Type& in_right) 31 | { 32 | return in_left < in_right ? in_left : in_right; 33 | } 34 | 35 | template 36 | MBASE_INLINE_EXPR Iterator max_element(Iterator in_first, Iterator in_last) 37 | { 38 | if (in_first == in_last) 39 | { 40 | return in_last; 41 | } 42 | 43 | Iterator largest = in_first; 44 | 45 | while (++in_first != in_last) 46 | { 47 | if (*largest < *in_first) 48 | { 49 | largest = in_first; 50 | } 51 | } 52 | 53 | return largest; 54 | } 55 | 56 | template 57 | MBASE_INLINE_EXPR Iterator min_element(Iterator in_first, Iterator in_last) 58 | { 59 | if (in_first == in_last) 60 | { 61 | return in_last; 62 | } 63 | 64 | Iterator smallest = in_first; 65 | 66 | while (++in_first != in_last) 67 | { 68 | if (*smallest > *in_first) 69 | { 70 | smallest = in_first; 71 | } 72 | } 73 | 74 | return smallest; 75 | } 76 | 77 | template 78 | MBASE_INLINE_EXPR typename Iterator::value_type average_element(Iterator in_first, Iterator in_last) 79 | { 80 | typename Iterator::value_type avg = 0; 81 | SIZE_T elemCount = 0; 82 | for(; in_first != in_last; ++in_first) 83 | { 84 | avg += *in_first; 85 | ++elemCount; 86 | } 87 | 88 | if(!elemCount) 89 | { 90 | return 0; 91 | } 92 | 93 | return avg / elemCount; 94 | } 95 | 96 | MBASE_STD_END 97 | 98 | #endif // MBASE_ALGORITHM_H -------------------------------------------------------------------------------- /docs/source/inference/programs/retrieval/about.rst: -------------------------------------------------------------------------------- 1 | ================= 2 | Retrieval Program 3 | ================= 4 | 5 | ---------------------- 6 | Program Identification 7 | ---------------------- 8 | 9 | .. |pi_author| replace:: M\. Emre Erdog 10 | .. |pi_maintainer| replace:: M\. Emre Erdog 11 | .. |pi_email| replace:: erdog@mbasesoftware.com 12 | .. |pi_name| replace:: mbase_retrieval 13 | .. |pi_version| replace:: v0.1.0 14 | .. |pi_type| replace:: Example 15 | .. |pi_net_usage| replace:: No 16 | .. |pi_lib_depends| replace:: mbase-std mbase-inference 17 | .. |pi_repo_location| replace:: https://github.com/Emreerdog/mbase/tree/main/examples/retrieval 18 | 19 | .. include:: ../../../program_identification.rst 20 | 21 | -------- 22 | Synopsis 23 | -------- 24 | 25 | .. code-block:: bash 26 | 27 | mbase_retrieval model_path *[option [value]] 28 | mbase_retrieval model.gguf -q 'What is MBASE' -pf file1.txt -pf file2.txt -gl 80 29 | 30 | ----------- 31 | Description 32 | ----------- 33 | 34 | An example for calculating the distance between the 35 | given query and multiple text files our documents in this context and applying retrieval operation on multiple texts seperated by . 36 | 37 | What this program does is that it takes a query from the user and creates the embeddings of the user input and all other texts. 38 | After the embeddings are generated, it applies the cosine similarity function to all embeddings and shows the distance of the query to each text. 39 | 40 | ------- 41 | Options 42 | ------- 43 | 44 | .. option:: -h, --help 45 | 46 | Print program information. 47 | 48 | .. option:: -v, --version 49 | 50 | Shows program version. 51 | 52 | .. option:: -pf file_path, --prompt-file file_path 53 | 54 | File containing prompt or prompts seperated by the seperator (default=''). 55 | To give multiple prompt files, call this option multiple times. 56 | 57 | .. option:: -q string, --query string 58 | 59 | User provided query. 60 | 61 | .. option:: -sp seperator, --seperator seperator 62 | 63 | Prompt seperator (default=""). 64 | 65 | .. option:: -t count, --thread-count count 66 | 67 | Threads used to compute embeddings (default=16). 68 | 69 | .. option:: -gl count, --gpu-layers count 70 | 71 | Number of layers too offload to GPU. 72 | Ignored if there is no GPU is present. (default=999) 73 | -------------------------------------------------------------------------------- /mbase/pc/pc_stream_manager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MBASE_BEGIN 4 | 5 | PcStreamManager::flags PcStreamManager::get_stream_by_handle(stream_handle& in_stream_handle, char_stream*& out_stream) 6 | { 7 | // TODO: DO ERROR CHECK 8 | out_stream = &mStreams[in_stream_handle]; 9 | 10 | return flags::STREAM_MNG_SUCCESS; 11 | } 12 | 13 | U32 PcStreamManager::get_stream_count() 14 | { 15 | return mStreamCount; 16 | } 17 | 18 | U32 PcStreamManager::get_stream_size() 19 | { 20 | return mStreamSize; 21 | } 22 | 23 | bool PcStreamManager::initialize(U32 in_stream_count, U32 in_stream_size) 24 | { 25 | mStreamCount = in_stream_count; 26 | mStreamSize = in_stream_size; 27 | 28 | if (!mStreamCount) 29 | { 30 | mStreamCount = gDefaultStreamCount; 31 | } 32 | 33 | if (!mStreamSize) 34 | { 35 | mStreamSize = gDefaultStreamSize; 36 | } 37 | 38 | mStreams = mbase::vector(in_stream_count); 39 | 40 | for (U32 i = 0; i < mStreamCount; i++) 41 | { 42 | mStreams.emplace_back(deep_char_stream(mStreamSize)); 43 | } 44 | 45 | for (U32 i = mStreamCount - 1; i > 0; i--) 46 | { 47 | mHandleStack.push(i); 48 | } 49 | 50 | return true; 51 | } 52 | 53 | PcStreamManager::flags PcStreamManager::acquire_stream(stream_handle& out_stream_handle) 54 | { 55 | if (mHandleStack.empty()) 56 | { 57 | return flags::STREAM_ERR_STREAMS_ARE_FULL; 58 | } 59 | // TODO: DO ERROR CHECK 60 | out_stream_handle = mHandleStack.top(); 61 | mHandleStack.pop(); 62 | mStreams[out_stream_handle].set_cursor_front(); 63 | return flags::STREAM_MNG_SUCCESS; 64 | } 65 | 66 | PcStreamManager::flags PcStreamManager::acquire_stream(stream_handle& out_stream_handle, char_stream*& out_stream) 67 | { 68 | if (mHandleStack.empty()) 69 | { 70 | return flags::STREAM_ERR_STREAMS_ARE_FULL; 71 | } 72 | // TODO: DO ERROR CHECK 73 | out_stream_handle = mHandleStack.top(); 74 | mHandleStack.pop(); 75 | out_stream = &mStreams[out_stream_handle]; 76 | out_stream->set_cursor_front(); 77 | 78 | return flags::STREAM_MNG_SUCCESS; 79 | } 80 | 81 | PcStreamManager::flags PcStreamManager::release_stream(stream_handle& in_stream_handle) 82 | { 83 | // TODO: DO ERROR CHECK 84 | if (in_stream_handle < 0) 85 | { 86 | return flags::STREAM_ERR_INVALID_HANDLE; 87 | } 88 | 89 | mHandleStack.push(in_stream_handle); 90 | return flags::STREAM_MNG_SUCCESS; 91 | } 92 | 93 | MBASE_END -------------------------------------------------------------------------------- /docs/source/inference/quickstart/project_setup.rst: -------------------------------------------------------------------------------- 1 | ====================== 2 | Complete Project Setup 3 | ====================== 4 | 5 | From now on, we will use the **CMakeLists.txt** and **main.cpp** files as a base 6 | for every demonstration example we build together. 7 | 8 | There are three simple inference examples implemented on the next three chapters. 9 | 10 | * :doc:`Single-Prompt `: First one is a simple prompt and answer example. At the end of the example, the user will supply a prompt in the terminal and LLM will give the response. 11 | * :doc:`Dialogue-Example `: The second is a more complex dialogue based prompt and answer example. At the end of the example, the user will be able to have a dialogue with LLM using terminal. 12 | * :doc:`Embedding-Example `: The third one is a vector embedding generator which is generally used by RAG programs and more. At the end of the example, the user will supply an input and vector embeddings will be generated by using embedder LLM model. 13 | 14 | .. important:: 15 | Unlike third example, the first two examples are inter-connected so that when you finish the first example, you will use that result as a base to implement the second example. 16 | 17 | Total CMakeLists.txt file: 18 | 19 | .. code-block:: cmake 20 | :caption: CMakeLists.txt 21 | 22 | cmake_minimum_required(VERSION 3.15...3.31) 23 | project("mbase_simple_project" LANGUAGES CXX) 24 | 25 | add_executable(simple_project main.cpp) 26 | 27 | find_package(mbase.libs REQUIRED COMPONENTS std inference) 28 | 29 | target_compile_features(simple_project PUBLIC cxx_std_17) 30 | target_link_libraries(simple_project PRIVATE mbase-std mbase-inference) 31 | target_include_directories(simple_project PUBLIC mbase-std mbase-inference) 32 | 33 | Total main.cpp file: 34 | 35 | .. code-block:: cpp 36 | :caption: main.cpp 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | int main() 43 | { 44 | mbase::vector deviceDesc = mbase::inf_query_devices(); 45 | 46 | for(mbase::vector::iterator It = deviceDesc.begin(); It != deviceDesc.end(); It++) 47 | { 48 | std::cout << It->get_device_description() << std::endl; 49 | } 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /docs/source/inference/programs/embedding/about.rst: -------------------------------------------------------------------------------- 1 | ================= 2 | Embedding Program 3 | ================= 4 | 5 | ---------------------- 6 | Program Identification 7 | ---------------------- 8 | 9 | .. |pi_author| replace:: M\. Emre Erdog 10 | .. |pi_maintainer| replace:: M\. Emre Erdog 11 | .. |pi_email| replace:: erdog@mbasesoftware.com 12 | .. |pi_name| replace:: mbase_embedding_simple 13 | .. |pi_version| replace:: v0.1.0 14 | .. |pi_type| replace:: Utility, Example 15 | .. |pi_net_usage| replace:: No 16 | .. |pi_lib_depends| replace:: mbase-std mbase-inference 17 | .. |pi_repo_location| replace:: https://github.com/Emreerdog/mbase/tree/main/examples/embedding 18 | 19 | .. include:: ../../../program_identification.rst 20 | 21 | -------- 22 | Synopsis 23 | -------- 24 | 25 | .. code-block:: bash 26 | 27 | mbase_embedding_simple model_path *[option [value]] 28 | mbase_embedding_simple model.gguf -gl 80 -p 'What is life?' 29 | mbase_embedding_simple model.gguf -gl 80 -pf prompt1.txt -pf prompt2.txt 30 | 31 | ----------- 32 | Description 33 | ----------- 34 | 35 | An example program for generating the embeddings of the given prompt or prompts. 36 | The given implementation is stable and shows the basics of how to generate embeddings using MBASE embedder processor :code:`InfEmbedderProcessor`. 37 | 38 | ------- 39 | Options 40 | ------- 41 | 42 | .. option:: -h, --help 43 | 44 | Print program information. 45 | 46 | .. option:: -v, --version 47 | 48 | Shows program version. 49 | 50 | .. option:: -pf file_path, --prompt-file file_path 51 | 52 | File containing prompt or prompts seperated by the seperator (default=''). If prompt is given, prompt file will be ignored. 53 | 54 | .. option:: -p string, --prompt string 55 | 56 | Prompt or prompts seperated by the seperator (default=''). This will be used even if the prompt file is supplied. 57 | 58 | .. option:: -sp seperator, --seperator seperator 59 | 60 | Prompt seperator (default=""). 61 | 62 | .. option:: -t count, --thread-count count 63 | 64 | Threads used to compute embeddings (default=16). 65 | 66 | .. option:: -gl count, --gpu-layers count 67 | 68 | Number of layers too offload to GPU. 69 | Ignored if there is no GPU is present. (default=999) 70 | 71 | .. option:: -jout output_path, --json-output-path output_path 72 | 73 | If the json output path is specified, result will be written there in file "openai_embeddings.json" (default=''). 74 | -------------------------------------------------------------------------------- /docs/source/inference/quickstart/dialogue_ex/about.rst: -------------------------------------------------------------------------------- 1 | ====================== 2 | About Dialogue Example 3 | ====================== 4 | 5 | In this example, we will extend the :doc:`single-prompt <../single_prompt_ex/about>` example and have a dialogue 6 | with the LLM. We will be having a dialogue with the LLM or in other words, making the LLM remember 7 | the context, old messages etc. this can be achieved by using either of two methods given as: 8 | 9 | * Prompt Resupplying (slower but easier to understand). 10 | * Manual Caching (much faster but requires attention). 11 | 12 | ------------------------ 13 | Brief on Prompt Resupply 14 | ------------------------ 15 | 16 | In prompt resupplying, user literally supplies the entire dialogue every time 17 | with the last message user sends. 18 | 19 | This is also the way how OpenAI API handles conversation history. 20 | The reason it is slower is that the entire prompt history is being computed and all KVs are generated 21 | every time a new message is sent. 22 | 23 | The default behavior of the processor is deleting the KV cache every time a new input comes. 24 | That is why you should supply the entire history every time. 25 | However, with the manual caching method, this could be prevented and achieve much higher performance but requires some understanding 26 | and management. 27 | 28 | ----------------------- 29 | Brief on Manual Caching 30 | ----------------------- 31 | 32 | In manual caching, user tells the processor that he/she doesn't want the processor to clear the cache every call by 33 | setting up the manual cache switch with logit store or kv lock mode using the :code:`set_manual_caching` method of the processor. 34 | 35 | By setting the logit store mode, processor will preserve the kv keys that are generated by the last prompt and response. 36 | By this way, the conversation is maintained internally by the processor. However, the user should clear the kv cache 37 | by himself using :code:`clear_kv_cache` method. 38 | 39 | We will make an implementation for both of these cases individually. 40 | 41 | .. note:: 42 | Make sure to refer to :doc:`../../info-reference/processor-in-detail` for further explanation. 43 | 44 | .. toctree:: 45 | :hidden: 46 | 47 | Context Lines 48 | Prompt Resupplying 49 | Complete Program Resupplying 50 | Manual Caching 51 | Complete Program Caching 52 | 53 | 54 | -------------------------------------------------------------------------------- /mbase/pc/pc_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | MBASE_BEGIN 9 | 10 | PcProgramBase::PcProgramBase() : 11 | mConfig(NULL), 12 | mDiagnostics(NULL), 13 | mIoManager(NULL), 14 | mNetManager(NULL), 15 | mProgramState(NULL), 16 | mIsRunning(false), 17 | mIsAuthorized(false), 18 | mIsInitialized(false) 19 | { 20 | 21 | } 22 | 23 | PcProgramBase::~PcProgramBase() 24 | { 25 | 26 | } 27 | 28 | PcConfig* PcProgramBase::get_config() noexcept 29 | { 30 | return mConfig; 31 | } 32 | 33 | PcDiagnostics* PcProgramBase::get_diagnostics_manager() noexcept 34 | { 35 | return mDiagnostics; 36 | } 37 | 38 | PcIoManager* PcProgramBase::get_io_manager() noexcept 39 | { 40 | return mIoManager; 41 | } 42 | 43 | PcNetManager* PcProgramBase::get_net_manager() noexcept 44 | { 45 | return mNetManager; 46 | } 47 | 48 | PcProgramInformation PcProgramBase::get_program_info() const noexcept 49 | { 50 | return mProgramInfo; 51 | } 52 | 53 | timer_loop* PcProgramBase::get_timer_loop() noexcept 54 | { 55 | return &mTimerLoop; 56 | } 57 | 58 | bool PcProgramBase::is_running() const noexcept 59 | { 60 | return mIsRunning; 61 | } 62 | 63 | bool PcProgramBase::is_initialized() const noexcept 64 | { 65 | return mIsInitialized; 66 | } 67 | 68 | GENERIC PcProgramBase::initialize_system( 69 | PcProgramInformation in_program_info, 70 | PcConfig* in_configurator, 71 | PcDiagnostics* in_diagnostics, 72 | PcIoManager* in_io_manager, 73 | PcNetManager* in_net_manager, 74 | PcState* in_program_state 75 | ) 76 | { 77 | mProgramInfo = in_program_info; 78 | mConfig = in_configurator; 79 | mDiagnostics = in_diagnostics; 80 | mIoManager = in_io_manager; 81 | mNetManager = in_net_manager; 82 | mProgramState = in_program_state; 83 | } 84 | 85 | GENERIC PcProgramBase::update_defaults() 86 | { 87 | mConfig->update(); 88 | mProgramState->update(); 89 | mNetManager->update(); 90 | //mIoManager->update(); 91 | mTimerLoop.run_timers(); 92 | } 93 | 94 | bool PcProgramBase::halt() 95 | { 96 | return false; 97 | } 98 | 99 | bool PcProgramBase::exit(I32 in_code, mbase::string in_message) 100 | { 101 | printf("%s\n", in_message.c_str()); 102 | ::exit(in_code); 103 | return false; 104 | } 105 | 106 | MBASE_END -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Notice 2 | 3 | This is a security notice proclaimed by the developers of MBASE for the users of the SDKs and programs that are provided 4 | by the MBASE Yazılım A.Ş. (https://www.mbasesoftware.com) in order to maintain the quality and safety of our and your software. 5 | 6 | ## SDK Usage Considerations 7 | 8 | First, since the MBASE SDK is primarily developed for C++ developers to easily integrate LLMs into their high-performance 9 | applications, the qualifications below are assumed to be present on the users of the SDK: 10 | 11 | - An extensive experience with C++. 12 | - A solid understanding of computer architecture. 13 | - Knowledge of secure coding practices to reduce the risk of common vulnerabilities. 14 | - Solid foundation in core computer science concepts. 15 | 16 | The language of the "MBASE Software Documentation" assumes the qualifications above. 17 | 18 | Secondly, the developer should program and integrate accordingly by understanding how MBASE SDK handles non-blocking behaviors and 19 | how the SDK behaves in general to avoid common errors and the developer shouldn't hesistate to refer to "MBASE Software Documentation" when things go astray. 20 | 21 | ## Executable Programs Usage Considerations 22 | 23 | There are both example and utility programs written using MBASE SDK for developers, contributors and non-developer users. 24 | 25 | Those programs will always be properly documented under the "MBASE Software Documentation" center. The program will also prints 26 | its usage if the user incorrectly calls the executable program. 27 | 28 | ## Before you Find out 29 | 30 | Before sending us an email about a security vulnerability, please ensure that the issue is caused by the MBASE SDK or its programs, rather than by incorrect usage of the MBASE SDK or its programs. 31 | 32 | ## Reporting vulnerabilities 33 | 34 | If you discover a security vulnerability, do not open a public issue. Instead, please report it to us by emailing to erdog@mbasesoftware.com. 35 | After you sent your email, we will notify you that we have started working on the issue you reported and we may ask for your support too. Plase, 36 | don't hesitate to resend your email if we don't respond to your email. 37 | 38 | In your message, include the details below: 39 | 40 | - A clean description of the vulnerability. 41 | - The potential and active impact of the vulnerability 42 | - A proposed solution, if available. 43 | 44 | ## Contact 45 | 46 | For anything related to MBASE, contact us at erdog@mbasesoftware.com -------------------------------------------------------------------------------- /docs/source/inference/quick_start.rst: -------------------------------------------------------------------------------- 1 | .. include:: links.rst 2 | 3 | ========== 4 | Quickstart 5 | ========== 6 | 7 | .. tip:: 8 | It is highly recommended that you read the sections in order, as they are not arranged randomly in the document. 9 | For example, the :doc:`dialogue example` and :doc:`single-prompt example` is inter-connected where we are doing 10 | improvements to the :doc:`single-prompt example`. 11 | 12 | ----- 13 | About 14 | ----- 15 | 16 | In this chapter, we will cover everything to setup, run and develop using MBASE inference library. 17 | Even if the setting up section covers everything to setup the inference library, 18 | the concepts that are implemented in the examples section may require further clarification. 19 | 20 | For that reason, when you have confusion at some parts, make sure to refer to its :doc:`info-reference/index` 21 | and get the detailed explanation. 22 | 23 | --------- 24 | Structure 25 | --------- 26 | 27 | This chapter is composed of five sections which cover topics as follows: 28 | 29 | * :doc:`Setting-up`: Creating a CMake project and linking the inference library. 30 | 31 | * :doc:`Complete Project Setup`: Properly configured :code:`CMakeLists.txt` and :code:`main.cpp` for reference. Those files are used as a base in subsequent examples. 32 | 33 | * :doc:`Single-Prompt Example`: An application example where we read stdin from user and send it to the LLM and get a response. 34 | 35 | * :doc:`Dialogue Example`: An application example where we have a dialogue with the LLM in terminal. 36 | 37 | * :doc:`Embeddings Example`: The third one is a vector embedding generator which is generally used by RAG programs and more. At the end of the example, the user will supply an input and vector embeddings will be generated by using embedder LLM model. 38 | 39 | In every example, the complete source code and the cmake file will be provided for ease of referencing. 40 | 41 | .. toctree:: 42 | :hidden: 43 | 44 | Setting-up 45 | Complete Project Setup 46 | Single-Prompt Example 47 | Dialogue Example 48 | Embeddings Example 49 | -------------------------------------------------------------------------------- /docs/source/mcp/quickstart/project_setup.rst: -------------------------------------------------------------------------------- 1 | ====================== 2 | Complete Project Setup 3 | ====================== 4 | 5 | From now on, we will use the **CMakeLists.txt**, **client.cpp** and **server.cpp** files as a base 6 | for almost every demonstration example we build together. 7 | 8 | The examples are: 9 | 10 | * :doc:`MCP STDIO/HTTP Server Usage`: MCP STDIO/HTTP server implementation example. Codes under this section will be written under the :code:`server.cpp`. 11 | 12 | * :doc:`MCP STDIO/HTTP Client Usage`: MCP STDIO/HTTP client implementation example. Codes under this section will be written under the :code:`client.cpp`. 13 | 14 | -------------------- 15 | Total CMakeLists.txt 16 | -------------------- 17 | 18 | .. code-block:: cmake 19 | :caption: CMakeLists.txt 20 | 21 | cmake_minimum_required(VERSION 3.15...3.31) 22 | project("mcp_simple_project" LANGUAGES CXX) 23 | 24 | add_executable(mcp_client_sample client.cpp) 25 | add_executable(mcp_server_sample server.cpp) 26 | 27 | find_package(mbase.libs REQUIRED COMPONENTS mcp) 28 | 29 | target_compile_features(mcp_client_sample PUBLIC cxx_std_17) 30 | target_link_libraries(mcp_client_sample PRIVATE mbase-mcp) 31 | target_include_directories(mcp_client_sample PUBLIC mbase-mcp) 32 | 33 | target_compile_features(mcp_server_sample PUBLIC cxx_std_17) 34 | target_link_libraries(mcp_server_sample PRIVATE mbase-mcp) 35 | target_include_directories(mcp_server_sample PUBLIC mbase-mcp) 36 | 37 | ---------------- 38 | Total client.cpp 39 | ---------------- 40 | 41 | .. code-block:: cpp 42 | :caption: client.cpp 43 | 44 | #include 45 | #include 46 | 47 | int main() 48 | { 49 | mbase::McpClientBase myMcpClient( 50 | "MCP Sample Client", 51 | "1.0.0" 52 | ); 53 | std::cout << myMcpClient.get_client_name() << " " << myMcpClient.get_client_version() << std::endl; 54 | return 0; 55 | } 56 | 57 | ---------------- 58 | Total server.cpp 59 | ---------------- 60 | 61 | .. code-block:: cpp 62 | :caption: server.cpp 63 | 64 | #include 65 | #include 66 | #include 67 | 68 | int main() 69 | { 70 | mbase::McpServerStdio mcpServer( 71 | "MCP Sample Server", 72 | "1.0.0" 73 | ); 74 | std::cout << mcpServer.get_server_name() << " " << mcpServer.get_server_version() << std::endl; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /include/mbase/pc/pc_diagnostics.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_PC_DIAGNOSTICS_H 2 | #define MBASE_PC_DIAGNOSTICS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | MBASE_BEGIN 12 | 13 | class MBASE_API PcDiagnostics { 14 | public: 15 | using log_list = mbase::vector; 16 | 17 | enum class flags : U8 { 18 | DIAGNOSTICS_SUCCESS = 0, 19 | LOGTYPE_SUCCESS, 20 | LOGTYPE_ERROR, 21 | LOGTYPE_WARNING, 22 | LOGTYPE_INFO, 23 | LOGIMPORTANCE_LOW, 24 | LOGIMPORTANCE_MID, 25 | LOGIMPORTANCE_HIGH, 26 | LOGIMPORTANCE_FATAL, 27 | DIAGNOSTICS_ERR_MISSING_MESSAGE, 28 | DIAGNOSTICS_ERR_MISSING_FILE 29 | }; 30 | 31 | PcDiagnostics() = default; 32 | ~PcDiagnostics() = default; 33 | 34 | log_list get_log_list() noexcept; 35 | 36 | bool initialize(const mbase::string& in_diagnostics_name); 37 | flags log(flags in_log_type, flags in_log_importance, const mbase::string& in_message) noexcept; 38 | template 39 | flags log(flags in_log_type, flags in_log_importance, MSTRING in_format, Params ... in_params) noexcept 40 | { 41 | if (in_format == NULL || !mbase::string::length_bytes(in_format)) 42 | { 43 | return flags::DIAGNOSTICS_ERR_MISSING_MESSAGE; 44 | } 45 | 46 | return log(in_log_type, in_log_importance, mbase::string::from_format(in_format, std::forward(in_params)...) + MBASE_PLATFORM_NEWLINE); 47 | } 48 | flags log_stdout(flags in_log_type, flags in_log_importance, const mbase::string& in_message) noexcept; 49 | template 50 | flags log_stdout(flags in_log_type, flags in_log_importance, MSTRING in_format, Params ... in_params) noexcept 51 | { 52 | if (in_format == NULL || !mbase::string::length_bytes(in_format)) 53 | { 54 | return flags::DIAGNOSTICS_ERR_MISSING_MESSAGE; 55 | } 56 | 57 | return log_stdout(in_log_type, in_log_importance, mbase::string::from_format(in_format, std::forward(in_params)...) + MBASE_PLATFORM_NEWLINE); 58 | } 59 | GENERIC flush_logs() noexcept; 60 | GENERIC print_logs() const noexcept; 61 | GENERIC dump_logs_to_file() noexcept; 62 | GENERIC dump_logs_to_file(const mbase::wstring& in_file) noexcept; 63 | 64 | private: 65 | mbase::string _build_log_heading(flags in_log_type, flags in_log_importance) noexcept; 66 | 67 | mbase::string mDiagnosticsName; // This will be the name of the log file on dump_logs_to_file 68 | log_list mLogList; 69 | }; 70 | 71 | MBASE_END 72 | 73 | #endif // !MBASE_PC_DIAGNOSTICS_H 74 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_to_client_requests.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_TO_CLIENT_REQUESTS_H 2 | #define MBASE_MCP_SERVER_TO_CLIENT_REQUESTS_H 3 | 4 | #include 5 | 6 | MBASE_BEGIN 7 | 8 | class McpServerBase; 9 | class McpServerStateBase; 10 | 11 | enum class mcp_sampling_content_type { 12 | IMAGE, 13 | TEXT, 14 | AUDIO 15 | }; 16 | 17 | struct McpSamplingMessage { 18 | mbase::string mRole = "user"; 19 | mcp_sampling_content_type mContentType = mcp_sampling_content_type::TEXT; 20 | mbase::string mTextContent; 21 | mbase::string mBase64Data; // base64 encoded binary data 22 | mbase::string mMimeType; // mime type 23 | }; 24 | 25 | struct McpSamplingModelPreferences { 26 | mbase::vector mModelHintsList; 27 | F64 mIntelligencePriority = 0.8; 28 | F64 mSpeedPriority = 0.5; 29 | }; 30 | 31 | struct McpSamplingRequest { 32 | mbase::vector mMessages; 33 | McpSamplingModelPreferences mModelPreferences; 34 | mbase::string mSystemPrompt; 35 | I32 mMaxTokens = 0; 36 | }; 37 | 38 | struct McpRootsResult { 39 | mbase::string mUri; 40 | mbase::string mName; 41 | }; 42 | 43 | struct McpSamplingResult { 44 | mbase::string mRole; 45 | mbase::string mModel; 46 | mbase::string mStopReason; 47 | mcp_sampling_content_type mContentType = mcp_sampling_content_type::TEXT; 48 | mbase::string mTextContent; 49 | mbase::string mBase64Content; 50 | mbase::string mMimeType; 51 | }; 52 | 53 | // error code, server instance, result object 54 | using mcp_st_list_roots_cb = std::function&)>; 55 | using mcp_st_sampling_cb = std::function; 56 | using mcp_st_cb = std::variant; 57 | 58 | struct McpStClientRequestObject { 59 | mbase::string mMessageId; 60 | mbase::I32 mTimeoutInSeconds = 10; 61 | mbase::I32 mAttemptCounter = 0; 62 | volatile bool mResponseWaiting = true; 63 | mcp_st_cb mStCallback; 64 | }; 65 | 66 | struct McpStRootsResultObject { 67 | mcp_st_cb rootsResultCallback; 68 | mbase::vector rootsResult; 69 | }; 70 | 71 | struct McpStSamplingResultObject { 72 | mcp_st_cb samplingResultCallback; 73 | McpSamplingResult samplingResult; 74 | }; 75 | 76 | struct McpSamplingRequestObject { 77 | mbase::Json mRequestId; 78 | McpServerStateBase* requestOwner = nullptr; 79 | McpSamplingRequest samplingRequest; 80 | }; 81 | 82 | MBASE_END 83 | 84 | #endif // -------------------------------------------------------------------------------- /include/mbase/inference/inf_common.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_COMMON_H 2 | #define MBASE_INF_COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // If llama.cpp is defined macro should be placed here 10 | #include 11 | 12 | #define MBASE_INFERENCE_SYS_STRING "MBASE Inference" 13 | #define MBASE_INFERENCE_LIB_NAME "inference" 14 | #define MBASE_INFERENCE_SYS_VERSION "1.0.0alpha" 15 | 16 | MBASE_BEGIN 17 | 18 | using inf_text_token = llama_token; 19 | using inf_text_token_vector = mbase::vector; 20 | using inf_embedding_vector = mbase::vector; 21 | 22 | template 23 | struct inf_processor_watcher { 24 | typename mbase::list>::iterator mItSelf; 25 | TargetObject* mSubject = NULL; 26 | U32 mContextLength = 0; 27 | }; 28 | 29 | struct inf_token_description { 30 | mbase::string mTokenString = ""; 31 | bool mIsSpecial = false; 32 | }; 33 | 34 | enum class inf_model_category { 35 | TEXT_TO_TEXT, 36 | EMBEDDING, 37 | UNDEFINED 38 | }; 39 | 40 | struct MBASE_API inf_lora_adapter { 41 | friend bool operator!=(const inf_lora_adapter& in_lhs, const inf_lora_adapter& in_rhs) 42 | { 43 | return in_lhs.mAdapterName != in_rhs.mAdapterName; 44 | } 45 | 46 | friend bool operator==(const inf_lora_adapter& in_lhs, const inf_lora_adapter& in_rhs) 47 | { 48 | return in_lhs.mAdapterName == in_rhs.mAdapterName; 49 | } 50 | 51 | mbase::string mAdapterName = ""; 52 | mbase::wstring mLoraPath = L""; 53 | llama_adapter_lora* mAdapterHandle = NULL; 54 | F32 mLoraScale = 1.0f; 55 | }; 56 | 57 | using lora_adapter_map = mbase::unordered_map; 58 | 59 | // The rest is common functionality inspired from llama.cpp common library for examples 60 | // The set of functions will be populated as new needs are found 61 | 62 | MBASE_API GENERIC inf_common_batch_add( 63 | llama_batch& in_batch, 64 | inf_text_token in_id, 65 | I32 in_pos, 66 | const mbase::vector& in_seq_ids, 67 | bool in_logits 68 | ); 69 | 70 | MBASE_API GENERIC inf_common_embd_normalize( 71 | const PTRF32 in_inp, 72 | PTRF32 out_normalized, 73 | const SIZE_T& in_n 74 | ); 75 | 76 | MBASE_API F32 inf_common_cosine_similarity( 77 | const PTRF32 in_data1, 78 | const PTRF32 in_data2, 79 | const I32& in_length 80 | ); 81 | 82 | MBASE_API mbase::string inf_get_sys_name_total(); 83 | 84 | MBASE_END 85 | 86 | #endif // MBASE_INF_COMMON_H 87 | -------------------------------------------------------------------------------- /include/mbase/pc/pc_config.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_PC_CONFIG 2 | #define MBASE_PC_CONFIG 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | MBASE_BEGIN 14 | 15 | struct PcConfigDescription { 16 | mbase::string mConfigKey; 17 | mbase::string mConfigValue; 18 | }; 19 | 20 | class PcConfig; 21 | 22 | class MBASE_API PcConfig { 23 | public: 24 | using config_descriptions = mbase::vector; 25 | 26 | enum class flags : U8 { 27 | CONFIG_SUCCESS, 28 | CONFIG_ERR_NOT_INITIALIZED, 29 | CONFIG_ERR_MISSING_PATH, 30 | CONFIG_ERR_MISSING_KEY, 31 | CONFIG_ERR_MISSING_VALUE, 32 | CONFIG_ERR_PARAM_NOT_FOUND, 33 | CONFIG_ERR_FILE_NOT_FOUND, 34 | CONFIG_WARN_KEY_OVERWRITTEN, 35 | CONFIG_WARN_EMPTY_CONFIG_MAP 36 | }; 37 | 38 | PcConfig(); 39 | ~PcConfig(); 40 | 41 | template 42 | flags get_config_param(const mbase::string& in_key, Type& out_param) noexcept 43 | { 44 | if(!is_initialized()) 45 | { 46 | return flags::CONFIG_ERR_NOT_INITIALIZED; 47 | } 48 | 49 | if(!in_key.size()) 50 | { 51 | return flags::CONFIG_ERR_MISSING_KEY; 52 | } 53 | 54 | for(config_descriptions::const_iterator cIt = mConfigDescriptions.begin(); cIt != mConfigDescriptions.end(); ++cIt) 55 | { 56 | if(cIt->mConfigKey == in_key) 57 | { 58 | out_param = mbase::smart_conversion::apply(cIt->mConfigValue.c_str()); 59 | return flags::CONFIG_SUCCESS; 60 | } 61 | } 62 | 63 | return flags::CONFIG_ERR_PARAM_NOT_FOUND; 64 | } 65 | const config_descriptions& get_config_descriptions() const noexcept; 66 | PcDiagnostics* get_assigned_diagnostics() noexcept; 67 | bool is_initialized() const noexcept; 68 | bool initialize( 69 | PcDiagnostics& in_diagnostics, 70 | const mbase::wstring& in_config_file_name 71 | ); 72 | bool load_config_file(const mbase::wstring& in_file, config_descriptions& out_cmap) noexcept; 73 | bool load_config_file(const mbase::wstring& in_file) noexcept; 74 | flags update() noexcept; 75 | flags set_config_param(const mbase::string& in_key, const mbase::string& in_param) noexcept; 76 | flags dump_to_string(mbase::string& out_config_string) noexcept; 77 | 78 | private: 79 | PcDiagnostics* mDiagnosticsManager; 80 | mbase::wstring mConfigFileName; 81 | config_descriptions mConfigDescriptions; 82 | bool mIsInitialized; 83 | bool mIsUpdated; 84 | }; 85 | 86 | MBASE_END 87 | 88 | #endif // !MBASE_PC_CONFIG 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 10 |
11 | 12 | ![GitHub Version](https://img.shields.io/github/v/release/Emreerdog/mbase) 13 | ![Static Badge](https://img.shields.io/badge/licence-MIT-blue) 14 | ![SDK Dev build badge](https://github.com/Emreerdog/mbase/actions/workflows/build.yml/badge.svg) 15 | 16 | MBASE SDK is a set of mostly LLM related libraries actively being maintained and developed by M. Emre Erdog 17 | and the [MBASE Yazılım A.Ş.]() 18 | 19 | The main purpose of the MBASE SDK is to supply the developer with necessary tools and procedures to easily integrate LLM capabilities into their 20 | C++ applications. 21 | 22 | Here is a list of libraries in the MBASE SDK: 23 | 24 | - [Inference Library](https://docs.mbasesoftware.com/inference/about.html): An LLM inference library built over https://github.com/ggerganov/llama.cpp library for integrating LLMs into programs. 25 | - [Model Context Protocol Library](https://docs.mbasesoftware.com/mcp/about.html): A C++ MCP client/server library that includes all fundamental features, with support for both STDIO and StreamableHTTP transport methods. 26 | - [Standard Library](https://docs.mbasesoftware.com/std/index.html): A standard library containing fundamental data-structures and useful utilities such as built-in uuid generation and timers. 27 | - [JSON Library](https://docs.mbasesoftware.com/json/index.html): A light-weight json library. 28 | 29 | ## Supported Platforms 30 | 31 | - macOS 32 | - Linux 33 | - Windows 34 | 35 | ## Documentation 36 | 37 | MBASE SDK documentation can be found [here](https://docs.mbasesoftware.com/index.html). 38 | 39 | ## Setting up 40 | 41 | [Setting-Up](https://docs.mbasesoftware.com/setting-up/about.html) 42 | 43 | ## Contributing 44 | 45 | Before you submit a change, make sure you read the [contribution guidelines](https://github.com/Emreerdog/mbase/blob/main/CONTRIBUTING.md). 46 | 47 | ## Company 48 | 49 | The main company webpage is https://mbasesoftware.com 50 | 51 | ## Contact 52 | 53 | If you have any questions, ideas, want information or, if you want to be active in MBASE project, send me an email at erdog@mbasesoftware.com 54 | -------------------------------------------------------------------------------- /include/mbase/framework/event_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_EVENT_HANDLER_H 2 | #define MBASE_EVENT_HANDLER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | MBASE_BEGIN 10 | 11 | class event_handler : public handler_base { 12 | public: 13 | enum class flags : U32 { 14 | EVENT_HANDLER_SUCCESS = 0, 15 | EVENT_ONCE = MBASE_EVENT_HANDLER_FLAGS_CONTROL_START, 16 | EVENT_ON, 17 | EVENT_REGISTERED, 18 | EVENT_UNREGISTERED, 19 | EVENT_ERR_UNKNOWN = MBASE_EVENT_HANDLER_FLAGS_MAX 20 | }; 21 | 22 | /* ===== BUILDER METHODS BEGIN ===== */ 23 | MBASE_INLINE event_handler(); 24 | /* ===== BUILDER METHODS END ===== */ 25 | 26 | /* ===== OBSERVATION METHODS BEGIN ===== */ 27 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE mbase::string get_event_name() const noexcept; 28 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE flags get_event_type() const noexcept; 29 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE flags get_event_status() const noexcept; 30 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE SIZE_T get_manager_id() const noexcept; 31 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE bool is_registered() const noexcept; 32 | /* ===== OBSERVATION METHODS END ===== */ 33 | 34 | virtual GENERIC on_register() { /* DEFAULT IMPL */ } 35 | virtual GENERIC on_unregister() { /* DEFAULT IMPL */ } 36 | 37 | friend class event_manager; 38 | private: 39 | using event_element = mbase::list::iterator; 40 | 41 | event_element mSelfIter; 42 | mbase::string mEventName; 43 | flags mEventType; 44 | flags mStatus; 45 | SIZE_T mManagerId; 46 | }; 47 | 48 | MBASE_INLINE event_handler::event_handler() : handler_base(), mSelfIter(nullptr), mEventName(""), mEventType(flags::EVENT_ON), mStatus(flags::EVENT_UNREGISTERED), mManagerId(0) 49 | { 50 | } 51 | 52 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE mbase::string event_handler::get_event_name() const noexcept 53 | { 54 | return mEventName; 55 | } 56 | 57 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE event_handler::flags event_handler::get_event_type() const noexcept 58 | { 59 | return mEventType; 60 | } 61 | 62 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE event_handler::flags event_handler::get_event_status() const noexcept 63 | { 64 | return mStatus; 65 | } 66 | 67 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE SIZE_T event_handler::get_manager_id() const noexcept 68 | { 69 | return mManagerId; 70 | } 71 | 72 | MBASE_ND(MBASE_OBS_IGNORE) MBASE_INLINE bool event_handler::is_registered() const noexcept 73 | { 74 | return mStatus == flags::EVENT_REGISTERED; 75 | } 76 | 77 | 78 | MBASE_END 79 | 80 | #endif // !MBASE_EVENT_HANDLER_H 81 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_common.h.in: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_COMMON_H 2 | #define MBASE_INF_COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // If llama.cpp is defined macro should be placed here 10 | #include 11 | 12 | #cmakedefine MBASE_INFERENCE_SYS_STRING "@MBASE_INFERENCE_SYS_STRING@" 13 | #cmakedefine MBASE_INFERENCE_LIB_NAME "@MBASE_INFERENCE_LIB_NAME@" 14 | #cmakedefine MBASE_INFERENCE_SYS_VERSION "@MBASE_INFERENCE_SYS_VERSION@" 15 | 16 | MBASE_BEGIN 17 | 18 | using inf_text_token = llama_token; 19 | using inf_text_token_vector = mbase::vector; 20 | using inf_embedding_vector = mbase::vector; 21 | 22 | template 23 | struct inf_processor_watcher { 24 | typename mbase::list>::iterator mItSelf; 25 | TargetObject* mSubject = NULL; 26 | U32 mContextLength = 0; 27 | }; 28 | 29 | struct inf_token_description { 30 | mbase::string mTokenString = ""; 31 | bool mIsSpecial = false; 32 | }; 33 | 34 | enum class inf_model_category { 35 | TEXT_TO_TEXT, 36 | EMBEDDING, 37 | UNDEFINED 38 | }; 39 | 40 | struct MBASE_API inf_lora_adapter { 41 | friend bool operator!=(const inf_lora_adapter& in_lhs, const inf_lora_adapter& in_rhs) 42 | { 43 | return in_lhs.mAdapterName != in_rhs.mAdapterName; 44 | } 45 | 46 | friend bool operator==(const inf_lora_adapter& in_lhs, const inf_lora_adapter& in_rhs) 47 | { 48 | return in_lhs.mAdapterName == in_rhs.mAdapterName; 49 | } 50 | 51 | mbase::string mAdapterName = ""; 52 | mbase::wstring mLoraPath = L""; 53 | llama_adapter_lora* mAdapterHandle = NULL; 54 | F32 mLoraScale = 1.0f; 55 | }; 56 | 57 | using lora_adapter_map = mbase::unordered_map; 58 | 59 | // The rest is common functionality inspired from llama.cpp common library for examples 60 | // The set of functions will be populated as new needs are found 61 | 62 | MBASE_API GENERIC inf_common_batch_add( 63 | llama_batch& in_batch, 64 | inf_text_token in_id, 65 | I32 in_pos, 66 | const mbase::vector& in_seq_ids, 67 | bool in_logits 68 | ); 69 | 70 | MBASE_API GENERIC inf_common_embd_normalize( 71 | const PTRF32 in_inp, 72 | PTRF32 out_normalized, 73 | const SIZE_T& in_n 74 | ); 75 | 76 | MBASE_API F32 inf_common_cosine_similarity( 77 | const PTRF32 in_data1, 78 | const PTRF32 in_data2, 79 | const I32& in_length 80 | ); 81 | 82 | MBASE_API mbase::string inf_get_sys_name_total(); 83 | 84 | MBASE_END 85 | 86 | #endif // MBASE_INF_COMMON_H 87 | -------------------------------------------------------------------------------- /mbase/inference/inf_common.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | MBASE_BEGIN 5 | 6 | GENERIC inf_llama_logger(ggml_log_level log_lvl, const char* usr_text, void* usr_data) 7 | { 8 | (void)log_lvl; 9 | (void)usr_text; 10 | (void)usr_data; 11 | } 12 | 13 | struct inf_llama_log_setter { 14 | inf_llama_log_setter() 15 | { 16 | llama_log_set(inf_llama_logger, nullptr); 17 | } 18 | }; 19 | 20 | static inf_llama_log_setter gInfLogSetter; 21 | 22 | GENERIC inf_common_batch_add( 23 | llama_batch& in_batch, 24 | inf_text_token in_id, 25 | I32 in_pos, 26 | const mbase::vector& in_seq_ids, 27 | bool in_logits 28 | ) 29 | { 30 | // No need for assertion because if this function is called, inputs are guaranteed to be well aligned and valid 31 | in_batch.token[in_batch.n_tokens] = in_id; 32 | in_batch.pos[in_batch.n_tokens] = in_pos; 33 | in_batch.n_seq_id[in_batch.n_tokens] = static_cast(in_seq_ids.size()); 34 | for(SIZE_T i = 0; i < in_seq_ids.size(); ++i) 35 | { 36 | in_batch.seq_id[in_batch.n_tokens][i] = in_seq_ids[i]; 37 | } 38 | 39 | in_batch.logits[in_batch.n_tokens] = in_logits; 40 | 41 | in_batch.n_tokens++; 42 | } 43 | 44 | GENERIC inf_common_embd_normalize( 45 | const PTRF32 in_inp, 46 | PTRF32 out_normalized, 47 | const SIZE_T& in_n 48 | ) 49 | { 50 | F32 tmpSum = 0.0; 51 | for(SIZE_T i = 0; i < in_n; i++) 52 | { 53 | tmpSum += in_inp[i] * in_inp[i]; 54 | } 55 | tmpSum = std::sqrt(tmpSum); 56 | 57 | const F32 tmpNorm = tmpSum > 0.0 ? 1.0 / tmpSum : 0.0f; 58 | 59 | for(SIZE_T i = 0; i < in_n; i++) 60 | { 61 | out_normalized[i] = in_inp[i] * tmpNorm; 62 | } 63 | } 64 | 65 | F32 inf_common_cosine_similarity( 66 | const PTRF32 in_data1, 67 | const PTRF32 in_data2, 68 | const I32& in_length 69 | ) 70 | { 71 | F32 sum = 0.0; 72 | F32 sum1 = 0.0; 73 | F32 sum2 = 0.0; 74 | 75 | for (I32 i = 0; i < in_length; i++) { 76 | sum += in_data1[i] * in_data2[i]; 77 | sum1 += in_data1[i] * in_data1[i]; 78 | sum2 += in_data2[i] * in_data2[i]; 79 | } 80 | 81 | // Handle the case where one or both vectors are zero vectors 82 | if (sum1 == 0.0 || sum2 == 0.0) { 83 | if (sum1 == 0.0 && sum2 == 0.0) { 84 | return 1.0f; // two zero vectors are similar 85 | } 86 | return 0.0f; 87 | } 88 | 89 | return sum / (std::sqrt(sum1) * std::sqrt(sum2)); 90 | } 91 | 92 | mbase::string inf_get_sys_name_total() 93 | { 94 | return mbase::string(MBASE_INFERENCE_SYS_STRING " " MBASE_INFERENCE_SYS_VERSION); 95 | } 96 | 97 | MBASE_END 98 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_t2t_client.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_CLIENT_T2T_H 2 | #define MBASE_CLIENT_T2T_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | MBASE_BEGIN 10 | 11 | class MBASE_API InfClientTextToText : public InfClientBase { 12 | public: 13 | using chat_history_map = mbase::unordered_map; 14 | 15 | enum flags : U8 { 16 | INF_CLIENT_SUCCESS, 17 | INF_CLIENT_ERR_MSG_ID_MISMATCH, 18 | INF_CLIENT_ERR_MISSING_INPUT, 19 | INF_CLIENT_ERR_NOT_REGISTERED 20 | }; 21 | 22 | /* ===== BUILDER METHODS BEGIN ===== */ 23 | InfClientTextToText(); 24 | InfClientTextToText(const InfClientTextToText& in_rhs); 25 | ~InfClientTextToText(); 26 | /* ===== BUILDER METHODS END ===== */ 27 | 28 | /* ===== OPERATOR BUILDER METHODS BEGIN ===== */ 29 | InfClientTextToText& operator=(const InfClientTextToText& in_rhs); 30 | /* ===== OPERATOR BUILDER METHODS END ===== */ 31 | 32 | /* ===== OBSERVATION METHODS BEGIN ===== */ 33 | bool has_message(const U32& in_msg_id) const; 34 | flags get_message(const U32& in_msg_id, context_line& out_message); 35 | flags get_message_array(PTRU32 in_msg_ids, size_type in_id_count, mbase::vector& out_messages); 36 | /* ===== OBSERVATION METHODS END ===== */ 37 | 38 | /* ===== STATE-MODIFIER METHODS BEGIN ===== */ 39 | flags add_message(CBYTEBUFFER in_data, size_type in_size, context_role in_role, U32& out_message_id); 40 | flags add_message(const mbase::string& in_data, context_role in_role, U32& out_message_id); 41 | flags add_message(const mbase::wstring& in_data, context_role in_role, U32& out_message_id); 42 | flags remove_messages(const mbase::vector& in_msg_ids = mbase::vector()); 43 | GENERIC clear_chat_history(); 44 | /* ===== STATE-MODIFIER METHODS END ===== */ 45 | 46 | /* ===== INTERFACE METHODS BEGIN ===== */ 47 | virtual GENERIC on_batch_processing(InfProcessorTextToText* out_processor); 48 | virtual GENERIC on_decoding(InfProcessorTextToText* out_processor); 49 | virtual GENERIC on_batch_processed(InfProcessorTextToText* out_processor, const U32& out_proc_batch_length, const bool& out_is_kv_locked) = 0; 50 | virtual GENERIC on_write(InfProcessorTextToText* out_processor, const inf_text_token_vector& out_token, bool out_is_finish) = 0; 51 | virtual GENERIC on_finish(InfProcessorTextToText* out_processor, size_type out_total_token_size, InfProcessorTextToText::finish_state out_finish_state) = 0; 52 | /* ===== INTERFACE METHODS END ===== */ 53 | protected: 54 | chat_history_map mChatHistory; 55 | U32 mMessageIndexer; 56 | }; 57 | 58 | MBASE_END 59 | 60 | #endif // !MBASE_CLIENT_T2T_H 61 | -------------------------------------------------------------------------------- /docs/source/inference/quickstart/dialogue_ex/context_lines.rst: -------------------------------------------------------------------------------- 1 | =================== 2 | About Context Lines 3 | =================== 4 | 5 | As we have mentioned in the :doc:`single-prompt example <../single_prompt_ex/about>`, 6 | we supply an array of context lines to the tokenizer in order to form the dialogue. 7 | 8 | The order of the context lines supplied to the tokenizer will determine the order of our conversation with LLM. 9 | 10 | The :code:`context_line` is defined in the header file :code:`mbase/inference/inf_context_line.h` as follows: 11 | 12 | .. code-block:: cpp 13 | :caption: mbase/inference/inf_context_line.h 14 | 15 | enum class context_role { 16 | SYSTEM, 17 | ASSISTANT, 18 | USER, 19 | NONE 20 | }; 21 | 22 | struct context_line { 23 | context_role mRole = context_role::NONE; 24 | mbase::string mMessage = ""; 25 | U32 mMessageIndex; 26 | }; 27 | 28 | The message index makes no sense for our example's case. It is relevant if you make the client 29 | manage the message history by itself. What matters here is the role and message fields of the struct. 30 | 31 | ---------------- 32 | Example Dialogue 33 | ---------------- 34 | 35 | Let's take an example of the given conversation: 36 | 37 | .. code-block:: 38 | :caption: example dialogue 39 | 40 | System: You are a helpful assistant. 41 | 42 | User: Can you code? 43 | Assistant: Yes I know how to write C++. 44 | 45 | User: Do you like MBASE? 46 | Assistant: Yep, pleasant experience. 47 | 48 | User: Are you an artifical intelligence? 49 | Assistant: Whatever you say... 50 | 51 | This dialogue can be represented in context lines as follows: 52 | 53 | .. code-block:: cpp 54 | 55 | mbase::vector chatHistory = { 56 | {mbase::context_role::SYSTEM, "You are a helpful assistant."}, 57 | {mbase::context_role::USER, "Can you code?"}, 58 | {mbase::context_role::ASSISTANT, "Yes I know how to write C++."}, 59 | {mbase::context_role::USER, "Do you like MBASE?"}, 60 | {mbase::context_role::ASSISTANT, "Yep, pleasant experience."}, 61 | {mbase::context_role::USER, "Are you an artifical intelligence?"}, 62 | {mbase::context_role::ASSISTANT, "Whatever you say..."} 63 | }; 64 | 65 | After the context lines are supplied to the tokenizer and the token vector is generated, 66 | we will execute the token vector using :code:`execute_input` method. 67 | 68 | --------- 69 | Basically 70 | --------- 71 | 72 | * If you want the method of prompt resupplying, tokenize the entire chat history and execute the token vector. (First option) 73 | * If you want the processor to keep the context, set the manual caching mode and tokenize and execute only the last prompt. (Second option) 74 | -------------------------------------------------------------------------------- /legal/LICENSE-full: -------------------------------------------------------------------------------- 1 | === MBASE SDK === 2 | 3 | Copyright (c) 2025 MBASE Yazilim A.S. (https://www.mbasesoftware.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | 11 | === llama.cpp === 12 | 13 | MIT License 14 | 15 | Copyright (c) 2023-2024 The ggml authors 16 | 17 | Permission is hereby granted, free of charge, to any person obtaining a copy 18 | of this software and associated documentation files (the "Software"), to deal 19 | in the Software without restriction, including without limitation the rights 20 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 21 | copies of the Software, and to permit persons to whom the Software is 22 | furnished to do so, subject to the following conditions: 23 | 24 | The above copyright notice and this permission notice shall be included in all 25 | copies or substantial portions of the Software. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 30 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 | SOFTWARE. 34 | 35 | === CUDA Redistributable Disclaimer === 36 | 37 | This software includes NVIDIA CUDA components – specifically, cudart64_12.dll, cublas64_12.dll, and cublasLt64_12.dll – which are redistributed under the terms of the NVIDIA CUDA Toolkit End User License Agreement. These files are provided “as is” and must not be modified or stripped of their proprietary notices. For full details of the license terms, please refer to the NVIDIA CUDA EULA available at: 38 | https://docs.nvidia.com/cuda/eula/index.html 39 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_features.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_FEATURES_H 2 | #define MBASE_MCP_SERVER_FEATURES_H 3 | 4 | #include 5 | #include // Description objects 6 | #include // Response objects 7 | 8 | MBASE_BEGIN 9 | 10 | class McpServerClient; 11 | class McpServerBase; 12 | struct McpFeatureRequest; 13 | 14 | using McpMessageArgument = std::variant, std::map>; 15 | using McpMessageMap = mbase::unordered_map; 16 | 17 | // Client instance, Message map, progress token 18 | typedef McpResponseTool(*mcp_server_tool_cb)(McpServerClient*, const McpMessageMap&, const mbase::Json&); 19 | typedef mbase::vector(*mcp_server_prompt_cb)(McpServerClient*, const McpMessageMap&, const mbase::Json&); 20 | typedef McpResponseResource(*mcp_server_resource_cb)(McpServerClient*, const mbase::Json&); 21 | 22 | class MBASE_API McpToolFeature { 23 | public: 24 | const McpToolDescription& get_tool_description() const noexcept; 25 | mcp_server_tool_cb get_tool_cb() const noexcept; 26 | 27 | GENERIC update_tool_description(const McpToolDescription& in_desc, mcp_server_tool_cb in_cb) noexcept; 28 | private: 29 | McpToolDescription mToolDescription; 30 | mcp_server_tool_cb mToolCb = nullptr; 31 | }; 32 | 33 | class MBASE_API McpPromptFeature { 34 | public: 35 | const McpPromptDescription& get_prompt_description() const noexcept; 36 | mcp_server_prompt_cb get_prompt_cb() const noexcept; 37 | 38 | GENERIC update_prompt_description(const McpPromptDescription& in_desc, mcp_server_prompt_cb in_cb) noexcept; 39 | private: 40 | McpPromptDescription mPromptDescription; 41 | mcp_server_prompt_cb mPromptCb = nullptr; 42 | }; 43 | 44 | class MBASE_API McpResourceFeature { 45 | public: 46 | const McpResourceDescription& get_resource_description() const noexcept; 47 | mcp_server_resource_cb get_resource_cb() const noexcept; 48 | 49 | GENERIC update_resource_description(const McpResourceDescription& in_desc, mcp_server_resource_cb in_cb) noexcept; 50 | private: 51 | McpResourceDescription mResourceDescription; 52 | mcp_server_resource_cb mResourceCb = nullptr; 53 | }; 54 | 55 | struct McpFeatureRequest { 56 | feature_type mFeatureType; 57 | union 58 | { 59 | McpToolFeature* toolFeature = nullptr; 60 | McpResourceFeature* resourceFeature; 61 | McpPromptFeature* promptFeature; 62 | }; 63 | McpServerClient* mRequestOwner = nullptr; 64 | McpMessageMap mMessageMap; 65 | mbase::Json mMessageId; 66 | mbase::Json mProgressId; 67 | bool mIsCancelled = false; 68 | }; 69 | 70 | MBASE_END 71 | 72 | #endif // MBASE_MCP_SERVER_FEATURES_H -------------------------------------------------------------------------------- /examples/openai-server/client.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_OPENAI_CLIENT_H 2 | #define MBASE_OPENAI_CLIENT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | MBASE_BEGIN 12 | 13 | class OpenaiTextToTextClient : public mbase::InfClientTextToText { 14 | public: 15 | 16 | bool is_processing() const; 17 | 18 | GENERIC set_http_data_sink( 19 | httplib::DataSink* in_data_sink, 20 | httplib::Response& in_resp, 21 | bool in_stream, 22 | long long in_gen_limit 23 | ); 24 | GENERIC set_is_processing(bool in_state); 25 | 26 | GENERIC on_register(InfProcessorBase* out_processor) override; 27 | GENERIC on_unregister(InfProcessorBase* out_processor) override; 28 | GENERIC on_batch_processed(InfProcessorTextToText* out_processor, const U32& out_proc_batch_length, [[maybe_unused]] const bool& out_is_kv_locked) override; 29 | GENERIC on_write(InfProcessorTextToText* out_processor, const inf_text_token_vector& out_token, bool out_is_finish) override; 30 | GENERIC on_finish(InfProcessorTextToText* out_processor, size_type out_total_token_size, InfProcessorTextToText::finish_state out_finish_state) override; 31 | 32 | private: 33 | bool mProcessing = false; 34 | bool mStreamMod = false; 35 | long long mGenLimit = 0; 36 | long long mGenCount = 0; 37 | httplib::DataSink* mDataSink = NULL; 38 | httplib::Response* mInResponse = NULL; 39 | mbase::string mClientId; 40 | mbase::string mAccumulatedResponse; 41 | }; 42 | 43 | class OpenaiEmbedderClient : public mbase::InfClientEmbedder { 44 | public: 45 | 46 | bool is_processing() const; 47 | 48 | GENERIC set_embedder_input( 49 | InfEmbedderProcessor* in_processor, 50 | httplib::Response& in_resp, 51 | const mbase::vector& in_embeddings_list 52 | ); 53 | 54 | GENERIC on_register(InfProcessorBase* out_processor) override; 55 | GENERIC on_unregister(InfProcessorBase* out_processor) override; 56 | GENERIC on_batch_processed(InfEmbedderProcessor* out_processor, const U32& out_proc_batch_length) override; 57 | GENERIC on_write(InfEmbedderProcessor* out_processor, PTRF32 out_embeddings, const U32& out_cursor, bool out_is_finished) override; 58 | GENERIC on_finish(InfEmbedderProcessor* out_processor, const size_type& out_total_processed_embeddings) override; 59 | 60 | private: 61 | bool mIsProcessing = false; 62 | httplib::Response* mInResponse = NULL; 63 | mbase::string mClientId; 64 | mbase::Json mTotalProcessedResponse; 65 | mbase::vector mEmbeddingTokensInput; 66 | I32 mPromptIndex; 67 | }; 68 | 69 | MBASE_END 70 | 71 | #endif // MBASE_OPENAI_CLIENT_H -------------------------------------------------------------------------------- /include/mbase/http_parser.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_HTTP_PARSER 2 | #define MBASE_HTTP_PARSER 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | MBASE_STD_BEGIN 12 | 13 | /* 14 | Essentials .. 15 | 16 | Content-Encoding = gzip, compress, deflate, x-gzip, br, zstd 17 | Accept-Ranges = bytes 18 | Allow = GET, HEAD 19 | Content-Length = integer 20 | Content-Type = text/html, text/javascript, text/plain, application/json 21 | Server = mb-http 22 | */ 23 | 24 | static const mbase::pair gHttpResponseList[] = { 25 | {200, "OK"}, 26 | {201, "CREATED"}, 27 | {202, "Acepted"}, 28 | {203, "Partial Information"}, 29 | {204, "No Response"}, 30 | {400, "Bad request"}, 31 | {401, "Unauthorized"}, 32 | {402, "PaymentRequired"}, 33 | {403, "Forbidden"}, 34 | {404, "Not found"}, 35 | {500, "Internal Error"}, 36 | {501, "Not implemented"}, 37 | {502, "Service temporarily overloaded"}, 38 | {503, "Gateway timeout"}, 39 | {301, "Moved"}, 40 | {302, "Found"}, 41 | {303, "Method"}, 42 | {304, "Not Modified"} 43 | }; 44 | 45 | static const char gHttpContentTypes[] = { 46 | "text/html", 47 | "text/javascript", 48 | "text/plain", 49 | "application/json" 50 | }; 51 | 52 | enum class http_content_type : U8 { 53 | TEXT_HTML, 54 | TEXT_JAVASCRIPT, 55 | TEXT_PLAIN, 56 | APP_JSON 57 | }; 58 | 59 | enum class http_response_code : U8 { 60 | OK, 61 | CREATED, 62 | ACCEPTED, 63 | PARTIAL_INFORMATION, 64 | NO_RESPONSE, 65 | BAD_REQUEST, 66 | UNAUTHORIZED, 67 | PAYMENTREQUIRED, 68 | FORBIDDEN, 69 | NOT_FOUND, 70 | INTERNAL_ERROR, 71 | NOT_IMPLEMENTED, 72 | SERVICE_TEMPORARILY_OVERLOADED, 73 | GATEWAY_TIMEOUT, 74 | MOVED, 75 | FOUND, 76 | METHOD, 77 | NOT_MODIFIED 78 | }; 79 | 80 | class http_response { 81 | public: 82 | using size_type = SIZE_T; 83 | 84 | // request-line = method SP request-target SP HTTP-version CRLF 85 | // method = GET, POST, etc... 86 | // HTTP-version = HTTP/1.1 87 | 88 | // response-line = HTTP-version SP resp-code SP resp-text CRLF 89 | // 90 | http_response(); 91 | MBASE_INLINE GENERIC set_response_code(http_response_code in_code) noexcept; 92 | MBASE_INLINE GENERIC set_server(const mbase::string& in_name) noexcept; 93 | MBASE_INLINE GENERIC set_content_type(http_content_type in_type) noexcept; 94 | MBASE_INLINE GENERIC set_header_value(const mbase::string& in_name, const mbase::string& in_value) noexcept; 95 | MBASE_INLINE GENERIC generate_response(mbase::string& out_response, CBYTEBUFFER in_data = NULL, size_type in_size = 0) noexcept; 96 | 97 | private: 98 | 99 | mbase::unordered_map mFields; 100 | }; 101 | 102 | MBASE_STD_END 103 | 104 | #endif // !MBASE_HTTP_PARSER 105 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: MBASE SDK Release 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | 8 | macos-metal-release: 9 | runs-on: mbase-arm-mac1 10 | steps: 11 | - name: Checkout the main branch 12 | uses: actions/checkout@v4 13 | with: 14 | fetch-depth: 0 15 | ref: ${{ github.ref }} 16 | submodules: recursive 17 | 18 | - name: Metal llama.cpp bundled build 19 | run: | 20 | rm -rf build_metal_bundle 21 | cmake -B build_metal_bundle -DMBASE_FORCE_BUNDLE=ON -DMBASE_SERVER_SSL=ON -DMBASE_PACKAGING=ON 22 | cmake --build build_metal_bundle --config Release -j 23 | rm -rf ./output 24 | mkdir output 25 | cd output 26 | cpack -G "ZIP;TGZ" --config ../build_metal_bundle/CPackConfig.cmake 27 | rm -rf ./_CPack_Packages 28 | cd .. 29 | 30 | - name: ARM MBASE standalone build 31 | run: | 32 | rm -rf build_standalone 33 | cmake -B build_standalone -DMBASE_SERVER_SSL=ON -DMBASE_PACKAGING=ON 34 | cmake --build build_standalone --config Release -j 35 | cd output 36 | cpack -G "ZIP;TGZ" --config ../build_standalone/CPackConfig.cmake 37 | rm -rf ./_CPack_Packages 38 | cd .. 39 | 40 | - name: Uploading artifacts 41 | run: | 42 | gh release upload "$(git describe --tags --abbrev=0)" ./output/* --clobber 43 | 44 | ubuntu-amd64-cuda-release: 45 | runs-on: mbase-ubuntu-cuda-amd64-r16p8-1 46 | steps: 47 | - name: Checkout the main branch 48 | uses: actions/checkout@v4 49 | with: 50 | fetch-depth: 0 51 | ref: ${{ github.ref }} 52 | submodules: recursive 53 | 54 | - name: CUDA amd64 llama.cpp bundled build 55 | run: | 56 | rm -rf build_cuda_amd64 57 | cmake -B build_cuda_amd64 -DMBASE_FORCE_BUNDLE=ON -DMBASE_SERVER_SSL=ON -DMBASE_PACKAGING=ON -DGGML_NATIVE=OFF -DGGML_CUDA=ON 58 | cmake --build build_cuda_amd64 --config Release -j 59 | rm -rf ./output 60 | mkdir output 61 | cd output 62 | cpack -G "ZIP;TGZ;DEB;RPM" --config ../build_cuda_amd64/CPackConfig.cmake 63 | rm -rf ./_CPack_Packages 64 | cd .. 65 | 66 | - name: AMD64 MBASE standalone build 67 | run: | 68 | rm -rf build_standalone 69 | cmake -B build_standalone -DMBASE_SERVER_SSL=ON -DMBASE_PACKAGING=ON 70 | cmake --build build_standalone --config Release -j 71 | cd output 72 | cpack -G "ZIP;TGZ;DEB;RPM" --config ../build_standalone/CPackConfig.cmake 73 | rm -rf ./_CPack_Packages 74 | cd .. 75 | 76 | - name: Uploading artifacts 77 | run: | 78 | gh release upload "$(git describe --tags --abbrev=0)" ./output/* --clobber -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_http_streamable.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_SERVER_HTTP_STREAMABLE_H 2 | #define MBASE_SERVER_HTTP_STREAMABLE_H 3 | 4 | #include 5 | #include // McpServerBase 6 | #include // McpServerHttpBase 7 | #include 8 | 9 | namespace httplib 10 | { 11 | class Server; 12 | } 13 | 14 | MBASE_BEGIN 15 | 16 | // TODO: implement the security layer and rate limits 17 | 18 | class MBASE_API McpServerHttpBase : public mbase::McpServerBase { 19 | public: 20 | McpServerHttpBase( 21 | const mbase::string& in_server_name, 22 | const mbase::string& in_version_string, 23 | const mbase::string& in_hostname, 24 | const I32& in_port, 25 | const mbase::string& in_api_key = mbase::string(), 26 | const mbase::string& in_public_key_file = mbase::string(), 27 | const mbase::string& in_private_key_file = mbase::string() 28 | ); 29 | 30 | const mbase::string& get_hostname() const noexcept; 31 | const mbase::string& get_api_key() const noexcept; 32 | const I32& get_port() const noexcept; 33 | protected: 34 | std::unique_ptr svr; 35 | mbase::string mHostname = "localhost"; 36 | mbase::string mApiKey; 37 | I32 mPort = 8000; 38 | }; 39 | 40 | class MBASE_API McpServerHttpStreamableStateful : public mbase::McpServerHttpBase { 41 | public: 42 | McpServerHttpStreamableStateful( 43 | const mbase::string& in_server_name, 44 | const mbase::string& in_version_string, 45 | const mbase::string& in_hostname, 46 | const I32& in_port, 47 | const mbase::string& in_api_key = mbase::string(), 48 | const mbase::string& in_public_key_file = mbase::string(), 49 | const mbase::string& in_private_key_file = mbase::string() 50 | ); 51 | ~McpServerHttpStreamableStateful(); 52 | bool is_server_running() const noexcept; 53 | GENERIC update_t() override; 54 | private: 55 | mbase::unordered_map mStreambleClients; 56 | }; 57 | 58 | class MBASE_API McpServerHttpStreamableStateless : public mbase::McpServerHttpBase { 59 | public: 60 | McpServerHttpStreamableStateless( 61 | const mbase::string& in_server_name, 62 | const mbase::string& in_version_string, 63 | const mbase::string& in_hostname, 64 | const I32& in_port, 65 | const mbase::string& in_api_key = mbase::string(), 66 | const mbase::string& in_public_key_file = mbase::string(), 67 | const mbase::string& in_private_key_file = mbase::string() 68 | ); 69 | ~McpServerHttpStreamableStateless(); 70 | bool is_server_running() const noexcept; 71 | GENERIC update() override; 72 | GENERIC update_t() override; 73 | private: 74 | mbase::vector mHttpClients; 75 | }; 76 | 77 | MBASE_END 78 | 79 | #endif // MBASE_SERVER_HTTP_STREAMABLE_H -------------------------------------------------------------------------------- /external/double-conversion/fixed-dtoa.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 the V8 project authors. All rights reserved. 2 | // Redistribution and use in source and binary forms, with or without 3 | // modification, are permitted provided that the following conditions are 4 | // met: 5 | // 6 | // * Redistributions of source code must retain the above copyright 7 | // notice, this list of conditions and the following disclaimer. 8 | // * Redistributions in binary form must reproduce the above 9 | // copyright notice, this list of conditions and the following 10 | // disclaimer in the documentation and/or other materials provided 11 | // with the distribution. 12 | // * Neither the name of Google Inc. nor the names of its 13 | // contributors may be used to endorse or promote products derived 14 | // from this software without specific prior written permission. 15 | // 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | #ifndef DOUBLE_CONVERSION_FIXED_DTOA_H_ 29 | #define DOUBLE_CONVERSION_FIXED_DTOA_H_ 30 | 31 | #include "utils.h" 32 | 33 | namespace double_conversion { 34 | 35 | // Produces digits necessary to print a given number with 36 | // 'fractional_count' digits after the decimal point. 37 | // The buffer must be big enough to hold the result plus one terminating null 38 | // character. 39 | // 40 | // The produced digits might be too short in which case the caller has to fill 41 | // the gaps with '0's. 42 | // Example: FastFixedDtoa(0.001, 5, ...) is allowed to return buffer = "1", and 43 | // decimal_point = -2. 44 | // Halfway cases are rounded towards +/-Infinity (away from 0). The call 45 | // FastFixedDtoa(0.15, 2, ...) thus returns buffer = "2", decimal_point = 0. 46 | // The returned buffer may contain digits that would be truncated from the 47 | // shortest representation of the input. 48 | // 49 | // This method only works for some parameters. If it can't handle the input it 50 | // returns false. The output is null-terminated when the function succeeds. 51 | bool FastFixedDtoa(double v, int fractional_count, 52 | Vector buffer, int* length, int* decimal_point); 53 | 54 | } // namespace double_conversion 55 | 56 | #endif // DOUBLE_CONVERSION_FIXED_DTOA_H_ 57 | -------------------------------------------------------------------------------- /include/mbase/inference/inf_maip_server.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_INF_MAIP_SERVER_H 2 | #define MBASE_INF_MAIP_SERVER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MBASE_BEGIN 11 | 12 | #define MBASE_MAIP_INF_OP_TYPE "INF" 13 | #define MBASE_MAIP_EXEC_OP_TYPE "EXEC" 14 | 15 | class InfMaipServerBase; 16 | class InfMaipDefaultServer; 17 | 18 | typedef bool (*maip_request_callback)(InfProgram&, std::shared_ptr, const maip_peer_request&, const mbase::string&, maip_packet_builder&); 19 | 20 | struct MBASE_API InfMaipPacketAccumulator { 21 | U64 mDataToRead; 22 | mbase::string mSessionToken; 23 | mbase::string mAccumulatedData; 24 | maip_peer_request mPeerRequest; 25 | }; 26 | 27 | class MBASE_API InfMaipServerBase : public mbase::PcNetTcpServer { 28 | public: 29 | using accumulation_map = std::unordered_map; 30 | using request_callback_map = std::unordered_map; 31 | 32 | GENERIC on_accept(std::shared_ptr out_peer) override; 33 | GENERIC on_data(std::shared_ptr out_peer, CBYTEBUFFER out_data, size_type out_size) override; 34 | GENERIC on_disconnect(std::shared_ptr out_peer) override; 35 | 36 | virtual GENERIC on_informatic_request(const maip_peer_request& out_request, std::shared_ptr out_peer) = 0; 37 | virtual GENERIC on_execution_request(const maip_peer_request& out_request, std::shared_ptr out_peer) = 0; 38 | virtual GENERIC on_custom_request(const maip_peer_request& out_request, std::shared_ptr out_peer) = 0; 39 | protected: 40 | 41 | GENERIC register_request_callback(const mbase::string& in_operation, maip_request_callback in_callback); 42 | GENERIC accumulated_processing(std::shared_ptr out_peer, accumulation_map::iterator in_accum_iterator, CBYTEBUFFER out_data, size_type out_size); 43 | GENERIC simple_processing(std::shared_ptr out_peer, CBYTEBUFFER out_data, size_type out_size); 44 | accumulation_map mAccumulationMap; 45 | request_callback_map mRequestCbMap; 46 | }; 47 | 48 | class MBASE_API InfMaipDefaultServer : public mbase::InfMaipServerBase { 49 | public: 50 | InfMaipDefaultServer(InfProgram& in_program); 51 | ~InfMaipDefaultServer() = default; 52 | 53 | GENERIC on_informatic_request(const maip_peer_request& out_request, std::shared_ptr out_peer) override; 54 | GENERIC on_execution_request(const maip_peer_request& out_request, std::shared_ptr out_peer) override; 55 | GENERIC on_custom_request([[maybe_unused]] const maip_peer_request& out_request, [[maybe_unused]] std::shared_ptr out_peer) override; 56 | 57 | GENERIC on_listen() override; 58 | GENERIC on_stop() override; 59 | private: 60 | InfProgram* mHostProgram; 61 | }; 62 | 63 | MBASE_END 64 | 65 | #endif // !MBASE_INF_MAIP_SERVER_H 66 | -------------------------------------------------------------------------------- /cmake/mbase_packaging.cmake: -------------------------------------------------------------------------------- 1 | set(CPACK_PACKAGE_VENDOR "MBASE Yazilim A.S.") 2 | set(CPACK_PACKAGE_CONTACT "erdog@mbasesoftware.com") 3 | set(CPACK_PACKAGE_VERSION_MAJOR ${MBASE_LIBS_MAJOR}) 4 | set(CPACK_PACKAGE_VERSION_MINOR ${MBASE_LIBS_MINOR}) 5 | set(CPACK_PACKAGE_VERSION_PATCH ${MBASE_LIBS_PATCH}) 6 | set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/description.txt) 7 | set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MBASE SDK") 8 | set(CPACK_PACKAGE_HOMEPAGE_URL "https://www.mbasesoftware.com") 9 | set(CPACK_SYSTEM_NAME ${MBASE_SYSTEM_NAME}) 10 | set(CPACK_RESOURCE_FILE_README ${MBASE_LIBS_README}) 11 | set(CPACK_RESOURCE_FILE_LICENSE ${MBASE_LIBS_LICENSE}) 12 | set(CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS 13 | OWNER_READ OWNER_WRITE OWNER_EXECUTE 14 | GROUP_READ GROUP_EXECUTE 15 | WORLD_READ WORLD_EXECUTE 16 | ) 17 | 18 | # DEB 19 | set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_CONTACT}") 20 | set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) 21 | set(CPACK_STRIP_FILES YES) 22 | set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS YES) 23 | 24 | if(MBASE_BUNDLED_INSTALL) 25 | set(CPACK_PACKAGE_NAME "mbase-bundled") 26 | else() 27 | set(CPACK_PACKAGE_NAME "mbase") 28 | endif(MBASE_BUNDLED_INSTALL) 29 | 30 | if(GGML_CUDA) 31 | set(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-cuda") 32 | if (WIN32) 33 | set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/legal/LICENSE-full) 34 | find_package(CUDAToolkit) 35 | install( 36 | FILES ${CUDAToolkit_BIN_DIR}/cublas64_${CUDAToolkit_VERSION_MAJOR}.dll ${CUDAToolkit_BIN_DIR}/cublasLt64_${CUDAToolkit_VERSION_MAJOR}.dll ${CUDAToolkit_BIN_DIR}/cudart64_${CUDAToolkit_VERSION_MAJOR}.dll 37 | DESTINATION ${MBASE_LIBS_BIN_INSTALL_DIR} 38 | ) 39 | install( 40 | FILES ${CMAKE_CURRENT_SOURCE_DIR}/legal/cuda_redis_disclaimer 41 | PERMISSIONS 42 | OWNER_READ 43 | OWNER_WRITE 44 | GROUP_READ 45 | WORLD_READ 46 | DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mbase/license 47 | ) 48 | endif() 49 | endif() 50 | 51 | if(GGML_METAL STREQUAL "ON") 52 | set(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-metal") 53 | endif() 54 | 55 | # NSIS 56 | set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL ON) 57 | set(CPACK_NSIS_MODIFY_PATH ON) 58 | set(CPACK_NSIS_DISPLAY_NAME "MBASE SDK") 59 | set(CPACK_NSIS_CONTACT "${CPACK_PACKAGE_CONTACT}") 60 | set(CPACK_NSIS_UNINSTALL_NAME "MBASE SDK Uninstall") 61 | set(CPACK_PACKAGE_INSTALL_DIRECTORY "mbase.libs") 62 | set(CPACK_NSIS_BRANDING_TEXT "${CPACK_PACKAGE_VENDOR}") 63 | set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}/favicon.ico") 64 | set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}/favicon.ico") 65 | #set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/docs/source/favicon.png") 66 | 67 | # RPM 68 | # CPACK_RPM_PACKAGE_SUMMARY === CPACK_PACKAGE_DESCRIPTION_SUMMARY 69 | # CPACK_RPM_PACKAGE_NAME === CPACK_PACKAGE_NAME 70 | # CPACK_RPM_PACKAGE_VERSION === CPACK_PACKAGE_VERSION 71 | 72 | set(CPACK_RPM_PACKAGE_LICENSE "MIT") 73 | 74 | include(CPack) -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") 2 | message(FATAL_ERROR " 3 | FATAL: In-source builds are not allowed. 4 | You should create a separate directory for build files. 5 | ") 6 | endif() 7 | 8 | cmake_minimum_required(VERSION 3.5...3.31) 9 | 10 | project("mbase.libs" LANGUAGES CXX) 11 | 12 | include(GNUInstallDirs) 13 | 14 | option(BUILD_SHARED_LIBS "MBASE Shared library build default." ON) 15 | 16 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") 17 | set(CMAKE_CXX_STANDARD 17) 18 | 19 | set(MBASE_LIBS_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}) 20 | set(MBASE_LIBS_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}) 21 | set(MBASE_LIBS_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR}) 22 | set(MBASE_LIBS_LIB_NAME "mbase.libs") 23 | set(MBASE_LIBS_MAJOR 0) 24 | set(MBASE_LIBS_MINOR 3) 25 | set(MBASE_LIBS_PATCH 1) 26 | set(MBASE_LIBS_VERSION "${MBASE_LIBS_MAJOR}.${MBASE_LIBS_MINOR}.${MBASE_LIBS_PATCH}") 27 | set(MBASE_LIBS_README ${CMAKE_CURRENT_SOURCE_DIR}/README.md) 28 | set(MBASE_LIBS_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE) 29 | 30 | if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) 31 | set(CMAKE_CUDA_ARCHITECTURES "120;100;90;89;87;86;80;75") # BASICALLY COMPILING FOR ALMOST ALL CUDA ARCHITECTURES 32 | endif() 33 | 34 | include(MBASEStdSys) 35 | include(MBASEJsonSys) 36 | include(MBASEFrameworkSys) 37 | include(MBASECoreSys) 38 | include(MBASEInferenceSys) 39 | include(MBASEMcpSys) 40 | 41 | if(MBASE_BUNDLED_INSTALL STREQUAL "ON") 42 | install( 43 | FILES ${CMAKE_CURRENT_SOURCE_DIR}/llama.cpp/LICENSE 44 | PERMISSIONS 45 | OWNER_READ 46 | OWNER_WRITE 47 | GROUP_READ 48 | WORLD_READ 49 | DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mbase/license 50 | RENAME LICENSE-llama.cpp.txt 51 | ) 52 | endif() 53 | 54 | include(InstallRequiredSystemLibraries) 55 | mbase_install_libraries() 56 | 57 | configure_package_config_file( 58 | ${MBASE_GLOBAL_CONFIG_IN_DIRECTORY}/mbase.libs-config.cmake.in 59 | ${CMAKE_CURRENT_BINARY_DIR}/mbase.libs-config.cmake 60 | INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mbase.libs-config.cmake 61 | PATH_VARS 62 | MBASE_LIBS_INCLUDE_INSTALL_DIR 63 | MBASE_LIBS_LIB_INSTALL_DIR 64 | MBASE_LIBS_BIN_INSTALL_DIR 65 | ) 66 | 67 | install( 68 | FILES ${CMAKE_CURRENT_BINARY_DIR}/mbase.libs-config.cmake 69 | PERMISSIONS 70 | OWNER_READ 71 | OWNER_WRITE 72 | GROUP_READ 73 | WORLD_READ 74 | DESTINATION ${MBASE_LIBS_LIB_INSTALL_DIR}/cmake/mbase.libs 75 | ) 76 | 77 | install( 78 | FILES ${MBASE_LIBS_LICENSE} 79 | PERMISSIONS 80 | OWNER_READ 81 | OWNER_WRITE 82 | GROUP_READ 83 | WORLD_READ 84 | DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mbase/license 85 | ) 86 | 87 | add_subdirectory(examples/openai-server) 88 | add_subdirectory(examples/simple-conversation) 89 | add_subdirectory(examples/typo-fixer) 90 | add_subdirectory(examples/benchmark) 91 | add_subdirectory(examples/embedding) 92 | add_subdirectory(examples/retrieval) 93 | 94 | if(MBASE_PACKAGING) 95 | include(mbase_packaging) 96 | endif() -------------------------------------------------------------------------------- /mbase/inference/inf_maip_peer_base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | MBASE_BEGIN 5 | 6 | InfMaipPeerBase::InfMaipPeerBase(std::shared_ptr in_peer, const mbase::string& in_maip_user, const inf_model_category& in_peer_category) : 7 | mPeer(in_peer), 8 | mMaipUsername(in_maip_user), 9 | mPeerCategory(in_peer_category) 10 | { 11 | 12 | } 13 | 14 | InfMaipPeerBase::~InfMaipPeerBase() 15 | { 16 | 17 | } 18 | 19 | std::shared_ptr InfMaipPeerBase::get_maip_peer() const 20 | { 21 | return mPeer; 22 | } 23 | 24 | const mbase::string& InfMaipPeerBase::get_maip_username() const 25 | { 26 | return mMaipUsername; 27 | } 28 | 29 | const inf_model_category& InfMaipPeerBase::get_peer_category() const 30 | { 31 | return mPeerCategory; 32 | } 33 | 34 | InfProcessorBase* InfMaipPeerBase::get_processor_by_id(const U64& in_id) 35 | { 36 | return mIndexedProcMap[in_id]; 37 | } 38 | 39 | mbase::vector InfMaipPeerBase::get_processor_ids() const 40 | { 41 | mbase::vector procVector; 42 | for(indexed_processor_map::const_iterator It = mIndexedProcMap.cbegin(); It != mIndexedProcMap.cend(); ++It) 43 | { 44 | procVector.push_back(It->first); 45 | } 46 | return procVector; 47 | } 48 | 49 | GENERIC InfMaipPeerBase::add_processor(InfProcessorBase* in_address, U64& out_context_id) 50 | { 51 | mRegisteredProcMap.insert({in_address, mContextCounter}); 52 | mIndexedProcMap.insert({mContextCounter, in_address}); 53 | 54 | out_context_id = mContextCounter; 55 | mContextCounter++; 56 | } 57 | 58 | GENERIC InfMaipPeerBase::set_network_peer(std::shared_ptr in_peer) 59 | { 60 | mPeer = in_peer; 61 | } 62 | 63 | GENERIC InfMaipPeerBase::set_maip_username(const mbase::string& in_user) 64 | { 65 | mMaipUsername = in_user; 66 | } 67 | 68 | GENERIC InfMaipPeerBase::remove_processor_by_address(InfProcessorBase* in_address) 69 | { 70 | registered_processor_map::iterator It = mRegisteredProcMap.find(in_address); 71 | if(It == mRegisteredProcMap.end()) 72 | { 73 | return; 74 | } 75 | 76 | mIndexedProcMap.erase(It->second); 77 | mRegisteredProcMap.erase(It); 78 | } 79 | 80 | GENERIC InfMaipPeerBase::remove_processor_by_id(const U64& in_id) 81 | { 82 | indexed_processor_map::iterator It = mIndexedProcMap.find(in_id); 83 | if(It == mIndexedProcMap.end()) 84 | { 85 | return; 86 | } 87 | 88 | mRegisteredProcMap.erase(It->second); 89 | mIndexedProcMap.erase(It); 90 | } 91 | 92 | GENERIC InfMaipPeerBase::mandatory_processor_cleanup() 93 | { 94 | for(registered_processor_map::iterator It = mRegisteredProcMap.begin(); It != mRegisteredProcMap.end(); ++It) 95 | { 96 | InfProcessorBase* procBase = It->first; 97 | procBase->release_inference_client_stacked(); 98 | delete It->first; 99 | } 100 | } 101 | 102 | MBASE_END -------------------------------------------------------------------------------- /include/mbase/behaviors.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_BEHAVIORS_H 2 | #define MBASE_BEHAVIORS_H 3 | 4 | #include 5 | 6 | MBASE_STD_BEGIN 7 | /* 8 | 9 | --- CLASS INFORMATION --- 10 | Identification: S0C4-STR-NA-ST 11 | 12 | Name: non_copyable 13 | 14 | Parent: None 15 | 16 | Behaviour List: 17 | - Default Constructible 18 | - Move Constructible 19 | - Move Assignable 20 | - Stateless 21 | 22 | Description: 23 | By inheriting this class, it makes the derived 24 | class non copyable. 25 | 26 | */ 27 | 28 | class MBASE_API non_copyable { 29 | public: 30 | /* ===== BUILDER METHODS BEGIN ===== */ 31 | non_copyable() = default; 32 | non_copyable(const non_copyable&) = delete; 33 | /* ===== BUILDER METHODS END ===== */ 34 | 35 | /* ===== OPERATOR BUILDER METHODS BEGIN ===== */ 36 | non_copyable& operator=(const non_copyable&) = delete; 37 | /* ===== OPERATOR BUILDER METHODS END ===== */ 38 | }; 39 | 40 | /* 41 | 42 | --- CLASS INFORMATION --- 43 | Identification: S0C5-STR-NA-ST 44 | 45 | Name: non_movable 46 | 47 | Parent: None 48 | 49 | Behaviour List: 50 | - Default Constructible 51 | - Copy Constructible 52 | - Copy Assignable 53 | - Stateless 54 | 55 | Description: 56 | By inheriting this class, it makes the derived 57 | class non movable. 58 | 59 | */ 60 | 61 | class MBASE_API non_movable { 62 | public: 63 | /* ===== BUILDER METHODS BEGIN ===== */ 64 | non_movable() = default; 65 | non_movable(const non_movable&) = default; 66 | non_movable(non_movable&&) = delete; 67 | /* ===== BUILDER METHODS END ===== */ 68 | 69 | /* ===== OPERATOR BUILDER METHODS BEGIN ===== */ 70 | non_movable& operator=(const non_movable&) = default; 71 | non_movable& operator=(non_movable&&) = delete; 72 | /* ===== OPERATOR BUILDER METHODS END ===== */ 73 | }; 74 | 75 | /* 76 | 77 | --- CLASS INFORMATION --- 78 | Identification: S0C6-STR-NA-ST 79 | 80 | Name: non_copymovable 81 | 82 | Parent: None 83 | 84 | Behaviour List: 85 | - Default Constructible 86 | - Stateless 87 | 88 | Description: 89 | By inheriting this class, it makes the derived 90 | both non copyable and non movable. 91 | 92 | */ 93 | 94 | class MBASE_API non_copymovable { 95 | public: 96 | /* ===== BUILDER METHODS BEGIN ===== */ 97 | non_copymovable() = default; 98 | non_copymovable(const non_copymovable&) = delete; 99 | non_copymovable(non_copymovable&&) = delete; 100 | /* ===== BUILDER METHODS END ===== */ 101 | 102 | /* ===== OPERATOR BUILDER METHODS BEGIN ===== */ 103 | non_copymovable& operator=(const non_copymovable&) = delete; 104 | non_copymovable& operator=(non_copymovable&&) = delete; 105 | /* ===== OPERATOR BUILDER METHODS END ===== */ 106 | }; 107 | 108 | template 109 | class MBASE_API singleton : public non_copymovable { 110 | public: 111 | static Object& get_instance() noexcept 112 | { 113 | static Object mInstance; 114 | return mInstance; 115 | } 116 | protected: 117 | singleton(){} 118 | ~singleton(){} 119 | }; 120 | 121 | MBASE_STD_END 122 | 123 | #endif // !MBASE_BEHAVIORS_H 124 | -------------------------------------------------------------------------------- /include/mbase/mcp/mcp_server_client_state.h: -------------------------------------------------------------------------------- 1 | #ifndef MBASE_MCP_SERVER_CLIENT_STATE_H 2 | #define MBASE_MCP_SERVER_CLIENT_STATE_H 3 | 4 | #include 5 | #include // Async IO 6 | #include // Notification objects 7 | #include // Sampling and root request objects 8 | 9 | MBASE_BEGIN 10 | 11 | class McpServerBase; 12 | class McpServerClient; 13 | 14 | class MBASE_API McpServerClient : public mbase::logical_processor { 15 | public: 16 | friend class McpServerBase; 17 | 18 | McpServerClient(McpServerBase* in_server_instance); 19 | ~McpServerClient(); 20 | 21 | mcp_log_levels get_log_level() const noexcept; 22 | McpServerBase* get_server_instance() noexcept; 23 | bool is_subscribed(const mbase::string& in_uri) const noexcept; 24 | bool is_initializing() const noexcept; 25 | bool is_initialized() const noexcept; 26 | bool has_roots() const noexcept; 27 | bool has_sampling() const noexcept; 28 | 29 | GENERIC on_client_init_request_t(const mbase::Json& in_msgid, mbase::Json& in_params); 30 | virtual GENERIC on_empty_processed_t(); // if the received message is processed but no response is generated 31 | 32 | GENERIC list_roots(mcp_st_list_roots_cb in_cb); 33 | GENERIC request_sampling(const McpSamplingRequest& in_params, mcp_st_sampling_cb in_cb); 34 | GENERIC subscribe(const mbase::string& in_uri); 35 | GENERIC unsubscribe(const mbase::string& in_uri); 36 | GENERIC set_progress(const I32& in_progress, const mbase::Json& in_token, const mbase::string& in_message = mbase::string()); 37 | GENERIC set_log_level(mcp_log_levels in_log_level); 38 | GENERIC send_log(const McpNotificationLogMessage& in_log); 39 | GENERIC read_mcp_payload(const mbase::string& in_payload); 40 | GENERIC update() override; 41 | virtual GENERIC send_mcp_payload(const mbase::string& in_payload) = 0; 42 | 43 | protected: 44 | GENERIC process_mcp_message(mbase::Json& in_message); 45 | GENERIC process_notification_message(mbase::Json& in_message); 46 | GENERIC process_request_message(mbase::Json& in_message); 47 | GENERIC process_response_message(mbase::Json& in_message); 48 | GENERIC default_update_method(); 49 | 50 | McpServerBase* mServerInstance = nullptr; 51 | mcp_log_levels mLogLevel; 52 | bool mIsInitializing = false; 53 | bool mIsClientInitialized = false; 54 | bool mHasRoots = false; 55 | bool mHasSampling = false; 56 | mbase::string mClientName; 57 | mbase::string mClientVersion; 58 | mbase::string mClientProtocolVersion; 59 | mbase::unordered_map mSubscriptionMap; 60 | mbase::unordered_map mRequestObject; 61 | mbase::vector mRootsResultObjects; 62 | mbase::vector mSamplingResultObjects; 63 | mbase::mutex mRootsResultObjectsSync; 64 | mbase::mutex mSamplingResultObjectsSync; 65 | }; 66 | 67 | MBASE_END 68 | 69 | #endif // MBASE_MCP_SERVER_CLIENT_STATE_H --------------------------------------------------------------------------------