|
1 | 1 | from loguru import logger |
| 2 | +from openai.api_resources import engine |
2 | 3 | from pathlib import Path |
3 | | -import fnmatch, os, subprocess, sys, tiktoken, yaml |
| 4 | +import fnmatch, openai, os, subprocess, sys, tiktoken, yaml |
4 | 5 |
|
5 | 6 | # ---------------------------------------------------------------------------- # |
6 | 7 | # Global logging configuration for loguru # |
@@ -46,41 +47,40 @@ def generate_directory_structure(path, ignore_patterns=None, use_gitignore=True, |
46 | 47 |
|
47 | 48 |
|
48 | 49 | def get_llm_model(token_size=0): |
49 | | - # https://platform.openai.com/docs/models/gpt-3-5 |
50 | | - # We want to use GPT-4, if it is available for this OPENAI_API_KEY, otherwise GPT-3.5 |
51 | | - # We also want to use the largest model that supports the token size we need |
52 | 50 | model_options = { |
53 | 51 | "gpt-4": 8192, |
54 | 52 | "gpt-4-32k": 32768, |
55 | 53 | "gpt-3.5-turbo": 4096, |
56 | 54 | "gpt-3.5-turbo-16k": 16384, |
57 | 55 | } |
| 56 | + |
58 | 57 | config = read_config() |
59 | | - gpt_4_supported = config["gpt_4_supported"] |
| 58 | + openai.api_key = config["openai_api_key"] |
| 59 | + engines = engine.Engine.list() |
| 60 | + logger.trace(f"Engines: {engines}") |
60 | 61 |
|
61 | 62 | # For some unknown reason, tiktoken often underestimates the token size by ~10%, so let's buffer |
62 | 63 | token_size = int(token_size * 1.1) |
63 | 64 |
|
64 | | - if gpt_4_supported: |
65 | | - if token_size <= model_options["gpt-4"]: |
66 | | - logger.info(f"Using GPT-4 for token size {token_size}") |
67 | | - return "gpt-4" |
68 | | - elif token_size <= model_options["gpt-4-32k"]: |
69 | | - logger.info(f"Using GPT-4-32k for token size {token_size}") |
70 | | - return "gpt-4-32k" |
71 | | - else: |
72 | | - logger.critical("🛑 The context is too large to for the Model. 😞") |
73 | | - return None |
| 65 | + # Try to use GPT-4 if it is supported and the token size is small enough |
| 66 | + if "gpt-4" in [engine.id for engine in engines.data] and token_size <= model_options["gpt-4"]: |
| 67 | + logger.info(f"Using GPT-4 for token size {token_size}") |
| 68 | + return "gpt-4" |
| 69 | + elif "gpt-4-32k" in [engine.id for engine in engines.data] and token_size <= model_options["gpt-4-32k"]: |
| 70 | + logger.info(f"Using GPT-4-32k for token size {token_size}") |
| 71 | + return "gpt-4-32k" |
| 72 | + elif token_size <= model_options["gpt-3.5-turbo"]: |
| 73 | + logger.info(f"Using GPT-3.5-turbo for token size {token_size}") |
| 74 | + return "gpt-3.5-turbo" |
| 75 | + elif token_size <= model_options["gpt-3.5-turbo-16k"]: |
| 76 | + logger.info(f"Using GPT-3.5-turbo-16k for token size {token_size}") |
| 77 | + return "gpt-3.5-turbo-16k" |
74 | 78 | else: |
75 | | - if token_size <= model_options["gpt-3.5-turbo"]: # noqa: PLR5501 |
76 | | - logger.info(f"Using GPT-3.5-turbo for token size {token_size}") |
77 | | - return "gpt-3.5-turbo" |
78 | | - elif token_size <= model_options["gpt-3.5-turbo-16k"]: |
79 | | - logger.info(f"Using GPT-3.5-turbo-16k for token size {token_size}") |
80 | | - return "gpt-3.5-turbo-16k" |
81 | | - else: |
82 | | - logger.critical("🛑 The context is too large to for the Model. 😞") |
83 | | - return None |
| 79 | + logger.critical( |
| 80 | + f"🛑 The context is too large ({token_size})" |
| 81 | + "for the any of the models supported by your Open AI API key. 😞" |
| 82 | + ) |
| 83 | + return None |
84 | 84 |
|
85 | 85 |
|
86 | 86 | def get_token_length(text, model="gpt-3.5-turbo"): |
|
0 commit comments