Skip to content

Commit

Permalink
updated t3nsor (gpt 3.5)
Browse files Browse the repository at this point in the history
changed iter lines to iter content chunks, way smoother and more updates + resolved dict issue
  • Loading branch information
xtekky committed Apr 6, 2023
1 parent 76571f2 commit ab75098
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
47 changes: 23 additions & 24 deletions t3nsor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def __init__(self, choices: dict) -> None:

class Usage:
def __init__(self, usage_dict: dict) -> None:
self.prompt_tokens = usage_dict['prompt_tokens']
self.completion_tokens = usage_dict['completion_tokens']
self.total_tokens = usage_dict['total_tokens']
self.prompt_tokens = usage_dict['prompt_chars']
self.completion_tokens = usage_dict['completion_chars']
self.total_tokens = usage_dict['total_chars']

def __repr__(self):
return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>'''
Expand Down Expand Up @@ -95,24 +95,23 @@ def create(
'prompt' : prompt
})

for resp in response.iter_lines():
if resp:
yield T3nsorResponse({
'id' : f'cmpl-1337-{int(time())}',
'object' : 'text_completion',
'created': int(time()),
'model' : Completion.model,

'choices': [{
'text' : resp.decode(),
'index' : 0,
'logprobs' : None,
'finish_reason' : 'stop'
}],

'usage': {
'prompt_chars' : len(prompt),
'completion_chars' : len(resp.decode()),
'total_chars' : len(prompt) + len(resp.decode())
}
})
for chunk in response.iter_content(chunk_size = 2046):
yield T3nsorResponse({
'id' : f'cmpl-1337-{int(time())}',
'object' : 'text_completion',
'created': int(time()),
'model' : Completion.model,

'choices': [{
'text' : chunk.decode(),
'index' : 0,
'logprobs' : None,
'finish_reason' : 'stop'
}],

'usage': {
'prompt_chars' : len(prompt),
'completion_chars' : len(chunk.decode()),
'total_chars' : len(prompt) + len(chunk.decode())
}
})
7 changes: 7 additions & 0 deletions testing/t3nsor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import t3nsor

for response in t3nsor.StreamCompletion.create(
prompt = 'write python code to reverse a string',
messages = []):

print(response.completion.choices[0].text)

0 comments on commit ab75098

Please sign in to comment.