index_builder.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from langchain.callbacks import CallbackManager
  2. from llama_index import ServiceContext, PromptHelper, LLMPredictor
  3. from core.callback_handler.std_out_callback_handler import DifyStdOutCallbackHandler
  4. from core.embedding.openai_embedding import OpenAIEmbedding
  5. from core.llm.llm_builder import LLMBuilder
  6. class IndexBuilder:
  7. @classmethod
  8. def get_default_service_context(cls, tenant_id: str) -> ServiceContext:
  9. # set number of output tokens
  10. num_output = 512
  11. # only for verbose
  12. callback_manager = CallbackManager([DifyStdOutCallbackHandler()])
  13. llm = LLMBuilder.to_llm(
  14. tenant_id=tenant_id,
  15. model_name='text-davinci-003',
  16. temperature=0,
  17. max_tokens=num_output,
  18. callback_manager=callback_manager,
  19. )
  20. llm_predictor = LLMPredictor(llm=llm)
  21. # These parameters here will affect the logic of segmenting the final synthesized response.
  22. # The number of refinement iterations in the synthesis process depends
  23. # on whether the length of the segmented output exceeds the max_input_size.
  24. prompt_helper = PromptHelper(
  25. max_input_size=3500,
  26. num_output=num_output,
  27. max_chunk_overlap=20
  28. )
  29. model_credentials = LLMBuilder.get_model_credentials(
  30. tenant_id=tenant_id,
  31. model_name='text-embedding-ada-002'
  32. )
  33. return ServiceContext.from_defaults(
  34. llm_predictor=llm_predictor,
  35. prompt_helper=prompt_helper,
  36. embed_model=OpenAIEmbedding(**model_credentials),
  37. )