mirror of
https://github.com/aljazceru/dev-gpt.git
synced 2025-12-23 16:44:20 +01:00
feat: playground
This commit is contained in:
12
src/gpt.py
12
src/gpt.py
@@ -11,6 +11,8 @@ from src.utils.string_tools import print_colored
|
||||
|
||||
openai.api_key = os.environ['OPENAI_API_KEY']
|
||||
|
||||
total_chars_prompt = 0
|
||||
total_chars_generation = 0
|
||||
|
||||
class Conversation:
|
||||
def __init__(self):
|
||||
@@ -26,6 +28,7 @@ class Conversation:
|
||||
|
||||
|
||||
def get_response(prompt_list: List[Tuple[str, str]]):
|
||||
global total_chars_prompt, total_chars_generation
|
||||
for i in range(10):
|
||||
try:
|
||||
response_generator = openai.ChatCompletion.create(
|
||||
@@ -42,7 +45,7 @@ def get_response(prompt_list: List[Tuple[str, str]]):
|
||||
]
|
||||
)
|
||||
response_generator_with_timeout = timeout_generator_wrapper(response_generator, 5)
|
||||
|
||||
total_chars_prompt += sum(len(prompt[1]) for prompt in prompt_list)
|
||||
complete_string = ''
|
||||
for chunk in response_generator_with_timeout:
|
||||
delta = chunk['choices'][0]['delta']
|
||||
@@ -50,6 +53,13 @@ def get_response(prompt_list: List[Tuple[str, str]]):
|
||||
content = delta['content']
|
||||
print_colored('' if complete_string else 'assistent', content, 'green', end='')
|
||||
complete_string += content
|
||||
total_chars_generation += len(content)
|
||||
print('\n')
|
||||
money_prompt = round(total_chars_prompt / 3.4 * 0.03 / 1000, 2)
|
||||
money_generation = round(total_chars_generation / 3.4 * 0.06 / 1000, 2)
|
||||
print('money prompt:', f'${money_prompt}')
|
||||
print('money generation:', f'${money_generation}')
|
||||
print('total money:', f'${money_prompt + money_generation}')
|
||||
print('\n')
|
||||
return complete_string
|
||||
except (RateLimitError, Timeout, ConnectionError, GenerationTimeoutError) as e:
|
||||
|
||||
Reference in New Issue
Block a user