run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.9k stars 5.28k forks source link

[Bug]: RunTime Error When using ServiceContext #16556

Open Hamiedamr opened 1 month ago

Hamiedamr commented 1 month ago

Bug Description

Confilct between langchain and llamaindex

Version

0.9.48

Steps to Reproduce

  1. install langchain version 0.2.x
  2. from llama_index.emebddings import HuggingFaceEmbedding
  3. Define embedding model from local path
  4. from llama_index import ServiceContext
  5. service_context = ServiceContext.from_defaults(embed_model=)

Relevant Logs/Tracbacks

RuntimeError                              Traceback (most recent call last) Cell In[63], line 1 ----> 1 sql_query = chat_with_DB('list all text with their classes from table transcriptions')[0] Cell In[62], line 208, in chat_with_DB(prompt)    207 def chat_with_DB(prompt): --> 208     return ask_db(prompt, engine, "phind-codellama" , "GPU") Cell In[62], line 198, in ask_db(query_str, engine, model, device)     190     llm = LlamaCPP(     191         model_path=model,     192         **config['LLM_PARAMS'],     193         messages_to_prompt=messages_to_prompt,     194         completion_to_prompt=completion_to_prompt     195     )     197 embedding = HuggingFaceEmbedding(model_name=EMBEDDING_MODEL_PATH, pooling="cls") --> 198 service_context = ServiceContext.from_defaults(llm=llm, embed_model=embedding)     199 set_global_service_context(service_context=service_context)     200 query_engine = build_query_engine(engine) File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/service_context.py:191, in ServiceContext.from_defaults(cls, llm_predictor, llm, prompt_helper, embed_model, node_parser, text_splitter, transformations, llama_logger, callback_manager, system_prompt, query_wrapper_prompt, pydantic_program_mode, chunk_size, chunk_overlap, context_window, num_output, chunk_size_limit)     186         llm_predictor.query_wrapper_prompt = query_wrapper_prompt     188 # NOTE: the embed_model isn't used in all indices     189 # NOTE: embed model should be a transformation, but the way the service     190 # context works, we can't put in there yet. --> 191 embed_model = resolve_embed_model(embed_model)     192 embed_model.callback_manager = callback_manager     194 prompt_helper = prompt_helper or _get_default_prompt_helper(     195     llm_metadata=llm_predictor.metadata,     196     context_window=context_window,     197     num_output=num_output,     198 ) File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/embeddings/utils.py:41, in resolve_embed_model(embed_model)      39 """Resolve embed model."""      40 try: ---> 41     from llama_index.bridge.langchain import Embeddings as LCEmbeddings      42 except ImportError:      43     LCEmbeddings = None  # type: ignore File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/bridge/langchain.py:2       1 import langchain ----> 2 from langchain.agents import AgentExecutor, AgentType, initialize_agent       4 # agents and tools       5 from langchain.agents.agent_toolkits.base import BaseToolkit File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/langchain/agents/__init__.py:40      37 from langchain_core.tools.convert import tool      39 from langchain._api import create_importer ---> 40 from langchain.agents.agent import (      41     Agent,      42     AgentExecutor,      43     AgentOutputParser,      44     BaseMultiActionAgent,      45     BaseSingleActionAgent,      46     LLMSingleActionAgent,      47 )      48 from langchain.agents.agent_iterator import AgentExecutorIterator      49 from langchain.agents.agent_toolkits.vectorstore.base import (      50     create_vectorstore_agent,      51     create_vectorstore_router_agent,      52 ) File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/langchain/agents/agent.py:639     624             final_output = await self.runnable.ainvoke(     625                 inputs, config={"callbacks": callbacks}     626             )     628         return final_output     631 @deprecated(     632     "0.1.0",     633     message=(     634         "Use new agent constructor methods like create_react_agent, create_json_agent, "     635         "create_structured_chat_agent, etc."     636     ),     637     removal="1.0",     638 ) --> 639 class LLMSingleActionAgent(BaseSingleActionAgent):     640     """Base class for single action agents."""     642     llm_chain: LLMChain File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/main.py:197, in ModelMetaclass.__new__(mcs, name, bases, namespace, **kwargs)     189     if (     190         is_untouched(value)     191         and ann_type != PyObject    (...)     194         )     195     ):     196         continue --> 197     fields[ann_name] = ModelField.infer(     198         name=ann_name,     199         value=value,     200         annotation=ann_type,     201         class_validators=vg.get_validators(ann_name),     202         config=config,     203     )     204 elif ann_name not in namespace and config.underscore_attrs_are_private:     205     private_attributes[ann_name] = PrivateAttr() File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:504, in ModelField.infer(cls, name, value, annotation, class_validators, config)     501     required = False     502 annotation = get_annotation_from_field_info(annotation, field_info, name, config.validate_assignment) --> 504 return cls(     505     name=name,     506     type_=annotation,     507     alias=field_info.alias,     508     class_validators=class_validators,     509     default=value,     510     default_factory=field_info.default_factory,     511     required=required,     512     model_config=config,     513     field_info=field_info,     514 ) File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:434, in ModelField.__init__(self, name, type_, class_validators, model_config, default, default_factory, required, final, alias, field_info)     432 self.shape: int = SHAPE_SINGLETON     433 self.model_config.prepare_field(self) --> 434 self.prepare() File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:555, in ModelField.prepare(self)     553 if self.default is Undefined and self.default_factory is None:     554     self.default = None --> 555 self.populate_validators() File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:829, in ModelField.populate_validators(self)     825 if not self.sub_fields or self.shape == SHAPE_GENERIC:     826     get_validators = getattr(self.type_, '__get_validators__', None)     827     v_funcs = (     828         *[v.func for v in class_validators_ if v.each_item and v.pre], --> 829         *(get_validators() if get_validators else list(find_validators(self.type_, self.model_config))),     830         *[v.func for v in class_validators_ if v.each_item and not v.pre],     831     )     832     self.validators = prep_validators(v_funcs)     834 self.pre_validators = [] File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/validators.py:765, in find_validators(type_, config)     763     yield make_arbitrary_type_validator(type_)     764 else: --> 765     raise RuntimeError(f'no validator found for {type_}, see arbitrary_types_allowed in Config') RuntimeError: no validator found for <class 'langchain.chains.llm.LLMChain'>, see arbitrary_types_allowed in Config

