Skip to content

Commit

Permalink
Merge pull request #4 from jeffrey-fong/add-finish-reason-length
Browse files Browse the repository at this point in the history
Add finish_reason=length
  • Loading branch information
jeffrey-fong authored Oct 2, 2023
2 parents 93880e9 + 89844ca commit a0ea16d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions invoker/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def generate_stream(self, input_text: str, params: Dict[str, Any]) -> Generator[
chunk = self._postprocess_stream_chunk(text=chunk)
if chunk:
yield chunk
if generated_tokens == self._max_new_tokens:
yield {"delta": {}, "finish_reason": "length"}
else:
input_ids = self._tokenizer(input_text, return_tensors="pt").input_ids.cuda()
logits_processor = self._get_logits_processor(temperature=temperature, top_p=top_p)
Expand Down Expand Up @@ -175,6 +177,8 @@ def _hf_generate_stream(self, input_ids, params, logits_processor) -> Generator[
if sampled_token == self._tokenizer.eos_token_id:
break
yield output
else:
yield "[|LENGTH|]"

def _postprocess(self, text):
output_json = json.loads(re.search(r"```(.*?)```?", text, re.DOTALL).group(1))
Expand Down Expand Up @@ -205,6 +209,9 @@ def _postprocess(self, text):

def _postprocess_stream_chunk(self, text):
self._curr_response += text
if text == "[|LENGTH|]":
self._finish_reason = "complete"
return {"delta": {}, "finish_reason": "length"}
if not self._response_type:
# Check for "content"
if '"content": null, "function_call": {' in self._curr_response:
Expand Down

0 comments on commit a0ea16d

Please sign in to comment.