Daniel Nichols commited on
Commit
f6de56e
·
unverified ·
2 Parent(s): 090e2ae a36b415

Merge pull request #1 from parallelcodefoundry/error_handling

Browse files
Files changed (3) hide show
  1. requirements.txt +2 -1
  2. src/perfguru.py +26 -2
  3. token_limits.json +9 -0
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  gradio==4.39.0
2
  hatchet==1.4.0
3
  google-generativeai==0.7.2
4
- openai==1.37.0
 
 
1
  gradio==4.39.0
2
  hatchet==1.4.0
3
  google-generativeai==0.7.2
4
+ openai==1.37.0
5
+ tiktoken==0.7.0
src/perfguru.py CHANGED
@@ -3,6 +3,7 @@ import json
3
  import os
4
  import datetime
5
  from itertools import zip_longest
 
6
 
7
  from models import select_random_model
8
  from rag import select_random_formatter
@@ -19,6 +20,26 @@ def code_upload(code_file_select):
19
  return gr.Button(interactive=True)
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def chat_with_llms(prompt, code_files, profile_file, profile_type):
23
  model1 = select_random_model()
24
  model2 = select_random_model()
@@ -32,7 +53,10 @@ def chat_with_llms(prompt, code_files, profile_file, profile_type):
32
 
33
  if formatted1 is None or formatted2 is None:
34
  error_helper("Failed to format prompt. Please try again.")
35
-
 
 
 
36
  response1 = model1.get_response(formatted1)
37
  response2 = model2.get_response(formatted2)
38
 
@@ -161,4 +185,4 @@ with gr.Blocks(css=".not-voted p { color: black; } .voted p { color: green; } .r
161
 
162
  # Launch the Gradio interface
163
  if __name__ == '__main__':
164
- interface.launch(share=True)
 
3
  import os
4
  import datetime
5
  from itertools import zip_longest
6
+ import tiktoken
7
 
8
  from models import select_random_model
9
  from rag import select_random_formatter
 
20
  return gr.Button(interactive=True)
21
 
22
 
23
+ def token_limit_getter(model: str) -> int:
24
+ with open("token_limits.json", "r") as f:
25
+ token_limits = json.load(f)
26
+ if model in token_limits:
27
+ return token_limits[model]
28
+ return int(5e6)
29
+
30
+ def check_length(text, model):
31
+ if model.name.startswith("gpt"):
32
+ encoder = lambda s: len(tiktoken.encoding_for_model(model.name).encode(text))
33
+ else:
34
+ encoder = lambda s: len(s)/4 # 4 char per token heuristic
35
+
36
+ token_length = encoder(text)
37
+ token_limit = token_limit_getter(model.name)
38
+
39
+ if token_length >= token_limit:
40
+ error_helper(f"Prompt is too long. Please try reducing the size of the prompt or code uploaded.")
41
+
42
+
43
  def chat_with_llms(prompt, code_files, profile_file, profile_type):
44
  model1 = select_random_model()
45
  model2 = select_random_model()
 
53
 
54
  if formatted1 is None or formatted2 is None:
55
  error_helper("Failed to format prompt. Please try again.")
56
+
57
+ check_length(formatted1, model1)
58
+ check_length(formatted2, model2)
59
+
60
  response1 = model1.get_response(formatted1)
61
  response2 = model2.get_response(formatted2)
62
 
 
185
 
186
  # Launch the Gradio interface
187
  if __name__ == '__main__':
188
+ interface.launch(share=False)
token_limits.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "gpt-4o": 128000,
3
+ "gpt-4o-mini": 128000,
4
+ "gpt-4-turbo": 128000,
5
+ "gpt-4": 8192,
6
+ "gpt-3.5-turbo": 16385,
7
+ "gemini-1.5-flash": 1048576,
8
+ "gemini-1.5-pro": 2097152
9
+ }