Skip to content

Commit 597b81d

Browse files
Copilotdrzo
andcommitted
Implement OpenCog transformation foundation structure
Co-authored-by: drzo <[email protected]>
1 parent 884abef commit 597b81d

18 files changed

+1312
-0
lines changed

Argcfile.sh

100644100755
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ build() {
7474
if [[ -f mcp.json ]]; then
7575
argc mcp merge-functions -S
7676
fi
77+
# Build OpenCog components if available
78+
if [[ -f CMakeLists.txt ]]; then
79+
echo 'Building OpenCog components...'
80+
argc build@opencog
81+
fi
82+
}
83+
84+
# @cmd Build OpenCog module
85+
# @alias opencog:build
86+
build@opencog() {
87+
if [[ ! -f build-opencog.sh ]]; then
88+
_die "error: build-opencog.sh not found"
89+
fi
90+
./build-opencog.sh
7791
}
7892

7993
# @cmd Build tools
@@ -584,6 +598,31 @@ create@tool() {
584598
./scripts/create-tool.sh "$@"
585599
}
586600
601+
# @cmd Test OpenCog module structure
602+
# @alias opencog:test
603+
test@opencog() {
604+
if [[ ! -f test-structure.sh ]]; then
605+
_die "error: test-structure.sh not found"
606+
fi
607+
./test-structure.sh
608+
}
609+
610+
# @cmd Run OpenCog example
611+
# @alias opencog:example
612+
example@opencog() {
613+
if [[ ! -f example.scm ]]; then
614+
_die "error: example.scm not found"
615+
fi
616+
if command -v guile &> /dev/null; then
617+
guile example.scm
618+
else
619+
echo "Guile not installed. Install it to run examples:"
620+
echo " Ubuntu/Debian: sudo apt-get install guile-3.0"
621+
echo " Fedora: sudo dnf install guile"
622+
echo " macOS: brew install guile"
623+
fi
624+
}
625+
587626
# @cmd Displays version information for required tools
588627
version() {
589628
uname -a

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(caichat-opencog VERSION 1.0.0)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
# Find required packages
8+
find_package(PkgConfig REQUIRED)
9+
10+
# Find OpenCog (optional for now)
11+
find_package(PkgConfig QUIET)
12+
if(PKG_CONFIG_FOUND)
13+
pkg_check_modules(OPENCOG opencog)
14+
endif()
15+
16+
# Find Guile
17+
pkg_check_modules(GUILE REQUIRED guile-3.0)
18+
if(NOT GUILE_FOUND)
19+
pkg_check_modules(GUILE REQUIRED guile-2.2)
20+
endif()
21+
22+
# Find other dependencies
23+
find_package(CURL REQUIRED)
24+
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
25+
26+
# Include directories
27+
include_directories(${GUILE_INCLUDE_DIRS})
28+
include_directories(${JSONCPP_INCLUDE_DIRS})
29+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/opencog)
30+
31+
# Add subdirectories
32+
add_subdirectory(opencog)
33+
34+
# Install Scheme modules
35+
install(DIRECTORY scm/ DESTINATION ${CMAKE_INSTALL_PREFIX}/share/guile/site/3.0/opencog
36+
FILES_MATCHING PATTERN "*.scm")
37+
38+
# Configuration
39+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/caichat-config.cmake.in
40+
${CMAKE_CURRENT_BINARY_DIR}/caichat-config.cmake @ONLY)
41+
42+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/caichat-config.cmake
43+
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/caichat)