Error:

RuntimeError                              Traceback (most recent call last)

Cell In[63], line 1

----> 1 sql_query = chat_with_DB('list all text with their classes from table transcriptions')[0]

Cell In[62], line 208, in chat_with_DB(prompt)

    207 def chat_with_DB(prompt):

--> 208     return ask_db(prompt, engine, "phind-codellama" , "GPU")

Cell In[62], line 198, in ask_db(query_str, engine, model, device)

    190     llm = LlamaCPP(

    191         model_path=model,

    192         **config['LLM_PARAMS'],

    193         messages_to_prompt=messages_to_prompt, 

    194         completion_to_prompt=completion_to_prompt

    195     )

    197 embedding = HuggingFaceEmbedding(model_name=EMBEDDING_MODEL_PATH, pooling="cls")

--> 198 service_context = ServiceContext.from_defaults(llm=llm, embed_model=embedding)

    199 set_global_service_context(service_context=service_context)

    200 query_engine = build_query_engine(engine)

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/service_context.py:191, in ServiceContext.from_defaults(cls, llm_predictor, llm, prompt_helper, embed_model, node_parser, text_splitter, transformations, llama_logger, callback_manager, system_prompt, query_wrapper_prompt, pydantic_program_mode, chunk_size, chunk_overlap, context_window, num_output, chunk_size_limit)

    186         llm_predictor.query_wrapper_prompt = query_wrapper_prompt

    188 # NOTE: the embed_model isn't used in all indices

    189 # NOTE: embed model should be a transformation, but the way the service

    190 # context works, we can't put in there yet.

--> 191 embed_model = resolve_embed_model(embed_model)

    192 embed_model.callback_manager = callback_manager

    194 prompt_helper = prompt_helper or _get_default_prompt_helper(

    195     llm_metadata=llm_predictor.metadata,

    196     context_window=context_window,

    197     num_output=num_output,

    198 )

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/embeddings/utils.py:41, in resolve_embed_model(embed_model)

     39 """Resolve embed model."""

     40 try:

---> 41     from llama_index.bridge.langchain import Embeddings as LCEmbeddings

     42 except ImportError:

     43     LCEmbeddings = None  # type: ignore

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/llama_index/bridge/langchain.py:2

      1 import langchain

----> 2 from langchain.agents import AgentExecutor, AgentType, initialize_agent

      4 # agents and tools

      5 from langchain.agents.agent_toolkits.base import BaseToolkit

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/langchain/agents/__init__.py:40

     37 from langchain_core.tools.convert import tool

     39 from langchain._api import create_importer

