aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2024-02-26 23:45:20 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2024-02-26 23:45:20 +0000
commita398252b0dfe3c112e5643104aaf22c411a15b1d (patch)
treeac53dd723597dac37d8139ea57f87cd0ad8f252b
parent12238c47081cd789c09e6677495cca3d31356cf1 (diff)
downloadnoetic-llama-a398252b0dfe3c112e5643104aaf22c411a15b1d.tar.gz
noetic-llama-a398252b0dfe3c112e5643104aaf22c411a15b1d.zip
Started on ROS1 workspace
-rw-r--r--.gitignore3
-rw-r--r--noetic-llama/.devcontainer/Dockerfile4
-rw-r--r--noetic-llama/.devcontainer/devcontainer.json3
-rwxr-xr-xnoetic-llama/src/CMakeLists.txt69
-rw-r--r--noetic-llama/src/ollamawrapper/CMakeLists.txt206
-rw-r--r--noetic-llama/src/ollamawrapper/package.xml68
-rw-r--r--noetic-llama/src/ollamawrapper/src/Modelfile (renamed from Modelfile)0
-rw-r--r--noetic-llama/src/ollamawrapper/src/Modelfile.jinja211
-rw-r--r--noetic-llama/src/ollamawrapper/src/capabilities/__init__.py2
-rw-r--r--noetic-llama/src/ollamawrapper/src/capabilities/basicmaths.py12
-rw-r--r--noetic-llama/src/ollamawrapper/src/capabilities/weather.py78
-rw-r--r--noetic-llama/src/ollamawrapper/src/ollamafunctiongrammar.ppeg12
-rw-r--r--noetic-llama/src/ollamawrapper/src/ollamawrapper.py86
-rw-r--r--noetic-llama/src/ollamawrapper/src/parser.py37
-rwxr-xr-xnoetic-llama/src/ollamawrapper/srv/OllamaCall.srv8
-rw-r--r--temp.py8
16 files changed, 597 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 68bc17f..47f393a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+*.env
+devel/
+
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
diff --git a/noetic-llama/.devcontainer/Dockerfile b/noetic-llama/.devcontainer/Dockerfile
index d86a5e7..6992623 100644
--- a/noetic-llama/.devcontainer/Dockerfile
+++ b/noetic-llama/.devcontainer/Dockerfile
@@ -21,10 +21,12 @@ RUN sudo usermod --append --groups video $USERNAME
RUN sudo apt update && sudo apt upgrade -y
# Install Git
-RUN sudo apt install -y git
+RUN sudo apt install -y git python3-pip
# Rosdep update
RUN rosdep update
+RUN pip3 install jinja2 ollama geocoder requests python-dotenv parsimonious
+
# Source the ROS setup file
RUN echo "source /opt/ros/${ROS_DISTRO}/setup.bash" >> ~/.bashrc
diff --git a/noetic-llama/.devcontainer/devcontainer.json b/noetic-llama/.devcontainer/devcontainer.json
index 64a355a..53bcc43 100644
--- a/noetic-llama/.devcontainer/devcontainer.json
+++ b/noetic-llama/.devcontainer/devcontainer.json
@@ -13,7 +13,8 @@
"--security-opt=apparmor:unconfined",
"--volume=/tmp/.X11-unix:/tmp/.X11-unix",
"--volume=/home/agilex/.Xauthority:/home/ros/.Xauthority",
- "--gpus=all"
+ "--gpus=all",
+ "--env-file","apikeys.env"
],
"containerEnv": {
"DISPLAY": ":0",
diff --git a/noetic-llama/src/CMakeLists.txt b/noetic-llama/src/CMakeLists.txt
new file mode 100755
index 0000000..cd58121
--- /dev/null
+++ b/noetic-llama/src/CMakeLists.txt
@@ -0,0 +1,69 @@
+# toplevel CMakeLists.txt for a catkin workspace
+# catkin/cmake/toplevel.cmake
+
+cmake_minimum_required(VERSION 3.0.2)
+
+project(Project)
+
+set(CATKIN_TOPLEVEL TRUE)
+
+# search for catkin within the workspace
+set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}")
+execute_process(COMMAND ${_cmd}
+ RESULT_VARIABLE _res
+ OUTPUT_VARIABLE _out
+ ERROR_VARIABLE _err
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ ERROR_STRIP_TRAILING_WHITESPACE
+)
+if(NOT _res EQUAL 0 AND NOT _res EQUAL 2)
+ # searching fot catkin resulted in an error
+ string(REPLACE ";" " " _cmd_str "${_cmd}")
+ message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}")
+endif()
+
+# include catkin from workspace or via find_package()
+if(_res EQUAL 0)
+ set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake")
+ # include all.cmake without add_subdirectory to let it operate in same scope
+ include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE)
+ add_subdirectory("${_out}")
+
+else()
+ # use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument
+ # or CMAKE_PREFIX_PATH from the environment
+ if(NOT DEFINED CMAKE_PREFIX_PATH)
+ if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
+ if(NOT WIN32)
+ string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
+ else()
+ set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
+ endif()
+ endif()
+ endif()
+
+ # list of catkin workspaces
+ set(catkin_search_path "")
+ foreach(path ${CMAKE_PREFIX_PATH})
+ if(EXISTS "${path}/.catkin")
+ list(FIND catkin_search_path ${path} _index)
+ if(_index EQUAL -1)
+ list(APPEND catkin_search_path ${path})
+ endif()
+ endif()
+ endforeach()
+
+ # search for catkin in all workspaces
+ set(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE)
+ find_package(catkin QUIET
+ NO_POLICY_SCOPE
+ PATHS ${catkin_search_path}
+ NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
+ unset(CATKIN_TOPLEVEL_FIND_PACKAGE)
+
+ if(NOT catkin_FOUND)
+ message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.")
+ endif()
+endif()
+
+catkin_workspace()
diff --git a/noetic-llama/src/ollamawrapper/CMakeLists.txt b/noetic-llama/src/ollamawrapper/CMakeLists.txt
new file mode 100644
index 0000000..c94424b
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/CMakeLists.txt
@@ -0,0 +1,206 @@
+cmake_minimum_required(VERSION 3.0.2)
+project(ollamawrapper)
+
+## Compile as C++11, supported in ROS Kinetic and newer
+# add_compile_options(-std=c++11)
+
+## Find catkin macros and libraries
+## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
+## is used, also find other catkin packages
+find_package(catkin REQUIRED COMPONENTS
+ roscpp
+ rospy
+ std_msgs
+ message_generation
+)
+
+## System dependencies are found with CMake's conventions
+# find_package(Boost REQUIRED COMPONENTS system)
+
+
+## Uncomment this if the package has a setup.py. This macro ensures
+## modules and global scripts declared therein get installed
+## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
+# catkin_python_setup()
+
+################################################
+## Declare ROS messages, services and actions ##
+################################################
+
+## To declare and build messages, services or actions from within this
+## package, follow these steps:
+## * Let MSG_DEP_SET be the set of packages whose message types you use in
+## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
+## * In the file package.xml:
+## * add a build_depend tag for "message_generation"
+## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
+## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
+## but can be declared for certainty nonetheless:
+## * add a exec_depend tag for "message_runtime"
+## * In this file (CMakeLists.txt):
+## * add "message_generation" and every package in MSG_DEP_SET to
+## find_package(catkin REQUIRED COMPONENTS ...)
+## * add "message_runtime" and every package in MSG_DEP_SET to
+## catkin_package(CATKIN_DEPENDS ...)
+## * uncomment the add_*_files sections below as needed
+## and list every .msg/.srv/.action file to be processed
+## * uncomment the generate_messages entry below
+## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
+
+## Generate messages in the 'msg' folder
+# add_message_files(
+# FILES
+# Message1.msg
+# Message2.msg
+# )
+
+## Generate services in the 'srv' folder
+add_service_files(
+ FILES
+ OllamaCall.srv
+)
+
+## Generate actions in the 'action' folder
+# add_action_files(
+# FILES
+# Action1.action
+# Action2.action
+# )
+
+## Generate added messages and services with any dependencies listed here
+generate_messages(
+ DEPENDENCIES
+ std_msgs
+)
+
+################################################
+## Declare ROS dynamic reconfigure parameters ##
+################################################
+
+## To declare and build dynamic reconfigure parameters within this
+## package, follow these steps:
+## * In the file package.xml:
+## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
+## * In this file (CMakeLists.txt):
+## * add "dynamic_reconfigure" to
+## find_package(catkin REQUIRED COMPONENTS ...)
+## * uncomment the "generate_dynamic_reconfigure_options" section below
+## and list every .cfg file to be processed
+
+## Generate dynamic reconfigure parameters in the 'cfg' folder
+# generate_dynamic_reconfigure_options(
+# cfg/DynReconf1.cfg
+# cfg/DynReconf2.cfg
+# )
+
+###################################
+## catkin specific configuration ##
+###################################
+## The catkin_package macro generates cmake config files for your package
+## Declare things to be passed to dependent projects
+## INCLUDE_DIRS: uncomment this if your package contains header files
+## LIBRARIES: libraries you create in this project that dependent projects also need
+## CATKIN_DEPENDS: catkin_packages dependent projects also need
+## DEPENDS: system dependencies of this project that dependent projects also need
+catkin_package(
+# INCLUDE_DIRS include
+# LIBRARIES ollamawrapper
+# CATKIN_DEPENDS roscpp rospy std_msgs
+# DEPENDS system_lib
+)
+
+###########
+## Build ##
+###########
+
+## Specify additional locations of header files
+## Your package locations should be listed before other locations
+include_directories(
+# include
+ ${catkin_INCLUDE_DIRS}
+)
+
+## Declare a C++ library
+# add_library(${PROJECT_NAME}
+# src/${PROJECT_NAME}/ollamawrapper.cpp
+# )
+
+## Add cmake target dependencies of the library
+## as an example, code may need to be generated before libraries
+## either from message generation or dynamic reconfigure
+# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
+
+## Declare a C++ executable
+## With catkin_make all packages are built within a single CMake context
+## The recommended prefix ensures that target names across packages don't collide
+# add_executable(${PROJECT_NAME}_node src/ollamawrapper_node.cpp)
+
+## Rename C++ executable without prefix
+## The above recommended prefix causes long target names, the following renames the
+## target back to the shorter version for ease of user use
+## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
+# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
+
+## Add cmake target dependencies of the executable
+## same as for the library above
+# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
+
+## Specify libraries to link a library or executable target against
+# target_link_libraries(${PROJECT_NAME}_node
+# ${catkin_LIBRARIES}
+# )
+
+#############
+## Install ##
+#############
+
+# all install targets should use catkin DESTINATION variables
+# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
+
+## Mark executable scripts (Python etc.) for installation
+## in contrast to setup.py, you can choose the destination
+catkin_install_python(PROGRAMS
+ src/ollamawrapper.py
+ DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
+)
+
+## Mark executables for installation
+## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
+# install(TARGETS ${PROJECT_NAME}_node
+# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
+# )
+
+## Mark libraries for installation
+## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
+# install(TARGETS ${PROJECT_NAME}
+# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
+# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
+# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
+# )
+
+## Mark cpp header files for installation
+# install(DIRECTORY include/${PROJECT_NAME}/
+# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
+# FILES_MATCHING PATTERN "*.h"
+# PATTERN ".svn" EXCLUDE
+# )
+
+## Mark other files for installation (e.g. launch and bag files, etc.)
+# install(FILES
+# # myfile1
+# # myfile2
+# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
+# )
+
+#############
+## Testing ##
+#############
+
+## Add gtest based cpp test target and link libraries
+# catkin_add_gtest(${PROJECT_NAME}-test test/test_ollamawrapper.cpp)
+# if(TARGET ${PROJECT_NAME}-test)
+# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
+# endif()
+
+## Add folders to be run by python nosetests
+# catkin_add_nosetests(test)
diff --git a/noetic-llama/src/ollamawrapper/package.xml b/noetic-llama/src/ollamawrapper/package.xml
new file mode 100644
index 0000000..cfad2a4
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/package.xml
@@ -0,0 +1,68 @@
+<?xml version="1.0"?>
+<package format="2">
+ <name>ollamawrapper</name>
+ <version>0.0.0</version>
+ <description>The ollamawrapper package</description>
+
+ <!-- One maintainer tag required, multiple allowed, one person per tag -->
+ <!-- Example: -->
+ <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
+ <maintainer email="root@todo.todo">root</maintainer>
+
+
+ <!-- One license tag required, multiple allowed, one license per tag -->
+ <!-- Commonly used license strings: -->
+ <!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
+ <license>TODO</license>
+
+
+ <!-- Url tags are optional, but multiple are allowed, one per tag -->
+ <!-- Optional attribute type can be: website, bugtracker, or repository -->
+ <!-- Example: -->
+ <!-- <url type="website">http://wiki.ros.org/ollamawrapper</url> -->
+
+
+ <!-- Author tags are optional, multiple are allowed, one per tag -->
+ <!-- Authors do not have to be maintainers, but could be -->
+ <!-- Example: -->
+ <!-- <author email="jane.doe@example.com">Jane Doe</author> -->
+
+
+ <!-- The *depend tags are used to specify dependencies -->
+ <!-- Dependencies can be catkin packages or system dependencies -->
+ <!-- Examples: -->
+ <!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
+ <!-- <depend>roscpp</depend> -->
+ <!-- Note that this is equivalent to the following: -->
+ <!-- <build_depend>roscpp</build_depend> -->
+ <!-- <exec_depend>roscpp</exec_depend> -->
+ <!-- Use build_depend for packages you need at compile time: -->
+ <build_depend>message_generation</build_depend>
+ <!-- Use build_export_depend for packages you need in order to build against this package: -->
+ <!-- <build_export_depend>message_generation</build_export_depend> -->
+ <!-- Use buildtool_depend for build tool packages: -->
+ <!-- <buildtool_depend>catkin</buildtool_depend> -->
+ <!-- Use exec_depend for packages you need at runtime: -->
+ <exec_depend>message_runtime</exec_depend>
+ <!-- Use test_depend for packages you need only for testing: -->
+ <!-- <test_depend>gtest</test_depend> -->
+ <!-- Use doc_depend for packages you need only for building documentation: -->
+ <!-- <doc_depend>doxygen</doc_depend> -->
+ <buildtool_depend>catkin</buildtool_depend>
+ <build_depend>roscpp</build_depend>
+ <build_depend>rospy</build_depend>
+ <build_depend>std_msgs</build_depend>
+ <build_export_depend>roscpp</build_export_depend>
+ <build_export_depend>rospy</build_export_depend>
+ <build_export_depend>std_msgs</build_export_depend>
+ <exec_depend>roscpp</exec_depend>
+ <exec_depend>rospy</exec_depend>
+ <exec_depend>std_msgs</exec_depend>
+
+
+ <!-- The export tag contains other, unspecified, tags -->
+ <export>
+ <!-- Other tools can request additional information be placed here -->
+
+ </export>
+</package>
diff --git a/Modelfile b/noetic-llama/src/ollamawrapper/src/Modelfile
index bc66595..bc66595 100644
--- a/Modelfile
+++ b/noetic-llama/src/ollamawrapper/src/Modelfile
diff --git a/noetic-llama/src/ollamawrapper/src/Modelfile.jinja2 b/noetic-llama/src/ollamawrapper/src/Modelfile.jinja2
new file mode 100644
index 0000000..25af1bc
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/Modelfile.jinja2
@@ -0,0 +1,11 @@
+FROM {{ model }}
+SYSTEM """
+{%- for functioncapability in functioncapabilities %}
+Function:
+def {{ functioncapability.functioname }}({{ ", ".join(functioncapability.argnames) }}):
+ '''
+ {{ functioncapability.docstring|indent(4, False) }}
+ '''
+{% endfor %}
+User Query: {query}<human_end>
+""" \ No newline at end of file
diff --git a/noetic-llama/src/ollamawrapper/src/capabilities/__init__.py b/noetic-llama/src/ollamawrapper/src/capabilities/__init__.py
new file mode 100644
index 0000000..7c07dce
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/capabilities/__init__.py
@@ -0,0 +1,2 @@
+from .weather import *
+from .basicmaths import * \ No newline at end of file
diff --git a/noetic-llama/src/ollamawrapper/src/capabilities/basicmaths.py b/noetic-llama/src/ollamawrapper/src/capabilities/basicmaths.py
new file mode 100644
index 0000000..e2491e0
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/capabilities/basicmaths.py
@@ -0,0 +1,12 @@
+
+def add(num1=1, num2=2):
+ """Returns the output of two numbers added together
+
+ Args:
+ num1 (int): The first number to add together
+ num2 (int): The second number to add together
+
+ Returns:
+ int: The sum of the two numbers
+ """
+ print("%f + %f = %f" % (num1, num2, num1 + num2)) \ No newline at end of file
diff --git a/noetic-llama/src/ollamawrapper/src/capabilities/weather.py b/noetic-llama/src/ollamawrapper/src/capabilities/weather.py
new file mode 100644
index 0000000..0108ee5
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/capabilities/weather.py
@@ -0,0 +1,78 @@
+import os
+import dotenv
+import geocoder
+import requests
+
+if os.path.exists(os.path.join(os.path.dirname(__file__), "..", "..", "..", "apikeys.env")):
+ dotenv.load_dotenv(os.path.join(os.path.dirname(__file__), "..", "..", "..", "apikeys.env"))
+
+if "BINGMAPS" not in os.environ:
+ raise Exception("'BINGMAPS' API key environment variable not found")
+
+WEATHER_CODES = {
+ 0: "Clear sky",
+ 1: "Mainly clear, partly cloudy, and overcast",
+ 2: "Mainly clear, partly cloudy, and overcast",
+ 3: "Mainly clear, partly cloudy, and overcast",
+ 45: "Fog and depositing rime fog",
+ 48: "Fog and depositing rime fog",
+ 51: "Drizzle: Light, moderate, and dense intensity",
+ 53: "Drizzle: Light, moderate, and dense intensity",
+ 55: "Drizzle: Light, moderate, and dense intensity",
+ 56: "Freezing Drizzle: Light and dense intensity",
+ 57: "Freezing Drizzle: Light and dense intensity",
+ 61: "Rain: Slight, moderate and heavy intensity",
+ 63: "Rain: Slight, moderate and heavy intensity",
+ 65: "Rain: Slight, moderate and heavy intensity",
+ 66: "Freezing Rain: Light and heavy intensity",
+ 67: "Freezing Rain: Light and heavy intensity",
+ 71: "Snow fall: Slight, moderate, and heavy intensity",
+ 73: "Snow fall: Slight, moderate, and heavy intensity",
+ 75: "Snow fall: Slight, moderate, and heavy intensity",
+ 77: "Snow grains",
+ 80: "Rain showers: Slight, moderate, and violent",
+ 81: "Rain showers: Slight, moderate, and violent",
+ 82: "Rain showers: Slight, moderate, and violent",
+ 85: "Snow showers slight and heavy",
+ 86: "Snow showers slight and heavy",
+ 95: "Thunderstorm: Slight or moderate",
+ 96: "Thunderstorm with slight and heavy hail",
+ 99: "Thunderstorm with slight and heavy hail"
+}
+
+def get_weather_data(coordinates):
+ '''
+ Fetches weather data from the Open-Meteo API for the given latitude and longitude.
+
+ Args:
+ coordinates (tuple): The latitude of the location.
+
+ Returns:
+ float: The current temperature in the coordinates you've asked for
+ '''
+ req = requests.get("https://api.open-meteo.com/v1/forecast?latitude=%f&longitude=%f&current=temperature_2m&current=weather_code" % coordinates)
+ j = req.json()
+ o = "The current temperature is %f°C with an outlook of '%s'" % (j["current"]["temperature_2m"], WEATHER_CODES[j["current"]["weather_code"]])
+ print(o)
+ return o
+
+
+def get_coordinates_from_city(city_name):
+ '''
+ Fetches the latitude and longitude of a given city name using the Bing Geocoding API.
+
+ Args:
+ city_name (str): The name of the city.
+
+ Returns:
+ tuple: The latitude and longitude of the city.
+ '''
+
+ g = geocoder.bing(city_name, key = os.environ["BINGMAPS"]).json
+ print("By '%s' I am assuming you mean '%s'" % (city_name, g["address"]))
+
+ # print(g["lat"], g["lng"])
+ return g["lat"], g["lng"]
+
+if __name__ == "__main__":
+ print(get_weather_data(get_coordinates_from_city("Lincoln")))
diff --git a/noetic-llama/src/ollamawrapper/src/ollamafunctiongrammar.ppeg b/noetic-llama/src/ollamawrapper/src/ollamafunctiongrammar.ppeg
new file mode 100644
index 0000000..b89e47f
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/ollamafunctiongrammar.ppeg
@@ -0,0 +1,12 @@
+functioncall = alphanums lparen argdecl* rparen
+argdecl = alphanums equals value comma?
+value = functioncall / string / real / int
+equals = ws? "=" ws?
+comma = ws? "," ws?
+lparen = ws? "(" ws?
+rparen = ws? ")" ws?
+alphanums = ~r"[a-z0-9_]+"
+string = ~r"['\"].*['\"]"
+real = ~r"\d*\.\d+"
+int = ~r"\d+"
+ws = ~"\s*"
diff --git a/noetic-llama/src/ollamawrapper/src/ollamawrapper.py b/noetic-llama/src/ollamawrapper/src/ollamawrapper.py
new file mode 100644
index 0000000..f7abec1
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/ollamawrapper.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+from dataclasses import dataclass
+from ollamawrapper.srv import OllamaCall, OllamaCallResponse
+import inspect
+import typing
+import jinja2
+import ollama
+import rospy
+import sys
+import os
+
+import capabilities
+# yes we need both
+from capabilities import *
+
+@dataclass
+class FunctionCapability:
+ modulename: str
+ module: None
+ functioname: str
+ function: None
+ docstring: str
+ argnames: list
+
+class FunctionCapablilites(list):
+ def to_modelfile(self, model):
+ environment = jinja2.Environment(loader = jinja2.FileSystemLoader(os.path.dirname(__file__)))
+ template = environment.get_template("Modelfile.jinja2")
+
+ return template.render(functioncapabilities = self, model = model)
+
+def getfunctioncapabilities():
+ functioncapabilities = FunctionCapablilites()
+
+ # not very complicated inspection... the library basically must
+ # consist only of multiple modules each with multiple functions,
+ # no classes or submodules will be dealt with
+ for modulename, module in inspect.getmembers(capabilities):
+ # \/ horrible
+ if inspect.ismodule(module) and 'capabilities' in inspect.getfile(module):
+ for functionname, function in inspect.getmembers(module):
+ if inspect.isfunction(function):
+ # print(functionname, function)
+ docstring = inspect.getdoc(function)
+ # only very simple arguments are fetched, i.e. no **kwargs, or default arguments
+ # will be dealt with
+ argnames = inspect.getfullargspec(function).args
+ functioncapabilities.append(FunctionCapability(modulename, module, functionname, function, docstring, argnames))
+
+ return functioncapabilities
+
+def get_functions(ollama_output):
+ return [f.strip() for f in ollama_output[8:].strip().split(";") if f != ""]
+
+def main():
+ functioncapabilities = getfunctioncapabilities()
+ modelfile = functioncapabilities.to_modelfile("nexusraven:13b-v2-q3_K_S")
+
+ client = ollama.Client(host = "http://192.168.122.1:11434")
+
+ client.create(model = "temp", modelfile = modelfile)
+
+ # with open("Modelfile", "r") as f:
+ # ollama.create(model = "temp", modelfile= f.read())
+ ollama_output = client.generate(model='temp', prompt='What\'s the weather like in Lincoln right now? What\'s 2 + 2?', options={"stop": ["Thought:"]})
+ print(ollama_output)
+
+ for func_str in get_functions(ollama_output["response"]):
+ print(func_str + ":")
+ exec(func_str)
+
+ client.delete("temp")
+
+def handle_ollama_call(req):
+ print("Recieved ollama request %s" % req.input)
+ return OllamaCallResponse(1, 2, 3, 4, 5, 6)
+
+def handle_ollama_server():
+ rospy.init_node("ollama_wrapper_server")
+ s = rospy.Service("ollama_wrapper", OllamaCall, handle_ollama_call)
+ print("Spin")
+ rospy.spin()
+
+if __name__ == "__main__":
+ handle_ollama_server() \ No newline at end of file
diff --git a/noetic-llama/src/ollamawrapper/src/parser.py b/noetic-llama/src/ollamawrapper/src/parser.py
new file mode 100644
index 0000000..001db97
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/src/parser.py
@@ -0,0 +1,37 @@
+"""This is a WIP that parses the ollama output with the idea being to call
+functions using the python API instead of using `exec()` which is rather unsafe
+Since the structure is rather complicated it requires defining a grammar to do
+parsing with.
+
+The grammar works but navigating the abstract syntax tree requires some work.
+"""
+from parsimonious.grammar import Grammar
+from parsimonious.nodes import NodeVisitor
+
+class FunctionCaller(NodeVisitor):
+
+ cur_args = {}
+
+ def visit_functioncall(self, node, visited_children):
+ """ Gets each key/value pair, returns a tuple. """
+ print("***functioncall***", node.text, self.cur_args, "sneed")
+ self.cur_args = {}
+ # key, _, value, *_ = node.children
+ # return key.text, value.text
+
+ def visit_argdecl(self, node, visited_children):
+ """ Gets each key/value pair, returns a tuple. """
+ key, _, value, *_ = node.children
+ self.cur_args[key.text] = value.text
+ return {key.text: value.text}
+
+ def generic_visit(self, node, visited_children):
+ """ The generic visit method. """
+ return visited_children or node
+
+with open("ollamafunctiongrammar.ppeg", "r") as f:
+ grammar = Grammar(f.read())
+
+tree = grammar.parse("get_weather_data(coordinates=get_coordinates_from_city(city_name='Lincoln'), add=sum(num1=2, num2=3))")
+iv = FunctionCaller()
+print(iv.visit(tree)) \ No newline at end of file
diff --git a/noetic-llama/src/ollamawrapper/srv/OllamaCall.srv b/noetic-llama/src/ollamawrapper/srv/OllamaCall.srv
new file mode 100755
index 0000000..872a74b
--- /dev/null
+++ b/noetic-llama/src/ollamawrapper/srv/OllamaCall.srv
@@ -0,0 +1,8 @@
+text input
+---
+int64 total_duration
+int64 load_duration
+int64 prompt_eval_count
+int64 prompt_eval_duration
+int64 eval_count
+int64 eval_duration
diff --git a/temp.py b/temp.py
deleted file mode 100644
index e61fe38..0000000
--- a/temp.py
+++ /dev/null
@@ -1,8 +0,0 @@
-import ollama
-
-with open("Modelfile", "r") as f:
- ollama.create(model = "temp", modelfile= f.read())
-
-print(ollama.generate(model='temp', prompt='What\'s the weather like right now in London?', options={"stop": ["\nThought:"]}))
-
-ollama.delete("temp") \ No newline at end of file