code: def chat_completion_action(body: dict, stream_output=False) -> dict:
      messages = body.get("messages", [])
      object_type = (
          "chat.completions"
          if not stream_output
          else "chat.completions.chunk"
      )
      created_time = int(time.time())
      req_id = "chat_cmpl_id-%s" % str(uuid.uuid4())
      resp_list = "choices"
  
      gen_kwargs = body
      (
          instruction,
          system_message,
          history,
      ) = convert_messages_to_structure(messages)
      gen_kwargs.update(
          {
              "system_prompt": system_message,
              "chat_conversation": history,
              "stream_output": stream_output,
          }
      )
  
      def chat_streaming_chunk(content):
          # begin streaming
          chunk = {
              "id": req_id,
              "object": object_type,
              "created": created_time,
              "model": "",
              resp_list: [
                  {
                      "index": 0,
                      "finish_reason": None,
                      "message": {
                          "role": "assistant",
                          "content": content,
                      },
                      "delta": {
                          "role": "assistant",
                          "content": content,
                      },
                  }
              ],
          }
          return chunk
  
      if stream_output:
          yield chat_streaming_chunk("")
  
      token_count = count_tokens(instruction)
      generator = get_response(
          instruction,
          gen_kwargs,
          chunk_response=stream_output,
          stream_output=stream_output,
      )
  
      answer = ""
      for chunk in generator:
          if stream_output:
              answer += chunk
              chat_chunk = chat_streaming_chunk(chunk)
              yield chat_chunk
          else:
              answer = chunk
  
      completion_token_count = count_tokens(answer)
      stop_reason = "stop"
  
      if stream_output:
          chunk = chat_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
      else:
          resp = {
              "id": req_id,
              "object": object_type,
              "created": created_time,
              "model": "",
              resp_list: [
                  {
                      "index": 0,
                      "finish_reason": stop_reason,
                      "message": {
                          "role": "assistant",
                          "content": answer,
                      },
                  }
              ],
              "usage": {
                  "prompt_tokens": token_count,
                  "completion_tokens": completion_token_count,
                  "total_tokens": token_count + completion_token_count,
              },
          }
  
          yield resp
  
imports: import ast
  import os
  import time
  import uuid
  from collections import deque
  from log import logger
label_desc: Use image node_id 8 for calling a global function with example usage: chat_completion_action(body, stream_output) and returns: chunk