Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Azure, OpenAI, Palm, Anthropic, Cohere Models - using litellm #77

Merged
merged 3 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import litellm
from common.context import Context
from config import conf
from common.singleton import singleton
from common.reply import Reply



@singleton
class Bot:
def __init__(self):
Expand All @@ -13,6 +15,13 @@ def __init__(self):

self.bot = AzureChatGPTBot()
else:
model = conf().get("model", "gpt-3.5-turbo")
if model in litellm.model_list:
# see litellm supported models here:
# https://litellm.readthedocs.io/en/latest/supported/
from bot.litellm import liteLLMChatGPTBot
self.bot = liteLLMChatGPTBot()

from bot.chatgpt import ChatGPTBot

self.bot = ChatGPTBot()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The self.bot will always be assigned by ChatGPTBot instance, seems there needs to be an else block here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch updated this

Expand Down
38 changes: 38 additions & 0 deletions bot/litellm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from bot.chatgpt import ChatGPTBot
from litellm import completion
import openai
from utils.log import logger

class liteLLMChatGPTBot(ChatGPTBot):
def reply_text(self, session):
try:
response = completion(
messages=session,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
**self.args,
)
return {
"total_tokens": response["usage"]["total_tokens"],
"completion_tokens": response["usage"]["completion_tokens"],
"content": response.choices[0]["message"]["content"],
}
except Exception as e:
result = {"completion_tokens": 0, "content": "Please ask me again"}
if isinstance(e, openai.error.RateLimitError):
logger.warn(f"[{self.name}] RateLimitError: {e}")
result["content"] = "Ask too frequently, please try again in 20s"
elif isinstance(e, openai.error.APIConnectionError):
logger.warn(f"[{self.name}] APIConnectionError: {e}")
result[
"content"
] = "I cannot connect the server, please check the network and try again"
elif isinstance(e, openai.error.Timeout):
logger.warn(f"[{self.name}] Timeout: {e}")
result["content"] = "I didn't receive your message, please try again"
elif isinstance(e, openai.error.APIError):
logger.warn(f"[{self.name}] APIError: {e}")
else:
logger.exception(f"[{self.name}] Exception: {e}")
return result
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ termcolor==2.1.1
websocket_client==1.5.1
openai==0.27.8
pydantic==2.0.2
dulwich==0.21.5
dulwich==0.21.5
litellm==0.1.226