Skip to content

Commit

Permalink
Merge pull request #378 from Lumerin-protocol/test
Browse files Browse the repository at this point in the history
Final 1.x update (before API AUTH and Agent)
  • Loading branch information
abs2023 authored Jan 10, 2025
2 parents 473a1eb + 6d11b36 commit 7ac394d
Show file tree
Hide file tree
Showing 66 changed files with 1,551 additions and 648 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ jobs:
echo "VFULL: $VFULL"
ARTIFACT="mor-launch-$TAG_NAME-ubuntu-x64.zip"
echo "Artifact: $ARTIFACT"
LLAMACPP=llama-b3256-bin-ubuntu-x64.zip
LLAMACPP=llama-b4354-bin-ubuntu-x64.zip
MODEL=tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b3256/$LLAMACPP
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b4354/$LLAMACPP
wget -nv https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/$MODEL
unzip -o -j $LLAMACPP build/bin/llama-server
echo '{"run":["./llama-server -m ./'"$MODEL"'","./proxy-router","./morpheus-ui-'"$VFULL"'-x86_64-linux.AppImage"]}' > mor-launch.json
echo '{"run":["./llama-server --no-webui -m ./'"$MODEL"'","./proxy-router","./morpheus-ui-'"$VFULL"'-x86_64-linux.AppImage"]}' > mor-launch.json
echo "Contents of mor-launch.json: "
cat mor-launch.json
mv ./cli/mor-cli mor-cli
Expand Down Expand Up @@ -235,12 +235,12 @@ jobs:
run: |
ARTIFACT="mor-launch-$TAG_NAME-macos-x64.zip"
echo "Artifact: $ARTIFACT"
LLAMACPP=llama-b3256-bin-macos-x64.zip
LLAMACPP=llama-b4354-bin-macos-x64.zip
MODEL=tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b3256/$LLAMACPP
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b4354/$LLAMACPP
wget -nv https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/$MODEL
unzip -o -j $LLAMACPP build/bin/llama-server
echo '{"run":["./llama-server -m ./'$MODEL'","./proxy-router","./MorpheusUI.app/Contents/MacOS/MorpheusUI"]}' > mor-launch.json
echo '{"run":["./llama-server --no-webui -m ./'$MODEL'","./proxy-router","./MorpheusUI.app/Contents/MacOS/MorpheusUI"]}' > mor-launch.json
echo "Contents of mor-launch.json: "
cat mor-launch.json
mv ./cli/mor-cli mor-cli
Expand Down Expand Up @@ -316,12 +316,12 @@ jobs:
run: |
ARTIFACT="mor-launch-$TAG_NAME-macos-arm64.zip"
echo "Artifact: $ARTIFACT"
LLAMACPP=llama-b3256-bin-macos-arm64.zip
LLAMACPP=llama-b4354-bin-macos-arm64.zip
MODEL=tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b3256/$LLAMACPP
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b4354/$LLAMACPP
wget -nv https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/$MODEL
unzip -o -j $LLAMACPP build/bin/llama-server
echo '{"run":["./llama-server -m ./'$MODEL'","./proxy-router","./MorpheusUI.app/Contents/MacOS/MorpheusUI"]}' > mor-launch.json
echo '{"run":["./llama-server --no-webui -m ./'$MODEL'","./proxy-router","./MorpheusUI.app/Contents/MacOS/MorpheusUI"]}' > mor-launch.json
echo "Contents of mor-launch.json: "
cat mor-launch.json
mv ./cli/mor-cli mor-cli
Expand Down Expand Up @@ -403,12 +403,12 @@ jobs:
echo "VFULL: $VFULL"
ARTIFACT="mor-launch-$TAG_NAME-win-x64.zip"
echo "Artifact: $ARTIFACT"
LLAMACPP=llama-b3256-bin-win-avx2-x64.zip
LLAMACPP=llama-b4354-bin-win-avx2-x64.zip
MODEL=tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b3256/$LLAMACPP
wget -nv https://github.com/ggerganov/llama.cpp/releases/download/b4354/$LLAMACPP
wget -nv https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/$MODEL
unzip -o -j $LLAMACPP llama-server.exe llama.dll ggml.dll
echo '{"run":["./llama-server.exe -m ./'"$MODEL"'","./proxy-router.exe","./morpheus-ui-'"$VFULL"'-x64-win.exe"]}' > mor-launch.json
echo '{"run":["./llama-server.exe --no-webui -m ./'"$MODEL"'","./proxy-router.exe","./morpheus-ui-'"$VFULL"'-x64-win.exe"]}' > mor-launch.json
echo "Contents of mor-launch.json: "
cat mor-launch.json
mv .env .env.tmp
Expand Down
9 changes: 3 additions & 6 deletions docs/proxy-router.all.env
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ LOG_IS_PROD=false
LOG_JSON=false
# Log levels for various components (one of debug info warn error dpanic panic fatal)
LOG_LEVEL_APP=debug
LOG_LEVEL_CONNECTION=info
LOG_LEVEL_PROXY=info
LOG_LEVEL_SCHEDULER=info
LOG_LEVEL_CONTRACT=debug
LOG_LEVEL_RPC=info
LOG_LEVEL_BADGER=info
LOG_LEVEL_TCP=info
LOG_LEVEL_ETH_RPC=info
LOG_LEVEL_STORAGE=info

