langchain-ai / langchain

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

cannot access local variable 'final' where it is not associated with a value #21953

Closed jamiesun closed 2 months ago

jamiesun commented 5 months ago

Checked other resources

Example Code


def tools_chain(model_output):
    tool_map = {tool.name: tool for tool in tools}
    namekey = model_output.get("name")
    if not namekey or  namekey not in tool_map:
        return model_output
    chosen_tool = tool_map[namekey]
    return itemgetter("arguments") | chosen_tool

class OpenAIChainFactory:

    @classmethod
    def get_chat_chain(
        cls,
        model: str,
        sysmsg: str,
        sessionid: str,
        indexname: str,
        history_token_limit=4096,
    ):
        """
        Returns a chat chain runnable that can be used for conversational AI chat interactions.

        Args:
            model (str): The name of the OpenAI model to use for chat.
            sysmsg (str): The system message to provide context for the conversation.
            sessionid (str): The session ID for the chat conversation.
            indexname (str): The name of the index to use for retrieving relevant documents.
            history_token_limit (int, optional): The maximum number of tokens to store in the chat history. Defaults to 4096.

        Returns:
            RunnableWithMessageHistory: A chat chain runnable with message history.
        """
        model = model or "gpt-4-turbo-preview"
        prompt = get_conversation_with_context_prompt(sysmsg)
        retriever = get_pgvector_retriever(indexname=indexname)

        _tools_prompt = get_tools_prompt(tools)
        _tools_chain = (
            _tools_prompt
            | ChatOpenAI(model=model, temperature=0.3)
            | JsonOutputParser()
            | tools_chain
            | StdOutputRunnable()
        )

        llmchain = (
            RunnableParallel(
                {
                    "tools_output": _tools_chain,
                    "context": CONDENSE_QUESTION_PROMPT
                    | ChatOpenAI(model=model, temperature=0.3)
                    | StrOutputParser()
                    | retriever 
                    | RunnableUtils.docs_to_string(),
                    "question": itemgetter("question"),
                    "history": itemgetter("history"),
                }
            )
            | prompt
            | ChatOpenAI(model=model, temperature=0.3)
        )

        return RunnableWithMessageHistory(
            llmchain,
            lambda session_id: RedisChatMessageHistory(
                sessionid,
                url=os.environ["REDIS_URL"],
                max_token_limit=history_token_limit,
            ),
            input_messages_key="question",
            history_messages_key="history",
            verbose=True,
        )

async def test_chat_chain():
    chain = OpenAIChainFactory.get_chat_chain(
        "gpt-3.5-turbo", "You are an interesting teacher,", "test", "test_index"
    )
    fresp = await chain.ainvoke(
        input={"question": "When is 1+1 equal to 3"},
        config={"configurable": {"session_id": "test"}},
    )
    print(fresp)

