From 62615cacc9532bcae1fe94d4ef27e10610edd758 Mon Sep 17 00:00:00 2001 From: Andres Caicedo Date: Tue, 4 Apr 2023 11:36:50 +0200 Subject: [PATCH] Adds necessary spaces Introduces spaces between code blocks. --- scripts/llm_utils.py | 1 + scripts/speak.py | 4 ++-- scripts/spinner.py | 3 +++ scripts/token_counter.py | 6 ++++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/llm_utils.py b/scripts/llm_utils.py index c512e99733..2981c8a0a2 100644 --- a/scripts/llm_utils.py +++ b/scripts/llm_utils.py @@ -1,5 +1,6 @@ import openai from config import Config + cfg = Config() openai.api_key = cfg.openai_api_key diff --git a/scripts/speak.py b/scripts/speak.py index 1e0eb4088a..5a0728a2c7 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -2,6 +2,7 @@ import os from playsound import playsound import requests from config import Config + cfg = Config() # TODO: Nicer names for these ids @@ -18,8 +19,7 @@ def say_text(text, voice_index=0): voice_id=voices[voice_index]) formatted_message = {"text": text} - response = requests.post( - tts_url, headers=tts_headers, json=formatted_message) + response = requests.post(tts_url, headers=tts_headers, json=formatted_message) if response.status_code == 200: with open("speech.mpeg", "wb") as f: diff --git a/scripts/spinner.py b/scripts/spinner.py index df39dbbd22..2b79ae262b 100644 --- a/scripts/spinner.py +++ b/scripts/spinner.py @@ -14,6 +14,7 @@ class Spinner: self.running = False self.spinner_thread = None + def spin(self): """Spin the spinner""" while self.running: @@ -22,12 +23,14 @@ class Spinner: time.sleep(self.delay) sys.stdout.write('\b' * (len(self.message) + 2)) + def __enter__(self): """Start the spinner""" self.running = True self.spinner_thread = threading.Thread(target=self.spin) self.spinner_thread.start() + def __exit__(self, exc_type, exc_value, exc_traceback): """Stop the spinner""" self.running = False diff --git a/scripts/token_counter.py b/scripts/token_counter.py index a28a9868ed..fc5b9c5118 100644 --- a/scripts/token_counter.py +++ b/scripts/token_counter.py @@ -1,6 +1,7 @@ import tiktoken from typing import List, Dict + def count_message_tokens(messages : List[Dict[str, str]], model : str = "gpt-3.5-turbo-0301") -> int: """ Returns the number of tokens used by a list of messages. @@ -17,6 +18,7 @@ def count_message_tokens(messages : List[Dict[str, str]], model : str = "gpt-3.5 except KeyError: print("Warning: model not found. Using cl100k_base encoding.") encoding = tiktoken.get_encoding("cl100k_base") + if model == "gpt-3.5-turbo": # !Node: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0301.") return count_message_tokens(messages, model="gpt-3.5-turbo-0301") @@ -32,15 +34,19 @@ def count_message_tokens(messages : List[Dict[str, str]], model : str = "gpt-3.5 else: raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""") num_tokens = 0 + for message in messages: num_tokens += tokens_per_message + for key, value in message.items(): num_tokens += len(encoding.encode(value)) + if key == "name": num_tokens += tokens_per_name num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> return num_tokens + def count_string_tokens(string: str, model_name: str) -> int: """ Returns the number of tokens in a text string.