Adds necessary spaces

Introduces spaces between code blocks.
This commit is contained in:
Andres Caicedo
2023-04-04 11:36:50 +02:00
parent 621f18eba7
commit 62615cacc9
4 changed files with 12 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import openai import openai
from config import Config from config import Config
cfg = Config() cfg = Config()
openai.api_key = cfg.openai_api_key openai.api_key = cfg.openai_api_key

View File

@@ -2,6 +2,7 @@ import os
from playsound import playsound from playsound import playsound
import requests import requests
from config import Config from config import Config
cfg = Config() cfg = Config()
# TODO: Nicer names for these ids # TODO: Nicer names for these ids
@@ -18,8 +19,7 @@ def say_text(text, voice_index=0):
voice_id=voices[voice_index]) voice_id=voices[voice_index])
formatted_message = {"text": text} formatted_message = {"text": text}
response = requests.post( response = requests.post(tts_url, headers=tts_headers, json=formatted_message)
tts_url, headers=tts_headers, json=formatted_message)
if response.status_code == 200: if response.status_code == 200:
with open("speech.mpeg", "wb") as f: with open("speech.mpeg", "wb") as f:

View File

@@ -14,6 +14,7 @@ class Spinner:
self.running = False self.running = False
self.spinner_thread = None self.spinner_thread = None
def spin(self): def spin(self):
"""Spin the spinner""" """Spin the spinner"""
while self.running: while self.running:
@@ -22,12 +23,14 @@ class Spinner:
time.sleep(self.delay) time.sleep(self.delay)
sys.stdout.write('\b' * (len(self.message) + 2)) sys.stdout.write('\b' * (len(self.message) + 2))
def __enter__(self): def __enter__(self):
"""Start the spinner""" """Start the spinner"""
self.running = True self.running = True
self.spinner_thread = threading.Thread(target=self.spin) self.spinner_thread = threading.Thread(target=self.spin)
self.spinner_thread.start() self.spinner_thread.start()
def __exit__(self, exc_type, exc_value, exc_traceback): def __exit__(self, exc_type, exc_value, exc_traceback):
"""Stop the spinner""" """Stop the spinner"""
self.running = False self.running = False

View File

@@ -1,6 +1,7 @@
import tiktoken import tiktoken
from typing import List, Dict from typing import List, Dict
def count_message_tokens(messages : List[Dict[str, str]], model : str = "gpt-3.5-turbo-0301") -> int: 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. 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: except KeyError:
print("Warning: model not found. Using cl100k_base encoding.") print("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base") encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo": if model == "gpt-3.5-turbo":
# !Node: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0301.") # !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") 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: 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.""") 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 num_tokens = 0
for message in messages: for message in messages:
num_tokens += tokens_per_message num_tokens += tokens_per_message
for key, value in message.items(): for key, value in message.items():
num_tokens += len(encoding.encode(value)) num_tokens += len(encoding.encode(value))
if key == "name": if key == "name":
num_tokens += tokens_per_name num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens return num_tokens
def count_string_tokens(string: str, model_name: str) -> int: def count_string_tokens(string: str, model_name: str) -> int:
""" """
Returns the number of tokens in a text string. Returns the number of tokens in a text string.