-
Notifications
You must be signed in to change notification settings - Fork 263
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 #1 from sicario001/llm_operator
LLM operator
- Loading branch information
Showing
19 changed files
with
792 additions
and
5 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
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,36 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Iterator | ||
|
||
from evadb.database import EvaDBDatabase | ||
from evadb.executor.abstract_executor import AbstractExecutor | ||
from evadb.models.storage.batch import Batch | ||
from evadb.plan_nodes.llm_plan import LLMPlan | ||
|
||
|
||
class LLMExecutor(AbstractExecutor): | ||
def __init__(self, db: EvaDBDatabase, node: LLMPlan): | ||
super().__init__(db, node) | ||
self.llm_expr = node.llm_expr | ||
self.alias = node.alias | ||
|
||
def exec(self, *args, **kwargs) -> Iterator[Batch]: | ||
child_executor = self.children[0] | ||
for batch in child_executor.exec(**kwargs): | ||
llm_result = self.llm_expr.evaluate(batch) | ||
|
||
output = Batch.merge_column_wise([batch, llm_result]) | ||
|
||
yield output |
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,110 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import json | ||
import os | ||
from abc import abstractmethod | ||
from typing import List | ||
|
||
import pandas as pd | ||
|
||
from evadb.catalog.catalog_type import NdArrayType | ||
from evadb.functions.abstract.abstract_function import AbstractFunction | ||
from evadb.functions.decorators.decorators import forward, setup | ||
from evadb.functions.decorators.io_descriptors.data_types import PandasDataframe | ||
|
||
|
||
class BaseLLM(AbstractFunction): | ||
""" """ | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.model_stats = None | ||
|
||
@setup(cacheable=True, function_type="chat-completion", batchable=True) | ||
def setup(self, *args, **kwargs) -> None: | ||
super().setup(*args, **kwargs) | ||
|
||
@forward( | ||
input_signatures=[ | ||
PandasDataframe( | ||
columns=["query", "content", "prompt"], | ||
column_types=[ | ||
NdArrayType.STR, | ||
NdArrayType.STR, | ||
NdArrayType.STR, | ||
], | ||
column_shapes=[(1,), (1,), (None,)], | ||
) | ||
], | ||
output_signatures=[ | ||
PandasDataframe( | ||
columns=["response", "model"], | ||
column_types=[ | ||
NdArrayType.STR, | ||
NdArrayType.STR, | ||
], | ||
column_shapes=[(1,), (1,)], | ||
) | ||
], | ||
) | ||
def forward(self, text_df): | ||
queries = text_df[text_df.columns[0]] | ||
contents = text_df[text_df.columns[0]] | ||
if len(text_df.columns) > 1: | ||
queries = text_df.iloc[:, 0] | ||
contents = text_df.iloc[:, 1] | ||
|
||
prompt = None | ||
if len(text_df.columns) > 2: | ||
prompt = text_df.iloc[0, 2] | ||
|
||
responses, models = self.generate(queries, contents, prompt) | ||
return pd.DataFrame({"response": responses, "model": models}) | ||
|
||
@abstractmethod | ||
def generate(self, queries: List[str], contents: List[str], prompt: str) -> List[str]: | ||
""" | ||
All the child classes should overload this function | ||
""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_cost(self, prompt: str, query: str, content: str, response: str = "") -> tuple[float]: | ||
""" | ||
Return the token usage as tuple of input_token_usage, output_token_usage, and dollar cost of running the LLM on the prompt and the getting the provided response. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def get_max_cost(self, prompt: str, query: str, content: str) -> tuple[float]: | ||
""" | ||
Return the token usage as tuple of input_token_usage, output_token_usage, and dollar cost of running the LLM on the prompt and the getting the provided response. | ||
""" | ||
pass | ||
|
||
def get_model_stats(self, model_name: str): | ||
# read the statistics if not already read | ||
if self.model_stats is None: | ||
current_file_path = os.path.dirname(os.path.realpath(__file__)) | ||
with open(f"{current_file_path}/llm_stats.json") as f: | ||
self.model_stats = json.load(f) | ||
|
||
assert ( | ||
model_name in self.model_stats | ||
), f"we do not have statistics for the model {model_name}" | ||
|
||
return self.model_stats[model_name] |
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,128 @@ | ||
{ | ||
"gpt-4": { | ||
"max_token_context": 8192, | ||
"input_cost_per_token": 0.00003, | ||
"output_cost_per_token": 0.00006, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-4-0314": { | ||
"max_token_context": 8192, | ||
"input_cost_per_token": 0.00003, | ||
"output_cost_per_token": 0.00006, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-4-0613": { | ||
"max_token_context": 8192, | ||
"input_cost_per_token": 0.00003, | ||
"output_cost_per_token": 0.00006, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-4-32k": { | ||
"max_token_context": 32768, | ||
"input_cost_per_token": 0.00006, | ||
"output_cost_per_token": 0.00012, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-4-32k-0314": { | ||
"max_token_context": 32768, | ||
"input_cost_per_token": 0.00006, | ||
"output_cost_per_token": 0.00012, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-4-32k-0613": { | ||
"max_token_context": 32768, | ||
"input_cost_per_token": 0.00006, | ||
"output_cost_per_token": 0.00012, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-3.5-turbo": { | ||
"max_token_context": 4097, | ||
"input_cost_per_token": 0.0000015, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-3.5-turbo-0301": { | ||
"max_token_context": 4097, | ||
"input_cost_per_token": 0.0000015, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-3.5-turbo-0613": { | ||
"max_token_context": 4097, | ||
"input_cost_per_token": 0.0000015, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-3.5-turbo-16k": { | ||
"max_token_context": 16385, | ||
"input_cost_per_token": 0.000003, | ||
"output_cost_per_token": 0.000004, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"gpt-3.5-turbo-16k-0613": { | ||
"max_token_context": 16385, | ||
"input_cost_per_token": 0.000003, | ||
"output_cost_per_token": 0.000004, | ||
"provider": "openai", | ||
"mode": "chat" | ||
}, | ||
"text-davinci-003": { | ||
"max_token_context": 4097, | ||
"input_cost_per_token": 0.000002, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"text-curie-001": { | ||
"max_token_context": 2049, | ||
"input_cost_per_token": 0.000002, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"text-babbage-001": { | ||
"max_token_context": 2049, | ||
"input_cost_per_token": 0.0000004, | ||
"output_cost_per_token": 0.0000004, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"text-ada-001": { | ||
"max_token_context": 2049, | ||
"input_cost_per_token": 0.0000004, | ||
"output_cost_per_token": 0.0000004, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"babbage-002": { | ||
"max_token_context": 16384, | ||
"input_cost_per_token": 0.0000004, | ||
"output_cost_per_token": 0.0000004, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"davinci-002": { | ||
"max_token_context": 16384, | ||
"input_cost_per_token": 0.000002, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "completion" | ||
}, | ||
"gpt-3.5-turbo-instruct": { | ||
"max_token_context": 8192, | ||
"input_cost_per_token": 0.0000015, | ||
"output_cost_per_token": 0.000002, | ||
"provider": "openai", | ||
"mode": "completion" | ||
} | ||
} |
Oops, something went wrong.