add gpt-4
Browse files
app.py
CHANGED
|
@@ -5,12 +5,22 @@ import openai
|
|
| 5 |
import tiktoken
|
| 6 |
|
| 7 |
# Config
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Private
|
| 12 |
_tokens: int = 0
|
| 13 |
-
|
|
|
|
| 14 |
_api_key = os.environ.get("OPENAI_API_KEY", "")
|
| 15 |
_show_openai_settings = _api_key == ""
|
| 16 |
|
|
@@ -20,18 +30,25 @@ def count_tokens(prompt_text: str) -> int:
|
|
| 20 |
return len(_encoding.encode(str(prompt_text)))
|
| 21 |
|
| 22 |
|
| 23 |
-
def get_cost(tokens: int) -> float:
|
| 24 |
-
return TOKEN_PRICE * tokens
|
| 25 |
|
| 26 |
|
| 27 |
-
def prompt(prompt_text: str, api_key: str) -> str:
|
| 28 |
global _tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
_tokens += count_tokens(prompt_text)
|
| 31 |
|
| 32 |
openai.api_key = api_key
|
| 33 |
content = openai.ChatCompletion.create(
|
| 34 |
-
model=
|
| 35 |
messages=[
|
| 36 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 37 |
{"role": "user", "content": prompt_text},
|
|
@@ -40,7 +57,7 @@ def prompt(prompt_text: str, api_key: str) -> str:
|
|
| 40 |
)["choices"][0]["message"]["content"]
|
| 41 |
|
| 42 |
_tokens += count_tokens(content)
|
| 43 |
-
cost = get_cost(_tokens)
|
| 44 |
|
| 45 |
return (
|
| 46 |
content,
|
|
@@ -60,6 +77,11 @@ with gr.Blocks() as demo:
|
|
| 60 |
type="password",
|
| 61 |
placeholder="sk-...",
|
| 62 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
num_tokens = gr.Number(value=0, label="Tokens used")
|
| 64 |
num_cost = gr.Number(value=0, label="Estimated cost in $", precision=3)
|
| 65 |
with gr.Column(scale=3):
|
|
@@ -68,7 +90,7 @@ with gr.Blocks() as demo:
|
|
| 68 |
with gr.Row():
|
| 69 |
gr.Button("Prompt").click(
|
| 70 |
fn=prompt,
|
| 71 |
-
inputs=[txt_prompt, txt_api_key],
|
| 72 |
outputs=[txt_response, num_tokens, num_cost],
|
| 73 |
)
|
| 74 |
gr.Button("Clear").click(
|
|
|
|
| 5 |
import tiktoken
|
| 6 |
|
| 7 |
# Config
|
| 8 |
+
CHAT_MODELS = [
|
| 9 |
+
"gpt-4",
|
| 10 |
+
"gpt-4-32k",
|
| 11 |
+
"gpt-3.5-turbo",
|
| 12 |
+
]
|
| 13 |
+
DEFAULT_MODEL = "gpt-3.5-turbo"
|
| 14 |
+
TOKEN_PRICE = {
|
| 15 |
+
"gpt-3.5-turbo": 0.002 / 1000,
|
| 16 |
+
"gpt-4": 0.06 / 1000,
|
| 17 |
+
"gpt-4-32k": 0.12 / 1000,
|
| 18 |
+
}
|
| 19 |
|
| 20 |
# Private
|
| 21 |
_tokens: int = 0
|
| 22 |
+
_selected_model = DEFAULT_MODEL
|
| 23 |
+
_encoding = tiktoken.encoding_for_model(DEFAULT_MODEL)
|
| 24 |
_api_key = os.environ.get("OPENAI_API_KEY", "")
|
| 25 |
_show_openai_settings = _api_key == ""
|
| 26 |
|
|
|
|
| 30 |
return len(_encoding.encode(str(prompt_text)))
|
| 31 |
|
| 32 |
|
| 33 |
+
def get_cost(tokens: int, model: str) -> float:
|
| 34 |
+
return TOKEN_PRICE.get(model, 0) * tokens
|
| 35 |
|
| 36 |
|
| 37 |
+
def prompt(prompt_text: str, api_key: str, model: str) -> str:
|
| 38 |
global _tokens
|
| 39 |
+
global _selected_model
|
| 40 |
+
global _encoding
|
| 41 |
+
|
| 42 |
+
# Check if selected model has changed
|
| 43 |
+
if model != _selected_model:
|
| 44 |
+
_selected_model = model
|
| 45 |
+
_encoding = tiktoken.encoding_for_model(_selected_model)
|
| 46 |
|
| 47 |
_tokens += count_tokens(prompt_text)
|
| 48 |
|
| 49 |
openai.api_key = api_key
|
| 50 |
content = openai.ChatCompletion.create(
|
| 51 |
+
model=_selected_model,
|
| 52 |
messages=[
|
| 53 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 54 |
{"role": "user", "content": prompt_text},
|
|
|
|
| 57 |
)["choices"][0]["message"]["content"]
|
| 58 |
|
| 59 |
_tokens += count_tokens(content)
|
| 60 |
+
cost = get_cost(_tokens, model)
|
| 61 |
|
| 62 |
return (
|
| 63 |
content,
|
|
|
|
| 77 |
type="password",
|
| 78 |
placeholder="sk-...",
|
| 79 |
)
|
| 80 |
+
dd_model = gr.Dropdown(
|
| 81 |
+
choices=CHAT_MODELS,
|
| 82 |
+
value=DEFAULT_MODEL,
|
| 83 |
+
label="Model",
|
| 84 |
+
)
|
| 85 |
num_tokens = gr.Number(value=0, label="Tokens used")
|
| 86 |
num_cost = gr.Number(value=0, label="Estimated cost in $", precision=3)
|
| 87 |
with gr.Column(scale=3):
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
gr.Button("Prompt").click(
|
| 92 |
fn=prompt,
|
| 93 |
+
inputs=[txt_prompt, txt_api_key, dd_model],
|
| 94 |
outputs=[txt_response, num_tokens, num_cost],
|
| 95 |
)
|
| 96 |
gr.Button("Clear").click(
|