code: def stream_chunk_builder_text_completion(
      chunks: list, messages: Optional[List] = None
  ):
      id = chunks[0]["id"]
      object = chunks[0]["object"]
      created = chunks[0]["created"]
      model = chunks[0]["model"]
      system_fingerprint = chunks[0].get("system_fingerprint", None)
      finish_reason = chunks[-1]["choices"][0]["finish_reason"]
      logprobs = chunks[-1]["choices"][0]["logprobs"]
  
      response = {
          "id": id,
          "object": object,
          "created": created,
          "model": model,
          "system_fingerprint": system_fingerprint,
          "choices": [
              {
                  "text": None,
                  "index": 0,
                  "logprobs": logprobs,
                  "finish_reason": finish_reason,
              }
          ],
          "usage": {
              "prompt_tokens": None,
              "completion_tokens": None,
              "total_tokens": None,
          },
      }
      content_list = []
      for chunk in chunks:
          choices = chunk["choices"]
          for choice in choices:
              if (
                  choice is not None
                  and hasattr(choice, "text")
                  and choice.get("text") is not None
              ):
                  _choice = choice.get("text")
                  content_list.append(_choice)
  
      # Combine the "content" strings into a single string || combine the 'function' strings into a single string
      combined_content = "".join(content_list)
  
      # Update the "content" field within the response dictionary
      response["choices"][0]["text"] = combined_content
  
      if len(combined_content) > 0:
          completion_output = combined_content
      else:
          completion_output = ""
      # # Update usage information if needed
      try:
          response["usage"]["prompt_tokens"] = token_counter(
              model=model, messages=messages
          )
      except:  # don't allow this failing to block a complete streaming response from being returned
          print_verbose(
              f"token_counter failed, assuming prompt tokens is 0"
          )
          response["usage"]["prompt_tokens"] = 0
      response["usage"]["completion_tokens"] = token_counter(
          model=model,
          text=combined_content,
          count_response_tokens=True,  # count_response_tokens is a Flag to tell token counter this is a response, No need to add extra tokens we do for input messages
      )
      response["usage"]["total_tokens"] = (
          response["usage"]["prompt_tokens"]
          + response["usage"]["completion_tokens"]
      )
      return response
  
imports: import os, openai, sys, json, inspect, uuid, datetime, threading
  from typing import Any, Literal, Union
  from functools import partial
  import dotenv, traceback, random, asyncio, time, contextvars
  from copy import deepcopy
  import httpx
  import litellm
  from litellm import client, exception_type, get_optional_params, get_litellm_params, Logging
  from litellm.utils import get_secret, CustomStreamWrapper, read_config_args, completion_with_fallbacks, get_llm_provider, get_api_key, mock_completion_streaming_obj, convert_to_model_response_object, token_counter, Usage, get_optional_params_embeddings, get_optional_params_image_gen
  from .llms import anthropic, together_ai, ai21, sagemaker, bedrock, huggingface_restapi, replicate, aleph_alpha, nlp_cloud, baseten, vllm, ollama, ollama_chat, cloudflare, cohere, petals, oobabooga, openrouter, palm, gemini, vertex_ai, maritalk
  from .llms.openai import OpenAIChatCompletion, OpenAITextCompletion
  from .llms.azure import AzureChatCompletion
  from .llms.huggingface_restapi import Huggingface
  from .llms.prompt_templates.factory import prompt_factory, custom_prompt, function_call_prompt
  import tiktoken
  from concurrent.futures import ThreadPoolExecutor
  from typing import Callable, List, Optional, Dict, Union, Mapping
  from .caching import enable_cache, disable_cache, update_cache
  from litellm.utils import get_secret, CustomStreamWrapper, TextCompletionStreamWrapper, ModelResponse, TextCompletionResponse, TextChoices, EmbeddingResponse, read_config_args, Choices, Message
label_desc: Use image node_id 13 for calling a global function with example usage: stream_chunk_builder_text_completion(chunks, messages) and returns: response