t.me/xtekky commited on
Commit
ab75098
1 Parent(s): 76571f2

updated t3nsor (gpt 3.5)

Browse files

changed iter lines to iter content chunks, way smoother and more updates + resolved dict issue

Files changed (2) hide show
  1. t3nsor/__init__.py +23 -24
  2. testing/t3nsor.py +7 -0
t3nsor/__init__.py CHANGED
@@ -21,9 +21,9 @@ class T3nsorResponse:
21
 
22
  class Usage:
23
  def __init__(self, usage_dict: dict) -> None:
24
- self.prompt_tokens = usage_dict['prompt_tokens']
25
- self.completion_tokens = usage_dict['completion_tokens']
26
- self.total_tokens = usage_dict['total_tokens']
27
 
28
  def __repr__(self):
29
  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>'''
@@ -95,24 +95,23 @@ class StreamCompletion:
95
  'prompt' : prompt
96
  })
97
 
98
- for resp in response.iter_lines():
99
- if resp:
100
- yield T3nsorResponse({
101
- 'id' : f'cmpl-1337-{int(time())}',
102
- 'object' : 'text_completion',
103
- 'created': int(time()),
104
- 'model' : Completion.model,
105
-
106
- 'choices': [{
107
- 'text' : resp.decode(),
108
- 'index' : 0,
109
- 'logprobs' : None,
110
- 'finish_reason' : 'stop'
111
- }],
112
-
113
- 'usage': {
114
- 'prompt_chars' : len(prompt),
115
- 'completion_chars' : len(resp.decode()),
116
- 'total_chars' : len(prompt) + len(resp.decode())
117
- }
118
- })
 
21
 
22
  class Usage:
23
  def __init__(self, usage_dict: dict) -> None:
24
+ self.prompt_tokens = usage_dict['prompt_chars']
25
+ self.completion_tokens = usage_dict['completion_chars']
26
+ self.total_tokens = usage_dict['total_chars']
27
 
28
  def __repr__(self):
29
  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>'''
 
95
  'prompt' : prompt
96
  })
97
 
98
+ for chunk in response.iter_content(chunk_size = 2046):
99
+ yield T3nsorResponse({
100
+ 'id' : f'cmpl-1337-{int(time())}',
101
+ 'object' : 'text_completion',
102
+ 'created': int(time()),
103
+ 'model' : Completion.model,
104
+
105
+ 'choices': [{
106
+ 'text' : chunk.decode(),
107
+ 'index' : 0,
108
+ 'logprobs' : None,
109
+ 'finish_reason' : 'stop'
110
+ }],
111
+
112
+ 'usage': {
113
+ 'prompt_chars' : len(prompt),
114
+ 'completion_chars' : len(chunk.decode()),
115
+ 'total_chars' : len(prompt) + len(chunk.decode())
116
+ }
117
+ })
 
testing/t3nsor.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import t3nsor
2
+
3
+ for response in t3nsor.StreamCompletion.create(
4
+ prompt = 'write python code to reverse a string',
5
+ messages = []):
6
+
7
+ print(response.completion.choices[0].text)