code: def text_completion(
      prompt: Union[
          str, List[Union[str, List[Union[str, List[int]]]]]
      ],  # Required: The prompt(s) to generate completions for.
      model: Optional[
          str
      ] = None,  # Optional: either `model` or `engine` can be set
      best_of: Optional[
          int
      ] = None,  # Optional: Generates best_of completions server-side.
      echo: Optional[
          bool
      ] = None,  # Optional: Echo back the prompt in addition to the completion.
      frequency_penalty: Optional[
          float
      ] = None,  # Optional: Penalize new tokens based on their existing frequency.
      logit_bias: Optional[
          Dict[int, int]
      ] = None,  # Optional: Modify the likelihood of specified tokens.
      logprobs: Optional[
          int
      ] = None,  # Optional: Include the log probabilities on the most likely tokens.
      max_tokens: Optional[
          int
      ] = None,  # Optional: The maximum number of tokens to generate in the completion.
      n: Optional[
          int
      ] = None,  # Optional: How many completions to generate for each prompt.
      presence_penalty: Optional[
          float
      ] = None,  # Optional: Penalize new tokens based on whether they appear in the text so far.
      stop: Optional[
          Union[str, List[str]]
      ] = None,  # Optional: Sequences where the API will stop generating further tokens.
      stream: Optional[
          bool
      ] = None,  # Optional: Whether to stream back partial progress.
      suffix: Optional[
          str
      ] = None,  # Optional: The suffix that comes after a completion of inserted text.
      temperature: Optional[
          float
      ] = None,  # Optional: Sampling temperature to use.
      top_p: Optional[
          float
      ] = None,  # Optional: Nucleus sampling parameter.
      user: Optional[
          str
      ] = None,  # Optional: A unique identifier representing your end-user.
      # set api_base, api_version, api_key
      api_base: Optional[str] = None,
      api_version: Optional[str] = None,
      api_key: Optional[str] = None,
      model_list: Optional[
          list
      ] = None,  # pass in a list of api_base,keys, etc.
      # Optional liteLLM function params
      custom_llm_provider: Optional[str] = None,
      *args,
      **kwargs,
  ):
      global print_verbose
      import copy
  
      """
      Generate text completions using the OpenAI API.
  
      Args:
          model (str): ID of the model to use.
          prompt (Union[str, List[Union[str, List[Union[str, List[int]]]]]): The prompt(s) to generate completions for.
          best_of (Optional[int], optional): Generates best_of completions server-side. Defaults to 1.
          echo (Optional[bool], optional): Echo back the prompt in addition to the completion. Defaults to False.
          frequency_penalty (Optional[float], optional): Penalize new tokens based on their existing frequency. Defaults to 0.
          logit_bias (Optional[Dict[int, int]], optional): Modify the likelihood of specified tokens. Defaults to None.
          logprobs (Optional[int], optional): Include the log probabilities on the most likely tokens. Defaults to None.
          max_tokens (Optional[int], optional): The maximum number of tokens to generate in the completion. Defaults to 16.
          n (Optional[int], optional): How many completions to generate for each prompt. Defaults to 1.
          presence_penalty (Optional[float], optional): Penalize new tokens based on whether they appear in the text so far. Defaults to 0.
          stop (Optional[Union[str, List[str]]], optional): Sequences where the API will stop generating further tokens. Defaults to None.
          stream (Optional[bool], optional): Whether to stream back partial progress. Defaults to False.
          suffix (Optional[str], optional): The suffix that comes after a completion of inserted text. Defaults to None.
          temperature (Optional[float], optional): Sampling temperature to use. Defaults to 1.
          top_p (Optional[float], optional): Nucleus sampling parameter. Defaults to 1.
          user (Optional[str], optional): A unique identifier representing your end-user.
      Returns:
          TextCompletionResponse: A response object containing the generated completion and associated metadata.
  
      Example:
          Your example of how to use this function goes here.
      """
      if "engine" in kwargs:
          if model == None:
              # only use engine when model not passed
              model = kwargs["engine"]
          kwargs.pop("engine")
  
      text_completion_response = TextCompletionResponse()
  
      optional_params: Dict[str, Any] = {}
      # default values for all optional params are none, litellm only passes them to the llm when they are set to non None values
      if best_of is not None:
          optional_params["best_of"] = best_of
      if echo is not None:
          optional_params["echo"] = echo
      if frequency_penalty is not None:
          optional_params["frequency_penalty"] = frequency_penalty
      if logit_bias is not None:
          optional_params["logit_bias"] = logit_bias
      if logprobs is not None:
          optional_params["logprobs"] = logprobs
      if max_tokens is not None:
          optional_params["max_tokens"] = max_tokens
      if n is not None:
          optional_params["n"] = n
      if presence_penalty is not None:
          optional_params["presence_penalty"] = presence_penalty
      if stop is not None:
          optional_params["stop"] = stop
      if stream is not None:
          optional_params["stream"] = stream
      if suffix is not None:
          optional_params["suffix"] = suffix
      if temperature is not None:
          optional_params["temperature"] = temperature
      if top_p is not None:
          optional_params["top_p"] = top_p
      if user is not None:
          optional_params["user"] = user
      if api_base is not None:
          optional_params["api_base"] = api_base
      if api_version is not None:
          optional_params["api_version"] = api_version
      if api_key is not None:
          optional_params["api_key"] = api_key
      if custom_llm_provider is not None:
          optional_params["custom_llm_provider"] = custom_llm_provider
  
      # get custom_llm_provider
      _, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(model=model, custom_llm_provider=custom_llm_provider, api_base=api_base)  # type: ignore
  
      if custom_llm_provider == "huggingface":
          # if echo == True, for TGI llms we need to set top_n_tokens to 3
          if echo == True:
              # for tgi llms
              if "top_n_tokens" not in kwargs:
                  kwargs["top_n_tokens"] = 3
  
          # processing prompt - users can pass raw tokens to OpenAI Completion()
          if type(prompt) == list:
              import concurrent.futures
  
              tokenizer = tiktoken.encoding_for_model(
                  "text-davinci-003"
              )
              ## if it's a 2d list - each element in the list is a text_completion() request
              if len(prompt) > 0 and type(prompt[0]) == list:
                  responses = [None for x in prompt]  # init responses
  
                  def process_prompt(i, individual_prompt):
                      decoded_prompt = tokenizer.decode(
                          individual_prompt
                      )
                      all_params = {**kwargs, **optional_params}
                      response = text_completion(
                          model=model,
                          prompt=decoded_prompt,
                          num_retries=3,  # ensure this does not fail for the batch
                          *args,
                          **all_params,
                      )
  
                      text_completion_response["id"] = response.get(
                          "id", None
                      )
                      text_completion_response[
                          "object"
                      ] = "text_completion"
                      text_completion_response[
                          "created"
                      ] = response.get("created", None)
                      text_completion_response["model"] = response.get(
                          "model", None
                      )
                      return response["choices"][0]
  
                  with concurrent.futures.ThreadPoolExecutor() as executor:
                      futures = [
                          executor.submit(
                              process_prompt, i, individual_prompt
                          )
                          for i, individual_prompt in enumerate(prompt)
                      ]
                      for i, future in enumerate(
                          concurrent.futures.as_completed(futures)
                      ):
                          responses[i] = future.result()
                      text_completion_response.choices = responses
  
                  return text_completion_response
      # else:
      # check if non default values passed in for best_of, echo, logprobs, suffix
      # these are the params supported by Completion() but not ChatCompletion
  
      # default case, non OpenAI requests go through here
      messages = [{"role": "system", "content": prompt}]
      kwargs.pop("prompt", None)
      response = completion(
          model=model,
          messages=messages,
          *args,
          **kwargs,
          **optional_params,
      )
      if kwargs.get("acompletion", False) == True:
          return response
      if stream == True or kwargs.get("stream", False) == True:
          response = TextCompletionStreamWrapper(
              completion_stream=response, model=model
          )
          return response
      transformed_logprobs = None
      # only supported for TGI models
      try:
          raw_response = response._hidden_params.get(
              "original_response", None
          )
          transformed_logprobs = litellm.utils.transform_logprobs(
              raw_response
          )
      except Exception as e:
          print_verbose(f"LiteLLM non blocking exception: {e}")
      text_completion_response["id"] = response.get("id", None)
      text_completion_response["object"] = "text_completion"
      text_completion_response["created"] = response.get(
          "created", None
      )
      text_completion_response["model"] = response.get("model", None)
      text_choices = TextChoices()
      text_choices["text"] = response["choices"][0]["message"][
          "content"
      ]
      text_choices["index"] = response["choices"][0]["index"]
      text_choices["logprobs"] = transformed_logprobs
      text_choices["finish_reason"] = response["choices"][0][
          "finish_reason"
      ]
      text_completion_response["choices"] = [text_choices]
      text_completion_response["usage"] = response.get("usage", None)
      return text_completion_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 8 for calling a global function with example usage: text_completion(prompt, model, best_of, echo, frequency_penalty, logit_bias, logprobs, max_tokens, n, presence_penalty, stop, stream, suffix, temperature, top_p, user, api_base, api_version, api_key, model_list, custom_llm_provider) and returns: text_completion_response, response, response, text_completion_response, response