langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.87k stars 14.89k forks source link

Connection Timeout and Max Retries Exceeded in HTTPS Request #10135

Closed fsndzomga closed 1 year ago

fsndzomga commented 1 year ago

System Info

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connection.py", line 174, in _new_conn conn = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/connection.py", line 95, in create_connection raise err File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/connection.py", line 85, in create_connection sock.connect(sa) TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 703, in urlopen httplib_response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 386, in _make_request self._validate_conn(conn) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1042, in _validate_conn conn.connect() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connection.py", line 358, in connect self.sock = conn = self._new_conn() ^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connection.py", line 179, in _new_conn raise ConnectTimeoutError( urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x159e351d0>, 'Connection to openaipublic.blob.core.windows.net timed out. (connect timeout=None)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/adapters.py", line 489, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 787, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='openaipublic.blob.core.windows.net', port=443): Max retries exceeded with url: /encodings/cl100k_base.tiktoken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x159e351d0>, 'Connection to openaipublic.blob.core.windows.net timed out. (connect timeout=None)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/fsndzomga/Downloads/react-mega/LCEL2.py", line 29, in vectorstore = Chroma.from_texts([obama_text], embedding=OpenAIEmbeddings()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/vectorstores/chroma.py", line 576, in from_texts chroma_collection.add_texts(texts=texts, metadatas=metadatas, ids=ids) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/vectorstores/chroma.py", line 186, in add_texts embeddings = self._embedding_function.embed_documents(texts) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/embeddings/openai.py", line 478, in embed_documents return self._get_len_safe_embeddings(texts, engine=self.deployment) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/embeddings/openai.py", line 331, in _get_len_safe_embeddings encoding = tiktoken.encoding_for_model(model_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken/model.py", line 75, in encoding_for_model return get_encoding(encoding_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken/registry.py", line 63, in get_encoding enc = Encoding(constructor()) ^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken_ext/openai_public.py", line 64, in cl100k_base mergeable_ranks = load_tiktoken_bpe( ^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken/load.py", line 114, in load_tiktoken_bpe contents = read_file_cached(tiktoken_bpe_file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken/load.py", line 46, in read_file_cached contents = read_file(blobpath) ^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tiktoken/load.py", line 24, in read_file return requests.get(blobpath).content ^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/api.py", line 73, in get return request("get", url, params=params, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/api.py", line 59, in request return session.request(method=method, url=url, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/sessions.py", line 587, in request resp = self.send(prep, send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/sessions.py", line 701, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/adapters.py", line 553, in send raise ConnectTimeout(e, request=request) requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='openaipublic.blob.core.windows.net', port=443): Max retries exceeded with url: /encodings/cl100k_base.tiktoken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x159e351d0>, 'Connection to openaipublic.blob.core.windows.net timed out. (connect timeout=None)'))

Who can help?

No response

Information

Related Components

Reproduction

Here is my code:

from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings from langchain.schema.runnable import RunnablePassthrough from langchain.schema.output_parser import StrOutputParser from langchain.prompts import ChatPromptTemplate from langchain.chat_models import ChatOpenAI from operator import itemgetter from apikey import OPENAI_API_KEY import os

Set the OpenAI API key

os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

Initialize the ChatOpenAI model

model = ChatOpenAI()

Create a long text about Barack Obama to serve as the context

obama_text = """ Barack Obama served as the 44th President of the United States from 2009 to 2017. He was born in Honolulu, Hawaii, on August 4, 1961. Obama is a graduate of Columbia University and Harvard Law School, where he served as president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree and worked as a civil rights attorney and taught constitutional law at the University of Chicago Law School between 1992 and 2004. He served three terms representing the 13th District in the Illinois Senate from 1997 until 2004, when he ran for the U.S. Senate. Obama received the Nobel Peace Prize in 2009. """

Create the retriever with the Obama text as the context

vectorstore = Chroma.from_texts([obama_text], embedding=OpenAIEmbeddings()) retriever = vectorstore.as_retriever()

Define the prompt template

template = """Answer the question based only on the following context: {context}

Question: {question} """ prompt = ChatPromptTemplate.from_template(template)

Create the chain for answering questions

chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() )

Invoke the chain to answer a question

print(chain.invoke("When was Barack Obama born?"))

Create a new prompt template that allows for translation

template_with_language = """Answer the question based only on the following context: {context}

Question: {question}

Answer in the following language: {language} """ prompt_with_language = ChatPromptTemplate.from_template(template_with_language)

Create the chain for answering questions in different languages

chain_with_language = { "context": itemgetter("question") | retriever, "question": itemgetter("question"), "language": itemgetter("language") } | prompt_with_language | model | StrOutputParser()

Invoke the chain to answer a question in Italian

print(chain_with_language.invoke({"question": "When was Barack Obama born?", "language": "italian"}))

Expected behavior

a response to my question

dosubot[bot] commented 1 year ago

πŸ€–

Hello,

From the traceback you've provided, it seems like the issue is related to a connection timeout when trying to access a resource from 'openaipublic.blob.core.windows.net'. This is not directly related to the LangChain framework, but rather the underlying requests library that is being used to fetch the resource.

The error message 'Max retries exceeded with url' suggests that the requests library has tried to establish a connection to the server multiple times and failed. This could be due to a number of reasons such as network issues, the server being down, or the server taking too long to respond.

Here are a few things you could try:

  1. Check your internet connection: Make sure you have a stable internet connection and you're able to access other websites.

  2. Check the server status: If possible, check if 'openaipublic.blob.core.windows.net' is up and running. You can do this by trying to access the URL in a web browser or using a tool like ping or curl from the command line.

  3. Increase the timeout: If the server is slow to respond, you might need to increase the timeout. In the requests library, you can do this by passing the timeout parameter to the get method like so: requests.get(url, timeout=10). This will wait for 10 seconds before giving up.

  4. Increase the max retries: You can also increase the maximum number of retries by creating a custom session with a Retry object. Here's an example:

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

session = requests.Session()
retry = Retry(total=5, backoff_factor=0.1, status_forcelist=[ 500, 502, 503, 504 ])
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

response = session.get(url)

This will retry the request 5 times with a delay between retries that increases each time (0.1, 0.2, 0.4, 0.8, 1.6 seconds).

Please try these suggestions and let me know if the issue persists.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.