code: #!/usr/bin/env python3
  # -*- coding utf-8 -*-
  
  import openai
  import yaml
  from langchain.chat_models import ChatOpenAI
  from langchain.text_splitter import SpacyTextSplitter
  from llama_index import GPTListIndex, LLMPredictor, ServiceContext, SimpleDirectoryReader
  from llama_index.node_parser import SimpleNodeParser
  
  '''
  llama_index对于文章进行小结
  本例中，相当于对文本分段，按树状结构逐步向上进行总结
  
  使用spaCy分词模型
  python -m spacy download zh_core_web_sm
  '''
  
  def get_api_key():
      with open("config.yaml", "r", encoding="utf-8") as yaml_file:
          yaml_data = yaml.safe_load(yaml_file)
          openai.api_key = yaml_data["openai"]["api_key"]
  
  
  if __name__ == '__main__':
      get_api_key()
  
      # 配置LLM
      llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=1024))
      service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
  
      # 分词模型，最长2048个token
      text_splitter = SpacyTextSplitter(pipeline="zh_core_web_sm", chunk_size = 2048)
      parser = SimpleNodeParser(text_splitter=text_splitter)
  
      # 加载目录中的语料
      documents = SimpleDirectoryReader('./data/mr_fujino').load_data()
  
      # 获取语料中的nodes
      nodes = parser.get_nodes_from_documents(documents)
  
      # 最简单的索引结构GPTListIndex
      list_index = GPTListIndex(nodes=nodes, service_context=service_context)
  
      # 树状模型进行总结
      response = list_index.query("下面鲁迅先生以第一人称‘我’写的内容，请你用中文总结一下:", response_mode="tree_summarize")
      print(response)
      
  
apis: llama_index.node_parser.SimpleNodeParser llama_index.ServiceContext.from_defaults llama_index.GPTListIndex llama_index.SimpleDirectoryReader
extract_api: [((803, 860), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor'}), '(llm_predictor=llm_predictor)\n', (831, 860), False, 'from llama_index import GPTListIndex, LLMPredictor, ServiceContext, SimpleDirectoryReader\n'), ((906, 967), 'langchain.text_splitter.SpacyTextSplitter', 'SpacyTextSplitter', ([], {'pipeline': '"""zh_core_web_sm"""', 'chunk_size': '(2048)'}), "(pipeline='zh_core_web_sm', chunk_size=2048)\n", (923, 967), False, 'from langchain.text_splitter import SpacyTextSplitter\n'), ((983, 1028), 'llama_index.node_parser.SimpleNodeParser', 'SimpleNodeParser', ([], {'text_splitter': 'text_splitter'}), '(text_splitter=text_splitter)\n', (999, 1028), False, 'from llama_index.node_parser import SimpleNodeParser\n'), ((1234, 1292), 'llama_index.GPTListIndex', 'GPTListIndex', ([], {'nodes': 'nodes', 'service_context': 'service_context'}), '(nodes=nodes, service_context=service_context)\n', (1246, 1292), False, 'from llama_index import GPTListIndex, LLMPredictor, ServiceContext, SimpleDirectoryReader\n'), ((530, 555), 'yaml.safe_load', 'yaml.safe_load', (['yaml_file'], {}), '(yaml_file)\n', (544, 555), False, 'import yaml\n'), ((709, 779), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'temperature': '(0)', 'model_name': '"""gpt-3.5-turbo"""', 'max_tokens': '(1024)'}), "(temperature=0, model_name='gpt-3.5-turbo', max_tokens=1024)\n", (719, 779), False, 'from langchain.chat_models import ChatOpenAI\n'), ((1061, 1102), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', (['"""./data/mr_fujino"""'], {}), "('./data/mr_fujino')\n", (1082, 1102), False, 'from llama_index import GPTListIndex, LLMPredictor, ServiceContext, SimpleDirectoryReader\n')]