-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration Completion api to chat completion api (#419)
Port completion api to chatCompletion api
- Loading branch information
Showing
5 changed files
with
172 additions
and
115 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
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,25 @@ | ||
from core.modelhelper import num_tokens_from_messages | ||
|
||
|
||
class MessageBuilder: | ||
""" | ||
A class for building and managing messages in a chat conversation. | ||
Attributes: | ||
message (list): A list of dictionaries representing chat messages. | ||
model (str): The name of the ChatGPT model. | ||
token_count (int): The total number of tokens in the conversation. | ||
Methods: | ||
__init__(self, system_content: str, chatgpt_model: str): Initializes the MessageBuilder instance. | ||
append_message(self, role: str, content: str, index: int = 1): Appends a new message to the conversation. | ||
""" | ||
|
||
def __init__(self, system_content: str, chatgpt_model: str): | ||
self.messages = [{'role': 'system', 'content': system_content}] | ||
self.model = chatgpt_model | ||
self.token_length = num_tokens_from_messages( | ||
self.messages[-1], self.model) | ||
|
||
def append_message(self, role: str, content: str, index: int = 1): | ||
self.messages.insert(index, {'role': role, 'content': content}) | ||
self.token_length += num_tokens_from_messages( | ||
self.messages[index], self.model) |
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,49 @@ | ||
import tiktoken | ||
|
||
MODELS_2_TOKEN_LIMITS = { | ||
"gpt-35-turbo": 4000, | ||
"gpt-3.5-turbo": 4000, | ||
"gpt-35-turbo-16k": 16000, | ||
"gpt-3.5-turbo-16k": 16000, | ||
"gpt-4": 8100, | ||
"gpt-4-32k": 32000 | ||
} | ||
|
||
AOAI_2_OAI = { | ||
"gpt-35-turbo": "gpt-3.5-turbo", | ||
"gpt-35-turbo-16k": "gpt-3.5-turbo-16k" | ||
} | ||
|
||
|
||
def get_token_limit(model_id: str) -> int: | ||
if model_id not in MODELS_2_TOKEN_LIMITS: | ||
raise ValueError("Expected Model Gpt-35-turbo and above") | ||
return MODELS_2_TOKEN_LIMITS.get(model_id) | ||
|
||
|
||
def num_tokens_from_messages(message: dict[str, str], model: str) -> int: | ||
""" | ||
Calculate the number of tokens required to encode a message. | ||
Args: | ||
message (dict): The message to encode, represented as a dictionary. | ||
model (str): The name of the model to use for encoding. | ||
Returns: | ||
int: The total number of tokens required to encode the message. | ||
Example: | ||
message = {'role': 'user', 'content': 'Hello, how are you?'} | ||
model = 'gpt-3.5-turbo' | ||
num_tokens_from_messages(message, model) | ||
output: 11 | ||
""" | ||
encoding = tiktoken.encoding_for_model(get_oai_chatmodel_tiktok(model)) | ||
num_tokens = 2 # For "role" and "content" keys | ||
for key, value in message.items(): | ||
num_tokens += len(encoding.encode(value)) | ||
return num_tokens | ||
|
||
|
||
def get_oai_chatmodel_tiktok(aoaimodel: str) -> str: | ||
if aoaimodel == "" or aoaimodel is None: | ||
raise ValueError("Expected AOAI chatGPT model name") | ||
|
||
return AOAI_2_OAI.get(aoaimodel) |