build-opencog.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
# Build script for CAIChat OpenCog module
3+
4+
set -e
5+
6+
echo "Building CAIChat OpenCog Module..."
7+
8+
# Check dependencies
9+
check_dependencies() {
10+
echo "Checking dependencies..."
11+
12+
local missing_deps=()
13+
14+
if ! command -v cmake &> /dev/null; then
15+
missing_deps+=("cmake")
16+
fi
17+
18+
if ! command -v pkg-config &> /dev/null; then
19+
missing_deps+=("pkg-config")
20+
fi
21+
22+
if ! pkg-config --exists guile-3.0 && ! pkg-config --exists guile-2.2; then
23+
missing_deps+=("guile-dev")
24+
fi
25+
26+
if ! pkg-config --exists libcurl; then
27+
missing_deps+=("libcurl-dev")
28+
fi
29+
30+
if ! pkg-config --exists jsoncpp; then
31+
missing_deps+=("libjsoncpp-dev")
32+
fi
33+
34+
if [ ${#missing_deps[@]} -ne 0 ]; then
35+
echo "Missing dependencies: ${missing_deps[*]}"
36+
echo "Please install them first:"
37+
echo " Ubuntu/Debian: sudo apt-get install cmake build-essential pkg-config guile-3.0-dev libcurl4-openssl-dev libjsoncpp-dev"
38+
echo " Fedora: sudo dnf install cmake gcc-c++ pkg-config guile-devel libcurl-devel jsoncpp-devel"
39+
echo " macOS: brew install cmake pkg-config guile curl jsoncpp"
40+
exit 1
41+
fi
42+
43+
echo "All dependencies found!"
44+
}
45+
46+
# Build function
47+
build_project() {
48+
echo "Creating build directory..."
49+
mkdir -p build
50+
cd build
51+
52+
echo "Running CMake..."
53+
cmake ..
54+
55+
echo "Building..."
56+
make -j$(nproc 2>/dev/null || echo 4)
57+
58+
echo "Build completed successfully!"
59+
echo "To install: sudo make install"
60+
}
61+
62+
# Test function (if Guile is available)
63+
test_modules() {
64+
if command -v guile &> /dev/null; then
65+
echo "Testing Scheme modules..."
66+
cd ..
67+
guile test-caichat.scm
68+
else
69+
echo "Guile not found, skipping tests"
70+
fi
71+
}
72+
73+
# Main execution
74+
main() {
75+
check_dependencies
76+
build_project
77+
test_modules
78+
}
79+
80+
# Run only if executed directly
81+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
82+
main "$@"
83+
fi

caichat-config.cmake.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@PACKAGE_INIT@
2+
3+
find_dependency(PkgConfig REQUIRED)
4+
find_dependency(CURL REQUIRED)
5+
6+
pkg_check_modules(GUILE REQUIRED guile-3.0)
7+
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
8+
9+
include("${CMAKE_CURRENT_LIST_DIR}/caichat-targets.cmake")
10+
11+
check_required_components(caichat)

example.scm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env guile
2+
!#
3+
4+
;; Example usage of CAIChat OpenCog module
5+
6+
(use-modules (opencog caichat init)
7+
(opencog caichat config)
8+
(opencog caichat repl)
9+
(opencog caichat rag))
10+
11+
;; Initialize configuration
12+
(caichat-config-init)
13+
14+
;; Example 1: Simple question
15+
(display "Example 1: Simple question\n")
16+
(display "Question: What is cognitive architecture?\n")
17+
(display "Answer: ")
18+
(display (caichat-ask "What is cognitive architecture in 2 sentences?"))
19+
(newline)
20+
(newline)
21+
22+
;; Example 2: Configuration
23+
(display "Example 2: Configuration\n")
24+
(caichat-config-set! 'default-provider "openai")
25+
(display (format #f "Default provider: ~a\n"
26+
(caichat-config-get 'default-provider)))
27+
(newline)
28+
29+
;; Example 3: RAG system
30+
(display "Example 3: RAG system\n")
31+
(caichat-rag-create-kb "ai-research" "AI research knowledge base")
32+
(caichat-rag-add-doc "ai-research" "paper1"
33+
"Cognitive architecture is a framework for understanding intelligence. It provides a blueprint for building AI systems."
34+
'(("author" . "Smith") ("year" . "2024")))
35+
(caichat-rag-add-doc "ai-research" "paper2"
36+
"OpenCog is an open-source cognitive architecture platform. It uses AtomSpace for knowledge representation."
37+
'(("author" . "Goertzel") ("year" . "2023")))
38+
39+
(display "RAG Query: What is OpenCog?\n")
40+
(display "Answer: ")
41+
(display (caichat-rag-query "ai-research" "What is OpenCog?"))
42+
(newline)
43+
(newline)
44+
45+
;; Example 4: Interactive REPL (commented out for non-interactive execution)
46+
;; (display "Starting interactive REPL...\n")
47+
;; (caichat-repl-start)
48+
49+
(display "Examples completed successfully!\n")

opencog/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# C++ Core Library
2+
3+
add_library(caichat SHARED
4+
caichat/LLMClient.cc
5+
caichat/ChatCompletion.cc
6+
caichat/SchemeBindings.cc
7+
)
8+
9+
# Include directories
10+
target_include_directories(caichat PRIVATE
11+
${CMAKE_CURRENT_SOURCE_DIR}
12+
${GUILE_INCLUDE_DIRS}
13+
${JSONCPP_INCLUDE_DIRS}
14+
)
15+
16+
# Link libraries
17+
target_link_libraries(caichat
18+
${GUILE_LIBRARIES}
19+
${JSONCPP_LIBRARIES}
20+
${CURL_LIBRARIES}
21+
)
22+
23+
# Compile flags
24+
target_compile_options(caichat PRIVATE ${GUILE_CFLAGS_OTHER})
25+
26+
# Install library
27+
install(TARGETS caichat
28+
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
29+
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
30+
31+
# Install headers
32+
install(FILES caichat/LLMClient.h caichat/ChatCompletion.h
33+
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/opencog/caichat)

opencog/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# CAIChat OpenCog Module
2+
3+
This directory contains the OpenCog implementation of CAIChat, transforming the original LLM Functions tools into a cognitive architecture component.
4+
5+
## Structure
6+
7+
- `caichat/` - C++ core library
8+
- `LLMClient.h/cc` - LLM provider interfaces and implementations
9+
- `ChatCompletion.h/cc` - Conversation management
10+
- `SchemeBindings.cc` - Guile Scheme bindings
11+
12+
## Building
13+
14+
```bash
15+
mkdir build
16+
cd build
17+
cmake ..
18+
make
19+
```
20+
21+
## Dependencies
22+
23+
- CMake 3.10+
24+
- C++14 compiler
25+
- Guile 3.0 (or 2.2)
26+
- libcurl
27+
- jsoncpp
28+
29+
## Usage
30+
31+
After building and installing, the module can be used in Guile:
32+
33+
```scheme
34+
(use-modules (opencog caichat init))
35+
(caichat-ask "Hello, how are you?")
36+
```
37+
38+
See `../example.scm` for more comprehensive examples.

opencog/caichat/ChatCompletion.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "ChatCompletion.h"
2+
3+
namespace opencog {
4+
namespace caichat {
5+
6+
ChatCompletion::ChatCompletion(std::unique_ptr<LLMClient> llmClient, const std::string& model)
7+
: client(std::move(llmClient)), defaultModel(model) {
8+
}
9+
10+
std::string ChatCompletion::sendMessage(const std::string& message, const std::string& role) {
11+
// Add user message to history
12+
conversationHistory.emplace_back(role, message);
13+
14+
// Get response from LLM
15+
std::string response = client->chatCompletion(conversationHistory, defaultModel);
16+
17+
// Add assistant response to history
18+
conversationHistory.emplace_back("assistant", response);
19+
20+
return response;
21+
}
22+
23+
const std::vector<Message>& ChatCompletion::getHistory() const {
24+
return conversationHistory;
25+
}
26+
27+
void ChatCompletion::clearHistory() {
28+
conversationHistory.clear();
29+
}
30+
31+
void ChatCompletion::setSystemMessage(const std::string& message) {
32+
// Remove existing system message if any
33+
auto it = conversationHistory.begin();
34+
if (it != conversationHistory.end() && it->role == "system") {
35+
conversationHistory.erase(it);
36+
}
37+
38+
// Add new system message at the beginning
39+
conversationHistory.insert(conversationHistory.begin(), Message("system", message));
40+
}
41+
42+
LLMClient* ChatCompletion::getClient() const {
43+
return client.get();
44+
}
45+
46+
} // namespace caichat
47+
} // namespace opencog

0 commit comments

Comments
 (0)