---> 40 from langchain.agents.agent import (

     41     Agent,

     42     AgentExecutor,

     43     AgentOutputParser,

     44     BaseMultiActionAgent,

     45     BaseSingleActionAgent,

     46     LLMSingleActionAgent,

     47 )

     48 from langchain.agents.agent_iterator import AgentExecutorIterator

     49 from langchain.agents.agent_toolkits.vectorstore.base import (

     50     create_vectorstore_agent,

     51     create_vectorstore_router_agent,

     52 )

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/langchain/agents/agent.py:639

    624             final_output = await self.runnable.ainvoke(

    625                 inputs, config={"callbacks": callbacks}

    626             )

    628         return final_output

    631 @deprecated(

    632     "0.1.0",

    633     message=(

    634         "Use new agent constructor methods like create_react_agent, create_json_agent, "

    635         "create_structured_chat_agent, etc."

    636     ),

    637     removal="1.0",

    638 )

--> 639 class LLMSingleActionAgent(BaseSingleActionAgent):

    640     """Base class for single action agents."""

    642     llm_chain: LLMChain

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/main.py:197, in ModelMetaclass.__new__(mcs, name, bases, namespace, **kwargs)

    189     if (

    190         is_untouched(value)

    191         and ann_type != PyObject

   (...)

    194         )

    195     ):

    196         continue

--> 197     fields[ann_name] = ModelField.infer(

    198         name=ann_name,

    199         value=value,

    200         annotation=ann_type,

    201         class_validators=vg.get_validators(ann_name),

    202         config=config,

    203     )

    204 elif ann_name not in namespace and config.underscore_attrs_are_private:

    205     private_attributes[ann_name] = PrivateAttr()

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:504, in ModelField.infer(cls, name, value, annotation, class_validators, config)

    501     required = False

    502 annotation = get_annotation_from_field_info(annotation, field_info, name, config.validate_assignment)

--> 504 return cls(

    505     name=name,

    506     type_=annotation,

    507     alias=field_info.alias,

    508     class_validators=class_validators,

    509     default=value,

    510     default_factory=field_info.default_factory,

    511     required=required,

    512     model_config=config,

    513     field_info=field_info,

    514 )

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:434, in ModelField.__init__(self, name, type_, class_validators, model_config, default, default_factory, required, final, alias, field_info)

    432 self.shape: int = SHAPE_SINGLETON

    433 self.model_config.prepare_field(self)

--> 434 self.prepare()

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:555, in ModelField.prepare(self)

    553 if self.default is Undefined and self.default_factory is None:

    554     self.default = None

--> 555 self.populate_validators()

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/fields.py:829, in ModelField.populate_validators(self)

    825 if not self.sub_fields or self.shape == SHAPE_GENERIC:

    826     get_validators = getattr(self.type_, '__get_validators__', None)

    827     v_funcs = (

    828         *[v.func for v in class_validators_ if v.each_item and v.pre],

--> 829         *(get_validators() if get_validators else list(find_validators(self.type_, self.model_config))),

    830         *[v.func for v in class_validators_ if v.each_item and not v.pre],

    831     )

    832     self.validators = prep_validators(v_funcs)

    834 self.pre_validators = []

File /disk01/shared_notebook/env/genai/genai_py311/lib/python3.11/site-packages/pydantic/v1/validators.py:765, in find_validators(type_, config)

    763     yield make_arbitrary_type_validator(type_)

    764 else:

--> 765     raise RuntimeError(f'no validator found for {type_}, see `arbitrary_types_allowed` in Config')

RuntimeError: no validator found for <class 'langchain.chains.llm.LLMChain'>, see `arbitrary_types_allowed` in Config
dosubot[bot] commented 1 month ago

Hello, @Hamiedamr! I'm here to help you with any bugs, questions, or contributions. Let's tackle this issue together!

I found a similar issue related to import errors in the LlamaIndex package, which might be relevant to your problem. The issue was about importing TrafilaturaWebReader and other classes, and it was suggested to adjust the import statements due to a reorganization in the package [1].

For your specific issue, it seems like there might be a conflict between the versions of LlamaIndex and Langchain you are using. The error message indicates a problem with the LLMSingleActionAgent class in Langchain, which might be related to compatibility issues between the two libraries.

To resolve this, you might want to check if there are any specific compatibility requirements between LlamaIndex version 0.9.48 and Langchain version 0.2.x. Unfortunately, I couldn't find specific compatibility information in the provided context, but ensuring both libraries are compatible with each other is a good starting point. Additionally, you might want to verify if the arbitrary_types_allowed setting in your Pydantic configuration is correctly set, as the error suggests this might be related to the issue.

To continue talking to Dosu, mention @dosu.

logan-markewich commented 1 month ago

@Hamiedamr v0.9.48 is very old, and v0.2 of langchain is quite new. There's no gauruntee that the two work together. And since it's an old version, it cannot be patched

My advice is either downgrade langchain (or untinstall it, you probably don't need it if you are already using llama-index?), or you can update llama-index (note that in v0.11, the service context was removed)