if __name__ == "__main__":
    from langchain.globals import set_verbose

    set_verbose(True)
    asyncio.run(test_chat_chain())

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/Volumes/ExtDISK/github/teamsgpt/teamsgpt/teamsbot.py", line 138, in on_message_activity
    await self.on_openai_chat_stream(turn_context)
  File "/Volumes/ExtDISK/github/teamsgpt/teamsgpt/openai_handler.py", line 57, in on_openai_chat_stream
    async for r in lchain.astream(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4583, in astream
    async for item in self.bound.astream(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4583, in astream
    async for item in self.bound.astream(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2769, in astream
    async for chunk in self.atransform(input_aiter(), config, **kwargs):
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
    async for output in final_pipeline:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4619, in atransform
    async for item in self.bound.atransform(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
    async for output in final_pipeline:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
    async for ichunk in input:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
    async for ichunk in input:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3184, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3171, in _atransform
    chunk = AddableDict({step_name: task.result()})
                                    ^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3154, in get_next_chunk
    return await py_anext(generator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
    async for output in final_pipeline:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
    async for ichunk in input:
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4049, in atransform
    async for output in self._atransform_stream_with_config(
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4019, in _atransform
    cast(Callable, afunc), cast(Input, final), config, run_manager, **kwargs

Description

I tried to use a chain to implement the agent, but I got an error, which I initially judged to be an execution error of _tools_chain, which is the function code added later.

But this error doesn't always occur, sometimes it works fine

System Info

-> % pip freeze                  
aiodebug==2.3.0
aiofiles==23.2.1
aiohttp==3.9.3
aiosignal==1.3.1
aiounittest==1.3.0
altair==5.3.0
amqp==5.2.0
annotated-types==0.6.0
anyio==4.3.0
appnope==0.1.4
asgiref==3.8.1
asttokens==2.4.1
async-timeout==4.0.3
asyncpg==0.29.0
attrs==23.2.0
audio-recorder-streamlit==0.0.8
azure-ai-translation-document==1.0.0
azure-ai-translation-text==1.0.0b1
azure-cognitiveservices-speech==1.37.0
azure-common==1.1.28
azure-core==1.30.1
azure-identity==1.16.0
azure-mgmt-botservice==2.0.0
azure-mgmt-core==1.4.0
azure-mgmt-resource==23.0.1
azure-storage-blob==12.20.0
Babel==2.9.1
backoff==2.2.1
bcrypt==4.1.3
beautifulsoup4==4.12.3
billiard==4.2.0
blinker==1.8.2
botbuilder-core==4.15.0
botbuilder-dialogs==4.15.0
botbuilder-integration-aiohttp==4.15.0
botbuilder-schema==4.15.0
botframework-connector==4.15.0
botframework-streaming==4.15.0
build==1.2.1
cachetools==5.3.3
celery==5.4.0
certifi==2024.2.2
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
chroma-hnswlib==0.7.3
chromadb==0.4.24
click==8.1.7
click-didyoumean==0.3.1
click-plugins==1.1.1
click-repl==0.3.0
coloredlogs==15.0.1
comm==0.2.2
cryptography==42.0.7
dataclasses-json==0.6.6
datedelta==1.4
debugpy==1.8.1
decorator==5.1.1
deepdiff==7.0.1
Deprecated==1.2.14
dirtyjson==1.0.8
diskcache==5.6.3
distro==1.9.0
dnspython==2.6.1
docarray==0.40.0
docker==7.0.0
email_validator==2.1.1
emoji==1.7.0
et-xmlfile==1.1.0
exceptiongroup==1.2.0
executing==2.0.1
fastapi==0.111.0
fastapi-cli==0.0.4
filelock==3.14.0
filetype==1.2.0
FLAML==2.1.2
flatbuffers==24.3.25
frozenlist==1.4.1
fsspec==2024.5.0
gitdb==4.0.11
GitPython==3.1.43
google-auth==2.29.0
googleapis-common-protos==1.63.0
grapheme==0.6.0
greenlet==3.0.3
grpcio==1.63.0
h11==0.14.0
h2==4.1.0
hpack==4.0.0
html2text==2024.2.26
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
huggingface-hub==0.23.0
humanfriendly==10.0
hyperframe==6.0.1
idna==3.7
importlib-metadata==7.0.0
importlib_resources==6.4.0
install==1.3.5
ipykernel==6.29.4
ipython==8.24.0
isodate==0.6.1
jedi==0.19.1
Jinja2==3.1.4
joblib==1.4.2
jq==1.7.0
jsonpatch==1.33
jsonpath-python==1.0.6
jsonpickle==1.4.2
jsonpointer==2.4
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
jupyter_client==8.6.1
jupyter_core==5.7.2
kombu==5.3.7
kubernetes==29.0.0
langchain==0.2.0
langchain-community==0.2.0
langchain-core==0.2.0
langchain-openai==0.1.7
langchain-postgres==0.0.6
langchain-text-splitters==0.2.0
langdetect==1.0.9
langsmith==0.1.59
llama-hub==0.0.75
llama-index==0.9.48
lxml==5.2.2
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
marshmallow==3.21.2
matplotlib-inline==0.1.7
mdurl==0.1.2
microsoft-kiota-abstractions==1.3.2
microsoft-kiota-authentication-azure==1.0.0
microsoft-kiota-http==1.3.1
microsoft-kiota-serialization-form==0.1.0
microsoft-kiota-serialization-json==1.2.0
microsoft-kiota-serialization-multipart==0.1.0
microsoft-kiota-serialization-text==1.0.0
mmh3==4.1.0
monotonic==1.6
mpmath==1.3.0
msal==1.28.0
msal-extensions==1.1.0
msal-streamlit-authentication==1.0.9
msgraph-core==1.0.0
msgraph-sdk==1.4.0
msrest==0.7.1
multidict==6.0.5
multipledispatch==1.0.0
mypy-extensions==1.0.0
nest-asyncio==1.6.0
networkx==3.3
nltk==3.8.1
numpy==1.26.4
oauthlib==3.2.2
onnxruntime==1.18.0
openai==1.30.1
openpyxl==3.1.2
opentelemetry-api==1.24.0
opentelemetry-exporter-otlp-proto-common==1.24.0
opentelemetry-exporter-otlp-proto-grpc==1.24.0
opentelemetry-instrumentation==0.45b0
opentelemetry-instrumentation-asgi==0.45b0
opentelemetry-instrumentation-fastapi==0.45b0
opentelemetry-proto==1.24.0
opentelemetry-sdk==1.24.0
opentelemetry-semantic-conventions==0.45b0
opentelemetry-util-http==0.45b0
ordered-set==4.1.0
orjson==3.10.3
overrides==7.7.0
packaging==23.2
pandas==2.2.2
parso==0.8.4
pendulum==3.0.0
pexpect==4.9.0
pgvector==0.2.5
pillow==10.3.0
platformdirs==4.2.2
portalocker==2.8.2
posthog==3.5.0
prompt-toolkit==3.0.43
protobuf==4.25.3
psutil==5.9.8
psycopg==3.1.19
psycopg-pool==3.2.2
psycopg2-binary==2.9.9
ptyprocess==0.7.0
pulsar-client==3.5.0
pure-eval==0.2.2
pyaml==23.12.0
pyarrow==16.1.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pyautogen==0.2.27
pycparser==2.22
pydantic==2.7.1
pydantic_core==2.18.2
pydeck==0.9.1
pydub==0.25.1
Pygments==2.18.0
PyJWT==2.8.0
PyMuPDF==1.24.4
PyMuPDFb==1.24.3
pypdf==4.2.0
PyPika==0.48.9
pyproject_hooks==1.1.0
python-dateutil==2.9.0.post0
python-docx==1.1.2
python-dotenv==0.20.0
python-iso639==2024.4.27
python-magic==0.4.27
python-multipart==0.0.9
python-pptx==0.6.23
pytz==2023.4
PyYAML==6.0.1
pyzmq==26.0.3
rapidfuzz==3.9.1
recognizers-text==1.0.2a2
recognizers-text-choice==1.0.2a2
recognizers-text-date-time==1.0.2a2
recognizers-text-number==1.0.2a2
recognizers-text-number-with-unit==1.0.2a2
redis==5.0.4
referencing==0.35.1
regex==2024.5.15
requests==2.31.0
requests-oauthlib==2.0.0
retrying==1.3.4
rich==13.7.1
rpds-py==0.18.1
rsa==4.9
shellingham==1.5.4
simsimd==3.7.7
six==1.16.0
smmap==5.0.1
sniffio==1.3.1
soupsieve==2.5
SQLAlchemy==2.0.30
srt==3.5.3
stack-data==0.6.3
starlette==0.37.2
std-uritemplate==0.0.57
streamlit==1.34.0
streamlit-ace==0.1.1
streamlit-audiorec==0.1.3
streamlit-cookie==0.1.0
style==1.1.0
sympy==1.12
tabulate==0.9.0
tenacity==8.3.0
termcolor==2.4.0
tiktoken==0.7.0
time-machine==2.14.0
tokenizers==0.19.1
toml==0.10.2
toolz==0.12.1
tornado==6.4
tqdm==4.66.4
traitlets==5.14.3
typer==0.12.3
types-requests==2.31.0.6
types-urllib3==1.26.25.14
typing-inspect==0.9.0
typing_extensions==4.11.0
tzdata==2024.1
ujson==5.10.0
unstructured==0.11.8
unstructured-client==0.22.0
update==0.0.1
urllib3==1.26.18
uvicorn==0.29.0
uvloop==0.19.0
validators==0.28.1
vine==5.1.0
watchdog==4.0.0
watchfiles==0.21.0
wcwidth==0.2.13
websocket-client==1.8.0
websockets==12.0
wrapt==1.16.0
xlrd==2.0.1
XlsxWriter==3.2.0
yarl==1.9.4
zipp==3.18.2
eyurtsev commented 5 months ago

Stack trace appears to be truncated. Would you mind including all of it?