langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.57k stars 15.08k forks source link

TypeError: argument 'text': 'dict' object cannot be converted to 'PyString' #27309

Open mingjun1120 opened 4 days ago

mingjun1120 commented 4 days ago

Checked other resources

Example Code

Here is my code for constructing the chain:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import AzureChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_groq import ChatGroq
from prompts import SYSTEM_PROMPT
import streamlit as st

class ConversationalAssistant:
    def __init__(self):
        self.system_prompt = SYSTEM_PROMPT
        self.llm = AzureChatOpenAI(azure_deployment='gpt-4o-mini', temperature=0.4)
        self.messages = st.session_state.messages
        self.vector_store = st.session_state.vector_store
        self.employee_information = st.session_state.customer
        self.chain = self.get_conversation_chain()

    def get_conversation_chain(self):

        prompt_template = ChatPromptTemplate(
            [
                ("system", self.system_prompt),
                MessagesPlaceholder("conversation_history"),
                ("human", "{user_question}"),
            ]
        )

        chain = (
            {
                "retrieved_policy_information": self.vector_store.as_retriever(),
                "employee_information": lambda x: self.employee_information,
                "user_question": RunnablePassthrough(),
                "conversation_history": lambda x: self.messages,
            }
            | prompt_template
            | self.llm
            | StrOutputParser()
        )
        return chain

    def get_response(self, question: str):
        return self.chain.stream({"user_question": question})

Error Message and Stack Trace (if applicable)

Traceback:
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\app.py", line 108, in <module>
    main()
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\app.py", line 99, in main
    st.write_stream(response)
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\streamlit\runtime\metrics_util.py", line 410, in wrapped_func
    result = non_optional_func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\streamlit\elements\write.py", line 174, in write_stream
    for chunk in stream:  # type: ignore
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 3407, in stream
    yield from self.transform(iter([input]), config, **kwargs)
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 3394, in transform
    yield from self._transform_stream_with_config(
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 2197, in _transform_stream_with_config
    chunk: Output = context.run(next, iterator)  # type: ignore
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 3357, in _transform
    yield from final_pipeline
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\output_parsers\transform.py", line 64, in transform
    yield from self._transform_stream_with_config(
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 2161, in _transform_stream_with_config
    final_input: Optional[Input] = next(input_for_tracing, None)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 1413, in transform
    for ichunk in input:
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 1413, in transform
    for ichunk in input:
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 3847, in transform
    yield from self._transform_stream_with_config(
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 2197, in _transform_stream_with_config
    chunk: Output = context.run(next, iterator)  # type: ignore
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 3832, in _transform
    chunk = AddableDict({step_name: future.result()})
                                    ^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\_base.py", line 401, in __get_result
    raise self._exception
File "C:\Users\GV631HJ\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 1431, in transform
    yield from self.stream(final, config, **kwargs)
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\runnables\base.py", line 998, in stream
    yield self.invoke(input, config, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\retrievers.py", line 254, in invoke
    raise e
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\retrievers.py", line 247, in invoke
    result = self._get_relevant_documents(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_core\vectorstores\base.py", line 1080, in _get_relevant_documents
    docs = self.vectorstore.similarity_search(query, **self.search_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_chroma\vectorstores.py", line 582, in similarity_search
    docs_and_scores = self.similarity_search_with_score(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_chroma\vectorstores.py", line 679, in similarity_search_with_score
    query_embedding = self._embedding_function.embed_query(query)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_openai\embeddings\base.py", line 629, in embed_query
    return self.embed_documents([text])[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_openai\embeddings\base.py", line 588, in embed_documents
    return self._get_len_safe_embeddings(texts, engine=engine)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_openai\embeddings\base.py", line 480, in _get_len_safe_embeddings
    _iter, tokens, indices = self._tokenize(texts, _chunk_size)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\langchain_openai\embeddings\base.py", line 441, in _tokenize
    token = encoding.encode_ordinary(text)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV631HJ\OneDrive - EY\Desktop\Streamlit Chat App\chatenv\Lib\site-packages\tiktoken\core.py", line 69, in encode_ordinary
    return self._core_bpe.encode_ordinary(text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Description

I encounter an error (as mentioned in the title) when attempting to append to the chat_history during the invoke method call. Am I incorrectly setting up the pipeline?

Am I wrongly composing the pipeline?

System Info

System Information

OS: Windows OS Version: 10.0.19045 Python Version: 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]

Package Information

langchain_core: 0.3.10 langchain: 0.3.3 langchain_community: 0.3.2 langsmith: 0.1.132 langchain_chroma: 0.1.4 langchain_groq: 0.2.0 langchain_openai: 0.2.2 langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.10.9 async-timeout: Installed. No version info available. chromadb: 0.5.13 dataclasses-json: 0.6.7 fastapi: 0.115.0 groq: 0.11.0 httpx: 0.27.2 jsonpatch: 1.33 numpy: 1.26.4 openai: 1.51.2 orjson: 3.10.7 packaging: 24.1 pydantic: 2.9.2 pydantic-settings: 2.5.2 PyYAML: 6.0.2 requests: 2.32.3 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.35 tenacity: 8.5.0 tiktoken: 0.8.0 typing-extensions: 4.12.2

kodychik commented 3 days ago

Hi can I work on this issue? Thanks

mingjun1120 commented 2 days ago

Hi can I work on this issue? Thanks

I also don't know as I am still facing the issue.