code: def completions_action(body: dict, stream_output=False):
      object_type = (
          "text_completion.chunk"
          if stream_output
          else "text_completion"
      )
      created_time = int(time.time())
      res_id = "res_id-%s" % str(uuid.uuid4())
      resp_list = "choices"
      prompt_str = "prompt"
      assert prompt_str in body, "Missing prompt"
  
      gen_kwargs = body
      gen_kwargs["stream_output"] = stream_output
  
      if not stream_output:
          prompt_arg = body[prompt_str]
          if isinstance(prompt_arg, str) or (
              isinstance(prompt_arg, list)
              and isinstance(prompt_arg[0], int)
          ):
              prompt_arg = [prompt_arg]
  
          resp_list_data = []
          total_completion_token_count = 0
          total_prompt_token_count = 0
  
          for idx, prompt in enumerate(prompt_arg, start=0):
              token_count = count_tokens(prompt)
              total_prompt_token_count += token_count
  
              response = deque(
                  get_response(prompt, gen_kwargs), maxlen=1
              ).pop()
              completion_token_count = count_tokens(response)
              total_completion_token_count += completion_token_count
              stop_reason = "stop"
  
              res_idx = {
                  "index": idx,
                  "finish_reason": stop_reason,
                  "text": response,
                  "logprobs": None,
              }
  
              resp_list_data.extend([res_idx])
  
          res_dict = {
              "id": res_id,
              "object": object_type,
              "created": created_time,
              "model": "",
              resp_list: resp_list_data,
              "usage": {
                  "prompt_tokens": total_prompt_token_count,
                  "completion_tokens": total_completion_token_count,
                  "total_tokens": total_prompt_token_count
                  + total_completion_token_count,
              },
          }
  
          yield res_dict
      else:
          prompt = body[prompt_str]
          token_count = count_tokens(prompt)
  
          def text_streaming_chunk(content):
              # begin streaming
              chunk = {
                  "id": res_id,
                  "object": object_type,
                  "created": created_time,
                  "model": "",
                  resp_list: [
                      {
                          "index": 0,
                          "finish_reason": None,
                          "text": content,
                          "logprobs": None,
                      }
                  ],
              }
  
              return chunk
  
          generator = get_response(
              prompt,
              gen_kwargs,
              chunk_response=stream_output,
              stream_output=stream_output,
          )
          response = ""
          for chunk in generator:
              response += chunk
              yield_chunk = text_streaming_chunk(chunk)
              yield yield_chunk
  
          completion_token_count = count_tokens(response)
          stop_reason = "stop"
          chunk = text_streaming_chunk("")
          chunk[resp_list][0]["finish_reason"] = stop_reason
          chunk["usage"] = {
              "prompt_tokens": token_count,
              "completion_tokens": completion_token_count,
              "total_tokens": token_count + completion_token_count,
          }
          yield chunk
  
imports: import ast
  import os
  import time
  import uuid
  from collections import deque
  from log import logger
label_desc: Use image node_id 9 for calling a global function with example usage: completions_action(body, stream_output) and returns: chunk