code: def summarize_if_overflowed(
      data, llm, max_tokens, budget_multiplier=0.8
  ) -> str:
      """
      This helper retries with a summarized version of the
      output if the previous call fails due to the token limit being exceeded.
  
      We trigger summarization when the token count exceeds the limit times a multiplier to be conservative.
      """
      # tokenize data for length check
      # note: this is a rough estimate, as the tokenizer used in each LLM may be different
      encoding = tiktoken.get_encoding("gpt2")
      n_tokens = len(encoding.encode(data))
  
      # map-reduce given token budget
      if n_tokens > max_tokens * budget_multiplier:
          # map
          map_template = """The following is a set of documents
                  {docs}
                  Based on this list of docs, please identify the main themes
                  Helpful Answer:"""
          map_prompt = PromptTemplate.from_template(map_template)
          map_chain = LLMChain(llm=llm, prompt=map_prompt)
  
          # reduce
          reduce_template = """The following is set of summaries:
                  {doc_summaries}
                  Take these and distill it into a final, consolidated summary of the main themes.
                  Helpful Answer:"""
          reduce_prompt = PromptTemplate.from_template(reduce_template)
          reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)
          combine_documents_chain = StuffDocumentsChain(
              llm_chain=reduce_chain,
              document_variable_name="doc_summaries",
          )
          reduce_documents_chain = ReduceDocumentsChain(
              combine_documents_chain=combine_documents_chain,
              collapse_documents_chain=combine_documents_chain,
              token_max=max_tokens
              * budget_multiplier,  # applies for each group of documents
          )
          map_reduce_chain = MapReduceDocumentsChain(
              llm_chain=map_chain,
              reduce_documents_chain=reduce_documents_chain,
              document_variable_name="docs",
              return_intermediate_steps=False,
          )
          # split
          text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
              chunk_size=1000, chunk_overlap=0
          )
          docs = text_splitter.create_documents([data])
          split_docs = text_splitter.split_documents(docs)
  
          # run chain
          data = map_reduce_chain.run(split_docs)
      return data
  
file_path: mindsdb/mindsdb/integrations/handlers/langchain_handler/tools.py
imports: import tiktoken
  from typing import Callable
  from mindsdb_sql import parse_sql, Insert
  from langchain.utilities import GoogleSerperAPIWrapper
  from langchain.prompts import PromptTemplate
  from langchain.agents import load_tools, Tool
  from langchain.chains.llm import LLMChain
  from langchain.text_splitter import CharacterTextSplitter
  from langchain.chains.combine_documents.stuff import StuffDocumentsChain
  from langchain.chains import ReduceDocumentsChain, MapReduceDocumentsChain
  from mindsdb.interfaces.skills.skill_tool import make_tools_from_skill
label_desc: Use image node_id 6 for calling a global function with example usage: summarize_if_overflowed(data, llm, max_tokens, budget_multiplier) and returns: data