from helpers.config import * from helpers.prompts import * from helpers.provider import * from helpers.models import model import re,ast def streamer(tok): completion_timestamp = int(time.time()) completion_id = ''.join(random.choices( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) completion_tokens = num_tokens_from_string(tok) completion_data = { 'id': f'chatcmpl-{completion_id}', 'object': 'chat.completion.chunk', 'created': completion_timestamp, 'model': 'gpt-4', "usage": { "prompt_tokens": 0, "completion_tokens": completion_tokens, "total_tokens": completion_tokens, }, 'choices': [ { 'delta': { 'role':"assistant", 'content':tok }, 'index': 0, 'finish_reason': None } ] } return completion_data def end(): completion_timestamp = int(time.time()) completion_id = ''.join(random.choices( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) end_completion_data = { 'id': f'chatcmpl-{completion_id}', 'object': 'chat.completion.chunk', 'created': completion_timestamp, 'model': model, 'provider': 'openai', 'choices': [ { 'index': 0, 'delta': {}, 'finish_reason': 'stop', } ], } return end_completion_data def output(tok): completion_timestamp = int(time.time()) completion_id = ''.join(random.choices( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) completion_tokens = 11 return { 'id': 'chatcmpl-%s' % completion_id, 'object': 'chat.completion', 'created': completion_timestamp, 'model': "gpt-4-0125-preview", "usage": { "prompt_tokens": 0, "completion_tokens": completion_tokens, "total_tokens": completion_tokens, }, 'choices': [{ 'message': { 'role': 'assistant', 'content': tok }, 'finish_reason': 'stop', 'index': 0 }] } def stream_func(tok,type_tool): print("-"*500) print(f"streaming {type_tool}") completion_timestamp = int(time.time()) completion_id = ''.join(random.choices( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) completion_tokens = 11 tool_calls=[] regex = r'```json\n(.*?)\n```' matches = re.findall(regex, tok, re.DOTALL) print(matches) if matches !=[]: info_blocks = ast.literal_eval(matches[0]) for info_block in info_blocks: tok=tok.replace(f"```json\n{info_block}\n```",'') tool_data=info_block # to_add={"function":{"arguments":re.sub(r"(?