# Proxy Configurations
# Address for the proxy (default is "0.0.0.0:3333" if not set)
Expand Down
66 changes: 26 additions & 40 deletions proxy-router/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,28 @@ func start() error {

appLog := log.Named("APP")

proxyLog, err := lib.NewLogger(cfg.Log.LevelProxy, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
tcpLog, err := lib.NewLogger(cfg.Log.LevelTCP, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
if err != nil {
return err
}

connLog, err := lib.NewLogger(cfg.Log.LevelConnection, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
if err != nil {
return err
}

schedulerLogFactory := func(remoteAddr string) (lib.ILogger, error) {
fp := ""
if logFolderPath != "" {
fp = filepath.Join(logFolderPath, fmt.Sprintf("scheduler-%s.log", lib.SanitizeFilename(remoteAddr)))
}
return lib.NewLogger(cfg.Log.LevelScheduler, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, fp)
}

contractLogStorage := lib.NewCollection[*interfaces.LogStorage]()

rpcLog, err := lib.NewLogger(cfg.Log.LevelRPC, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
rpcLog, err := lib.NewLogger(cfg.Log.LevelEthRPC, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
if err != nil {
return err
}

badgerLog, err := lib.NewLogger(cfg.Log.LevelBadger, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
storageLog, err := lib.NewLogger(cfg.Log.LevelStorage, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
if err != nil {
return err
}

defer func() {
_ = connLog.Close()
_ = proxyLog.Close()
_ = tcpLog.Close()
_ = log.Close()
_ = rpcLog.Close()
_ = badgerLog.Close()
_ = storageLog.Close()
}()

appLog.Infof("proxy-router %s", config.BuildVersion)
Expand Down Expand Up @@ -198,7 +184,7 @@ func start() error {
appLog.Info("Wallet deleted")
}

ethNodeStorage := ethclient.NewRPCClientStoreKeychain(keychainStorage, nil, log)
ethNodeStorage := ethclient.NewRPCClientStoreKeychain(keychainStorage, nil, rpcLog.Named("RPC"))
err = ethNodeStorage.RemoveURLs()
if err != nil {
appLog.Warnf("Failed to remove eth node urls\n%s", err)
Expand Down Expand Up @@ -227,16 +213,16 @@ func start() error {
}
appLog.Infof("connected to ethereum node: %s, chainID: %d", cfg.Blockchain.EthNodeAddress, chainID)

storage := storages.NewStorage(badgerLog, cfg.Proxy.StoragePath)
storage := storages.NewStorage(storageLog, cfg.Proxy.StoragePath)
sessionStorage := storages.NewSessionStorage(storage)

var wallet interfaces.Wallet
if len(*cfg.Marketplace.WalletPrivateKey) > 0 {
wallet = wlt.NewEnvWallet(*cfg.Marketplace.WalletPrivateKey)
log.Warnf("Using env wallet. Private key persistance unavailable")
appLog.Warnf("Using env wallet. Private key persistance unavailable")
} else {
wallet = wlt.NewKeychainWallet(keychainStorage)
log.Infof("Using keychain wallet")
appLog.Infof("Using keychain wallet")
}

var logWatcher contracts.LogWatcher
Expand All @@ -248,7 +234,7 @@ func start() error {
appLog.Infof("using polling for blockchain events")
}

scorer, err := config.LoadRating(cfg.Proxy.RatingConfigPath, log)
scorer, err := config.LoadRating(cfg.Proxy.RatingConfigPath, appLog)
if err != nil {
return err
}
Expand All @@ -257,35 +243,35 @@ func start() error {
chatStorage := chatstorage.NewChatStorage(chatStoragePath)

multicallBackend := multicall.NewMulticall3Custom(ethClient, *cfg.Blockchain.Multicall3Addr)
sessionRouter := registries.NewSessionRouter(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, log)
marketplace := registries.NewMarketplace(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, log)
sessionRouter := registries.NewSessionRouter(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, rpcLog)
marketplace := registries.NewMarketplace(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, rpcLog)
sessionRepo := sessionrepo.NewSessionRepositoryCached(sessionStorage, sessionRouter, marketplace)
proxyRouterApi := proxyapi.NewProxySender(chainID, wallet, contractLogStorage, sessionStorage, sessionRepo, log)
proxyRouterApi := proxyapi.NewProxySender(chainID, wallet, contractLogStorage, sessionStorage, sessionRepo, appLog)
explorer := blockchainapi.NewExplorerClient(cfg.Blockchain.ExplorerApiUrl, *cfg.Marketplace.MorTokenAddress, cfg.Blockchain.ExplorerRetryDelay, cfg.Blockchain.ExplorerMaxRetries)
blockchainApi := blockchainapi.NewBlockchainService(ethClient, multicallBackend, *cfg.Marketplace.DiamondContractAddress, *cfg.Marketplace.MorTokenAddress, explorer, wallet, proxyRouterApi, sessionRepo, scorer, proxyLog, cfg.Blockchain.EthLegacyTx)
blockchainApi := blockchainapi.NewBlockchainService(ethClient, multicallBackend, *cfg.Marketplace.DiamondContractAddress, *cfg.Marketplace.MorTokenAddress, explorer, wallet, proxyRouterApi, sessionRepo, scorer, appLog, rpcLog, cfg.Blockchain.EthLegacyTx)
proxyRouterApi.SetSessionService(blockchainApi)

modelConfigLoader := config.NewModelConfigLoader(cfg.Proxy.ModelsConfigPath, valid, blockchainApi, &aiengine.ConnectionChecker{}, log)
modelConfigLoader := config.NewModelConfigLoader(cfg.Proxy.ModelsConfigPath, valid, blockchainApi, &aiengine.ConnectionChecker{}, appLog)
err = modelConfigLoader.Init()
if err != nil {
log.Warnf("failed to load model config, running with empty: %s", err)
appLog.Warnf("failed to load model config, running with empty: %s", err)
}

aiEngine := aiengine.NewAiEngine(proxyRouterApi, chatStorage, modelConfigLoader, log)
aiEngine := aiengine.NewAiEngine(proxyRouterApi, chatStorage, modelConfigLoader, appLog)

eventListener := blockchainapi.NewEventsListener(sessionRepo, sessionRouter, wallet, logWatcher, log)
eventListener := blockchainapi.NewEventsListener(sessionRepo, sessionRouter, wallet, logWatcher, appLog)

sessionExpiryHandler := blockchainapi.NewSessionExpiryHandler(blockchainApi, sessionStorage, wallet, log)
blockchainController := blockchainapi.NewBlockchainController(blockchainApi, log)
sessionExpiryHandler := blockchainapi.NewSessionExpiryHandler(blockchainApi, sessionStorage, wallet, appLog)
blockchainController := blockchainapi.NewBlockchainController(blockchainApi, appLog)

ethConnectionValidator := system.NewEthConnectionValidator(*big.NewInt(int64(cfg.Blockchain.ChainID)))
proxyController := proxyapi.NewProxyController(proxyRouterApi, aiEngine, chatStorage, *cfg.Proxy.StoreChatContext.Bool, *cfg.Proxy.ForwardChatContext.Bool, log)
proxyController := proxyapi.NewProxyController(proxyRouterApi, aiEngine, chatStorage, *cfg.Proxy.StoreChatContext.Bool, *cfg.Proxy.ForwardChatContext.Bool, appLog)
walletController := walletapi.NewWalletController(wallet)
systemController := system.NewSystemController(&cfg, wallet, rpcClientStore, sysConfig, appStartTime, chainID, log, ethConnectionValidator)
systemController := system.NewSystemController(&cfg, wallet, rpcClientStore, sysConfig, appStartTime, chainID, appLog, ethConnectionValidator)

apiBus := apibus.NewApiBus(blockchainController, proxyController, walletController, systemController)
httpHandler := httphandlers.CreateHTTPServer(log, apiBus)
httpServer := transport.NewServer(cfg.Web.Address, httpHandler, log.Named("HTTP"))
httpHandler := httphandlers.CreateHTTPServer(appLog, apiBus)
httpServer := transport.NewServer(cfg.Web.Address, httpHandler, appLog.Named("HTTP"))

// http server should shut down latest to keep pprof running
serverErrCh := make(chan error, 1)
Expand All @@ -295,9 +281,9 @@ func start() error {
cancel()
}()

log.Infof("API docs available at %s/swagger/index.html", cfg.Web.PublicUrl)
appLog.Infof("API docs available at %s/swagger/index.html", cfg.Web.PublicUrl)

proxy := proxyctl.NewProxyCtl(eventListener, wallet, chainID, log, connLog, cfg.Proxy.Address, schedulerLogFactory, sessionStorage, modelConfigLoader, valid, aiEngine, blockchainApi, sessionRepo, sessionExpiryHandler)
proxy := proxyctl.NewProxyCtl(eventListener, wallet, chainID, appLog, tcpLog, cfg.Proxy.Address, sessionStorage, modelConfigLoader, valid, aiEngine, blockchainApi, sessionRepo, sessionExpiryHandler)
err = proxy.Run(ctx)

cancelServer()
Expand Down
12 changes: 10 additions & 2 deletions proxy-router/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/system.ConfigResponse"
"$ref": "#/definitions/system.StatusRes"
}
}
}
Expand All @@ -1143,7 +1143,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/system.ConfigResponse"
"$ref": "#/definitions/system.StatusRes"
}
}
}
Expand Down Expand Up @@ -2486,6 +2486,14 @@ const docTemplate = `{
}
}
}
},
"system.StatusRes": {
"type": "object",
"properties": {
"status": {
"type": "string"
}
}
}
},
"externalDocs": {
Expand Down
12 changes: 10 additions & 2 deletions proxy-router/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/system.ConfigResponse"
"$ref": "#/definitions/system.StatusRes"
}
}
}
Expand All @@ -1135,7 +1135,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/system.ConfigResponse"
"$ref": "#/definitions/system.StatusRes"
}
}
}
Expand Down Expand Up @@ -2478,6 +2478,14 @@
}
}
}
},
"system.StatusRes": {
"type": "object",
"properties": {
"status": {
"type": "string"
}
}
}
},
"externalDocs": {
Expand Down
9 changes: 7 additions & 2 deletions proxy-router/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ definitions:
required:
- urls
type: object
system.StatusRes:
properties:
status:
type: string
type: object
externalDocs:
description: OpenAPI
url: https://swagger.io/resources/open-api/
Expand Down Expand Up @@ -1337,7 +1342,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/system.ConfigResponse'
$ref: '#/definitions/system.StatusRes'
summary: Delete Eth Node URLs
tags:
- system
Expand All @@ -1358,7 +1363,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/system.ConfigResponse'
$ref: '#/definitions/system.StatusRes'
summary: Set Eth Node URLs
tags:
- system
Expand Down
2 changes: 1 addition & 1 deletion proxy-router/internal/aiengine/ai_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (a *AiEngine) GetAdapter(ctx context.Context, chatID, modelID, sessionID co
return nil, fmt.Errorf("model not found: %s", modelID.Hex())
}
var ok bool
engine, ok = ApiAdapterFactory(modelConfig.ApiType, modelConfig.ModelName, modelConfig.ApiURL, modelConfig.ApiKey, a.log)
engine, ok = ApiAdapterFactory(modelConfig.ApiType, modelConfig.ModelName, modelConfig.ApiURL, modelConfig.ApiKey, modelConfig.Parameters, a.log)
if !ok {
return nil, fmt.Errorf("api adapter not found: %s", modelConfig.ApiType)
}
Expand Down
4 changes: 3 additions & 1 deletion proxy-router/internal/aiengine/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package aiengine

import "github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/lib"

func ApiAdapterFactory(apiType string, modelName string, url string, apikey string, log lib.ILogger) (AIEngineStream, bool) {
func ApiAdapterFactory(apiType string, modelName string, url string, apikey string, parameters ModelParameters, log lib.ILogger) (AIEngineStream, bool) {
switch apiType {
case API_TYPE_OPENAI:
return NewOpenAIEngine(modelName, url, apikey, log), true
Expand All @@ -12,6 +12,8 @@ func ApiAdapterFactory(apiType string, modelName string, url string, apikey stri
return NewProdiaSDXLEngine(modelName, url, apikey, log), true
case API_TYPE_PRODIA_V2:
return NewProdiaV2Engine(modelName, url, apikey, log), true
case API_TYPE_HYPERBOLIC_SD:
return NewHyperbolicSDEngine(modelName, url, apikey, parameters, log), true
}
return nil, false
}
Loading

0 comments on commit 7ac394d

Please sign in to comment.