Skip to content

Commit

Permalink
feat: ability to start LMAO external API
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Apr 19, 2024
1 parent 4ccbb0f commit 17659a1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ venv
env
.env
__pycache__
certificate.*
private.*
20 changes: 19 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment01__": "Version of config file. Please don't change it",
"config_version": 6,
"config_version": 7,

"__comment02__": "General config of enabled modules",
"__comment03__": "Available modules: lmao_chatgpt, lmao_ms_copilot, ms_copilot, ms_copilot_designer, gemini",
Expand Down Expand Up @@ -121,5 +121,23 @@

"__comment07__": "Timestamp format",
"timestamp_format": "%Y-%m-%d %H:%M:%S"
},

"__comment08__": "Configs to enable external API. See <https://github.com/F33RNI/LlM-Api-Open> for more info",
"lmao": {
"__comment01__": "true to enable API, false to disable",
"api_enabled": false,

"__comment02__": "Server IP and port",
"ip": "0.0.0.0",
"port": 1312,

"__comment03__": "Start HTTPS server instead of HTTP",
"ssl": false,
"ssl_certificate_path": "certificate.crt",
"ssl_key_path": "private.key",

"__comment04__": "Provide some strong random API tokens here to enable authorization (ex.: iuyeeNoh6hohhohSh)",
"tokens": []
}
}
42 changes: 41 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import multiprocessing
import os
import sys
import threading
from typing import Dict

from lmao.external_api import ExternalAPI

from _version import __version__
import logging_handler
Expand All @@ -37,7 +39,7 @@

# Default config file
CONFIG_FILE = "config.json"
CONFIG_COMPATIBLE_VERSIONS = [5, 6]
CONFIG_COMPATIBLE_VERSIONS = [5, 6, 7]


def load_and_parse_config(config_file: str) -> Dict:
Expand Down Expand Up @@ -112,6 +114,26 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()


def lmao_external_api(external_api_: ExternalAPI, config: Dict) -> None:
"""Starts ExternalAPI from LMAO
Args:
external_api_ (ExternalAPI): ExternalAPI class instance
config (Dict): global config as JSON
"""
try:
if config["lmao"].get("ssl"):
external_api_.run(
config["lmao"]["ip"],
config["lmao"]["port"],
ssl_context=(config["lmao"].get("ssl_certificate_path"), config["lmao"].get("ssl_key_path")),
)
else:
external_api_.run(config["lmao"]["ip"], config["lmao"]["port"])
except Exception as e:
logging.error("LMAO external API error", exc_info=e)


def main():
"""Main entry"""
# Multiprocessing fix for Windows
Expand Down Expand Up @@ -185,6 +207,24 @@ def main():
except Exception as e:
logging.error("Initialization error", exc_info=e)

# Start LMAO API if needed
if "lmao" in config and config["lmao"].get("api_enabled"):
logging.info("Trying to start LMAO external API")
try:
external_api_ = ExternalAPI(config, tokens=config["lmao"].get("tokens", []))
external_api_.modules = modules
lmao_external_api_thread = threading.Thread(
target=lmao_external_api,
args=(
external_api_,
config,
),
)
lmao_external_api_thread.daemon = True
lmao_external_api_thread.start()
except Exception as e:
logging.error("Error starting LMAO external API", exc_info=e)

# Finally, start queue handler and bot polling (blocking)
if initialization_ok:
queue_handler_.start_processing_loop()
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
git+https://github.com/F33RNI/EdgeGPT@main#egg=EdgeGPT
git+https://github.com/F33RNI/md2tgmd.git@main
llm-api-open~=2.1
llm-api-open~=2.2
revChatGPT==6.8.6
python-telegram-bot==20.3
python-telegram-bot>=20.3
openai>=0.26.4
tiktoken>=0.2.0
OpenAIAuth>=0.3.2
Expand Down

0 comments on commit 17659a1

Please sign in to comment.