GPTLens / src /model.py
Aishwarya Solanki
initial commit
ee7776a
import time
import openai
import os
from utils import dotdict
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') #"Enter your openai API key"
completion_tokens = 0
prompt_tokens = 0
def gpt(prompt, model, temperature=0.7, max_tokens=4000, n=1, stop=None) -> list:
messages = [{"role": "user", "content": prompt}]
if model == "gpt-4":
pass
time.sleep(30) # to prevent speed limitation exception
return chatgpt(messages, model=model, temperature=temperature, max_tokens=max_tokens, n=n, stop=stop)
def chatgpt(messages, model, temperature, max_tokens, n, stop) -> list:
global completion_tokens, prompt_tokens
outputs = []
while n > 0:
cnt = min(n, 20)
n -= cnt
res = openai.chat.completions.create(model=model, messages=messages, temperature=temperature, max_tokens=max_tokens,
n=cnt, stop=stop)
outputs.extend([choice.message.content for choice in res.choices])
# log completion tokens
completion_tokens += res.usage.completion_tokens
prompt_tokens += res.usage.prompt_tokens
return outputs
def gpt_usage(backend="gpt-4"):
global completion_tokens, prompt_tokens
if backend == "gpt-4":
cost = completion_tokens / 1000 * 0.06 + prompt_tokens / 1000 * 0.03
elif backend == "gpt-4-turbo-preview":
cost = completion_tokens / 1000 * 0.03 + prompt_tokens / 1000 * 0.01
elif backend == "gpt-3.5-turbo":
cost = completion_tokens / 1000 * 0.002 + prompt_tokens / 1000 * 0.0015
return {"completion_tokens": completion_tokens, "prompt_tokens": prompt_tokens, "cost": cost}