-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from n3d1117/feature/support-functions
Support functions (aka plugins)
- Loading branch information
Showing
23 changed files
with
1,429 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ __pycache__ | |
.DS_Store | ||
/usage_logs | ||
venv | ||
/.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
|
||
from plugins.gtts_text_to_speech import GTTSTextToSpeech | ||
from plugins.dice import DicePlugin | ||
from plugins.youtube_audio_extractor import YouTubeAudioExtractorPlugin | ||
from plugins.ddg_image_search import DDGImageSearchPlugin | ||
from plugins.ddg_translate import DDGTranslatePlugin | ||
from plugins.spotify import SpotifyPlugin | ||
from plugins.crypto import CryptoPlugin | ||
from plugins.weather import WeatherPlugin | ||
from plugins.ddg_web_search import DDGWebSearchPlugin | ||
from plugins.wolfram_alpha import WolframAlphaPlugin | ||
from plugins.deepl import DeeplTranslatePlugin | ||
from plugins.worldtimeapi import WorldTimeApiPlugin | ||
from plugins.whois_ import WhoisPlugin | ||
|
||
|
||
class PluginManager: | ||
""" | ||
A class to manage the plugins and call the correct functions | ||
""" | ||
|
||
def __init__(self, config): | ||
enabled_plugins = config.get('plugins', []) | ||
plugin_mapping = { | ||
'wolfram': WolframAlphaPlugin, | ||
'weather': WeatherPlugin, | ||
'crypto': CryptoPlugin, | ||
'ddg_web_search': DDGWebSearchPlugin, | ||
'ddg_translate': DDGTranslatePlugin, | ||
'ddg_image_search': DDGImageSearchPlugin, | ||
'spotify': SpotifyPlugin, | ||
'worldtimeapi': WorldTimeApiPlugin, | ||
'youtube_audio_extractor': YouTubeAudioExtractorPlugin, | ||
'dice': DicePlugin, | ||
'deepl_translate': DeeplTranslatePlugin, | ||
'gtts_text_to_speech': GTTSTextToSpeech, | ||
'whois': WhoisPlugin, | ||
} | ||
self.plugins = [plugin_mapping[plugin]() for plugin in enabled_plugins if plugin in plugin_mapping] | ||
|
||
def get_functions_specs(self): | ||
""" | ||
Return the list of function specs that can be called by the model | ||
""" | ||
return [spec for specs in map(lambda plugin: plugin.get_spec(), self.plugins) for spec in specs] | ||
|
||
async def call_function(self, function_name, arguments): | ||
""" | ||
Call a function based on the name and parameters provided | ||
""" | ||
plugin = self.__get_plugin_by_function_name(function_name) | ||
if not plugin: | ||
return json.dumps({'error': f'Function {function_name} not found'}) | ||
return json.dumps(await plugin.execute(function_name, **json.loads(arguments)), default=str) | ||
|
||
def get_plugin_source_name(self, function_name) -> str: | ||
""" | ||
Return the source name of the plugin | ||
""" | ||
plugin = self.__get_plugin_by_function_name(function_name) | ||
if not plugin: | ||
return '' | ||
return plugin.get_source_name() | ||
|
||
def __get_plugin_by_function_name(self, function_name): | ||
return next((plugin for plugin in self.plugins | ||
if function_name in map(lambda spec: spec.get('name'), plugin.get_spec())), None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Dict | ||
|
||
import requests | ||
|
||
from .plugin import Plugin | ||
|
||
|
||
# Author: https://github.com/stumpyfr | ||
class CryptoPlugin(Plugin): | ||
""" | ||
A plugin to fetch the current rate of various cryptocurrencies | ||
""" | ||
def get_source_name(self) -> str: | ||
return "CoinCap" | ||
|
||
def get_spec(self) -> [Dict]: | ||
return [{ | ||
"name": "get_crypto_rate", | ||
"description": "Get the current rate of various crypto currencies", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"asset": {"type": "string", "description": "Asset of the crypto"} | ||
}, | ||
"required": ["asset"], | ||
}, | ||
}] | ||
|
||
async def execute(self, function_name, **kwargs) -> Dict: | ||
return requests.get(f"https://api.coincap.io/v2/rates/{kwargs['asset']}").json() |
Oops, something went wrong.