Skip to content

Commit

Permalink
Merge pull request ConvLab#175 from ConvLab/llm
Browse files Browse the repository at this point in the history
add litellm for using many LLM APIs, refer to ConvLab#173
  • Loading branch information
zqwerty authored Feb 8, 2024
2 parents 6e98552 + 2f12a04 commit 1b3f115
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions convlab/base_models/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import torch
from copy import deepcopy
from transformers import pipeline, AutoTokenizer, AutoModel
from litellm import completion as litellm_completion

class LLM:
def __init__(self, api_type, model_name_or_path, system_instruction=None, generation_kwargs=None):
Expand All @@ -23,6 +24,8 @@ def __init__(self, api_type, model_name_or_path, system_instruction=None, genera
self.model = ChatGLM2(model_name_or_path)
else:
self.model = HFModels(model_name_or_path)
elif api_type == 'litellm':
self.model = LiteLLM_API(model_name_or_path)
else:
raise NotImplementedError

Expand Down Expand Up @@ -69,6 +72,31 @@ def generate(self, system_instruction, prompt, **kwargs):
"""Generate interface"""
raise NotImplementedError

class LiteLLM_API(BaseLLM):
DEFAULT_SYSTEM_INSTRUCTION = "You are a helpful assistant."
def __init__(self, model_name_or_path) -> None:
# make sure you set the corresponding API_KEY environment variable for your model
self.model_name_or_path = model_name_or_path

def chat(self, messages, **kwargs) -> str:
completion = litellm_completion(
model=self.model_name_or_path,
messages=messages,
**kwargs
)
return completion.choices[0].message['content']

def generate(self, system_instruction, prompt, **kwargs) -> str:
completion = litellm_completion(
model=self.model_name_or_path,
messages=[
{"role": "system", "content": system_instruction},
{"role": "user", "content": prompt}
],
**kwargs
)
return completion.choices[0].message['content']

class OpenAI_API(BaseLLM):
DEFAULT_SYSTEM_INSTRUCTION = "You are a helpful assistant."

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'gtts',
'pydub',
'openai',
'litellm==0.1.516',
'GitPython'
],
extras_require={
Expand Down

0 comments on commit 1b3f115

Please sign in to comment.