Thanks to visit codestin.com
Credit goes to github.com

Skip to content

openai.APITimeoutError: Request timed #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
kunkun8866 opened this issue Mar 5, 2024 · 1 comment
Closed
1 task done

openai.APITimeoutError: Request timed #1215

kunkun8866 opened this issue Mar 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kunkun8866
Copy link

kunkun8866 commented Mar 5, 2024

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

我用from openai import OpenAI这个库访问时候回复速度很快,但是我在利用llamaindex来结合gpt与模型时候,queryengine.query()这个语句就会报错openai.APITimeoutError: Request timed,以下是我的报错信息:
03/05/2024 16:42:02 - [INFO] -openai._base_client->>> Retrying request to /chat/completions in 0.859262 seconds
03/05/2024 16:42:23 - [INFO] -openai._base_client->>> Retrying request to /chat/completions in 1.629988 seconds
03/05/2024 16:42:46 - [INFO] -openai._base_client->>> Retrying request to /chat/completions in 3.447563 seconds
Traceback (most recent call last):
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_transports\default.py", line 69, in map_httpcore_exceptions
yield
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_transports\default.py", line 233, in handle_request
resp = self._pool.handle_request(req)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_sync\connection_pool.py", line 216, in handle_request
raise exc from None
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_sync\connection_pool.py", line 196, in handle_request
response = connection.handle_request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_sync\connection.py", line 99, in handle_request
raise exc
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_sync\connection.py", line 76, in handle_request
stream = self._connect(request)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_sync\connection.py", line 122, in _connect
stream = self._network_backend.connect_tcp(**kwargs)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_backends\sync.py", line 205, in connect_tcp
with map_exceptions(exc_map):
File "C:\Users\590\anaconda3\envs\bce\lib\contextlib.py", line 153, in exit
self.gen.throw(typ, value, traceback)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpcore_exceptions.py", line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectTimeout: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 918, in _request
response = self._client.send(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_client.py", line 914, in send
response = self._send_handling_auth(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_client.py", line 942, in _send_handling_auth
response = self._send_handling_redirects(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_client.py", line 979, in _send_handling_redirects
response = self._send_single_request(request)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_client.py", line 1015, in _send_single_request
response = transport.handle_request(request)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_transports\default.py", line 232, in handle_request
with map_httpcore_exceptions():
File "C:\Users\590\anaconda3\envs\bce\lib\contextlib.py", line 153, in exit
self.gen.throw(typ, value, traceback)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\httpx_transports\default.py", line 86, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectTimeout: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "D:\BCE\pycharmcode\test2.py", line 51, in
query_response = query_engine.query(query)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\core\base_query_engine.py", line 40, in query
return self._query(str_or_query_bundle)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\query_engine\retriever_query_engine.py", line 172, in _query
response = self._response_synthesizer.synthesize(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\response_synthesizers\base.py", line 168, in synthesize
response_str = self.get_response(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\response_synthesizers\compact_and_refine.py", line 38, in get_response
return super().get_response(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\response_synthesizers\refine.py", line 146, in get_response
response = self._give_response_single(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\response_synthesizers\refine.py", line 202, in _give_response_single
program(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\response_synthesizers\refine.py", line 64, in call
answer = self._llm.predict(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\llms\llm.py", line 239, in predict
chat_response = self.chat(messages)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\llms\base.py", line 100, in wrapped_llm_chat
f_return_val = f(_self, messages, **kwargs)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\llms\openai.py", line 237, in chat
return chat_fn(messages, **kwargs)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\llama_index\llms\openai.py", line 296, in _chat
response = client.chat.completions.create(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_utils_utils.py", line 275, in wrapper
return func(*args, **kwargs)
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai\resources\chat\completions.py", line 663, in create
return self._post(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 1200, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 889, in request
return self._request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 927, in _request
return self._retry_request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 1013, in _retry_request
return self._request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 927, in _request
return self._retry_request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 1013, in _retry_request
return self._request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 927, in _request
return self._retry_request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 1013, in _retry_request
return self._request(
File "C:\Users\590\anaconda3\envs\bce\lib\site-packages\openai_base_client.py", line 937, in _request
raise APITimeoutError(request=request) from err
openai.APITimeoutError: Request timed out.

To Reproduce

我的代码是这样的:(我删除了apikey和apiurl)

我们在BCEmbedding中提供llama_index直接集成的接口。

from BCEmbedding.tools.llama_index import BCERerank

import os
from llama_index.embeddings import HuggingFaceEmbedding
from llama_index import VectorStoreIndex, ServiceContext, SimpleDirectoryReader
from llama_index.node_parser import SimpleNodeParser
from llama_index.llms import OpenAI
from llama_index.retrievers import VectorIndexRetriever

init embedding model and reranker model

embed_args = {'model_name': 'D:\BCE\model\bce-embedding-base_v1', 'max_length': 512, 'embed_batch_size': 32, 'device': 'cuda:0'}
embed_model = HuggingFaceEmbedding(**embed_args)

reranker_args = {'model': 'D:\BCE\model\bce-reranker-base_v1', 'top_n': 5, 'device': 'cuda:0'}
reranker_model = BCERerank(**reranker_args)

example #1. extract embeddings

query1 = 'apples'
passages = [
'I like apples',
'I like oranges',
'Apples and oranges are fruits'
]
query_embedding = embed_model.get_query_embedding(query1)
passages_embeddings = embed_model.get_text_embedding_batch(passages)
print('passages_em:'+str(passages_embeddings))
print('query_em:'+str(query_embedding))

example #2. rag example

llm = OpenAI(model='gpt-3.5-turbo', api_key=os.environ.get(''),api_base=os.environ.get(''))
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)

documents = SimpleDirectoryReader(input_files=["D:\BCE\pycharmcode\文章.pdf","D:\BCE\pycharmcode\文章2.pdf","D:\BCE\pycharmcode\文章3.pdf"]).load_data()
node_parser = SimpleNodeParser.from_defaults(chunk_size=512)
nodes = node_parser.get_nodes_from_documents(documents[0:36])
index = VectorStoreIndex(nodes, service_context=service_context)
query = "请你给我介绍一下这个游戏"

example #2.1. retrieval with EmbeddingModel and RerankerModel

vector_retriever = VectorIndexRetriever(index=index, similarity_top_k=5, service_context=service_context)
retrieval_by_embedding = vector_retriever.retrieve(query)
retrieval_by_reranker = reranker_model.postprocess_nodes(retrieval_by_embedding, query_str=query)
print(retrieval_by_embedding)
print(retrieval_by_reranker)

example #2.2. query with EmbeddingModel and RerankerModel

query_engine = index.as_query_engine(node_postprocessors=[reranker_model])
print(query_engine)
query_response = query_engine.query(query)

print(query_response)

Code snippets

No response

OS

windows11

Python version

Python 3.10.13

Library version

1.13.3

@kunkun8866 kunkun8866 added the bug Something isn't working label Mar 5, 2024
@rattrayalex
Copy link
Collaborator

If your API server cannot connect to OpenAI's API server, you may wish to check the list of supported countries and regions: https://platform.openai.com/docs/supported-countries

Regardless, this is not likely to be an SDK bug, but a problem with how you're connecting to the API. For help developing with the OpenAI API from fellow developers, I recommend the OpenAI Discord server!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants