generated from slack-samples/bolt-python-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ai_utils functions moved into providers/__init__.py
- Loading branch information
Showing
12 changed files
with
83 additions
and
70 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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,50 @@ | ||
from .anthropic import AnthropicAPI | ||
from .openai import OpenAI_API | ||
from ..ai_constants import DEFAULT_SYSTEM_CONTENT | ||
from logging import Logger | ||
from state_store.get_user_state import get_user_state | ||
from typing import Optional, List | ||
|
||
""" | ||
The `_get_provider()` function returns an instance of the appropriate API provider based on the given provider name. | ||
New providers must be added below. | ||
New AI providers must be added below | ||
`get_available_providers()` | ||
This function retrieves available API models from different AI providers. | ||
It combines the available models into a single dictionary. | ||
`_get_provider()` | ||
This function returns an instance of the appropriate API provider based on the given provider name. | ||
`get_provider_response`() | ||
This function retrieves the user's selected API provider and model, | ||
sets the model, and generates a response. | ||
Note that context is an optional parameter because some functionalities, | ||
such as commands, do not allow access to conversation history if the bot | ||
isn't in the channel where the command is run. | ||
""" | ||
|
||
|
||
def get_available_providers(): | ||
return {**AnthropicAPI().get_models(), **OpenAI_API().get_models()} | ||
|
||
|
||
def _get_provider(provider_name: str): | ||
if provider_name.lower() == "openai": | ||
return OpenAI_API() | ||
elif provider_name.lower() == "anthropic": | ||
return AnthropicAPI() | ||
else: | ||
raise ValueError(f"Unknown provider: {provider_name}") | ||
|
||
|
||
def get_provider_response(user_id: str, prompt: str, context: Optional[List] = None, system_content=DEFAULT_SYSTEM_CONTENT): | ||
try: | ||
formatted_context = "\n".join([f"{msg['user']}: {msg['text']}" for msg in context]) | ||
full_prompt = f"Prompt: {prompt}\nContext: {formatted_context}" | ||
provider_name, model_name = get_user_state(user_id) | ||
provider = _get_provider(provider_name) | ||
provider.set_model(model_name) | ||
return provider.generate_response(full_prompt, system_content) | ||
except Exception as e: | ||
Logger.error(e) |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
from slack_bolt import App | ||
from .app_home_opened import app_home_opened_callback | ||
from .app_mentioned import app_mentioned_callback | ||
from .dm_sent import dm_sent_callback | ||
from .app_messaged import app_messaged_callback | ||
|
||
|
||
def register(app: App): | ||
app.event("app_home_opened")(app_home_opened_callback) | ||
app.event("app_mention")(app_mentioned_callback) | ||
app.event("message")(dm_sent_callback) | ||
app.event("message")(app_messaged_callback) |
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
13 changes: 8 additions & 5 deletions
13
listeners/events/dm_sent.py → listeners/events/app_messaged.py
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